]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman-Common/src/org/splat/service/DocumentServiceImpl.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / DocumentServiceImpl.java
index fef0b9931e8e160367f3a7df3a902348a4c151d5..d922281e761e42f555d9645acd96a83b36d4a5b9 100644 (file)
@@ -7,7 +7,7 @@
  * @version        $Revision$
  *****************************************************************************/
 
-package org.splat.service; 
+package org.splat.service;
 
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
@@ -18,23 +18,66 @@ import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.Study;
 import org.splat.dal.bo.som.Document.Properties;
 import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.DocumentDAO;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.NotApplicableException;
 import org.splat.service.technical.ProjectSettingsService;
 import org.splat.service.technical.ProjectSettingsServiceImpl;
 import org.splat.service.technical.ProjectSettingsServiceImpl.FileNaming;
-import org.splat.som.Step;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
+ * Document service implementation.
+ * 
  * @author RKV
- *
+ * 
  */
 public class DocumentServiceImpl implements DocumentService {
 
+       /**
+        * Injected study service.
+        */
        private StudyService _studyService;
+       /**
+        * Injected project settings service.
+        */
        private ProjectSettingsService _projectSettingsService;
+       /**
+        * Injected document service.
+        */
+       private DocumentDAO _documentDAO;
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.DocumentService#selectDocument(long)
+        */
+       @Transactional(readOnly = true)
+       public Document selectDocument(long index) {
+               // -------------------------------------------------
+               return getDocumentDAO().get(index);
+       }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.DocumentService#selectDocument(java.lang.String, java.lang.String)
+        */
+       public Document selectDocument(String refid, String version) {
+               // --------------------------------------------------------------------
+               StringBuffer query = new StringBuffer("from Document where did='")
+                               .append(refid).append("' and version='").append(version)
+                               .append("'");
+               return (Document) Database.getSession().createQuery(query.toString())
+                               .uniqueResult();
+       }
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.DocumentService#generateDocumentId(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
+        */
        public void generateDocumentId(Document aDoc, Properties dprop) {
                Study owner = null;
                if (dprop.getOwner() instanceof Study)
@@ -48,19 +91,31 @@ public class DocumentServiceImpl implements DocumentService {
                String path = owner.getReference();
                ProjectSettingsService.Step step = ProjectSettingsServiceImpl
                                .getStep(aDoc.getStep());
-               aDoc.setDid( new StringBuffer(path).append(".%").append(aDoc.suformat).toString()); // Document reference
+               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
-                               .append(filename).append(".").append(aDoc.getFile().getFormat()) // File name and extension
+                               .append(step.getPath())
+                               // File path relative to the repository vault
+                               .append(filename).append(".")
+                               .append(aDoc.getFile().getFormat()) // File name and extension
                                .toString();
                aDoc.getFile().changePath(path);
        }
 
+       /**
+        * Generate encoded document file name according to the naming scheme from project settings.
+        * 
+        * @param aDoc
+        *            the document
+        * @param scope
+        *            the study
+        * @return document reference name
+        */
        private String generateEncodedName(Document aDoc, Study scope) {
                // ------------------------------------------------
                StringBuffer encoding = new StringBuffer();
                FileNaming scheme = getProjectSettings().getFileNamingScheme();
-               DecimalFormat tostring = new DecimalFormat(aDoc.suformat);
+               DecimalFormat tostring = new DecimalFormat(Document.suformat);
 
                int number = getStudyService().generateLocalIndex(scope);
 
@@ -68,11 +123,21 @@ public class DocumentServiceImpl implements DocumentService {
                        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();
        }
 
+       /**
+        * Get encoded root name for a document file.
+        * 
+        * @param aDoc
+        *            the document
+        * @param scope
+        *            the study
+        * @return file name
+        */
        private String getEncodedRootName(Document aDoc, Study scope) {
                // -----------------------------------------------
                FileNaming scheme = getProjectSettings().getFileNamingScheme();
@@ -84,16 +149,13 @@ public class DocumentServiceImpl implements DocumentService {
        }
 
        /**
-        * Defines this document.
-        * 
-        * @param dprop
-        *            the properties of the document
+        * {@inheritDoc}
         * 
-        * @see Step#createDocument(Properties)
-        * @see #isUndefined()
+        * @see org.splat.service.DocumentService#initialize(org.splat.dal.bo.som.Document, org.splat.dal.bo.som.Document.Properties)
         */
-       public void initialize(Document aDoc, Properties dprop) throws MissedPropertyException,
-                       InvalidPropertyException, NotApplicableException {
+       public void initialize(Document aDoc, Properties dprop)
+                       throws MissedPropertyException, InvalidPropertyException,
+                       NotApplicableException {
                // --------------------------------------------
                if (!aDoc.isUndefined())
                        throw new NotApplicableException(
@@ -108,10 +170,11 @@ public class DocumentServiceImpl implements DocumentService {
                // throw new InvalidPropertyException("step");
                // }
                aDoc.setTitle(dprop.getName());
-               aDoc.getFile().changePath(aDoc.getFile().getRelativePath().replace("%n",
-                               getEncodedRootName(aDoc, (Study) dprop.getOwner())));
+               aDoc.getFile().changePath(
+                               aDoc.getFile().getRelativePath().replace("%n",
+                                               getEncodedRootName(aDoc, (Study) dprop.getOwner())));
                if (aDoc.getHistory() == -1)
-                       aDoc.setHistory( 0 );
+                       aDoc.setHistory(0);
                if (dprop.getDate() == null) {
                        Calendar current = Calendar.getInstance();
                        aDoc.setLastModificationDate(current.getTime()); // Today
@@ -121,8 +184,21 @@ public class DocumentServiceImpl implements DocumentService {
                Database.getSession().update(aDoc);
        }
 
+       public java.io.File getSaveDirectory(Document aDoc) {
+               String mypath = Database.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++)
+                       path = path.append("/").append(table[i]);
+               return new java.io.File(path.append("/").toString());
+       }
+
        /**
         * Get the studyService.
+        * 
         * @return the studyService
         */
        public StudyService getStudyService() {
@@ -131,7 +207,9 @@ public class DocumentServiceImpl implements DocumentService {
 
        /**
         * Set the studyService.
-        * @param studyService the studyService to set
+        * 
+        * @param studyService
+        *            the studyService to set
         */
        public void setStudyService(StudyService studyService) {
                _studyService = studyService;
@@ -156,4 +234,23 @@ public class DocumentServiceImpl implements DocumentService {
                _projectSettingsService = projectSettingsService;
        }
 
+       /**
+        * Get the documentDAO.
+        * 
+        * @return the documentDAO
+        */
+       public DocumentDAO getDocumentDAO() {
+               return _documentDAO;
+       }
+
+       /**
+        * Set the documentDAO.
+        * 
+        * @param documentDAO
+        *            the documentDAO to set
+        */
+       public void setDocumentDAO(DocumentDAO documentDAO) {
+               _documentDAO = documentDAO;
+       }
+
 }