r33 - in trunk: . faxtomail-persistence/src/main/xmi faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande faxtomail-ui-web faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web faxtomail-ui-web/src/main/resources
Author: kmorin Date: 2014-04-14 15:43:16 +0200 (Mon, 14 Apr 2014) New Revision: 33 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/33 Log: refs #4653 [TECH] Gestion des mails / fax entrants Added: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/MailFilterJob.java trunk/faxtomail-ui-web/src/main/resources/quartz.properties Removed: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFilterService.java Modified: trunk/faxtomail-persistence/src/main/xmi/faxtomail.zargo trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/CompanyService.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ConfigurationService.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/EmailService.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/InitFaxToMailService.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.css trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.jaxx trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIHandler.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIModel.java trunk/faxtomail-ui-web/ trunk/faxtomail-ui-web/pom.xml trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailApplicationListener.java trunk/pom.xml Modified: trunk/faxtomail-persistence/src/main/xmi/faxtomail.zargo =================================================================== (Binary files differ) Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/CompanyService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/CompanyService.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/CompanyService.java 2014-04-14 13:43:16 UTC (rev 33) @@ -3,7 +3,10 @@ import com.franciaflex.faxtomail.persistence.entities.Company; import com.franciaflex.faxtomail.persistence.entities.CompanyTopiaDao; import com.franciaflex.faxtomail.persistence.entities.Configuration; +import com.franciaflex.faxtomail.persistence.entities.EmailAccount; +import com.franciaflex.faxtomail.persistence.entities.EmailAccountTopiaDao; import com.franciaflex.faxtomail.persistence.entities.MailFilter; +import com.franciaflex.faxtomail.persistence.entities.MailFilterTopiaDao; import com.franciaflex.faxtomail.persistence.entities.MailFolder; import com.franciaflex.faxtomail.services.FaxToMailServiceSupport; import com.google.common.base.Function; @@ -63,8 +66,7 @@ filter.setMailFolder(folder); } } - MailFilterService mailFilterService = getMailFilterService(); - Collection<MailFilter> filters = mailFilterService.saveMailFilters(mailFilters.keySet()); + Collection<MailFilter> filters = saveMailFilters(mailFilters.keySet()); company.setMailFilter(filters); CompanyTopiaDao dao = getPersistenceContext().getCompanyDao(); @@ -74,4 +76,35 @@ return company; } + + public Collection<MailFilter> saveMailFilters(Collection<MailFilter> mailFilters) { + MailFilterTopiaDao dao = getPersistenceContext().getMailFilterDao(); + + Collection<MailFilter> result = new ArrayList<>(); + for (MailFilter mailFilter : mailFilters) { + + MailFilter filter; + boolean create = mailFilter.getTopiaId() == null; + if (create) { + filter = dao.newInstance(); + } else { + filter = dao.findByTopiaId(mailFilter.getTopiaId()); + } + filter.setExpression(mailFilter.getExpression()); + filter.setMailFolder(mailFilter.getMailFolder()); + + if (create) { + filter = dao.create(filter); + } else { + filter = dao.update(filter); + } + result.add(filter); + } + return result; + } + + public Collection<EmailAccount> getEmailAccounts() { + EmailAccountTopiaDao emailAccountTopiaDao = getPersistenceContext().getEmailAccountDao(); + return new ArrayList<>(emailAccountTopiaDao.findAll()); + } } Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ConfigurationService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ConfigurationService.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ConfigurationService.java 2014-04-14 13:43:16 UTC (rev 33) @@ -3,12 +3,16 @@ import com.franciaflex.faxtomail.persistence.entities.CompanyTopiaDao; import com.franciaflex.faxtomail.persistence.entities.Configuration; import com.franciaflex.faxtomail.persistence.entities.ConfigurationTopiaDao; +import com.franciaflex.faxtomail.persistence.entities.MailFilter; +import com.franciaflex.faxtomail.persistence.entities.MailFilterTopiaDao; import com.franciaflex.faxtomail.services.FaxToMailServiceSupport; import com.google.common.collect.Lists; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.persistence.TopiaIdFactory; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** 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-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/EmailService.java 2014-04-14 13:43:16 UTC (rev 33) @@ -43,10 +43,6 @@ private static final Log log = LogFactory.getLog(EmailService.class); - public Email getRandomEmail() { - return getPersistenceContext().getEmailDao().forArchiveDateEquals(null).findAny(); - } - public Email getEmailById(String id) { Email email = getPersistenceContext().getEmailDao().findByTopiaId(id); return email; Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/InitFaxToMailService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/InitFaxToMailService.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/InitFaxToMailService.java 2014-04-14 13:43:16 UTC (rev 33) @@ -33,6 +33,8 @@ import com.franciaflex.faxtomail.persistence.entities.DemandType; import com.franciaflex.faxtomail.persistence.entities.DemandTypeTopiaDao; import com.franciaflex.faxtomail.persistence.entities.Email; +import com.franciaflex.faxtomail.persistence.entities.EmailAccount; +import com.franciaflex.faxtomail.persistence.entities.EmailAccountTopiaDao; import com.franciaflex.faxtomail.persistence.entities.EmailTopiaDao; import com.franciaflex.faxtomail.persistence.entities.EtatAttente; import com.franciaflex.faxtomail.persistence.entities.EtatAttenteTopiaDao; @@ -62,11 +64,16 @@ import org.nuiton.jaxx.application.ApplicationTechnicalException; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.Properties; import java.util.Random; import static org.nuiton.i18n.I18n.t; @@ -138,16 +145,7 @@ long count = demandTypeDao.count(); if (count == 0) { - List<Client> clients; - try { - File clientFile = new File(ClassLoader.getSystemResource("fx_clients.csv").toURI()); - clients = getReferentielService().importClients(clientFile); - } catch(Exception e) { - log.error("error", e); - return; - } - List<DemandType> types = Lists.newArrayList( demandTypeDao.create(DemandType.PROPERTY_LABEL, "Commande"), demandTypeDao.create(DemandType.PROPERTY_LABEL, "Commande Réponse"), @@ -243,31 +241,16 @@ MailFolder.PROPERTY_ETAT_ATTENTE, etatAttentes.subList(0, random.nextInt(etatAttenteSize - 8) + 2)); chargesClientelle.addChildren(cyrilFolder); - int r = random.nextInt(10) + 5; - for (int i = 0 ; i < r ; i++) { - createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, cyrilFolder, cyril); - } - MailFolder claireFolder = folderDao.create(MailFolder.PROPERTY_NAME, "Claire", MailFolder.PROPERTY_PARENT, chargesClientelle); chargesClientelle.addChildren(claireFolder); - r = random.nextInt(10) + 5; - for (int i = 0 ; i < r ; i++) { - createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, claireFolder, claire); - } - Collections.shuffle(etatAttentes); MailFolder agatheFolder = folderDao.create(MailFolder.PROPERTY_NAME, "Agathe", MailFolder.PROPERTY_PARENT, chargesClientelle, MailFolder.PROPERTY_ETAT_ATTENTE, etatAttentes.subList(0, random.nextInt(etatAttenteSize - 8) + 2)); chargesClientelle.addChildren(agatheFolder); - r = random.nextInt(10) + 5; - for (int i = 0 ; i < r ; i++) { - createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, agatheFolder, agathe); - } - MailFolder sav = folderDao.create(MailFolder.PROPERTY_NAME, "SAV", MailFolder.PROPERTY_ETAT_ATTENTE, etatAttentes.subList(0, random.nextInt(etatAttenteSize - 8) + 2)); folders.add(sav); @@ -276,47 +259,83 @@ MailFolder.PROPERTY_PARENT, sav); sav.addChildren(marcFolder); - r = random.nextInt(10) + 5; - for (int i = 0 ; i < r ; i++) { - createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, marcFolder, marc); - } - MailFolder fredericFolder = folderDao.create(MailFolder.PROPERTY_NAME, "Frédéric", MailFolder.PROPERTY_PARENT, sav); sav.addChildren(fredericFolder); - r = random.nextInt(10) + 5; - for (int i = 0 ; i < r ; i++) { - createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, fredericFolder, frederic); - } - MailFolder jeanneFolder = folderDao.create(MailFolder.PROPERTY_NAME, "Jeanne", MailFolder.PROPERTY_PARENT, sav); sav.addChildren(jeanneFolder); - r = random.nextInt(10) + 5; - for (int i = 0 ; i < r ; i++) { - createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, jeanneFolder, jeanne); - } - Collections.shuffle(etatAttentes); - // companies CompanyTopiaDao companyTopiaDao = getPersistenceContext().getCompanyDao(); ConfigurationTopiaDao configurationTopiaDao = getPersistenceContext().getConfigurationDao(); - companyTopiaDao.create(Company.PROPERTY_NAME, "Franciaflex", Company.PROPERTY_CONFIGURATION, configurationTopiaDao.create(), Company.PROPERTY_MAIL_FOLDER, folders); + Company fx = companyTopiaDao.create(Company.PROPERTY_NAME, "Franciaflex", Company.PROPERTY_CONFIGURATION, configurationTopiaDao.create(), Company.PROPERTY_MAIL_FOLDER, folders); companyTopiaDao.create(Company.PROPERTY_NAME, "Faber", Company.PROPERTY_CONFIGURATION, configurationTopiaDao.create()); companyTopiaDao.create(Company.PROPERTY_NAME, "France Fermeture", Company.PROPERTY_CONFIGURATION, configurationTopiaDao.create()); + try { + URL mailAccountsPropertiesURL = ClassLoader.getSystemResource("mail_accounts.properties"); + if (mailAccountsPropertiesURL != null) { + InputStream mailAccountStream = new FileInputStream(mailAccountsPropertiesURL.getPath()); + Properties properties = new Properties(); + properties.load(mailAccountStream); + String protocol = (String) properties.get("mail.protocol"); + String host = (String) properties.get("mail.host"); + String user = (String) properties.get("mail.user"); + String password = (String) properties.get("mail.password"); + + EmailAccountTopiaDao emailAccountTopiaDao = getPersistenceContext().getEmailAccountDao(); + EmailAccount emailAccount = emailAccountTopiaDao.create(EmailAccount.PROPERTY_PROTOCOL, protocol, + EmailAccount.PROPERTY_HOST, host, + EmailAccount.PROPERTY_USER, user, + EmailAccount.PROPERTY_PASSWORD, password); + fx.addEmailAccount(emailAccount); + companyTopiaDao.update(fx); + } + + } catch(IOException e) { + log.error("error getting the email accounts", e); + return; + } + FaxToMailUserGroupTopiaDao userGroupDao = getPersistenceContext().getFaxToMailUserGroupDao(); userGroupDao.create(FaxToMailUserGroup.PROPERTY_NAME, "Chargés de clientèle"); userGroupDao.create(FaxToMailUserGroup.PROPERTY_NAME, "Commerciaux"); userGroupDao.create(FaxToMailUserGroup.PROPERTY_NAME, "SAV"); userGroupDao.create(FaxToMailUserGroup.PROPERTY_NAME, "Administrateurs"); + try { + URL fxClientsURL = ClassLoader.getSystemResource("fx_clients.csv"); + if (fxClientsURL != null) { + File clientFile = new File(fxClientsURL.toURI()); + List<Client> clients = getReferentielService().importClients(clientFile); + + createEmails(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, cyrilFolder, cyril); + createEmails(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, claireFolder, claire); + createEmails(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, agatheFolder, agathe); + createEmails(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, marcFolder, marc); + createEmails(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, fredericFolder, frederic); + createEmails(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, jeanneFolder, jeanne); + } + + } catch(Exception e) { + log.error("error getting the client file", e); + } + getPersistenceContext().commit(); } } + protected void createEmails(Random random, HistoryTopiaDao historyDao, RangeRowTopiaDao rangeRowDao, EmailTopiaDao emailDao, + List<Range> ranges, List<Client> clients, List<DemandType> types, List<Priority> priorities, + MailFolder folder, FaxToMailUser user) { + int r = random.nextInt(10) + 5; + for (int i = 0 ; i < r ; i++) { + createEmail(random, historyDao, rangeRowDao, emailDao, ranges, clients, types, priorities, folder, user); + } + } + protected void createEmail(Random random, HistoryTopiaDao historyDao, RangeRowTopiaDao rangeRowDao, Deleted: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFilterService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFilterService.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFilterService.java 2014-04-14 13:43:16 UTC (rev 33) @@ -1,41 +0,0 @@ -package com.franciaflex.faxtomail.services.service; - -import com.franciaflex.faxtomail.persistence.entities.MailFilter; -import com.franciaflex.faxtomail.persistence.entities.MailFilterTopiaDao; -import com.franciaflex.faxtomail.services.FaxToMailServiceSupport; - -import java.util.ArrayList; -import java.util.Collection; - -/** - * @author kmorin <kmorin@codelutin.com> - * @since x.x - */ -public class MailFilterService extends FaxToMailServiceSupport { - - public Collection<MailFilter> saveMailFilters(Collection<MailFilter> mailFilters) { - MailFilterTopiaDao dao = getPersistenceContext().getMailFilterDao(); - - Collection<MailFilter> result = new ArrayList<>(); - for (MailFilter mailFilter : mailFilters) { - - MailFilter filter; - boolean create = mailFilter.getTopiaId() == null; - if (create) { - filter = dao.newInstance(); - } else { - filter = dao.findByTopiaId(mailFilter.getTopiaId()); - } - filter.setExpression(mailFilter.getExpression()); - filter.setMailFolder(mailFilter.getMailFolder()); - - if (create) { - filter = dao.create(filter); - } else { - filter = dao.update(filter); - } - result.add(filter); - } - return result; - } -} Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java 2014-04-14 13:43:16 UTC (rev 33) @@ -1,5 +1,6 @@ package com.franciaflex.faxtomail.services.service; +import com.franciaflex.faxtomail.persistence.entities.Email; import com.franciaflex.faxtomail.persistence.entities.EtatAttente; import com.franciaflex.faxtomail.persistence.entities.MailFolder; import com.franciaflex.faxtomail.persistence.entities.MailFolderTopiaDao; @@ -24,6 +25,11 @@ private static final Log log = LogFactory.getLog(MailFolderService.class); + + public MailFolder getRandomFolder() { + return getPersistenceContext().getMailFolderDao().forParentEquals(null).findAny(); + } + public List<MailFolder> getAllMailFolders() { MailFolderTopiaDao dao = getPersistenceContext().getMailFolderDao(); return new ArrayList<MailFolder>(dao.findAll()); Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.css =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.css 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.css 2014-04-14 13:43:16 UTC (rev 33) @@ -154,6 +154,7 @@ #mailBodyField { editable: false; + text: { model.getContent() }; } #takenByLabel { Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.jaxx =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.jaxx 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUI.jaxx 2014-04-14 13:43:16 UTC (rev 33) @@ -293,7 +293,7 @@ constraints="BorderLayout.NORTH"/> <JScrollPane id='mailBodyPane' constraints="BorderLayout.CENTER"> - <JEditorPane id='mailBodyField'/> + <JTextPane id='mailBodyField'/> </JScrollPane> </JPanel> <JPanel layout="{new BorderLayout()}"> Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIHandler.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIHandler.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIHandler.java 2014-04-14 13:43:16 UTC (rev 33) @@ -60,9 +60,15 @@ import org.nuiton.util.beans.BeanMonitor; import javax.swing.*; +import javax.swing.event.HyperlinkEvent; +import javax.swing.event.HyperlinkListener; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.TableCellEditor; +import javax.swing.text.Document; +import javax.swing.text.Style; +import javax.swing.text.StyleConstants; +import javax.swing.text.StyleContext; import javax.swing.text.html.HTMLEditorKit; import java.awt.*; import java.awt.event.KeyAdapter; @@ -134,26 +140,37 @@ } initBeanFilterableComboBox(ui.getEtatAttenteComboBox(), etatAttentes, model.getEtatAttente()); - JEditorPane editor = ui.getMailBodyField(); - HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); - editor.setEditorKit(htmlEditorKit); -// create some simple html as a string - String htmlString = "<html>\n" - + "<body>\n" - + "<script>document.getElementById('test').innerHTML = 'chaussette'</script>" - + "<h1 id='test'>Welcome!</h1>\n" - + "<h2>This is an H2 header</h2><br/>" - + "<p>This is some sample text</p>\n" - + "<p><a href=\"http://devdaily.com/blog/\">devdaily blog</a></p>\n" - + "<p><img src=\"http://jarnal.wikispaces.com/space/showlogo/1301230030/logo.png\"/></p>\n" - + "</body>\n"; + JTextPane editor = ui.getMailBodyField(); + String content = model.getContent(); - htmlString = htmlString.replaceAll("<img src=\"(.*?)\"(/>|>.*?</img>)", "[image : $1]"); - htmlString = htmlString.replaceAll("<a href=\"(.*?)\">(.*?)</a>", "[$2 : $1]"); + if (model.getContentType() != null) { + editor.setContentType(model.getContentType()); -// create a document, set it on the jeditorpane, then add the html - editor.setText(htmlString); + if (model.getContentType().contains("text/html")) { + HTMLEditorKit htmlEditorKit = new HTMLEditorKit(); + editor.setEditorKit(htmlEditorKit); + editor.addHyperlinkListener(new HyperlinkListener() { + @Override + public void hyperlinkUpdate(HyperlinkEvent e) { + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { + FaxToMailUIUtil.openLink(e.getURL()); + } + } + }); + for (Attachment attachment : model.getAttachment()) { + File file = attachment.getOriginalFile(); + log.info("file " + file.getName()); + String key = file.getName(); + // the meta tag makes the content is not displayed + content = content.replaceAll("<meta (.*?)>(</meta>)?", ""); + // replace the inline attachments with the extracted attachment file url + content = content.replaceAll("<img src=\"cid:" + key + "\"(.*?>)", "<img src=\"file://" + file.getAbsolutePath() + "\"$1"); + } + } + } + editor.setText(content); + // init table final JXTable table = ui.getRangeTable(); Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIModel.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIModel.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/content/demande/DemandeUIModel.java 2014-04-14 13:43:16 UTC (rev 33) @@ -189,6 +189,26 @@ return editObject.getObject(); } + public String getContent() { + return editObject.getContent(); + } + + public void setContent(String content) { + Object oldValue = getContent(); + editObject.setContent(content); + firePropertyChanged(Email.PROPERTY_CONTENT, oldValue, content); + } + + public String getContentType() { + return editObject.getContentType(); + } + + public void setContentType(String contentType) { + Object oldValue = getContentType(); + editObject.setContentType(contentType); + firePropertyChanged(Email.PROPERTY_CONTENT_TYPE, oldValue, contentType); + } + public void setClientCode(String clientCode) { Object oldValue = getClientCode(); Client client = editObject.getClient(); Property changes on: trunk/faxtomail-ui-web ___________________________________________________________________ Added: svn:ignore + target *.iml Modified: trunk/faxtomail-ui-web/pom.xml =================================================================== --- trunk/faxtomail-ui-web/pom.xml 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-ui-web/pom.xml 2014-04-14 13:43:16 UTC (rev 33) @@ -171,6 +171,16 @@ <scope>runtime</scope> </dependency> + <dependency> + <groupId>org.quartz-scheduler</groupId> + <artifactId>quartz</artifactId> + </dependency> + + <dependency> + <groupId>javax.mail</groupId> + <artifactId>mail</artifactId> + </dependency> + </dependencies> <build> Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailApplicationListener.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailApplicationListener.java 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/FaxToMailApplicationListener.java 2014-04-14 13:43:16 UTC (rev 33) @@ -25,8 +25,20 @@ */ import com.franciaflex.faxtomail.FaxToMailApplicationContext; +import com.franciaflex.faxtomail.persistence.entities.FaxToMailTopiaPersistenceContext; +import com.franciaflex.faxtomail.persistence.entities.MailFilter; +import com.franciaflex.faxtomail.services.FaxToMailServiceContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.quartz.CronScheduleBuilder; +import org.quartz.JobBuilder; +import org.quartz.JobDataMap; +import org.quartz.JobDetail; +import org.quartz.Scheduler; +import org.quartz.SchedulerException; +import org.quartz.Trigger; +import org.quartz.TriggerBuilder; +import org.quartz.impl.StdSchedulerFactory; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -44,6 +56,8 @@ log.info("init FaxToMail web ui"); } + log.debug("scheduler lanuched"); + applicationContext = new FaxToMailApplicationContext(); applicationContext.init(); @@ -52,6 +66,35 @@ FaxToMailApplicationContext.APPLICATION_CONTEXT_PARAMETER, applicationContext); + JobDataMap data = new JobDataMap(); + + FaxToMailTopiaPersistenceContext persistenceContext = applicationContext.newPersistenceContext(); + FaxToMailServiceContext serviceContext = applicationContext.newServiceContext(persistenceContext); + data.put(MailFilterJob.SERVICE_CONTEXT, serviceContext); + + JobDetail job = JobBuilder.newJob(MailFilterJob.class) + .usingJobData(data) + .withIdentity("faxToMailJobs", "job1") + .build(); + + try { + Trigger trigger = TriggerBuilder + .newTrigger() + .withIdentity("mailFiltering", "trigger1") +// .withSchedule(CronScheduleBuilder.cronSchedule("* 0/1 * * * ?")) + .withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?")) + .build(); + + Scheduler scheduler = new StdSchedulerFactory().getScheduler(); + scheduler.start(); + scheduler.scheduleJob(job, trigger); + + log.debug("scheduler launched"); + + } catch (SchedulerException e) { + log.error("Error while launching the mail filter job", e); + } + } @Override Added: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/MailFilterJob.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/MailFilterJob.java (rev 0) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/MailFilterJob.java 2014-04-14 13:43:16 UTC (rev 33) @@ -0,0 +1,269 @@ +package com.franciaflex.faxtomail.web; + +import com.franciaflex.faxtomail.persistence.entities.Attachment; +import com.franciaflex.faxtomail.persistence.entities.AttachmentImpl; +import com.franciaflex.faxtomail.persistence.entities.DemandStatus; +import com.franciaflex.faxtomail.persistence.entities.Email; +import com.franciaflex.faxtomail.persistence.entities.EmailAccount; +import com.franciaflex.faxtomail.persistence.entities.EmailImpl; +import com.franciaflex.faxtomail.persistence.entities.MailFolder; +import com.franciaflex.faxtomail.services.DecoratorService; +import com.franciaflex.faxtomail.services.FaxToMailServiceContext; +import com.franciaflex.faxtomail.services.service.CompanyService; +import com.franciaflex.faxtomail.services.service.EmailService; +import com.franciaflex.faxtomail.services.service.MailFolderService; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.decorator.Decorator; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; + +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.mail.Address; +import javax.mail.BodyPart; +import javax.mail.Flags; +import javax.mail.Folder; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Part; +import javax.mail.Session; +import javax.mail.Store; +import javax.mail.URLName; +import javax.mail.internet.MimeMultipart; +import java.io.File; +import java.io.FileOutputStream; +import java.util.Collection; +import java.util.Date; +import java.util.Properties; + +import static org.nuiton.i18n.I18n.t; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class MailFilterJob implements Job { + + private static final Log log = LogFactory.getLog(MailFilterJob.class); + + public static final String SERVICE_CONTEXT = "serviceContext"; + + protected FaxToMailServiceContext serviceContext; + + //TODO kmorin 20140414 handle imap protocol + @Override + public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + serviceContext = (FaxToMailServiceContext) jobExecutionContext.getMergedJobDataMap().get(SERVICE_CONTEXT); + + CompanyService companyService = serviceContext.newService(CompanyService.class); + Collection<EmailAccount> emailAccounts = companyService.getEmailAccounts(); + + for (EmailAccount account : emailAccounts) { + if ("pop3".equals(account.getProtocol().toLowerCase())) { + checkEmailsOnPop3(account); + } + } + } + + /** + * Checks the emails of the account + * @param account + */ + public void checkEmailsOnPop3(EmailAccount account) { + Properties properties = new Properties(); + properties.setProperty("mail.store.protocol", "pop3"); + properties.setProperty("mail.pop3.host", account.getHost()); + properties.setProperty("mail.pop3.user", account.getUser()); + + + Session session = Session.getInstance(properties); + Store store = null; + Folder defaultFolder = null; + Folder inbox = null; + + try { + store = session.getStore(new URLName("pop3://" + account.getHost())); + store.connect(account.getUser(), account.getPassword()); + defaultFolder = store.getDefaultFolder(); + inbox = defaultFolder.getFolder("INBOX"); + checkEmailsOfFolder(inbox); + + } catch (Exception e) { + log.error("Error while getting emails from the mailbox", e); + + } finally { + close(inbox); + close(defaultFolder); + try { + if (store != null && store.isConnected()) { + store.close(); + } + } catch (MessagingException e) { + log.error("Error while closing the store", e); + } + } + + } + + protected void close(Folder folder) { + if (folder != null && folder.isOpen()) { + try { + // TODO 20140414 kmorin close(true) in production + folder.close(false); // false -> On n'efface pas les messages marqués DELETED + + } catch (Exception e) { + log.error("Error while closing the folder", e); + } + } + } + + /** + * Check the emails of teh folder, create the emails in the database and delete the email in the folder + * @param folder the folder to check + */ + protected void checkEmailsOfFolder(Folder folder) { + try { + folder.open(Folder.READ_WRITE); + + int count = folder.getMessageCount(); + int unread = folder.getUnreadMessageCount(); + + log.debug(count + " messages, " + unread + " unread"); + + for (int i = 0 ; i < count ; i++ ) { + Email email = new EmailImpl(); + + int messageNumber = count - i; + Message message = folder.getMessage(messageNumber); + + log.debug("Message n°" + messageNumber); + log.debug("Object: " + message.getSubject()); + email.setObject(message.getSubject()); + + log.debug("Sender: "); + Address[] addresses = message.getFrom(); + if (addresses != null) { + for (Address address : addresses) { + email.setSender(address.toString()); + } + } + + log.debug("Recipients: "); + addresses = message.getRecipients(Message.RecipientType.TO); + if (addresses != null) { + for (Address address : addresses) { + log.debug("\tTo : " + address); + email.setRecipient(address.toString()); + } + } + + addresses = message.getRecipients(Message.RecipientType.CC); + if (addresses != null) { + for (Address address : addresses) { + log.debug("\tCopy : " + address); + } + } + + email.setReceptionDate(message.getReceivedDate()); + + Date now = new Date(); + + DecoratorService decoratorService = serviceContext.newService(DecoratorService.class); + Decorator<Date> dateDecorator = decoratorService.getDecoratorByType(Date.class, DecoratorService.DATE); + String projectRef = t("faxtomail.email.projectReference.default", dateDecorator.toString(now)); + email.setProjectReference(projectRef); + + email.setDemandStatus(DemandStatus.UNTREATED); + + String originalContent = IOUtils.toString(message.getInputStream()); + email.setOriginalEmail(originalContent); + + log.debug("ContentType: " + message.getContentType()); + + if (message.isMimeType("multipart/*")) { + decomposeMultipartEmail(message, email); + + } else { + String content = IOUtils.toString(message.getInputStream()); + email.setContent(content); + email.setContentType(message.getContentType()); + } + + log.debug("Content: "); + log.debug(email.getContent()); + +// content = content.replaceAll("<img src=\"(.*?)\"(/>|>.*?</img>)", "[image : $1]"); +// content = content.replaceAll("<a href=\"(.*?)\">(.*?)</a>", "[$2 : $1]"); +// log.debug("Content : "); +// log.debug(content); +// email.setContent(content); + + MailFolder mailFolder = serviceContext.newService(MailFolderService.class).getRandomFolder(); + email.setMailFolder(mailFolder); + + serviceContext.newService(EmailService.class).saveEmail(email, null); + + message.setFlag(Flags.Flag.DELETED, true); + } + + } catch (Exception e) { + log.error("Error while reading the emails", e); + } + } + + /** + * Decompose a multipart part. + * - sets the email content if the part contains a text bodypart + * - adds attachments to the email + * + * @param part the part to decompose + * @param email the email object to fill with the attachments or content + * @throws Exception + */ + protected void decomposeMultipartEmail(Part part, Email email) throws Exception { + DataSource dataSource = part.getDataHandler().getDataSource(); + MimeMultipart mimeMultipart = new MimeMultipart(dataSource); + int multiPartCount = mimeMultipart.getCount(); + + for (int j = 0; j < multiPartCount; j++) { + BodyPart bp = mimeMultipart.getBodyPart(j); + + // if it is a text part, the,n this is the email content + if (bp.isMimeType("text/*")) { + String content = IOUtils.toString(bp.getInputStream()); + email.setContent(content); + email.setContentType(bp.getContentType()); + + // if it is multipart part, decompose it + } else if (bp.isMimeType("multipart/*")) { + decomposeMultipartEmail(bp, email); + + // else, this is an attachment + } else { + String fileName = bp.getFileName(); + log.debug("FileName : " + fileName); + if (fileName == null) { + fileName = bp.getHeader("Content-ID")[0]; + // remove the guillemets between the id + fileName = fileName.replaceFirst("^<(.*)>$", "$1"); + } + + File file = new File(FileUtils.getTempDirectory(), fileName); + FileOutputStream fos = new FileOutputStream(file); + + DataHandler dh = bp.getDataHandler(); + dh.writeTo(fos); + + Attachment attachment = new AttachmentImpl(); + attachment.setAddedByUser(false); + attachment.setOriginalFile(file); + email.addAttachment(attachment); + } + } + } + +} Added: trunk/faxtomail-ui-web/src/main/resources/quartz.properties =================================================================== --- trunk/faxtomail-ui-web/src/main/resources/quartz.properties (rev 0) +++ trunk/faxtomail-ui-web/src/main/resources/quartz.properties 2014-04-14 13:43:16 UTC (rev 33) @@ -0,0 +1,3 @@ +org.quartz.scheduler.instanceName = FaxToMailScheduler +org.quartz.threadPool.threadCount = 3 +org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore \ No newline at end of file Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2014-04-10 12:18:23 UTC (rev 32) +++ trunk/pom.xml 2014-04-14 13:43:16 UTC (rev 33) @@ -495,6 +495,18 @@ <scope>compile</scope> </dependency> + <dependency> + <groupId>org.quartz-scheduler</groupId> + <artifactId>quartz</artifactId> + <version>2.2.1</version> + </dependency> + + <dependency> + <groupId>javax.mail</groupId> + <artifactId>mail</artifactId> + <version>1.4.7</version> + </dependency> + <!--<dependency>--> <!--<groupId>net.atlanticbb.tantlinger</groupId>--> <!--<artifactId>shef</artifactId>-->
participants (1)
-
kmorin@users.forge.codelutin.com