Salome HOME
More business logic has been moved from BO to services. ServiceLocator is created...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StepServiceImpl.java
index ccb0faca57e227217ef0ecfd7a7a1756fee87623..d40b87a50d22c329a76ec721b96721a337ea5b29 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Company         EURIWARE
+ * Company         OPEN CASCADE
  * Application     SIMAN
  * File            $Id$ 
  * Creation date   06.10.2012
@@ -9,13 +9,17 @@
 
 package org.splat.service;
 
-import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
+import java.util.Vector;
 
 import org.splat.dal.bo.kernel.Relation;
+import org.splat.dal.bo.som.ConvertsRelation;
 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.Publication;
 import org.splat.dal.bo.som.Scenario;
@@ -25,6 +29,7 @@ import org.splat.dal.bo.som.UsedByRelation;
 import org.splat.dal.bo.som.UsesRelation;
 import org.splat.dal.bo.som.VersionsRelation;
 import org.splat.dal.dao.som.DocumentDAO;
+import org.splat.dal.dao.som.FileDAO;
 import org.splat.dal.dao.som.ProjectElementDAO;
 import org.splat.dal.dao.som.SimulationContextDAO;
 import org.splat.kernel.InvalidPropertyException;
@@ -58,10 +63,18 @@ public class StepServiceImpl implements StepService {
         * Injected document service.
         */
        private DocumentService _documentService;
+       /**
+        * Injected document type service.
+        */
+       private DocumentTypeService _documentTypeService;
        /**
         * Injected document DAO.
         */
        private DocumentDAO _documentDAO;
+       /**
+        * Injected file DAO.
+        */
+       private FileDAO _fileDAO;
        /**
         * Injected simulation context service.
         */
@@ -82,8 +95,7 @@ public class StepServiceImpl implements StepService {
         */
        public SimulationContext addSimulationContext(Step aStep,
                        SimulationContext.Properties dprop) throws MissedPropertyException,
-                       InvalidPropertyException, MultiplyDefinedException,
-                       RuntimeException {
+                       InvalidPropertyException, MultiplyDefinedException {
                SimulationContext context = new SimulationContext(dprop.setStep(aStep
                                .getStep()));
                return addSimulationContext(aStep, context);
@@ -143,7 +155,8 @@ public class StepServiceImpl implements StepService {
                                updateScenarioIndex(scene);
                        }
                } catch (Exception error) {
-                       logger.error("Unable to re-index Knowledge Elements, reason:", error);
+                       logger.error("Unable to re-index Knowledge Elements, reason:",
+                                       error);
                }
        }
 
@@ -209,14 +222,14 @@ public class StepServiceImpl implements StepService {
                getDocumentService().generateDocumentId(newdoc, dprop);
 
                // Creation of the save directory
-               File wdir = getDocumentService().getSaveDirectory(newdoc);
+               java.io.File wdir = getDocumentService().getSaveDirectory(newdoc);
                if (!wdir.exists())
                        if (!wdir.mkdirs())
                                throw new IOException(
                                                "Cannot create the repository vault directory");
 
                // Identification and save
-               newdoc.buildReferenceFrom(aStep.getOwnerStudy());
+               getDocumentService().buildReferenceFrom(newdoc, aStep.getOwnerStudy());
                getDocumentDAO().create(newdoc);
 
                return new Publication(newdoc, aStep.getOwner());
@@ -247,14 +260,23 @@ public class StepServiceImpl implements StepService {
        }
 
        /**
+        * Create a new version of a document in the given study step.
+        * 
         * @param aStep
+        *            the study step
         * @param base
-        * @return
+        *            the base document published version
+        * @return the new version publication
         * @throws MissedPropertyException
+        *             if a mandatory property is missed
         * @throws InvalidPropertyException
+        *             if some property doesn't exist
         * @throws MultiplyDefinedException
+        *             if some property is defined several times
         * @throws IOException
+        *             if a file system error occurs
         * @throws MismatchException
+        *             if the document is not applicable to the given study step
         */
        public Publication versionDocument(Step aStep, Publication base)
                        throws MissedPropertyException, InvalidPropertyException,
@@ -263,15 +285,25 @@ public class StepServiceImpl implements StepService {
        }
 
        /**
+        * Create a new version of a document in the given study step.
+        * 
         * @param aStep
+        *            the study step
         * @param base
+        *            the base document published version
         * @param reason
-        * @return
+        *            the comment for the new version
+        * @return the new version publication
         * @throws MissedPropertyException
+        *             if a mandatory property is missed
         * @throws InvalidPropertyException
+        *             if some property doesn't exist
         * @throws MultiplyDefinedException
+        *             if some property is defined several times
         * @throws IOException
+        *             if a file system error occurs
         * @throws MismatchException
+        *             if the document is not applicable to the given study step
         */
        public Publication versionDocument(Step aStep, Publication base,
                        String reason) throws MissedPropertyException,
@@ -282,10 +314,25 @@ public class StepServiceImpl implements StepService {
        }
 
        /**
-        * {@inheritDoc}
+        * Create a new version of a document in the given study step.
         * 
-        * @see org.splat.service.StepService#versionDocument(org.splat.som.Step, org.splat.dal.bo.som.Publication,
-        *      org.splat.dal.bo.som.Document.Properties)
+        * @param aStep
+        *            the study step
+        * @param base
+        *            the base document published version
+        * @param dprop
+        *            properties of the new version
+        * @return the new version publication
+        * @throws MissedPropertyException
+        *             if a mandatory property is missed
+        * @throws InvalidPropertyException
+        *             if some property doesn't exist
+        * @throws MultiplyDefinedException
+        *             if some property is defined several times
+        * @throws IOException
+        *             if a file system error occurs
+        * @throws MismatchException
+        *             if the document is not applicable to the given study step
         */
        @Transactional
        public Publication versionDocument(Step aStep, Publication base,
@@ -306,7 +353,8 @@ public class StepServiceImpl implements StepService {
                Document newdoc = new Document(dprop.setOwner(aStep.getOwner())
                                .setStep(aStep.getStep()));
                getDocumentService().generateDocumentId(newdoc, dprop);
-               newdoc.buildReferenceFrom(aStep.getOwner(), previous);
+               getDocumentService().buildReferenceFrom(newdoc, aStep.getOwner(),
+                               previous);
                getDocumentDAO().create(newdoc);
 
                // Versioning
@@ -329,6 +377,91 @@ public class StepServiceImpl implements StepService {
                return new Publication(newdoc, aStep.getOwner());
        }
 
+       /**
+        * Get document types which are applicable for the given study step (activity).
+        * 
+        * @param aStep
+        *            the study step
+        * @return the list of document types
+        */
+       public List<DocumentType> getValidDocumentTypes(Step aStep) {
+               return getDocumentTypeService().selectTypesOf(aStep.getStep());
+       }
+
+       /**
+        * Add a document publication to the given step.
+        * 
+        * @param aStep
+        *            the target study step
+        * @param newdoc
+        *            the document publication to add
+        * @return true if publication succeeded
+        */
+       public boolean add(Step aStep, Publication newdoc) {
+               if (!aStep.getOwner().add(newdoc))
+                       return false; // Updates the study in memory
+               aStep.getDocuments().add(0, newdoc); // Updates this step
+               getDocumentService().hold(newdoc.value()); // Increments the configuration tag count of document
+               // If not yet saved, the Publication MUST NOT be saved here, although this creates a temporary inconsistent state into the
+               // database (it will be saved later by cascading the update of owner scenario).
+               return true;
+       }
+
+       /**
+        * Remove a document publication from the given step.
+        * 
+        * @param aStep
+        *            the study step
+        * @param oldoc
+        *            the document publication to remove
+        * @return true if removing of the publication succeeded
+        */
+       public boolean remove(Step aStep, Publication oldoc) {
+               if (!aStep.getOwner().remove(oldoc))
+                       return false; // Updates the study in memory
+               aStep.getDocuments().remove(oldoc); // Updates this step
+               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;
+       }
+
+       /**
+        * Remove a document from the given step.
+        * 
+        * @param aStep
+        *            the study step
+        * @param doctag
+        *            the document publication
+        * @return true if removing of the document succeeded
+        */
+       public boolean removeDocument(Step aStep, Publication doctag) {
+               Document value = doctag.value();
+               Publication torem = aStep.getDocument(value.getIndex());
+
+               if (torem == null)
+                       return false;
+
+               remove(aStep, torem);
+               getProjectElementDAO().update(aStep.getOwner());
+               if (!value.isPublished() && !value.isVersioned()) { // The referenced document is no more used
+                       Set<Relation> links = value.getAllRelations(); // Get all relation of the document to remove them
+                       List<Document> using = new Vector<Document>();
+                       for (Iterator<Relation> i = links.iterator(); i.hasNext();) {
+                               Relation link = i.next();
+                               if (link.getClass().equals(ConvertsRelation.class)) { // File conversion
+                                       getFileDAO().delete((File) link.getTo()); // The corresponding physical file is not removed from the vault
+                               } else if (link.getClass().equals(UsesRelation.class)) { // Document dependency
+                                       using.add((Document) link.getTo());
+                               }
+                       }
+                       for (Iterator<Document> i = using.iterator(); i.hasNext();) {
+                               i.next().removeRelation(UsedByRelation.class, value); // TODO: RKV: don't use Database.getSession in removeRelation
+                       }
+                       getDocumentDAO().delete(value); // The corresponding physical file is not removed from the vault
+               }
+               return true;
+       }
+
        /**
         * Get the documentService.
         * 
@@ -444,4 +577,42 @@ public class StepServiceImpl implements StepService {
        public void setIndexService(IndexService indexService) {
                _indexService = indexService;
        }
+
+       /**
+        * Get the fileDAO.
+        * 
+        * @return the fileDAO
+        */
+       public FileDAO getFileDAO() {
+               return _fileDAO;
+       }
+
+       /**
+        * Set the fileDAO.
+        * 
+        * @param fileDAO
+        *            the fileDAO to set
+        */
+       public void setFileDAO(FileDAO fileDAO) {
+               _fileDAO = fileDAO;
+       }
+
+       /**
+        * Get the documentTypeService.
+        * 
+        * @return the documentTypeService
+        */
+       public DocumentTypeService getDocumentTypeService() {
+               return _documentTypeService;
+       }
+
+       /**
+        * Set the documentTypeService.
+        * 
+        * @param documentTypeService
+        *            the documentTypeService to set
+        */
+       public void setDocumentTypeService(DocumentTypeService documentTypeService) {
+               _documentTypeService = documentTypeService;
+       }
 }