From 0805412176a9f8b1d3aa9019a25a822d06efbe21 Mon Sep 17 00:00:00 2001 From: mkrout Date: Mon, 20 Jan 2020 11:49:33 +0100 Subject: [PATCH] -Add two servers version --- .../org/exoplatform/leadcapture/Utils.java | 10 ++ .../dao/NotSynchronizedLeadsDAO.java | 33 +++++ .../exoplatform/leadcapture/dto/FormInfo.java | 12 ++ .../leadcapture/dto/LeadCaptureSettings.java | 9 ++ .../entity/NotSynchroLeadEntity.java | 47 +++++++ .../leadcapture/job/LeadsSynchronizeJob.java | 37 +++++ .../listeners/LeadSynchronizeListener.java | 25 ++++ .../NewCommunityRegistrationListener.java | 36 +++-- .../listeners/NewLeadListener.java | 90 +++++++------ .../listeners/NewResponseListener.java | 60 ++++++--- .../listeners/NewUserListener.java | 30 +++-- .../services/LeadsManagementService.java | 127 +++++++++++++++--- .../resources/conf/portal/configuration.xml | 33 ++++- .../lead-capture.db.changelog-1.0.0.xml | 19 +++ .../components/LeadCaptureSettingsApp.vue | 54 ++++++-- 15 files changed, 508 insertions(+), 114 deletions(-) create mode 100644 services/src/main/java/org/exoplatform/leadcapture/dao/NotSynchronizedLeadsDAO.java create mode 100644 services/src/main/java/org/exoplatform/leadcapture/entity/NotSynchroLeadEntity.java create mode 100644 services/src/main/java/org/exoplatform/leadcapture/job/LeadsSynchronizeJob.java create mode 100644 services/src/main/java/org/exoplatform/leadcapture/listeners/LeadSynchronizeListener.java diff --git a/services/src/main/java/org/exoplatform/leadcapture/Utils.java b/services/src/main/java/org/exoplatform/leadcapture/Utils.java index 0bb2d38..aae1fc4 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/Utils.java +++ b/services/src/main/java/org/exoplatform/leadcapture/Utils.java @@ -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; @@ -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); @@ -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 fromJsonString(String value, Class resultClass) { try { if (StringUtils.isBlank(value)) { diff --git a/services/src/main/java/org/exoplatform/leadcapture/dao/NotSynchronizedLeadsDAO.java b/services/src/main/java/org/exoplatform/leadcapture/dao/NotSynchronizedLeadsDAO.java new file mode 100644 index 0000000..3a08406 --- /dev/null +++ b/services/src/main/java/org/exoplatform/leadcapture/dao/NotSynchronizedLeadsDAO.java @@ -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 { + + private static final Log LOG = ExoLogger.getLogger(NotSynchronizedLeadsDAO.class); + + public NotSynchroLeadEntity getNotSynchLead(Long leadId, Long responseId) { + + TypedQuery 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; + } + } + +} diff --git a/services/src/main/java/org/exoplatform/leadcapture/dto/FormInfo.java b/services/src/main/java/org/exoplatform/leadcapture/dto/FormInfo.java index 5f20dd8..98cb75c 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/dto/FormInfo.java +++ b/services/src/main/java/org/exoplatform/leadcapture/dto/FormInfo.java @@ -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() { + + } + } diff --git a/services/src/main/java/org/exoplatform/leadcapture/dto/LeadCaptureSettings.java b/services/src/main/java/org/exoplatform/leadcapture/dto/LeadCaptureSettings.java index 4ce2447..45da530 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/dto/LeadCaptureSettings.java +++ b/services/src/main/java/org/exoplatform/leadcapture/dto/LeadCaptureSettings.java @@ -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(); diff --git a/services/src/main/java/org/exoplatform/leadcapture/entity/NotSynchroLeadEntity.java b/services/src/main/java/org/exoplatform/leadcapture/entity/NotSynchroLeadEntity.java new file mode 100644 index 0000000..90c5053 --- /dev/null +++ b/services/src/main/java/org/exoplatform/leadcapture/entity/NotSynchroLeadEntity.java @@ -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; + } + +} diff --git a/services/src/main/java/org/exoplatform/leadcapture/job/LeadsSynchronizeJob.java b/services/src/main/java/org/exoplatform/leadcapture/job/LeadsSynchronizeJob.java new file mode 100644 index 0000000..e45f897 --- /dev/null +++ b/services/src/main/java/org/exoplatform/leadcapture/job/LeadsSynchronizeJob.java @@ -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 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); + } + } + } +} diff --git a/services/src/main/java/org/exoplatform/leadcapture/listeners/LeadSynchronizeListener.java b/services/src/main/java/org/exoplatform/leadcapture/listeners/LeadSynchronizeListener.java new file mode 100644 index 0000000..d2dbb13 --- /dev/null +++ b/services/src/main/java/org/exoplatform/leadcapture/listeners/LeadSynchronizeListener.java @@ -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 { + + private static final Log LOG = ExoLogger.getLogger(LeadSynchronizeListener.class); + + private LeadsManagementService leadsManagementService; + + public LeadSynchronizeListener(LeadsManagementService leadsManagementService) { + this.leadsManagementService = leadsManagementService; + } + + @Override + public void onEvent(Event event) throws Exception { + FormInfo formInfo = event.getSource(); + leadsManagementService.synchronizeLead(formInfo); + } +} diff --git a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewCommunityRegistrationListener.java b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewCommunityRegistrationListener.java index fb0a708..a0f8fb6 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewCommunityRegistrationListener.java +++ b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewCommunityRegistrationListener.java @@ -2,13 +2,15 @@ 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; @@ -16,31 +18,39 @@ public class NewCommunityRegistrationListener extends Listener { - 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 event) throws Exception { LeadEntity lead = event.getSource(); - List 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 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); + } } - } + } } } } diff --git a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewLeadListener.java b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewLeadListener.java index f6d597e..3ce6b29 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewLeadListener.java +++ b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewLeadListener.java @@ -8,6 +8,7 @@ import org.exoplatform.commons.utils.ListAccess; import org.exoplatform.leadcapture.Utils; import org.exoplatform.leadcapture.dao.LeadDAO; +import org.exoplatform.leadcapture.dto.LeadCaptureSettings; import org.exoplatform.leadcapture.dto.MailContentDTO; import org.exoplatform.leadcapture.dto.MailTemplateDTO; import org.exoplatform.leadcapture.entity.LeadEntity; @@ -25,29 +26,30 @@ import org.exoplatform.social.core.activity.model.ExoSocialActivity; import org.exoplatform.task.domain.Status; import org.exoplatform.task.domain.Task; -import org.exoplatform.task.service.*; +import org.exoplatform.task.service.ProjectService; +import org.exoplatform.task.service.StatusService; +import org.exoplatform.task.service.TaskService; import org.exoplatform.task.util.TaskUtil; -import static org.exoplatform.leadcapture.Utils.*; public class NewLeadListener extends Listener { - private static final Log LOG = ExoLogger.getLogger(NewLeadListener.class); + private static final Log LOG = ExoLogger.getLogger(NewLeadListener.class); - private LCMailService lcMailService; + private LCMailService lcMailService; private MailTemplatesManagementService mailTemplatesManagementService; - private OrganizationService organizationService; + private OrganizationService organizationService; - private ProjectService projectService; + private ProjectService projectService; - private StatusService statusService; + private StatusService statusService; - private TaskService taskService; + private TaskService taskService; - private LeadDAO leadDAO; + private LeadDAO leadDAO; - private LeadCaptureSettingsService leadCaptureSettingsService; + private LeadCaptureSettingsService leadCaptureSettingsService; public NewLeadListener(LCMailService lcMailService, MailTemplatesManagementService mailTemplatesManagementService, @@ -70,42 +72,46 @@ public NewLeadListener(LCMailService lcMailService, @Override public void onEvent(Event event) throws Exception { LeadEntity lead = event.getSource(); - if (StringUtils.isEmpty(lead.getCommunityUserName())) { - Query query = new Query(); - query.setEmail(lead.getMail()); - ListAccess users = organizationService.getUserHandler().findUsersByQuery(query); - if (users.getSize() > 0) { - User communityUser = users.load(0, 1)[0]; - lead.setCommunityUserName(communityUser.getUserName()); - lead.setCommunityRegistrationDate(communityUser.getCreatedDate().getTime()); + LeadCaptureSettings settings = leadCaptureSettingsService.getSettings(); + if (settings.isLeadManagementServer()) { + if (StringUtils.isEmpty(lead.getCommunityUserName())) { + Query query = new Query(); + query.setEmail(lead.getMail()); + ListAccess users = organizationService.getUserHandler().findUsersByQuery(query); + if (users.getSize() > 0) { + User communityUser = users.load(0, 1)[0]; + lead.setCommunityUserName(communityUser.getUserName()); + lead.setCommunityRegistrationDate(communityUser.getCreatedDate().getTime()); + } } - } - ExoSocialActivity activity = Utils.createActivity(lead); - lead.setActivityId(activity.getId()); - Status status = statusService.getDefaultStatus(Utils.getTaskProject().getId()); - Task task = new Task(); - task.setTitle(lead.getMail()); - task.setDescription(""); - task.setStatus(status); - task.setCreatedBy(leadCaptureSettingsService.getSettings().getMarketingBotUserName()); - task.setCreatedTime(new Date()); - task = taskService.createTask(task); - lead.setTaskId(task.getId()); - lead.setTaskUrl(TaskUtil.buildTaskURL(task)); - leadDAO.update(lead); - - List templates = mailTemplatesManagementService.getTemplatesbyEvent("newLead"); - 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); + ExoSocialActivity activity = Utils.createActivity(lead); + lead.setActivityId(activity.getId()); + Status status = statusService.getDefaultStatus(Utils.getTaskProject().getId()); + Task task = new Task(); + task.setTitle(lead.getMail()); + task.setDescription(""); + task.setStatus(status); + task.setCreatedBy(leadCaptureSettingsService.getSettings().getMarketingBotUserName()); + task.setCreatedTime(new Date()); + task = taskService.createTask(task); + lead.setTaskId(task.getId()); + lead.setTaskUrl(TaskUtil.buildTaskURL(task)); + leadDAO.update(lead); + } + if (settings.isMailingEnabled()) { + List templates = mailTemplatesManagementService.getTemplatesbyEvent("newLead"); + 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); + } } - } + } } } } diff --git a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewResponseListener.java b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewResponseListener.java index b718627..452e9ca 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewResponseListener.java +++ b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewResponseListener.java @@ -1,10 +1,15 @@ package org.exoplatform.leadcapture.listeners; +import static org.exoplatform.leadcapture.Utils.LEAD_SYNCHRONIZE_EVENT; + import java.util.List; import org.apache.commons.lang.StringUtils; +import org.exoplatform.leadcapture.Utils; import org.exoplatform.leadcapture.dao.FieldDAO; +import org.exoplatform.leadcapture.dto.FormInfo; +import org.exoplatform.leadcapture.dto.LeadCaptureSettings; import org.exoplatform.leadcapture.dto.MailContentDTO; import org.exoplatform.leadcapture.dto.MailTemplateDTO; import org.exoplatform.leadcapture.entity.FieldEntity; @@ -12,45 +17,70 @@ import org.exoplatform.leadcapture.entity.MailTemplateEntity; import org.exoplatform.leadcapture.entity.ResponseEntity; import org.exoplatform.leadcapture.services.LCMailService; +import org.exoplatform.leadcapture.services.LeadCaptureSettingsService; +import org.exoplatform.leadcapture.services.LeadsManagementService; 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.listener.ListenerService; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; public class NewResponseListener extends Listener { - private static final Log LOG = ExoLogger.getLogger(NewResponseListener.class); + private static final Log LOG = ExoLogger.getLogger(NewResponseListener.class); - private LCMailService lcMailService; + private LCMailService lcMailService; private MailTemplatesManagementService mailTemplatesManagementService; - private FieldDAO fieldDAO; + private ListenerService listenerService; + + private FieldDAO fieldDAO; + + private LeadCaptureSettingsService leadCaptureSettingsService; - public NewResponseListener(LCMailService lcMailService, MailTemplatesManagementService mailTemplatesManagementService, FieldDAO fieldDAO) { + private LeadsManagementService leadsManagementService; + + public NewResponseListener(LCMailService lcMailService, + MailTemplatesManagementService mailTemplatesManagementService, + LeadsManagementService leadsManagementService, + FieldDAO fieldDAO, + ListenerService listenerService, + LeadCaptureSettingsService leadCaptureSettingsService) { this.lcMailService = lcMailService; this.mailTemplatesManagementService = mailTemplatesManagementService; + this.leadsManagementService = leadsManagementService; this.fieldDAO = fieldDAO; + this.listenerService = listenerService; + this.leadCaptureSettingsService = leadCaptureSettingsService; } @Override public void onEvent(Event event) throws Exception { LeadEntity lead = event.getSource(); + LeadCaptureSettings settings = leadCaptureSettingsService.getSettings(); ResponseEntity responseEntity = event.getData(); - List templates = mailTemplatesManagementService.getTemplatesbyEvent("newResponse"); - 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 && shouldBeSend(template, responseEntity)) { - lcMailService.sendMail(content.getContent(), content.getSubject(), lead); - LOG.info("Mail sent to {}",lead.getMail()); + if (!settings.isLeadManagementServer()) { + FormInfo formInfo = new FormInfo(null, + leadsManagementService.toLeadDto(lead), + leadsManagementService.toResponseDto(responseEntity)); + listenerService.broadcast(LEAD_SYNCHRONIZE_EVENT, formInfo, ""); + } + if (settings.isMailingEnabled()) { + List templates = mailTemplatesManagementService.getTemplatesbyEvent("newResponse"); + 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 && shouldBeSend(template, responseEntity)) { + lcMailService.sendMail(content.getContent(), content.getSubject(), lead); + LOG.info("Mail sent to {}", lead.getMail()); + } } - } + } } } diff --git a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewUserListener.java b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewUserListener.java index a3d2db6..cc4cf1d 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/listeners/NewUserListener.java +++ b/services/src/main/java/org/exoplatform/leadcapture/listeners/NewUserListener.java @@ -20,6 +20,7 @@ import org.exoplatform.leadcapture.dao.LeadDAO; import org.exoplatform.leadcapture.entity.LeadEntity; +import org.exoplatform.leadcapture.services.LeadCaptureSettingsService; import org.exoplatform.services.listener.Asynchronous; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; @@ -29,30 +30,37 @@ @Asynchronous public class NewUserListener extends UserEventListener { - private static final Log LOG = ExoLogger.getLogger(NewUserListener.class); - private LeadDAO leadDAO; + private static final Log LOG = ExoLogger.getLogger(NewUserListener.class); - public NewUserListener(LeadDAO leadDAO) throws Exception { + private LeadDAO leadDAO; + + private LeadCaptureSettingsService leadCaptureSettingsService; + + public NewUserListener(LeadDAO leadDAO, LeadCaptureSettingsService leadCaptureSettingsService) throws Exception { this.leadDAO = leadDAO; + this.leadCaptureSettingsService = leadCaptureSettingsService; } @Override public void postSave(User user, boolean isNew) throws Exception { try { - LeadEntity leadEntity = leadDAO.getLeadByMail(user.getEmail()); - if (leadEntity != null) { - if (leadEntity.getCommunityRegistration()==null || !leadEntity.getCommunityRegistration()) { - leadEntity.setUpdatedDate(new Date().getTime()); - leadEntity.setCommunityRegistration(true); - leadEntity.setCommunityRegistrationDate(user.getCreatedDate().getTime()); - leadEntity.setCommunityUserName(user.getUserName()); - leadDAO.update(leadEntity); + if (leadCaptureSettingsService.getSettings().isLeadManagementServer()) { + LeadEntity leadEntity = leadDAO.getLeadByMail(user.getEmail()); + if (leadEntity != null) { + if (leadEntity.getCommunityRegistration() == null || !leadEntity.getCommunityRegistration()) { + leadEntity.setUpdatedDate(new Date().getTime()); + leadEntity.setCommunityRegistration(true); + leadEntity.setCommunityRegistrationDate(user.getCreatedDate().getTime()); + leadEntity.setCommunityUserName(user.getUserName()); + leadDAO.update(leadEntity); + } } } } catch (Exception e) { LOG.error("an error accured", e); } + } @Override diff --git a/services/src/main/java/org/exoplatform/leadcapture/services/LeadsManagementService.java b/services/src/main/java/org/exoplatform/leadcapture/services/LeadsManagementService.java index dbdbb1d..445c4af 100644 --- a/services/src/main/java/org/exoplatform/leadcapture/services/LeadsManagementService.java +++ b/services/src/main/java/org/exoplatform/leadcapture/services/LeadsManagementService.java @@ -1,6 +1,14 @@ package org.exoplatform.leadcapture.services; import static org.exoplatform.leadcapture.Utils.*; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -12,17 +20,14 @@ import org.json.JSONException; import org.json.JSONObject; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + import org.exoplatform.commons.utils.CommonsUtils; import org.exoplatform.leadcapture.Utils; -import org.exoplatform.leadcapture.dao.FieldDAO; -import org.exoplatform.leadcapture.dao.FormDAO; -import org.exoplatform.leadcapture.dao.LeadDAO; -import org.exoplatform.leadcapture.dao.ResponseDAO; +import org.exoplatform.leadcapture.dao.*; import org.exoplatform.leadcapture.dto.*; -import org.exoplatform.leadcapture.entity.FieldEntity; -import org.exoplatform.leadcapture.entity.FormEntity; -import org.exoplatform.leadcapture.entity.LeadEntity; -import org.exoplatform.leadcapture.entity.ResponseEntity; +import org.exoplatform.leadcapture.entity.*; import org.exoplatform.services.listener.ListenerService; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; @@ -34,43 +39,53 @@ import org.exoplatform.task.service.ProjectService; import org.exoplatform.task.service.StatusService; import org.exoplatform.task.service.TaskService; +import org.exoplatform.ws.frameworks.json.value.JsonValue; +import org.exoplatform.ws.frameworks.json.value.impl.ObjectValue; public class LeadsManagementService { - private final Log LOG = ExoLogger.getLogger(LeadsManagementService.class); + private final Log LOG = ExoLogger.getLogger(LeadsManagementService.class); + + private LeadDAO leadDAO; - private LeadDAO leadDAO; + private FormDAO formDAO; - private FormDAO formDAO; + private FieldDAO fieldDAO; - private FieldDAO fieldDAO; + private ResponseDAO responseDAO; - private ResponseDAO responseDAO; + private NotSynchronizedLeadsDAO notSynchronizedLeadsDAO; - private ListenerService listenerService; + private ListenerService listenerService; - private TaskService taskService; + private TaskService taskService; - private StatusService statusService; + private StatusService statusService; - private ProjectService projectService; + private ProjectService projectService; + + private LeadCaptureSettingsService leadCaptureSettingsService; public LeadsManagementService(LeadDAO leadDAO, FormDAO formDAO, FieldDAO fieldDAO, ResponseDAO responseDAO, + NotSynchronizedLeadsDAO notSynchronizedLeadsDAO, TaskService taskService, StatusService statusService, ProjectService projectService, - ListenerService listenerService) { + ListenerService listenerService, + LeadCaptureSettingsService leadCaptureSettingsService) { this.leadDAO = leadDAO; this.formDAO = formDAO; this.fieldDAO = fieldDAO; this.responseDAO = responseDAO; + this.notSynchronizedLeadsDAO = notSynchronizedLeadsDAO; this.listenerService = listenerService; this.taskService = taskService; this.statusService = statusService; this.projectService = projectService; + this.leadCaptureSettingsService = leadCaptureSettingsService; } public void addLeadInfo(FormInfo leadInfo, boolean broadcast) { @@ -78,8 +93,11 @@ public void addLeadInfo(FormInfo leadInfo, boolean broadcast) { LeadDTO lead = leadInfo.getLead(); LeadEntity leadEntity = leadDAO.getLeadByMail(lead.getMail()); if (leadEntity == null) { - lead.setCreatedDate(new Date().getTime()); - lead.setUpdatedDate(new Date().getTime()); + leadInfo.getLead().setId(null); + if (lead.getCreatedDate() == null) + lead.setCreatedDate(new Date().getTime()); + if (lead.getUpdatedDate() == null) + lead.setUpdatedDate(new Date().getTime()); if (lead.getStatus() == null) { lead.setStatus(LEAD_DEFAULT_STATUS); } @@ -87,6 +105,7 @@ public void addLeadInfo(FormInfo leadInfo, boolean broadcast) { lead.setBlogSubscriptionDate(new Date().getTime()); } leadEntity = createLead(lead); + leadInfo.setLead(toLeadDto(leadEntity)); if (broadcast) { listenerService.broadcast(NEW_LEAD_EVENT, leadEntity, ""); } @@ -94,6 +113,7 @@ public void addLeadInfo(FormInfo leadInfo, boolean broadcast) { leadEntity = mergeLead(leadEntity, lead); leadEntity.setUpdatedDate(new Date().getTime()); leadEntity = leadDAO.update(leadEntity); + leadInfo.setLead(toLeadDto(leadEntity)); } addResponse(leadInfo.getResponse(), leadEntity); } catch (Exception e) { @@ -336,6 +356,56 @@ public LeadEntity mergeLead(LeadEntity leadEntity, LeadDTO leadDTO) { return leadEntity; } + public void synchronizeLead(FormInfo leadInfo) throws Exception { + LeadCaptureSettings settings = leadCaptureSettingsService.getSettings(); + if (!settings.isLeadManagementServer()) { + String errMessage = ""; + String url = settings.getLeadManagementServerUrl(); + JsonValue leadInfo_ = new ObjectValue(); + leadInfo_.addElement("lead", toJson(leadInfo.getLead())); + leadInfo_.addElement("response", toJson(leadInfo.getResponse())); + URL obj = new URL(url); + HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection(); + postConnection.setRequestMethod("POST"); + postConnection.setRequestProperty("Token", settings.getLeadManagementToken()); + postConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + postConnection.setDoOutput(true); + try (OutputStream os = postConnection.getOutputStream()) { + os.write(leadInfo_.toString().getBytes(StandardCharsets.UTF_8)); + os.flush(); + } catch (IOException e) { + createUnsynchronizedLead(leadInfo); + LOG.error("User not synchronized",e.getMessage()); + } + int responseCode = postConnection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { // success + NotSynchroLeadEntity notSynchroLeadEntity = notSynchronizedLeadsDAO.getNotSynchLead(leadInfo.getLead().getId(), + leadInfo.getResponse().getId()); + if (notSynchroLeadEntity != null) { + notSynchronizedLeadsDAO.delete(notSynchroLeadEntity); + } + LOG.info("New lead synchronized with the lead management server"); + } else { + createUnsynchronizedLead(leadInfo); + try (BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getErrorStream()))) { + String inputLine; + StringBuffer response = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode infoNode = objectMapper.readTree(response.toString()); + errMessage = infoNode.get("message").textValue(); + if (infoNode.get("errors") != null) { + errMessage = errMessage + ": " + infoNode.get("errors").elements().next().get("message").textValue(); + } + } catch (IOException e) { + LOG.error(errMessage, e); + } + } + } + } + public JSONObject toFormJson(FormEntity formEntity) throws JSONException { JSONObject formJson = new JSONObject(); formJson.put("id", formEntity.getId()); @@ -456,6 +526,7 @@ public ResponseDTO toResponseDto(ResponseEntity responseEntity) { ResponseDTO responseDTO = new ResponseDTO(); responseDTO.setId(responseEntity.getId()); responseDTO.setForm(toFormDto(responseEntity.getFormEntity())); + responseDTO.setFormName(responseEntity.getFormEntity().getName()); List fields = new ArrayList<>(); for (FieldEntity fieald : fieldDAO.getFileldsByResponse(responseEntity.getId())) { fields.add(toFieldDto(fieald)); @@ -476,4 +547,20 @@ public ResponseEntity toResponseEntity(ResponseDTO responseDTO) { return responseEntity; } + public void createUnsynchronizedLead(FormInfo leadInfo){ + + NotSynchroLeadEntity notSynchroLeadEntity = notSynchronizedLeadsDAO.getNotSynchLead(leadInfo.getLead().getId(), + leadInfo.getResponse().getId()); + if (notSynchroLeadEntity == null) { + notSynchroLeadEntity = new NotSynchroLeadEntity(); + notSynchroLeadEntity.setAtteptsNumber(1); + notSynchroLeadEntity.setLeadEntity(toLeadEntity(leadInfo.getLead())); + notSynchroLeadEntity.setResponseEntity(toResponseEntity(leadInfo.getResponse())); + notSynchronizedLeadsDAO.create(notSynchroLeadEntity); + } else { + notSynchroLeadEntity.setAtteptsNumber(notSynchroLeadEntity.getAtteptsNumber() + 1); + notSynchronizedLeadsDAO.create(notSynchroLeadEntity); + } + } + } diff --git a/services/src/main/resources/conf/portal/configuration.xml b/services/src/main/resources/conf/portal/configuration.xml index 0c42d74..75ec9f1 100644 --- a/services/src/main/resources/conf/portal/configuration.xml +++ b/services/src/main/resources/conf/portal/configuration.xml @@ -29,6 +29,10 @@ org.exoplatform.leadcapture.dao.MailContentDAO + + org.exoplatform.leadcapture.dao.NotSynchronizedLeadsDAO + + org.exoplatform.leadcapture.services.LeadsManagementService @@ -85,7 +89,14 @@ org.exoplatform.leadcapture.listeners.NewCommunityRegistrationListener - + + org.exoplatform.services.listener.ListenerService + + leadCapture.newLeadSynchronize.event + addListener + org.exoplatform.leadcapture.listeners.LeadSynchronizeListener + + org.exoplatform.services.listener.ListenerService @@ -111,6 +122,24 @@ - + + org.exoplatform.services.scheduler.JobSchedulerService + + AddCronJob + addCronJob + org.exoplatform.services.scheduler.CronJob + + + + cronjob.info + Configuration for Lead Synchronize Job + + + + + + + + \ No newline at end of file diff --git a/services/src/main/resources/db/changelog/lead-capture.db.changelog-1.0.0.xml b/services/src/main/resources/db/changelog/lead-capture.db.changelog-1.0.0.xml index 13ee2e6..e2e03e5 100644 --- a/services/src/main/resources/db/changelog/lead-capture.db.changelog-1.0.0.xml +++ b/services/src/main/resources/db/changelog/lead-capture.db.changelog-1.0.0.xml @@ -174,6 +174,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/war/src/main/webapp/vue-app/components/LeadCaptureSettingsApp.vue b/war/src/main/webapp/vue-app/components/LeadCaptureSettingsApp.vue index 79d5dda..cbf607b 100644 --- a/war/src/main/webapp/vue-app/components/LeadCaptureSettingsApp.vue +++ b/war/src/main/webapp/vue-app/components/LeadCaptureSettingsApp.vue @@ -9,18 +9,50 @@ - - - - - - - + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + +
@@ -47,7 +79,7 @@ export default { alert_type: '', alertIcon: '', valid: true, - setting:{}, + setting: {}, rules: { required: value => !!value || 'Required.', counter: value => value.length >= 3 || 'Min 3 characters', @@ -97,7 +129,7 @@ export default { }); }); }, - displaySusccessMessage(message) { + displaySusccessMessage(message) { this.message = message; this.alert_type = 'alert-success'; this.alertIcon = 'uiIconSuccess'; @@ -135,4 +167,4 @@ select { #leadCaptureSettingsApp .v-toolbar .v-input { margin-left: 18px; } - \ No newline at end of file +