]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java
Salome HOME
Default document types mappings are moved from application settings (my.xml) to proje...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / DocumentServiceImpl.java
index 325bfd19acec845fbcabae94ac1af47429629081..cfa49834adb4b1804f9d6a430c2c856924e4ce91 100644 (file)
@@ -34,13 +34,14 @@ import org.splat.dal.bo.som.Document.Properties;
 import org.splat.dal.dao.som.DocumentDAO;
 import org.splat.dal.dao.som.DocumentTypeDAO;
 import org.splat.dal.dao.som.FileDAO;
+import org.splat.dal.dao.som.StudyDAO;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.NotApplicableException;
+import org.splat.log.AppLogger;
 import org.splat.manox.Reader;
 import org.splat.manox.Toolbox;
 import org.splat.service.technical.ProjectSettingsService;
-import org.splat.service.technical.ProjectSettingsServiceImpl;
 import org.splat.service.technical.RepositoryService;
 import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
 import org.splat.som.Revision;
@@ -54,6 +55,12 @@ import org.springframework.transaction.annotation.Transactional;
  */
 public class DocumentServiceImpl implements DocumentService {
 
+       /**
+        * The logger for the service.
+        */
+       public final static AppLogger LOG = AppLogger
+                       .getLogger(DocumentServiceImpl.class);
+
        /**
         * Injected study service.
         */
@@ -61,7 +68,7 @@ public class DocumentServiceImpl implements DocumentService {
        /**
         * Injected project settings service.
         */
-       private ProjectSettingsService _projectSettingsService;
+       private ProjectSettingsService _projectSettings;
        /**
         * Injected repository service.
         */
@@ -78,6 +85,10 @@ public class DocumentServiceImpl implements DocumentService {
         * Injected file DAO.
         */
        private FileDAO _fileDAO;
+       /**
+        * Injected study DAO.
+        */
+       private StudyDAO _studyDAO;
 
        /**
         * {@inheritDoc}
@@ -85,7 +96,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @see org.splat.service.DocumentService#selectDocument(long)
         */
        @Transactional(readOnly = true)
-       public Document selectDocument(long index) {
+       public Document selectDocument(final long index) {
                return getDocumentDAO().get(index);
        }
 
@@ -95,7 +106,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
         */
        @Transactional(readOnly = true)
-       public Document selectDocument(String refid, String version) {
+       public Document selectDocument(final String refid, final String version) {
                Criterion aCondition = Restrictions.and(Restrictions.eq("did", refid),
                                Restrictions.eq("version", version));
                return getDocumentDAO().findByCriteria(aCondition);
@@ -107,21 +118,25 @@ public class DocumentServiceImpl implements DocumentService {
         * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
         */
        @Transactional
-       public void generateDocumentId(Document aDoc, Properties dprop) {
+       public void generateDocumentId(final Document aDoc, final Properties dprop) {
                Study owner = null;
-               if (dprop.getOwner() instanceof Study)
+               if (dprop.getOwner() instanceof Study) {
                        owner = (Study) dprop.getOwner();
-               else
+               } else {
                        owner = ((Scenario) dprop.getOwner()).getOwnerStudy();
+               }
+
+               // Synchronize the object with the current Hibernate session.
+               owner = getStudyDAO().get(owner.getIndex());
 
-               SimpleDateFormat tostring = new SimpleDateFormat("yyyy");
+               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();
-               ProjectSettingsService.Step step = ProjectSettingsServiceImpl
-                               .getStep(aDoc.getStep());
-               aDoc.setDid(new StringBuffer(path).append(".%")
-                               .append(Document.suformat).toString()); // Document reference
+               ProjectSettingsService.Step step = getProjectSettings().getStep(
+                               aDoc.getStep());
+               aDoc.setDid(new StringBuffer(path).append(".%").append(
+                               Document.suformat).toString()); // Document reference
                path = new StringBuffer(year).append("/").append(path).append("/")
                                .append(step.getPath())
                                // File path relative to the repository vault
@@ -140,7 +155,7 @@ public class DocumentServiceImpl implements DocumentService {
         *            the study
         * @return document reference name
         */
-       private String generateEncodedName(Document aDoc, Study scope) {
+       private String generateEncodedName(final Document aDoc, final Study scope) {
                StringBuffer encoding = new StringBuffer();
                FileNaming scheme = getProjectSettings().getFileNamingScheme();
                DecimalFormat tostring = new DecimalFormat(Document.suformat);
@@ -148,11 +163,11 @@ public class DocumentServiceImpl implements DocumentService {
                int number = getStudyService().generateLocalIndex(scope);
 
                if (scheme == FileNaming.encoded) {
-                       encoding.append(scope.getReference()).append(".")
-                                       .append(tostring.format(number));
+                       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(aDoc.getTitle()).append(".").append(
+                                       tostring.format(number));
                }
                return encoding.toString();
        }
@@ -166,13 +181,14 @@ public class DocumentServiceImpl implements DocumentService {
         *            the study
         * @return file name
         */
-       private String getEncodedRootName(Document aDoc, Study scope) {
+       private String getEncodedRootName(final Document aDoc, final Study scope) {
                FileNaming scheme = getProjectSettings().getFileNamingScheme();
 
-               if (scheme == FileNaming.encoded)
+               if (scheme == FileNaming.encoded) {
                        return scope.getReference();
-               else
+               } else {
                        return aDoc.getTitle();
+               }
        }
 
        /**
@@ -181,31 +197,32 @@ public class DocumentServiceImpl implements DocumentService {
         * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
         */
        @Transactional
-       public void initialize(Document aDoc, Properties dprop)
+       public void initialize(final Document aDoc, final Properties dprop)
                        throws MissedPropertyException, InvalidPropertyException,
                        NotApplicableException {
-               if (!aDoc.isUndefined())
+               if (!aDoc.isUndefined()) {
                        throw new NotApplicableException(
                                        "Cannot initialize an existing Document");
-               if (dprop.getName() == null)
+               }
+               if (dprop.getName() == null) {
                        throw new MissedPropertyException("name");
-               if (dprop.getName().length() == 0)
+               }
+               if (dprop.getName().length() == 0) {
                        throw new InvalidPropertyException("name");
-               if (dprop.getOwner() == null)
+               }
+               if (dprop.getOwner() == null) {
                        throw new MissedPropertyException("owner");
+               }
                // if (dprop.owner instanceof Study && !ProjectSettings.getStep(step).appliesTo(Study.class)) {
                // throw new InvalidPropertyException("step");
                // }
                aDoc.setTitle(dprop.getName());
                aDoc.getFile().changePath(
-                               aDoc.getFile()
-                                               .getRelativePath()
-                                               .replace(
-                                                               "%n",
-                                                               getEncodedRootName(aDoc,
-                                                                               (Study) dprop.getOwner())));
-               if (aDoc.getHistory() == -1)
+                               aDoc.getFile().getRelativePath().replace("%n",
+                                               getEncodedRootName(aDoc, (Study) dprop.getOwner())));
+               if (aDoc.getHistory() == -1) {
                        aDoc.setHistory(0);
+               }
                if (dprop.getDate() == null) {
                        Calendar current = Calendar.getInstance();
                        aDoc.setLastModificationDate(current.getTime()); // Today
@@ -220,15 +237,16 @@ public class DocumentServiceImpl implements DocumentService {
         * 
         * @see org.splat.service.DocumentService#getSaveDirectory(org.splat.dal.bo.som.Document)
         */
-       public java.io.File getSaveDirectory(Document aDoc) {
+       public java.io.File getSaveDirectory(final Document aDoc) {
                String mypath = getRepositoryService().getRepositoryVaultPath()
                                + aDoc.getSourceFile().getRelativePath();
                String[] table = mypath.split("/");
 
                // Cutting the filename
                StringBuffer path = new StringBuffer(table[0]);
-               for (int i = 1; i < table.length - 1; i++)
+               for (int i = 1; i < table.length - 1; i++) {
                        path = path.append("/").append(table[i]);
+               }
                return new java.io.File(path.append("/").toString());
        }
 
@@ -239,21 +257,25 @@ public class DocumentServiceImpl implements DocumentService {
         *            the file to parse
         * @return the extracted properties
         */
-       public Properties extractProperties(java.io.File file) {
+       public Properties extractProperties(final java.io.File file) {
                Properties fprop = new Properties();
                Reader tool = Toolbox.getReader(file);
                String value;
-               if (tool != null)
+               if (tool != null) {
                        try {
                                value = tool.extractProperty("title");
-                               if (value != null)
+                               if (value != null) {
                                        fprop.setName(value);
+                               }
 
                                value = tool.extractProperty("reference");
-                               if (value != null)
+                               if (value != null) {
                                        fprop.setReference(value);
+                               }
                        } catch (Exception e) {
+                               LOG.debug(e.getMessage(), e);
                        }
+               }
                return fprop;
        }
 
@@ -266,7 +288,7 @@ public class DocumentServiceImpl implements DocumentService {
         *            the format
         * @return the created "Converts" relation
         */
-       public ConvertsRelation attach(Document aDoc, String format) {
+       public ConvertsRelation attach(final Document aDoc, final String format) {
                return attach(aDoc, format, null);
        }
 
@@ -282,14 +304,13 @@ public class DocumentServiceImpl implements DocumentService {
         * @return the created "Converts" relation
         */
        @Transactional
-       public ConvertsRelation attach(Document aDoc, String format,
-                       String description) {
+       public ConvertsRelation attach(final Document aDoc, final String format,
+                       final String description) {
                String path = aDoc.getRelativePath();
                File export = new File(path + "." + format);
                ConvertsRelation attach = new ConvertsRelation(aDoc, export,
                                description);
 
-               // RKV Session session = Database.getSession();
                // RKV session.save(export);
                // RKV session.save(attach);
 
@@ -312,15 +333,16 @@ public class DocumentServiceImpl implements DocumentService {
         *            the original document
         * @return true if the new reference is set
         */
-       public boolean buildReferenceFrom(Document aDoc, ProjectElement scope,
-                       Document lineage) {
-               if (aDoc.getProgressState() != ProgressState.inWORK)
+       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)
+               if (scope instanceof Study) {
                        owner = (Study) scope;
-               else {
+               else {
                        context = ((Scenario) scope);
                        owner = context.getOwnerStudy();
                }
@@ -341,10 +363,11 @@ public class DocumentServiceImpl implements DocumentService {
         *            the study
         * @return true if the new reference is set
         */
-       public boolean buildReferenceFrom(Document aDoc, Study scope) {
+       public boolean buildReferenceFrom(final Document aDoc, final Study scope) {
                if (aDoc.getProgressState() != ProgressState.inWORK
-                               && aDoc.getProgressState() != ProgressState.EXTERN)
+                               && aDoc.getProgressState() != ProgressState.EXTERN) {
                        return false;
+               }
                DecimalFormat tostring = new DecimalFormat(Document.suformat);
 
                aDoc.setDid(aDoc.getDid().replace("%" + Document.suformat,
@@ -360,7 +383,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @return true if demoting succeeded
         */
        @Transactional
-       public boolean demote(Document aDoc) {
+       public boolean demote(final Document aDoc) {
                ValidationStep torem;
 
                if (aDoc.getProgressState() == ProgressState.inCHECK) {
@@ -377,10 +400,12 @@ public class DocumentServiceImpl implements DocumentService {
                for (Iterator<Relation> i = aDoc.getAllRelations().iterator(); i
                                .hasNext();) {
                        Relation link = i.next();
-                       if (!(link instanceof StampRelation))
+                       if (!(link instanceof StampRelation)) {
                                continue;
-                       if (((StampRelation) link).getStampType() != torem)
+                       }
+                       if (((StampRelation) link).getStampType() != torem) {
                                continue;
+                       }
                        i.remove();
                        break;
                }
@@ -398,7 +423,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @return true if promotion succeeded
         */
        @Transactional
-       public boolean promote(Document aDoc, Timestamp stamp) {
+       public boolean promote(final Document aDoc, final Timestamp stamp) {
                ProgressState newstate = null;
 
                if (aDoc.getProgressState() == ProgressState.inWORK) {
@@ -431,7 +456,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @see #release()
         */
        @Transactional
-       public void hold(Document aDoc) {
+       public void hold(final Document aDoc) {
                aDoc.setCountag(aDoc.getCountag() + 1);
                if (aDoc.isSaved()) {
                        getDocumentDAO().update(aDoc);
@@ -446,10 +471,10 @@ public class DocumentServiceImpl implements DocumentService {
         * @see #hold()
         */
        @Transactional
-       public void release(Document aDoc) {
+       public void release(final Document aDoc) {
                aDoc.setCountag(aDoc.getCountag() - 1);
                if (aDoc.isSaved()) {
-                       getDocumentDAO().update(aDoc);
+                       getDocumentDAO().merge(aDoc);
                }
        }
 
@@ -464,10 +489,11 @@ public class DocumentServiceImpl implements DocumentService {
         *             if the new title is empty
         */
        @Transactional
-       public void rename(Document aDoc, String title)
+       public void rename(final Document aDoc, final String title)
                        throws InvalidPropertyException {
-               if (title.length() == 0)
+               if (title.length() == 0) {
                        throw new InvalidPropertyException("name");
+               }
 
                Calendar current = Calendar.getInstance();
                aDoc.setTitle(title);
@@ -483,11 +509,12 @@ public class DocumentServiceImpl implements DocumentService {
         * @param newvers
         *            the new version
         */
-       public void updateAs(Document aDoc, Revision newvers) {
+       public void updateAs(final Document aDoc, final Revision newvers) {
                aDoc.setVersion(newvers.setBranch(aDoc.getVersion()).toString()); // Branch names are propagated by the versionning
                ProgressState newstate = ProgressState.inCHECK;
-               if (newvers.isMinor())
+               if (newvers.isMinor()) {
                        newstate = ProgressState.inWORK;
+               }
                aDoc.setProgressState(null); // Just to tell updateAs(state) to not increment the version number
                updateAs(aDoc, newstate);
        }
@@ -501,33 +528,35 @@ public class DocumentServiceImpl implements DocumentService {
         *            the new state
         */
        @Transactional
-       public void updateAs(Document aDoc, ProgressState state) {
+       public void updateAs(final Document aDoc, final ProgressState state) {
                Document previous = null;
 
                // Set of version number
                if (state == ProgressState.EXTERN) {
-                       if (aDoc.getProgressState() != ProgressState.EXTERN)
+                       if (aDoc.getProgressState() != ProgressState.EXTERN) {
                                aDoc.setVersion(null); // Strange use-case...
+                       }
                } 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;
+                                       if (link.getClass().equals(VersionsRelation.class)) {
+                                               previous = (Document) link.getTo(); // Versioned document
+                                               break;
+                                       }
                                }
                        }
-                       if (aDoc.getProgressState() != null)
+                       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
                if (previous != null) {
                        previous.setHistory(previous.getHistory() + 1);
-                       getDocumentDAO().update(previous);
+                       getDocumentDAO().merge(previous);
                }
                aDoc.setProgressState(state);
                getDocumentDAO().update(aDoc);
@@ -563,8 +592,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @see #isStepResult()
         * @see #isResultOf(org.splat.service.technical.ProjectSettingsServiceImpl.Step)
         */
-       public boolean isStudyResult(DocumentType aType) {
-               // -------------------------------
+       public boolean isStudyResult(final DocumentType aType) {
                List<ProjectSettingsService.Step> step = getProjectSettings()
                                .getAllSteps();
                ProjectSettingsService.Step lastep = step.get(step.size() - 1);
@@ -573,10 +601,12 @@ public class DocumentServiceImpl implements DocumentService {
 
        /**
         * Get document by its path.
-        * @param path the document path
+        * 
+        * @param path
+        *            the document path
         * @return the document if found or null
         */
-       @Transactional(readOnly=true)
+       @Transactional(readOnly = true)
        public Document getDocumentByPath(String path) {
                String[] parse = path.split("'");
 
@@ -603,7 +633,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @param studyService
         *            the studyService to set
         */
-       public void setStudyService(StudyService studyService) {
+       public void setStudyService(final StudyService studyService) {
                _studyService = studyService;
        }
 
@@ -613,7 +643,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @return Project settings service
         */
        private ProjectSettingsService getProjectSettings() {
-               return _projectSettingsService;
+               return _projectSettings;
        }
 
        /**
@@ -622,8 +652,9 @@ public class DocumentServiceImpl implements DocumentService {
         * @param projectSettingsService
         *            project settings service
         */
-       public void setProjectSettings(ProjectSettingsService projectSettingsService) {
-               _projectSettingsService = projectSettingsService;
+       public void setProjectSettings(
+                       final ProjectSettingsService projectSettingsService) {
+               _projectSettings = projectSettingsService;
        }
 
        /**
@@ -641,7 +672,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @param documentDAO
         *            the documentDAO to set
         */
-       public void setDocumentDAO(DocumentDAO documentDAO) {
+       public void setDocumentDAO(final DocumentDAO documentDAO) {
                _documentDAO = documentDAO;
        }
 
@@ -660,7 +691,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @param repositoryService
         *            the repositoryService to set
         */
-       public void setRepositoryService(RepositoryService repositoryService) {
+       public void setRepositoryService(final RepositoryService repositoryService) {
                _repositoryService = repositoryService;
        }
 
@@ -679,7 +710,7 @@ public class DocumentServiceImpl implements DocumentService {
         * @param documentTypeDAO
         *            the documentTypeDAO to set
         */
-       public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO) {
+       public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
                _documentTypeDAO = documentTypeDAO;
        }
 
@@ -698,8 +729,27 @@ public class DocumentServiceImpl implements DocumentService {
         * @param fileDAO
         *            the fileDAO to set
         */
-       public void setFileDAO(FileDAO fileDAO) {
+       public void setFileDAO(final FileDAO fileDAO) {
                _fileDAO = fileDAO;
        }
 
+       /**
+        * Get the studyDAO.
+        * 
+        * @return the studyDAO
+        */
+       public StudyDAO getStudyDAO() {
+               return _studyDAO;
+       }
+
+       /**
+        * Set the studyDAO.
+        * 
+        * @param studyDAO
+        *            the studyDAO to set
+        */
+       public void setStudyDAO(final StudyDAO studyDAO) {
+               _studyDAO = studyDAO;
+       }
+
 }