r125 - in trunk: faxtomail-service/src/main/java/com/franciaflex/faxtomail/services faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job
Author: kmorin Date: 2014-06-02 23:06:54 +0200 (Mon, 02 Jun 2014) New Revision: 125 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/125 Log: find client for the incoming emails Added: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ClientService.java Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceSupport.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/EmailService.java trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceSupport.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceSupport.java 2014-06-02 20:34:14 UTC (rev 124) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/FaxToMailServiceSupport.java 2014-06-02 21:06:54 UTC (rev 125) @@ -26,6 +26,7 @@ import com.franciaflex.faxtomail.FaxToMailConfiguration; import com.franciaflex.faxtomail.persistence.entities.FaxToMailTopiaPersistenceContext; +import com.franciaflex.faxtomail.services.service.ClientService; import com.franciaflex.faxtomail.services.service.ConfigurationService; import com.franciaflex.faxtomail.services.service.EmailService; import com.franciaflex.faxtomail.services.service.FaxToMailWebApplicationContext; @@ -82,4 +83,8 @@ public ReferentielService getReferentielService() { return newService(ReferentielService.class); } + + public ClientService getClientService() { + return newService(ClientService.class); + } } Added: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ClientService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ClientService.java (rev 0) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ClientService.java 2014-06-02 21:06:54 UTC (rev 125) @@ -0,0 +1,74 @@ +package com.franciaflex.faxtomail.services.service; + +import com.franciaflex.faxtomail.persistence.entities.Client; +import com.franciaflex.faxtomail.persistence.entities.ClientTopiaDao; +import com.franciaflex.faxtomail.persistence.entities.MailFolder; +import com.franciaflex.faxtomail.services.FaxToMailServiceSupport; +import com.google.common.base.Function; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multimaps; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.t; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class ClientService extends FaxToMailServiceSupport { + + private static final Log log = LogFactory.getLog(ClientService.class); + + public Client getClientForEmailAddress(String emailAddress, MailFolder folder) { + ClientTopiaDao clientDao = getPersistenceContext().getClientDao(); + + Client client = null; + List<Client> clients; + if (StringUtils.isNotBlank(emailAddress)) { + clients = clientDao.forEmailAddressEquals(emailAddress).findAll(); + + if (CollectionUtils.isEmpty(clients)) { + clients = clientDao.forFaxNumberEquals(emailAddress).findAll(); + } + + if (CollectionUtils.isNotEmpty(clients)) { + if (clients.size() == 1) { + client = clients.get(0); + + } else if (folder != null) { + Multimap<String, Client> clientsByBrand = Multimaps.index(clients, new Function<Client, String>() { + @Override + public String apply(Client client) { + return client.getBrand(); + } + }); + Set<String> brands = clientsByBrand.keySet(); + while (client == null && folder != null) { + if (brands.contains(folder.getName())) { + client = new ArrayList<>(clientsByBrand.get(folder.getName())).get(0); + } + folder = folder.getParent(); + } + } + } + } + return client; + } + + public Client getClientForCode(String code) { + ClientTopiaDao clientDao = getPersistenceContext().getClientDao(); + Client client = null; + if (StringUtils.isNotBlank(code)) { + client = clientDao.forCodeEquals(code).findAnyOrNull(); + } + return client; + } +} Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/EmailService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/EmailService.java 2014-06-02 20:34:14 UTC (rev 124) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/EmailService.java 2014-06-02 21:06:54 UTC (rev 125) @@ -116,19 +116,19 @@ * @throws InvalidClientException if client code is not valid */ public Email saveEmail(Email email, FaxToMailUser user, String... modifiedFields) throws InvalidClientException { - return saveEmail(email, null, null, user, modifiedFields); + Client client = email.getClient(); + return saveEmail(email, null, client != null ? client.getCode() : null, user, modifiedFields); } public Email saveEmail(Email email, Collection<Attachment> attachments, String clientCode, FaxToMailUser user, String... modifiedFields) throws InvalidClientException { Date now = getNow(); - ClientTopiaDao clientDao = getPersistenceContext().getClientDao(); EmailTopiaDao dao = getPersistenceContext().getEmailDao(); HistoryTopiaDao historyDao = getPersistenceContext().getHistoryDao(); // if client code is null, do not manage client at all Client client = null; if (StringUtils.isNotBlank(clientCode)) { - client = clientDao.forCodeEquals(clientCode).findAnyOrNull(); + client = getClientService().getClientForCode(clientCode); if (client == null) { String message = t("faxtomail.service.email.save.clientCode.error", clientCode); throw new InvalidClientException(message); @@ -171,12 +171,14 @@ if (!attachment.isPersisted()) { // persist using cascade - currentAttachments.add(currentAttachment); + email.addAttachment(currentAttachment); } } // delete not found attachments - currentAttachments.removeAll(currentAttachmentIndex.values()); + for (Attachment attachment : currentAttachmentIndex.values()) { + email.removeAttachment(attachment); + } } if (email.getRangeRow() != null) { Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java 2014-06-02 20:34:14 UTC (rev 124) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java 2014-06-02 21:06:54 UTC (rev 125) @@ -55,6 +55,8 @@ import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; +import com.franciaflex.faxtomail.persistence.entities.Client; +import com.franciaflex.faxtomail.services.service.ClientService; import org.apache.commons.io.Charsets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -171,7 +173,6 @@ for (int i = 0 ; i < count ; i++) { Email email = new EmailImpl(); - emailService.saveEmail(email, null); int messageNumber = count - i; Message message = folder.getMessage(messageNumber); @@ -184,12 +185,6 @@ // email.s(message.getSubject()); // modifiedProperties.add(Email.PROPERTY_OBJECT); - Address[] addresses = message.getFrom(); - if (addresses != null && addresses.length > 0) { - email.setSender(addresses[0].toString()); - modifiedProperties.add(Email.PROPERTY_SENDER); - } - List<Address> recipients = new ArrayList<>(); Address[] toRecipients = message.getRecipients(Message.RecipientType.TO); if (toRecipients != null) { @@ -214,7 +209,26 @@ break; } } + if (email.getMailFolder() == null) { + break; + } + emailService.saveEmail(email, null); + Address[] addresses = message.getFrom(); + if (addresses != null && addresses.length > 0) { + String sender = addresses[0].toString(); + email.setSender(sender); + modifiedProperties.add(Email.PROPERTY_SENDER); + + Client client = serviceContext.newService(ClientService.class) + .getClientForEmailAddress(sender, email.getMailFolder()); + //TODO kmorin 20140602 find the responsible's folder +// if (client != null) { +// client.getCustomerResponsible() +// } + email.setClient(client); + } + Date receivedDate = message.getReceivedDate(); if (receivedDate == null) { receivedDate = message.getSentDate();
participants (1)
-
kmorin@users.forge.codelutin.com