r240 - in trunk: faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/action/admin
Author: echatellier Date: 2014-06-20 11:38:28 +0200 (Fri, 20 Jun 2014) New Revision: 240 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/240 Log: Refactoring de la sauvegarde de la conf pour avoir un seul et unique commit. 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/MailFolderService.java trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ReferentielService.java trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/action/admin/ConfigurationAction.java 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-06-20 09:16:31 UTC (rev 239) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ConfigurationService.java 2014-06-20 09:38:28 UTC (rev 240) @@ -46,10 +46,16 @@ import com.franciaflex.faxtomail.persistence.entities.Configuration; import com.franciaflex.faxtomail.persistence.entities.ConfigurationImpl; import com.franciaflex.faxtomail.persistence.entities.ConfigurationTopiaDao; +import com.franciaflex.faxtomail.persistence.entities.DemandType; +import com.franciaflex.faxtomail.persistence.entities.DemandTypeImpl; +import com.franciaflex.faxtomail.persistence.entities.DemandTypeTopiaDao; import com.franciaflex.faxtomail.persistence.entities.EmailAccount; import com.franciaflex.faxtomail.persistence.entities.EmailAccountImpl; import com.franciaflex.faxtomail.persistence.entities.EmailAccountTopiaDao; import com.franciaflex.faxtomail.persistence.entities.EmailProtocol; +import com.franciaflex.faxtomail.persistence.entities.EtatAttente; +import com.franciaflex.faxtomail.persistence.entities.EtatAttenteImpl; +import com.franciaflex.faxtomail.persistence.entities.EtatAttenteTopiaDao; import com.franciaflex.faxtomail.persistence.entities.FaxToMailUser; import com.franciaflex.faxtomail.persistence.entities.FaxToMailUserGroup; import com.franciaflex.faxtomail.persistence.entities.FaxToMailUserGroupTopiaDao; @@ -59,6 +65,8 @@ 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.persistence.entities.MailFolderImpl; +import com.franciaflex.faxtomail.persistence.entities.MailFolderTopiaDao; import com.franciaflex.faxtomail.services.FaxToMailServiceSupport; import com.google.common.base.Function; import com.google.common.collect.Lists; @@ -87,37 +95,192 @@ } /** + * Get all mail filter ordered by position. + * + * @return all mail filter + */ + public List<MailFilter> getMailFilters() { + MailFilterTopiaDao mailFilterDao = getPersistenceContext().getMailFilterDao(); + List<MailFilter> result = mailFilterDao.forAll().setOrderByArguments(MailFilter.PROPERTY_POSITION + " asc").findAll(); + return result; + } + + /** * Save configuration. * * @param config configuration to save */ - public void saveConfiguration(Configuration config) { + protected void saveConfiguration(Configuration config) { ConfigurationTopiaDao dao = getPersistenceContext().getConfigurationDao(); if (config.isPersisted()) { config = dao.update(config); } else { config = dao.create(config); } + } + + /** + * Sauve l'ensemble de la configuration. + * + * @param configuration configuration + * @param demandTypes types de demande + * @param etatAttentes etat d'attentes + * @param mailFolders mail folder + * @param mailFilters mail filters + * @param emailAccounts mail accounts + */ + public void save(Configuration configuration, List<DemandType> demandTypes, List<EtatAttente> etatAttentes, List<MailFolder> mailFolders, + List<MailFilter> mailFilters, List<EmailAccount> emailAccounts) { + + saveConfiguration(configuration); + saveDemandTypes(demandTypes); + saveMailFolders(mailFolders); // save before etat d'attente to remove reference + saveEtatAttente(etatAttentes); + saveMailFilters(mailFilters); + saveEmailAccounts(emailAccounts); + + // unique commit pour toute la conf getPersistenceContext().commit(); } + protected void saveDemandTypes(List<DemandType> demandTypes) { + Binder<DemandType, DemandType> binderDemandType = BinderFactory.newBinder(DemandType.class); + DemandTypeTopiaDao demandTypeDAO = getPersistenceContext().getDemandTypeDao(); + + List<DemandType> allDemandType = demandTypeDAO.findAll(); + Map<String, DemandType> allDemandTypeIndex = new HashMap<>(Maps.uniqueIndex(allDemandType, TopiaEntities.getTopiaIdFunction())); + for (DemandType demandType : demandTypes) { + // get current etat attente + DemandType currentDemandType; + if (StringUtils.isBlank(demandType.getTopiaId()) || demandType.getTopiaId().startsWith("new_")) { + currentDemandType = new DemandTypeImpl(); + } else { + currentDemandType = allDemandTypeIndex.remove(demandType.getTopiaId()); + } + + // copy + binderDemandType.copyExcluding(demandType, currentDemandType, + EtatAttente.PROPERTY_TOPIA_ID, + EtatAttente.PROPERTY_TOPIA_CREATE_DATE, + EtatAttente.PROPERTY_TOPIA_VERSION); + + // persist + if (currentDemandType.isPersisted()) { + demandTypeDAO.update(currentDemandType); + } else { + demandTypeDAO.create(currentDemandType); + } + } + } + + protected void saveEtatAttente(Collection<EtatAttente> etatAttentes) { + + Binder<EtatAttente, EtatAttente> binderEtatAttente = BinderFactory.newBinder(EtatAttente.class); + EtatAttenteTopiaDao etatAttenteDAO = getPersistenceContext().getEtatAttenteDao(); + + List<EtatAttente> allEtatAttente = etatAttenteDAO.findAll(); + Map<String, EtatAttente> allEtatAttenteIndex = new HashMap<>(Maps.uniqueIndex(allEtatAttente, TopiaEntities.getTopiaIdFunction())); + for (EtatAttente etatAttente : etatAttentes) { + // get current etat attente + EtatAttente currentEtatAttente; + if (StringUtils.isBlank(etatAttente.getTopiaId()) || etatAttente.getTopiaId().startsWith("new_")) { + currentEtatAttente = new EtatAttenteImpl(); + } else { + currentEtatAttente = allEtatAttenteIndex.remove(etatAttente.getTopiaId()); + } + + // copy + binderEtatAttente.copyExcluding(etatAttente, currentEtatAttente, + EtatAttente.PROPERTY_TOPIA_ID, + EtatAttente.PROPERTY_TOPIA_CREATE_DATE, + EtatAttente.PROPERTY_TOPIA_VERSION); + + // persist + if (currentEtatAttente.isPersisted()) { + etatAttenteDAO.update(currentEtatAttente); + } else { + etatAttenteDAO.create(currentEtatAttente); + } + } + + // delete remaining + etatAttenteDAO.deleteAll(allEtatAttenteIndex.values()); + } + /** - * Get all mail filter ordered by position. + * Save mail folder without commit. * - * @return all mail filter + * @param newMailFolders mail folders */ - public List<MailFilter> getMailFilters() { - MailFilterTopiaDao mailFilterDao = getPersistenceContext().getMailFilterDao(); - List<MailFilter> result = mailFilterDao.forAll().setOrderByArguments(MailFilter.PROPERTY_POSITION + " asc").findAll(); + protected void saveMailFolders(Collection<MailFolder> newMailFolders) { + // get current folders + MailFolderTopiaDao dao = getPersistenceContext().getMailFolderDao(); + List<MailFolder> mailFolders = dao.findAll(); + Map<String, MailFolder> mailFolderMap = new HashMap<>(Maps.uniqueIndex(mailFolders, TopiaEntities.getTopiaIdFunction())); + + // recursive update + saveMailFolders(dao, mailFolderMap, null, newMailFolders); + + // if map is not empty after recursive iteration, remaining folder must be deleted + dao.deleteAll(mailFolderMap.values()); + } + + /** + * Save mail folder without commit. + * + * @param dao + * @param mailFolderMap + * @param parent + * @param mailFolders + * @return + */ + protected Collection<MailFolder> saveMailFolders(MailFolderTopiaDao dao, Map<String, MailFolder> mailFolderMap, + MailFolder parent, Collection<MailFolder> mailFolders) { + + Collection<MailFolder> result = Lists.newArrayList(); + if (mailFolders == null) { + return result; + } + + Binder<MailFolder, MailFolder> binderMailFolder = BinderFactory.newBinder(MailFolder.class); + for (MailFolder mailFolder : mailFolders) { + + MailFolder currentMailFolder; + if (StringUtils.isBlank(mailFolder.getTopiaId()) || mailFolder.getTopiaId().startsWith("new_")) { + currentMailFolder = new MailFolderImpl(); + } else { + currentMailFolder = mailFolderMap.remove(mailFolder.getTopiaId()); + } + + binderMailFolder.copyExcluding(mailFolder, currentMailFolder, + MailFolder.PROPERTY_TOPIA_ID, + MailFolder.PROPERTY_TOPIA_CREATE_DATE, + MailFolder.PROPERTY_TOPIA_VERSION, + MailFolder.PROPERTY_CHILDREN, + MailFolder.PROPERTY_PARENT); + + currentMailFolder.setParent(parent); + + if (!currentMailFolder.isPersisted()) { + currentMailFolder = dao.create(currentMailFolder); + } + + Collection<MailFolder> children = saveMailFolders(dao, mailFolderMap, currentMailFolder, mailFolder.getChildren()); + currentMailFolder.setChildren(children); + dao.update(currentMailFolder); + + result.add(currentMailFolder); + } + return result; } /** - * Save all mail filters. + * Save all mail filters without commit. * * @param mailFilters mail filters to save */ - public void saveMailFilters(List<MailFilter> mailFilters) { + protected void saveMailFilters(List<MailFilter> mailFilters) { MailFilterTopiaDao dao = getPersistenceContext().getMailFilterDao(); int position = 0; @@ -146,7 +309,6 @@ } dao.deleteAll(filterById.values()); - getPersistenceContext().commit(); } /** @@ -177,10 +339,11 @@ } /** + * Save email account without commit. * * @param newEmailAccounts new email account list to save */ - public void saveEmailAccounts(List<EmailAccount> newEmailAccounts) { + protected void saveEmailAccounts(List<EmailAccount> newEmailAccounts) { EmailAccountTopiaDao emailAccountTopiaDao = getPersistenceContext().getEmailAccountDao(); List<EmailAccount> emailAccounts = emailAccountTopiaDao.findAll(); Map<String, EmailAccount> emailAccountMap = new HashMap<>(Maps.uniqueIndex(emailAccounts, TopiaEntities.getTopiaIdFunction())); @@ -213,7 +376,6 @@ } emailAccountTopiaDao.deleteAll(emailAccountMap.values()); - getPersistenceContext().commit(); } public List<FaxToMailUser> getAllUsers() { 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-06-20 09:16:31 UTC (rev 239) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java 2014-06-20 09:38:28 UTC (rev 240) @@ -94,62 +94,6 @@ return new ArrayList<MailFolder>(dao.forTopiaIdIn(ids).findAll()); } - public void saveMailFolders(Collection<MailFolder> newMailFolders) { - // get current folders - MailFolderTopiaDao dao = getPersistenceContext().getMailFolderDao(); - List<MailFolder> mailFolders = dao.findAll(); - Map<String, MailFolder> mailFolderMap = new HashMap<>(Maps.uniqueIndex(mailFolders, TopiaEntities.getTopiaIdFunction())); - - // recursive update - saveMailFolders(dao, mailFolderMap, null, newMailFolders); - - // if map is not empty after recursive iteration, remaining folder must be deleted - dao.deleteAll(mailFolderMap.values()); - - getPersistenceContext().commit(); - } - - protected Collection<MailFolder> saveMailFolders(MailFolderTopiaDao dao, Map<String, MailFolder> mailFolderMap, - MailFolder parent, Collection<MailFolder> mailFolders) { - - Collection<MailFolder> result = Lists.newArrayList(); - if (mailFolders == null) { - return result; - } - - Binder<MailFolder, MailFolder> binderMailFolder = BinderFactory.newBinder(MailFolder.class); - for (MailFolder mailFolder : mailFolders) { - - MailFolder currentMailFolder; - if (StringUtils.isBlank(mailFolder.getTopiaId()) || mailFolder.getTopiaId().startsWith("new_")) { - currentMailFolder = new MailFolderImpl(); - } else { - currentMailFolder = mailFolderMap.remove(mailFolder.getTopiaId()); - } - - binderMailFolder.copyExcluding(mailFolder, currentMailFolder, - MailFolder.PROPERTY_TOPIA_ID, - MailFolder.PROPERTY_TOPIA_CREATE_DATE, - MailFolder.PROPERTY_TOPIA_VERSION, - MailFolder.PROPERTY_CHILDREN, - MailFolder.PROPERTY_PARENT); - - currentMailFolder.setParent(parent); - - if (!currentMailFolder.isPersisted()) { - currentMailFolder = dao.create(currentMailFolder); - } - - Collection<MailFolder> children = saveMailFolders(dao, mailFolderMap, currentMailFolder, mailFolder.getChildren()); - currentMailFolder.setChildren(children); - dao.update(currentMailFolder); - - result.add(currentMailFolder); - } - - return result; - } - public Collection<MailFolder> getFoldersWithEtatAttente(EtatAttente etatAttente) { Collection<MailFolder> result = new HashSet<>(); Modified: trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ReferentielService.java =================================================================== --- trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ReferentielService.java 2014-06-20 09:16:31 UTC (rev 239) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/ReferentielService.java 2014-06-20 09:38:28 UTC (rev 240) @@ -103,74 +103,6 @@ return result; } - public void saveEtatAttente(Collection<EtatAttente> etatAttentes) { - - Binder<EtatAttente, EtatAttente> binderEtatAttente = BinderFactory.newBinder(EtatAttente.class); - EtatAttenteTopiaDao etatAttenteDAO = getPersistenceContext().getEtatAttenteDao(); - - List<EtatAttente> allEtatAttente = getAllEtatAttente(); - Map<String, EtatAttente> allEtatAttenteIndex = new HashMap<>(Maps.uniqueIndex(allEtatAttente, TopiaEntities.getTopiaIdFunction())); - for (EtatAttente etatAttente : etatAttentes) { - // get current etat attente - EtatAttente currentEtatAttente; - if (StringUtils.isBlank(etatAttente.getTopiaId()) || etatAttente.getTopiaId().startsWith("new_")) { - currentEtatAttente = new EtatAttenteImpl(); - } else { - currentEtatAttente = allEtatAttenteIndex.remove(etatAttente.getTopiaId()); - } - - // copy - binderEtatAttente.copyExcluding(etatAttente, currentEtatAttente, - EtatAttente.PROPERTY_TOPIA_ID, - EtatAttente.PROPERTY_TOPIA_CREATE_DATE, - EtatAttente.PROPERTY_TOPIA_VERSION); - - // persist - if (currentEtatAttente.isPersisted()) { - etatAttenteDAO.update(currentEtatAttente); - } else { - etatAttenteDAO.create(currentEtatAttente); - } - } - - // delete remaining - etatAttenteDAO.deleteAll(allEtatAttenteIndex.values()); - - getPersistenceContext().commit(); - } - - public void saveDemandTypes(List<DemandType> demandTypes) { - Binder<DemandType, DemandType> binderDemandType = BinderFactory.newBinder(DemandType.class); - DemandTypeTopiaDao demandTypeDAO = getPersistenceContext().getDemandTypeDao(); - - List<DemandType> allDemandType = getAllDemandType(); - Map<String, DemandType> allDemandTypeIndex = new HashMap<>(Maps.uniqueIndex(allDemandType, TopiaEntities.getTopiaIdFunction())); - for (DemandType demandType : demandTypes) { - // get current etat attente - DemandType currentDemandType; - if (StringUtils.isBlank(demandType.getTopiaId()) || demandType.getTopiaId().startsWith("new_")) { - currentDemandType = new DemandTypeImpl(); - } else { - currentDemandType = allDemandTypeIndex.remove(demandType.getTopiaId()); - } - - // copy - binderDemandType.copyExcluding(demandType, currentDemandType, - EtatAttente.PROPERTY_TOPIA_ID, - EtatAttente.PROPERTY_TOPIA_CREATE_DATE, - EtatAttente.PROPERTY_TOPIA_VERSION); - - // persist - if (currentDemandType.isPersisted()) { - demandTypeDAO.update(currentDemandType); - } else { - demandTypeDAO.create(currentDemandType); - } - } - - getPersistenceContext().commit(); - } - public List<Client> getAllClients() { ClientTopiaDao dao = getPersistenceContext().getClientDao(); return new ArrayList<>(dao.findAll()); Modified: trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java =================================================================== --- trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java 2014-06-20 09:16:31 UTC (rev 239) +++ trunk/faxtomail-ui-swing/src/main/java/com/franciaflex/faxtomail/ui/swing/util/FaxToMailUIUtil.java 2014-06-20 09:38:28 UTC (rev 240) @@ -67,7 +67,6 @@ import org.nuiton.jaxx.application.ApplicationBusinessException; import org.nuiton.jaxx.application.ApplicationTechnicalException; import org.nuiton.jaxx.application.swing.util.ApplicationUIUtil; -import org.nuiton.util.DesktopUtil; import org.nuiton.util.FileUtil; import com.franciaflex.faxtomail.persistence.entities.Attachment; Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/action/admin/ConfigurationAction.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/action/admin/ConfigurationAction.java 2014-06-20 09:16:31 UTC (rev 239) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/action/admin/ConfigurationAction.java 2014-06-20 09:38:28 UTC (rev 240) @@ -123,12 +123,7 @@ public String execute() throws Exception { String result = super.execute(); - configurationService.saveConfiguration(configuration); - referentielService.saveDemandTypes(demandTypes); - mailFolderService.saveMailFolders(mailFolders); // before etat attente - referentielService.saveEtatAttente(etatAttentes); - configurationService.saveMailFilters(mailFilters); - configurationService.saveEmailAccounts(emailAccounts); + configurationService.save(configuration, demandTypes, etatAttentes, mailFolders, mailFilters, emailAccounts); return result; }
participants (1)
-
echatellier@users.forge.codelutin.com