Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Add two servers version #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions services/src/main/java/org/exoplatform/leadcapture/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.exoplatform.ws.frameworks.json.JsonGenerator;
import org.exoplatform.ws.frameworks.json.JsonParser;
import org.exoplatform.ws.frameworks.json.impl.*;
import org.exoplatform.ws.frameworks.json.value.JsonValue;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class Utils {
public static final String MAIL_DEFAULT_LANGUAGE = "en";
public static final String NEW_LEAD_EVENT = "leadCapture.newLead.event";
public static final String NEW_RESPONSE_EVENT = "leadCapture.newResponse.event";
public static final String LEAD_SYNCHRONIZE_EVENT = "leadCapture.newLeadSynchronize.event";
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String EMPTY_STR = "";
public static final SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
Expand Down Expand Up @@ -195,6 +197,14 @@ public static final String toJsonString(Object object) {
}
}

public static JsonValue toJson(Object object) {
try {
return JSON_GENERATOR.createJsonObject(object);
} catch (JsonException e) {
throw new IllegalStateException("Error parsing object to string " + object, e);
}
}

public static final <T> T fromJsonString(String value, Class<T> resultClass) {
try {
if (StringUtils.isBlank(value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.exoplatform.leadcapture.dao;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.exoplatform.commons.persistence.impl.GenericDAOJPAImpl;
import org.exoplatform.leadcapture.entity.NotSynchroLeadEntity;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

public class NotSynchronizedLeadsDAO extends GenericDAOJPAImpl<NotSynchroLeadEntity, Long> {

private static final Log LOG = ExoLogger.getLogger(NotSynchronizedLeadsDAO.class);

public NotSynchroLeadEntity getNotSynchLead(Long leadId, Long responseId) {

TypedQuery<NotSynchroLeadEntity> query = getEntityManager()
.createNamedQuery("NotSynchroLeadEntity.getNotSynchLead",
NotSynchroLeadEntity.class)
.setParameter("leadId", leadId)
.setParameter("responseId", responseId);

try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
} catch (Exception e) {
LOG.error("Error occurred when trying to get lead by leadId {} and responseId {}", leadId, responseId, e);
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package org.exoplatform.leadcapture.dto;

import java.io.Serializable;
import java.util.List;

import lombok.Data;

@Data
public class FormInfo implements Serializable {

private Long id;

private LeadDTO lead;

private ResponseDTO response;

public FormInfo(Long id, LeadDTO lead, ResponseDTO response) {
this.id = id;
this.lead = lead;
this.response = response;
}
public FormInfo() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ public class LeadCaptureSettings implements Serializable, Cloneable {

private String senderMail = null;

private String leadManagementServerUrl = null;

private String leadCaptureToken = null;

private String leadManagementToken = null;

private boolean mailingEnabled;

private boolean captureEnabled;

private boolean leadManagementServer;


public LeadCaptureSettings clone() { // NOSONAR
try {
return (LeadCaptureSettings) super.clone();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.exoplatform.leadcapture.entity;

import java.io.Serializable;

import javax.persistence.*;

import org.exoplatform.commons.api.persistence.ExoEntity;

import lombok.Data;

@Entity(name = "NotSynchroLeadEntity")
@ExoEntity
@Table(name = "ADDONS_LC_NOT_SYNCHRO_LEAD")
@Data

@NamedQueries({
@NamedQuery(name = "NotSynchroLeadEntity.getNotSynchLead", query = "SELECT lead FROM NotSynchroLeadEntity lead where lead.leadEntity.id = :leadId and lead.responseEntity.id = :responseId ") })

public class NotSynchroLeadEntity implements Serializable {

@Id
@SequenceGenerator(name = "SEQ_ADDONS_LC_NOT_SYNCHRO_LEAD_ID", sequenceName = "SEQ_ADDONS_LC_NOT_SYNCHRO_LEAD_ID")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_ADDONS_LC_NOT_SYNCHRO_LEAD_ID")
@Column(name = "ID")
protected Long id;

@ManyToOne
@JoinColumn(name = "LC_LEAD_ID")
private LeadEntity leadEntity;

@ManyToOne
@JoinColumn(name = "LC_RESPONSE_ID")
private ResponseEntity responseEntity;

@Column(name = "ATTEMPTS_NUMBER")
protected int atteptsNumber;

public NotSynchroLeadEntity() {
}

public NotSynchroLeadEntity(LeadEntity leadEntity, ResponseEntity responseEntity, int atteptsNumber) {
this.leadEntity = leadEntity;
this.responseEntity = responseEntity;
this.atteptsNumber = atteptsNumber;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.exoplatform.leadcapture.job;

import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.leadcapture.dao.NotSynchronizedLeadsDAO;
import org.exoplatform.leadcapture.dto.FormInfo;
import org.exoplatform.leadcapture.entity.NotSynchroLeadEntity;
import org.exoplatform.leadcapture.services.LeadsManagementService;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

public class LeadsSynchronizeJob implements Job {

private final Log LOG = ExoLogger.getLogger(LeadsSynchronizeJob.class);

@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
LeadsManagementService leadsManagementService = CommonsUtils.getService(LeadsManagementService.class);
NotSynchronizedLeadsDAO notSynchronizedLeadsDAO = CommonsUtils.getService(NotSynchronizedLeadsDAO.class);
List<NotSynchroLeadEntity> notSynchroLeadEntityList = notSynchronizedLeadsDAO.findAll();
for (NotSynchroLeadEntity notSynchroLeadEntity : notSynchroLeadEntityList) {
try {
FormInfo formInfo = new FormInfo(notSynchroLeadEntity.getId(),
leadsManagementService.toLeadDto(notSynchroLeadEntity.getLeadEntity()),
leadsManagementService.toResponseDto(notSynchroLeadEntity.getResponseEntity()));
leadsManagementService.synchronizeLead(formInfo);
} catch (Exception e) {
LOG.error("Cannot synchronise lead", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.exoplatform.leadcapture.listeners;

import org.exoplatform.leadcapture.dto.FormInfo;
import org.exoplatform.leadcapture.services.LeadsManagementService;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

public class LeadSynchronizeListener extends Listener<FormInfo, String> {

private static final Log LOG = ExoLogger.getLogger(LeadSynchronizeListener.class);

private LeadsManagementService leadsManagementService;

public LeadSynchronizeListener(LeadsManagementService leadsManagementService) {
this.leadsManagementService = leadsManagementService;
}

@Override
public void onEvent(Event<FormInfo, String> event) throws Exception {
FormInfo formInfo = event.getSource();
leadsManagementService.synchronizeLead(formInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,55 @@

import java.util.List;

import org.exoplatform.leadcapture.Utils;
import org.exoplatform.leadcapture.dto.LeadCaptureSettings;
import org.exoplatform.leadcapture.dto.MailContentDTO;
import org.exoplatform.leadcapture.dto.MailTemplateDTO;
import org.exoplatform.leadcapture.entity.LeadEntity;
import org.exoplatform.leadcapture.entity.MailTemplateEntity;
import org.exoplatform.leadcapture.services.LCMailService;
import org.exoplatform.leadcapture.services.LeadCaptureSettingsService;
import org.exoplatform.leadcapture.services.MailTemplatesManagementService;
import org.exoplatform.leadcapture.Utils;
import org.exoplatform.services.listener.Event;
import org.exoplatform.services.listener.Listener;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

public class NewCommunityRegistrationListener extends Listener<LeadEntity, String> {

private static final Log LOG = ExoLogger.getLogger(NewCommunityRegistrationListener.class);
private static final Log LOG = ExoLogger.getLogger(NewCommunityRegistrationListener.class);

private LCMailService lcMailService;
private LCMailService lcMailService;

private MailTemplatesManagementService mailTemplatesManagementService;

public NewCommunityRegistrationListener(LCMailService lcMailService, MailTemplatesManagementService mailTemplatesManagementService) {
private LeadCaptureSettingsService leadCaptureSettingsService;

public NewCommunityRegistrationListener(LCMailService lcMailService,
MailTemplatesManagementService mailTemplatesManagementService,
LeadCaptureSettingsService leadCaptureSettingsService) {
this.lcMailService = lcMailService;
this.mailTemplatesManagementService = mailTemplatesManagementService;
this.leadCaptureSettingsService = leadCaptureSettingsService;
}

@Override
public void onEvent(Event<LeadEntity, String> event) throws Exception {
LeadEntity lead = event.getSource();
List<MailTemplateEntity> templates = mailTemplatesManagementService.getTemplatesbyEvent("newCommunityRegistration");
for (MailTemplateEntity template : templates) {
MailContentDTO content = null;
MailTemplateDTO mailTemplateDTO = mailTemplatesManagementService.toMailTemplateDTO(template);
if (mailTemplateDTO.getContents().size() > 0) {
content = Utils.getContentForMail(mailTemplateDTO, lead);
if (content != null) {
lcMailService.sendMail(content.getContent(), content.getSubject(), lead);
LeadCaptureSettings settings = leadCaptureSettingsService.getSettings();
if (settings.isMailingEnabled()) {
List<MailTemplateEntity> templates = mailTemplatesManagementService.getTemplatesbyEvent("newCommunityRegistration");
for (MailTemplateEntity template : templates) {
MailContentDTO content = null;
MailTemplateDTO mailTemplateDTO = mailTemplatesManagementService.toMailTemplateDTO(template);
if (mailTemplateDTO.getContents().size() > 0) {
content = Utils.getContentForMail(mailTemplateDTO, lead);
if (content != null) {
lcMailService.sendMail(content.getContent(), content.getSubject(), lead);
}
}
}

}
}
}
}
Loading