Author: echatellier Date: 2014-06-05 17:57:29 +0200 (Thu, 05 Jun 2014) New Revision: 138 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/138 Log: Fix tree saving when adding and deleting nodes Modified: trunk/faxtomail-persistence/src/main/xmi/faxtomail.properties trunk/faxtomail-persistence/src/main/xmi/faxtomail.zargo trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java Modified: trunk/faxtomail-persistence/src/main/xmi/faxtomail.properties =================================================================== --- trunk/faxtomail-persistence/src/main/xmi/faxtomail.properties 2014-06-05 14:47:30 UTC (rev 137) +++ trunk/faxtomail-persistence/src/main/xmi/faxtomail.properties 2014-06-05 15:57:29 UTC (rev 138) @@ -60,6 +60,7 @@ com.franciaflex.faxtomail.persistence.entities.AttachmentFile.attribute.content.tagvalue.notNull=true # MailFolder +com.franciaflex.faxtomail.persistence.entities.MailFolder.class.tagvalue.naturalIdMutable=true com.franciaflex.faxtomail.persistence.entities.MailFolder.attribute.parent.tagvalue.naturalId=true com.franciaflex.faxtomail.persistence.entities.MailFolder.attribute.parent.tagvalue.notNull=false com.franciaflex.faxtomail.persistence.entities.MailFolder.attribute.name.tagvalue.naturalId=true Modified: trunk/faxtomail-persistence/src/main/xmi/faxtomail.zargo =================================================================== (Binary files differ) 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-05 14:47:30 UTC (rev 137) +++ trunk/faxtomail-service/src/main/java/com/franciaflex/faxtomail/services/service/MailFolderService.java 2014-06-05 15:57:29 UTC (rev 138) @@ -35,6 +35,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.topia.persistence.TopiaEntities; import org.nuiton.util.beans.Binder; import org.nuiton.util.beans.BinderFactory; import org.nuiton.util.pagination.PaginationParameter; @@ -48,6 +49,7 @@ import com.franciaflex.faxtomail.persistence.entities.MailFolderTopiaDao; import com.franciaflex.faxtomail.services.FaxToMailServiceSupport; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; /** * @author kmorin <kmorin@codelutin.com> @@ -92,28 +94,37 @@ return new ArrayList<MailFolder>(dao.forTopiaIdIn(ids).findAll()); } - public void saveMailFolders(Collection<MailFolder> mailFolders) { - saveMailFolders(null, mailFolders); + 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(MailFolder parent, Collection<MailFolder> mailFolders) { + protected Collection<MailFolder> saveMailFolders(MailFolderTopiaDao dao, Map<String, MailFolder> mailFolderMap, + MailFolder parent, Collection<MailFolder> mailFolders) { + + Collection<MailFolder> result = Lists.newArrayList(); if (mailFolders == null) { - return null; + return result; } - Collection<MailFolder> result = Lists.newArrayList(); - MailFolderTopiaDao dao = getPersistenceContext().getMailFolderDao(); - //TopiaIdFactory factory = getPersistenceContext().getTopiaIdFactory(); Binder<MailFolder, MailFolder> binderMailFolder = BinderFactory.newBinder(MailFolder.class); for (MailFolder mailFolder : mailFolders) { - Collection<MailFolder> children = saveMailFolders(mailFolder, mailFolder.getChildren()); MailFolder currentMailFolder; if (StringUtils.isBlank(mailFolder.getTopiaId()) || mailFolder.getTopiaId().startsWith("new_")) { currentMailFolder = new MailFolderImpl(); } else { - currentMailFolder = dao.findByTopiaId(mailFolder.getTopiaId()); + currentMailFolder = mailFolderMap.remove(mailFolder.getTopiaId()); } binderMailFolder.copyExcluding(mailFolder, currentMailFolder, @@ -123,18 +134,19 @@ MailFolder.PROPERTY_CHILDREN, MailFolder.PROPERTY_PARENT); - currentMailFolder.setChildren(children); currentMailFolder.setParent(parent); - if (mailFolder.isPersisted()) { - currentMailFolder = dao.update(currentMailFolder); - } else { + 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); } - - getPersistenceContext().commit(); + return result; }