Salome HOME
Fix for some problems with definition uses.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / PublicationServiceImpl.java
index 5439663040c205e4316f2a9ddf9ac79c429ae2d0..92652e8d0056eaf050e335ba0bf6ef1f4e0e3809 100644 (file)
@@ -155,9 +155,12 @@ public class PublicationServiceImpl implements PublicationService {
                        final List<Long> docuses) throws MissedPropertyException,
                        InvalidPropertyException, MultiplyDefinedException, IOException,
                        NotApplicableException, InterruptedException, ParseException {
+               DocumentType type = getDocumentTypeService().selectType(
+                               (int) documentTypeId);
                User user = getUserService().selectUser(userId);
                File updir = getRepositoryService().getDownloadDirectory(user);
                File upfile = new File(updir.getPath() + "/" + fname);
+               String[] table = fname.split("\\x2E");
 
                // Creation of the document
                Document.Properties dprop = new Document.Properties();
@@ -166,9 +169,6 @@ public class PublicationServiceImpl implements PublicationService {
 
                if (reference.length() == 0) { // Importation of a foreign document
                        // TODO: Extract property of supported documents (DOCX, ODT...)
-                       DocumentType type = getDocumentTypeService().selectType(
-                                       (int) documentTypeId);
-                       String[] table = fname.split("\\x2E");
                        addoc = getStepService().createDocument(
                                        step,
                                        dprop.setName(doctitle).setType(type).setFormat(
@@ -209,8 +209,10 @@ public class PublicationServiceImpl implements PublicationService {
                // Creation of uses relations
                if (docuses != null) {
                        for (Long index : docuses) {
-                               Document used = getDocumentService().selectDocument(index);
+                               Document used = getDocumentService().
+                                       selectDocument(index);
                                Publication pub = step.getDocument(index);
+                               // updating uses relations on publication in step
                                if (pub == null) {
                                    for (Publication doc : step.getOwner().
                                            getDocums()) {
@@ -221,7 +223,8 @@ public class PublicationServiceImpl implements PublicationService {
                                    }
                                }
                                if (pub == null) {
-                                   for (Publication doc : step.getOwnerStudy().getDocums()) {
+                                   for (Publication doc : step.getOwnerStudy().
+                                           getDocums()) {
                         if (doc.value().getIndex() == index) {
                             pub = doc;
                             break;
@@ -302,12 +305,39 @@ public class PublicationServiceImpl implements PublicationService {
 
                // Creation of uses relations
                Publication syncronizedCurrent = _publicationDAO.get(current.getIndex());
-               updateRelations(syncronizedCurrent, next, docuses, docusedby);
+               updateRelations(step, syncronizedCurrent, next, docuses, docusedby);
        }
 
+       /**
+     * Find the documents, which using the old version of document.
+     *
+     * @param list
+     *            the list of documents uses versioning document
+     * @param publication
+     *            publication, which is check by use the documents in _defuses list
+     * @param defuses
+     *            the list of default uses documents
+     */
+    public final void findSequenceUses(final List<Document> list,
+            final Publication publication, final List<Document> defuses) {
+        for (Iterator<Document> document = defuses.iterator(); document
+                .hasNext();) {
+            Publication pub = publication.getOwner().getPublication(
+                    document.next());
+            if (pub != null
+                    && pub.getRelations(UsesRelation.class).
+                    contains(publication)) {
+                findSequenceUses(list, pub, defuses);
+                list.add(pub.value());
+            }
+        }
+    }
+
        /**
         * Update relations after creation of a new document version.
-        * 
+        *
+        * @param step
+     *            current Step
         * @param current
         *            the current version
         * @param next
@@ -317,30 +347,40 @@ public class PublicationServiceImpl implements PublicationService {
         * @param docusedby
         *            ids of documents used by the versioned one.
         */
-       private void updateRelations(final Publication current,
-                       final Publication next, final String[] docuses,
-                       final long[] docusedby) {
-               if (docuses != null) {
-                       for (int i = 0; i < docuses.length; i++) {
-                               Long index = Long.valueOf(docuses[i].trim());
-                               Document used = getDocumentService().selectDocument(index);// RKV: getPublication(index, steps);
-                               next.addDependency(used);
-                       }
-               }
-               // Outdating impacted document
-               HashSet<Long> compatible = new HashSet<Long>();
-               if (docusedby != null) {
-                       for (int i = 0; i < docusedby.length; i++) {
-                               compatible.add(docusedby[i]);
-                       }
-               }
-               for (Publication using : current.getRelations(UsedByRelation.class)) {
-                       if (!compatible.contains(using.getIndex())) {
-                               outdate(using);
-                       }
-               }
-       }
-
+    public void updateRelations(final Step step, final Publication current,
+            final Publication next, final String[] docuses,
+            final long[] docusedby) {
+        if (docuses != null) {
+            for (int i = 0; i < docuses.length; i++) {
+                Long index = Long.valueOf(docuses[i].trim());
+                Document used = getDocumentService().selectDocument(index);// RKV:getPublication(index, steps);
+                next.addDependency(used);
+                if (used.getStep() == step.getNumber()) {
+                    step.getDocument(used.getIndex()).setValue(used);
+                }
+            }
+        }
+        // Outdating impacted document
+        HashSet<Long> compatible = new HashSet<Long>();
+        if (docusedby != null) {
+            for (int i = 0; i < docusedby.length; i++) {
+                compatible.add(docusedby[i]);
+            }
+        }
+        for (Publication using : current.getRelations(UsedByRelation.class)) {
+            if (compatible.contains(using.getIndex())) {
+                current.value().removeRelation(UsedByRelation.class,
+                        using.value());
+                using.addDependency(next);
+                if (step.getNumber() == using.value().getStep()) {
+                    step.getDocument(using.value().getIndex()).setValue(
+                            using.value());
+                }
+            } else {
+                outdate(using);
+            }
+        }
+    }
        /*
         * protected Publication getPublication(int index, List<Step> steps) { for (Iterator<Step> i = steps.iterator(); i.hasNext();) { List<Publication>
         * published = i.next().getAllDocuments(); for (Iterator<Publication> j = published.iterator(); j.hasNext();) { Publication found =