Salome HOME
File naming strategy is set to "asis" and generated number is in the prefix now inste...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / DocumentServiceImpl.java
index 7e6947d99d9affa9f25af9c7ee1ef3f5b392d9dd..c8494b13e70cf1fcd931b082cd3b4ca9ba3152ab 100644 (file)
@@ -29,8 +29,8 @@ import org.splat.dal.bo.som.StampRelation;
 import org.splat.dal.bo.som.Study;
 import org.splat.dal.bo.som.Timestamp;
 import org.splat.dal.bo.som.ValidationStep;
-import org.splat.dal.bo.som.VersionsRelation;
 import org.splat.dal.bo.som.Document.Properties;
+import org.splat.dal.dao.kernel.RelationDAO;
 import org.splat.dal.dao.som.DocumentDAO;
 import org.splat.dal.dao.som.DocumentTypeDAO;
 import org.splat.dal.dao.som.FileDAO;
@@ -77,6 +77,10 @@ public class DocumentServiceImpl implements DocumentService {
         * Injected document DAO.
         */
        private DocumentDAO _documentDAO;
+       /**
+        * Injected relation DAO.
+        */
+       private RelationDAO _relationDAO;
        /**
         * Injected document type DAO.
         */
@@ -119,17 +123,12 @@ public class DocumentServiceImpl implements DocumentService {
         */
        @Transactional
        public void generateDocumentId(final Document aDoc, final Properties dprop) {
-               Study owner = null;
-               if (dprop.getOwner() instanceof Study) {
-                       owner = (Study) dprop.getOwner();
-               } else {
-                       owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
-               }
+               Study owner = dprop.getOwner().getOwnerStudy();
 
                // Synchronize the object with the current Hibernate session.
-               owner = getStudyDAO().get(owner.getIndex());
+               // owner = getStudyDAO().merge(owner);
 
-               SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); //RKV: NOPMD: TODO: Use locale here?
+               SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); // RKV: NOPMD: TODO: Use locale here?
                String year = tostring.format(owner.getDate());
                String filename = generateEncodedName(aDoc, owner);
                String path = owner.getReference();
@@ -144,6 +143,8 @@ public class DocumentServiceImpl implements DocumentService {
                                .append(aDoc.getFile().getFormat()) // File name and extension
                                .toString();
                aDoc.getFile().changePath(path);
+               owner = getStudyDAO().merge(owner);
+               // getStudyDAO().update(owner);
        }
 
        /**
@@ -163,11 +164,11 @@ public class DocumentServiceImpl implements DocumentService {
                int number = getStudyService().generateLocalIndex(scope);
 
                if (scheme == FileNaming.encoded) {
-                       encoding.append(scope.getReference()).append(".").append(
+                       encoding.append(scope.getReference()).append("_").append(
                                        tostring.format(number));
                } else { // title and (temporarily) asis
-                       encoding.append(aDoc.getTitle()).append(".").append(
-                                       tostring.format(number));
+                       encoding.append("_").append(tostring.format(number)).append(
+                                       aDoc.getFile().getName());
                }
                return encoding.toString();
        }
@@ -288,6 +289,7 @@ public class DocumentServiceImpl implements DocumentService {
         *            the format
         * @return the created "Converts" relation
         */
+       @Transactional
        public ConvertsRelation attach(final Document aDoc, final String format) {
                return attach(aDoc, format, null);
        }
@@ -311,13 +313,11 @@ public class DocumentServiceImpl implements DocumentService {
                ConvertsRelation attach = new ConvertsRelation(aDoc, export,
                                description);
 
-               // RKV session.save(export);
-               // RKV session.save(attach);
-
                getFileDAO().create(export);
+               getRelationDAO().create(attach);
 
-               // RKV aDoc.addRelation(attach); // Updates this
-               aDoc.getAllRelations().add(attach); // Updates this //RKV
+               aDoc.getAllRelations().add(attach);
+               getDocumentDAO().merge(aDoc);
 
                return attach;
        }
@@ -335,23 +335,21 @@ public class DocumentServiceImpl implements DocumentService {
         */
        public boolean buildReferenceFrom(final Document aDoc,
                        final ProjectElement scope, final Document lineage) {
-               if (aDoc.getProgressState() != ProgressState.inWORK) {
-                       return false;
-               }
-               Study owner = null;
-               Scenario context = null;
-               if (scope instanceof Study) {
-                       owner = (Study) scope;
-               } else {
-                       context = ((Scenario) scope);
-                       owner = context.getOwnerStudy();
-               }
-               aDoc.setDid(lineage.getDid());
-               if (context != null && (lineage.isVersioned() || owner.shares(lineage))) {
-                       aDoc.setVersion(new Revision(aDoc.getVersion()).setBranch(
-                                       context.getReference()).toString());
+               boolean res = (aDoc.getProgressState() == ProgressState.inWORK);
+               if (res) {
+                       Study owner = scope.getOwnerStudy();
+                       Scenario context = null;
+                       if (scope instanceof Scenario) {
+                               context = ((Scenario) scope);
+                       }
+                       aDoc.setDid(lineage.getDid());
+                       if (context != null
+                                       && (lineage.isVersioned() || owner.shares(lineage))) {
+                               aDoc.setVersion(new Revision(aDoc.getVersion()).setBranch(
+                                               context.getReference()).toString());
+                       }
                }
-               return true;
+               return res;
        }
 
        /**
@@ -539,28 +537,20 @@ public class DocumentServiceImpl implements DocumentService {
                } else {
                        Revision myvers = new Revision(aDoc.getVersion());
                        if (!myvers.isNull()) { // Versionning context
-                               for (Iterator<Relation> i = aDoc.getAllRelations().iterator(); i
-                                               .hasNext();) {
-                                       Relation link = i.next();
-                                       if (!link.getClass().equals(VersionsRelation.class)) {
-                                               continue;
-                                       }
-                                       previous = (Document) link.getTo(); // Versioned document
-                                       break;
-                               }
+                               previous = aDoc.getPreviousVersion();
                        }
                        if (aDoc.getProgressState() != null) {
                                myvers.incrementAs(state); // Incrementation if the reversion number is not imposed
                        }
                        aDoc.setVersion(myvers.toString());
                }
-               // Update this document and the previous version, if exit
+               // Update this document and the previous version, if exist
                if (previous != null) {
                        previous.setHistory(previous.getHistory() + 1);
                        getDocumentDAO().merge(previous);
                }
                aDoc.setProgressState(state);
-               getDocumentDAO().update(aDoc);
+               // RKV: getDocumentDAO().update(aDoc);
        }
 
        // protected void upgrade () {
@@ -753,4 +743,23 @@ public class DocumentServiceImpl implements DocumentService {
                _studyDAO = studyDAO;
        }
 
+       /**
+        * Get the relationDAO.
+        * 
+        * @return the relationDAO
+        */
+       public RelationDAO getRelationDAO() {
+               return _relationDAO;
+       }
+
+       /**
+        * Set the relationDAO.
+        * 
+        * @param relationDAO
+        *            the relationDAO to set
+        */
+       public void setRelationDAO(final RelationDAO relationDAO) {
+               _relationDAO = relationDAO;
+       }
+
 }