Salome HOME
Fix for removing a document with new attached file: ConvertsRelation was not removed...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StepServiceImpl.java
index 0faf6d8d702592abe6f306bbc49429394b74fed2..6a10e4c9c2d3581d070c81cfc79f524e8aa4e3cb 100644 (file)
@@ -20,7 +20,6 @@ import org.splat.dal.bo.som.Document;
 import org.splat.dal.bo.som.DocumentType;
 import org.splat.dal.bo.som.File;
 import org.splat.dal.bo.som.KnowledgeElement;
-import org.splat.dal.bo.som.ProjectElement;
 import org.splat.dal.bo.som.Publication;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.SimulationContext;
@@ -258,11 +257,9 @@ public class StepServiceImpl implements StepService {
                }
 
                // Identification and save
-               aStep.getOwnerStudy().setLastLocalIndex(
-                               dprop.getOwner().getOwnerStudy().getLastLocalIndex());
                if (LOG.isDebugEnabled()) {
                        LOG.debug("Local index after: "
-                                       + dprop.getOwner().getOwnerStudy().getLastLocalIndex());
+                                       + aStep.getOwnerStudy().getLastLocalIndex());
                }
                getDocumentService().buildReferenceFrom(newdoc, aStep.getOwnerStudy());
                getDocumentDAO().create(newdoc);
@@ -461,12 +458,8 @@ public class StepServiceImpl implements StepService {
         */
        public boolean remove(final Step aStep, final Publication oldoc) {
                aStep.getDocuments().remove(oldoc); // Updates this step
-               // Synchronize with database
-               ProjectElement owner = getProjectElementDAO().merge(aStep.getOwner());
-               if (owner != null) {
-                       aStep.getOwner().remove(oldoc); // in transient copy
-                       owner.remove(oldoc); // in persistent copy
-               }
+               aStep.getOwner().remove(oldoc); // remove from the parent project element
+               getProjectElementDAO().merge(aStep.getOwner());
                getDocumentService().release(oldoc.value()); // Decrements the configuration tag count of document
                // The publication becoming orphan, it should automatically be removed from the database when updating of owner scenario.
                return true;
@@ -496,15 +489,14 @@ public class StepServiceImpl implements StepService {
                        Document value = torem.value();
                        if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used
                                List<Document> using = new ArrayList<Document>();
-                               List<Relation> converts = new ArrayList<Relation>();
+                               List<File> files = new ArrayList<File>();
                                for (Relation link : value.getAllRelations()) {
                                        if (link.getClass().equals(ConvertsRelation.class)) { // File conversion
-                                               converts.add(link);
+                                               files.add((File) link.getTo());
                                        } else if (link.getClass().equals(UsesRelation.class)) { // Document dependency
                                                using.add((Document) link.getTo());
                                        }
                                }
-                               // value.getAllRelations().removeAll(converts);
                                // Remove relations from depending documents
                                if (LOG.isDebugEnabled()) {
                                        LOG.debug("Remove " + using.size() + " UsedByRelation(s).");
@@ -513,20 +505,24 @@ public class StepServiceImpl implements StepService {
                                        if (LOG.isDebugEnabled()) {
                                                LOG.debug("Remove UsedByRelation from "
                                                                + doc.getTitle() + " to " + value.getTitle());
+                                               LOG.debug("Nb relations of doc " + doc.getTitle()
+                                                               + " before: " + doc.getAllRelations().size());
                                        }
                                        doc.removeRelation(UsedByRelation.class, value);
+                                       if (LOG.isDebugEnabled()) {
+                                               LOG.debug("Nb relations of doc " + doc.getTitle()
+                                                               + " after: " + doc.getAllRelations().size());
+                                       }
                                        getDocumentDAO().merge(doc);
                                }
-                               value = getPublicationDAO().merge(torem).value();
                                // Synchronize deleted objects with the database to avoid hibernate exception
-                               // org.hibernate.PropertyValueException: not-null property references a null or transient value:
-                               // org.splat.dal.bo.som.UsedByRelation.refer
-                               getDocumentDAO().flush(); // To show to the database that files from ConvertsRelation(s) are deleted already
-                               getDocumentDAO().delete(value); // The corresponding physical file is not removed from the vault
+                               // org.hibernate.PropertyValueException: not-null property references a null or transient value
+                               getDocumentDAO().flush();
+                               // The corresponding physical file is not removed from the vault
+                               getDocumentDAO().delete(getDocumentDAO().merge(torem.value()));
                                // Delete document's files
-                               for (Relation link : converts) {
-                                       File file = (File) link.getTo();
-                                       getFileDAO().delete(file); // The corresponding physical file is not removed from the vault
+                               for (File file : files) {
+                                       getFileDAO().delete(getFileDAO().merge(file)); // The corresponding physical file is not removed from the vault
                                }
                        }
                }