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 / PublicationServiceImpl.java
index e2e530214d12c71196bf1c657701e1edeff93e41..1516ba4305010b555607115c84c8c5c4006cf76c 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Company         EURIWARE
+ * Company         OPEN CASCADE
  * Application     SIMAN
  * File            $Id$ 
  * Creation date   06.10.2012
@@ -15,8 +15,8 @@ import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
-import org.hibernate.Session;
 import org.splat.dal.bo.kernel.User;
+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.ProgressState;
@@ -28,12 +28,15 @@ import org.splat.dal.bo.som.Study;
 import org.splat.dal.bo.som.Timestamp;
 import org.splat.dal.bo.som.ValidationCycle;
 import org.splat.dal.bo.som.ValidationStep;
-import org.splat.dal.dao.som.Database;
+import org.splat.dal.dao.som.ProjectElementDAO;
+import org.splat.dal.dao.som.PublicationDAO;
+import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.NotApplicableException;
 import org.splat.manox.Reader;
 import org.splat.manox.Toolbox;
 import org.splat.som.Revision;
 import org.splat.som.Step;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * Publication service implementation.
@@ -47,21 +50,29 @@ public class PublicationServiceImpl implements PublicationService {
         */
        private StudyService _studyService;
        /**
-        * Injected study service.
+        * Injected step service.
         */
        private StepService _stepService;
        /**
-        * Injected study service.
+        * Injected document service.
         */
-       private DocumentTypeService _documentTypeService;
+       private DocumentService _documentService;
        /**
-        * Injected study service.
+        * Injected project element service.
         */
        private ProjectElementService _projectElementService;
        /**
         * Injected simulation context service.
         */
        private SimulationContextService _simulationContextService;
+       /**
+        * Injected publication DAO.
+        */
+       private PublicationDAO _publicationDAO;
+       /**
+        * Injected project element DAO.
+        */
+       private ProjectElementDAO _projectElementDAO;
 
        /**
         * {@inheritDoc}
@@ -86,7 +97,6 @@ public class PublicationServiceImpl implements PublicationService {
         * @see org.splat.service.PublicationService#approve(org.splat.dal.bo.som.Publication, java.util.Date)
         */
        public Timestamp approve(Publication aPublication, Date adate) {
-               // -------------------------------------
                if (aPublication.isOutdated())
                        return null;
                else if (aPublication.value().getProgressState() != ProgressState.inCHECK)
@@ -94,13 +104,14 @@ public class PublicationServiceImpl implements PublicationService {
 
                DocumentType type = aPublication.value().getType();
                Study owner = aPublication.getOwnerStudy();
-               ValidationCycle cycle = owner.getValidationCycleOf(type);
+               ValidationCycle cycle = getStudyService().getValidationCycleOf(owner,
+                               type);
                User approver = cycle.getActor(ValidationStep.APPROVAL);
                Timestamp stamp = new Timestamp(ValidationStep.APPROVAL, aPublication
                                .value(), approver, adate);
-               if (!aPublication.value().promote(stamp))
+               if (!getDocumentService().promote(aPublication.value(), stamp))
                        return null;
-               if (getDocumentTypeService().isStudyResult(type)
+               if (getDocumentService().isStudyResult(type)
                                && owner.getProgressState() == ProgressState.inCHECK)
                        getStudyService().promote(owner);
                return stamp; // Hoping that promotion of the study succeeded
@@ -112,27 +123,27 @@ public class PublicationServiceImpl implements PublicationService {
         * @see org.splat.service.PublicationService#demote(org.splat.dal.bo.som.Publication)
         */
        public boolean demote(Publication aPublication) {
-               // ------------------------
                DocumentType type = aPublication.value().getType();
                Study owner = aPublication.getOwnerStudy();
 
                if (aPublication.value().getProgressState() == ProgressState.inCHECK) {
-                       ValidationCycle cycle = owner.getValidationCycleOf(type);
+                       ValidationCycle cycle = getStudyService().getValidationCycleOf(
+                                       owner, type);
                        if (cycle.enables(ValidationStep.REVIEW)) {
-                               if (!aPublication.value().demote())
+                               if (!getDocumentService().demote(aPublication.value()))
                                        return false;
                        } else {
-                               if (!aPublication.value().demote())
+                               if (!getDocumentService().demote(aPublication.value()))
                                        return false;
-                               aPublication.value().demote();
+                               getDocumentService().demote(aPublication.value());
                        }
                } else if (aPublication.value().getProgressState() == ProgressState.inDRAFT) {
-                       if (!aPublication.value().demote())
+                       if (!getDocumentService().demote(aPublication.value()))
                                return false;
                } else {
                        return false;
                }
-               if (getDocumentTypeService().isStudyResult(type)
+               if (getDocumentService().isStudyResult(type)
                                && owner.getProgressState() != ProgressState.inWORK)
                        getStudyService().demote(owner);
                return true;
@@ -144,14 +155,13 @@ public class PublicationServiceImpl implements PublicationService {
         * @see org.splat.service.PublicationService#invalidate(org.splat.dal.bo.som.Publication)
         */
        public boolean invalidate(Publication aPublication) {
-               // ----------------------------
                if (aPublication.value().getProgressState() != ProgressState.inCHECK)
                        return false;
-               if (!aPublication.value().demote()) // Removes the reviewer if this document is In-Check
+               if (!getDocumentService().demote(aPublication.value())) // Removes the reviewer if this document is In-Check
                        return false;
                DocumentType type = aPublication.value().getType();
                Study owner = aPublication.getOwnerStudy();
-               if (getDocumentTypeService().isStudyResult(type)
+               if (getDocumentService().isStudyResult(type)
                                && owner.getProgressState() == ProgressState.inCHECK)
                        getStudyService().demote(owner);
                return true;
@@ -170,7 +180,8 @@ public class PublicationServiceImpl implements PublicationService {
                else {
                        DocumentType type = aPublication.value().getType();
                        Study owner = aPublication.getOwnerStudy();
-                       ValidationCycle cycle = owner.getValidationCycleOf(type);
+                       ValidationCycle cycle = getStudyService().getValidationCycleOf(
+                                       owner, type);
                        User promoter = cycle.getActor(ValidationStep.PROMOTION);
                        if (promoter == null)
                                promoter = getInvolvedStep(aPublication).getActor();
@@ -179,12 +190,12 @@ public class PublicationServiceImpl implements PublicationService {
                        Timestamp stamp = new Timestamp(ValidationStep.PROMOTION,
                                        aPublication.value(), promoter, pdate);
 
-                       if (!aPublication.value().promote(stamp)) // Promotion to being reviewed
+                       if (!getDocumentService().promote(aPublication.value(), stamp)) // Promotion to being reviewed
                                return null;
                        if (!cycle.enables(ValidationStep.REVIEW)) {
-                               aPublication.value().promote(null);
+                               getDocumentService().promote(aPublication.value(), null);
                        }
-                       if (getDocumentTypeService().isStudyResult(type)
+                       if (getDocumentService().isStudyResult(type)
                                        && owner.getProgressState() == ProgressState.inWORK)
                                getStudyService().promote(owner);
                        return stamp; // Hoping that promotion of the study succeeded
@@ -204,13 +215,14 @@ public class PublicationServiceImpl implements PublicationService {
 
                DocumentType type = aPublication.value().getType();
                Study owner = aPublication.getOwnerStudy();
-               ValidationCycle cycle = owner.getValidationCycleOf(type);
+               ValidationCycle cycle = getStudyService().getValidationCycleOf(owner,
+                               type);
                User reviewer = cycle.getActor(ValidationStep.REVIEW);
                Timestamp stamp = new Timestamp(ValidationStep.REVIEW, aPublication
                                .value(), reviewer, rdate);
-               if (!aPublication.value().promote(stamp))
+               if (!getDocumentService().promote(aPublication.value(), stamp))
                        return null;
-               if (getDocumentTypeService().isStudyResult(type)
+               if (getDocumentService().isStudyResult(type)
                                && owner.getProgressState() == ProgressState.inDRAFT)
                        getStudyService().promote(owner);
                return stamp; // Hoping that promotion of the study succeeded
@@ -222,17 +234,17 @@ public class PublicationServiceImpl implements PublicationService {
         * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.som.Revision)
         * @deprecated
         */
+       @Transactional
        public void saveAs(Publication aPublication, Revision newvers)
                        throws FileNotFoundException, NotApplicableException {
-               // -------------------------------------
                if (aPublication.value().isUndefined())
                        throw new NotApplicableException(
                                        "Cannot save a Publication object refering an undefined Document");
                if (!aPublication.value().getSourceFile().exists())
                        throw new FileNotFoundException();
 
-               Database.getSession().save(aPublication); // Must be done before updating the study in order to fix this final (rid-based) hascode
-               aPublication.value().updateAs(newvers); // May change the branch name of given revision
+               getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based) hascode
+               getDocumentService().updateAs(aPublication.value(), newvers); // May change the branch name of given revision
                updateOwner(aPublication);
        }
 
@@ -241,9 +253,9 @@ public class PublicationServiceImpl implements PublicationService {
         * 
         * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.dal.bo.som.ProgressState)
         */
+       @Transactional
        public void saveAs(Publication aPublication, ProgressState state)
                        throws FileNotFoundException, NotApplicableException {
-               // ----------------------------------------
                if (aPublication.value().isUndefined())
                        throw new NotApplicableException(
                                        "Cannot save a Publication object refering an undefined Document");
@@ -251,13 +263,14 @@ public class PublicationServiceImpl implements PublicationService {
                        throw new FileNotFoundException();
 
                if (state == ProgressState.inWORK || state == ProgressState.EXTERN) {
-                       Database.getSession().save(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
+                       getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
                        // hascode
-                       aPublication.value().updateAs(state);
+                       getDocumentService().updateAs(aPublication.value(), state);
                } else {
                        DocumentType mytype = aPublication.value().getType();
                        Study owner = aPublication.getOwnerStudy();
-                       ValidationCycle cycle = owner.getValidationCycleOf(mytype);
+                       ValidationCycle cycle = getStudyService().getValidationCycleOf(
+                                       owner, mytype);
                        boolean review = cycle.enables(ValidationStep.REVIEW);
                        if (!(state == ProgressState.inDRAFT && review)
                                        && !(state == ProgressState.inCHECK && !review)) {
@@ -265,9 +278,10 @@ public class PublicationServiceImpl implements PublicationService {
                                                "Cannot save a result document in " + state.toString()
                                                                + " state");
                        }
-                       Database.getSession().save(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
+                       getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
                        // hascode
-                       aPublication.value().updateAs(ProgressState.inWORK);
+                       getDocumentService().updateAs(aPublication.value(),
+                                       ProgressState.inWORK);
 
                        promote(aPublication, aPublication.value()
                                        .getLastModificationDate()); // Promotes to the appropriate state in accordance to the validation cycle
@@ -281,24 +295,24 @@ public class PublicationServiceImpl implements PublicationService {
         * @param aPublication
         *            the document publication
         */
+       @Transactional
        private void updateOwner(Publication aPublication) {
-               Session session = Database.getSession();
                Step step = getInvolvedStep(aPublication);
 
                // Update of involved step
                Document previous = aPublication.value().getPreviousVersion();
                if (previous != null) {
                        Publication oldoc = step.getDocument(previous.getIndex());
-                       boolean done = step.remove(oldoc); // Decrements the configuration tag count of document
+                       boolean done = getStepService().remove(step, oldoc); // Decrements the configuration tag count of document
                        if (done)
-                               session.delete(oldoc); // WARNING: Potential problem because it's not automatically done as orphan object
+                               getPublicationDAO().delete(oldoc); // WARNING: Potential problem because it's not automatically done as orphan object
                }
-               step.add(aPublication); // Increments the configuration tag count of document
+               getStepService().add(step, aPublication); // Increments the configuration tag count of document
 
                // Import the document properties and update of the study
                forwardProperties(aPublication, aPublication.value().getSourceFile()
                                .asFile(), step);
-               session.update(aPublication.getOwner());
+               getProjectElementDAO().update(aPublication.getOwner());
        }
 
        /**
@@ -313,7 +327,6 @@ public class PublicationServiceImpl implements PublicationService {
         */
        private void forwardProperties(Publication aPublication, java.io.File from,
                        Step to) {
-               // -----------------------------------------------------------
                Reader tool = Toolbox.getReader(from);
                if (tool == null)
                        return; // No properties extractor available for this type of document
@@ -380,6 +393,51 @@ public class PublicationServiceImpl implements PublicationService {
                return aPublication.getStep();
        }
 
+       /**
+        * Create "Converts" relation for the given document publication and format.
+        * 
+        * @param aPublication
+        *            the document publication
+        * @param format
+        *            the format
+        * @return the created "Converts" relation
+        */
+       public ConvertsRelation attach(Publication aPublication, String format) {
+               return getDocumentService().attach(aPublication.value(), format);
+       }
+
+       /**
+        * Create "Converts" relation for the given document publication, format and description.
+        * 
+        * @param aPublication
+        *            the document publication
+        * @param format
+        *            the format
+        * @param description
+        *            the description of the relation
+        * @return the created "Converts" relation
+        */
+       public ConvertsRelation attach(Publication aPublication, String format,
+                       String description) {
+               return getDocumentService().attach(aPublication.value(), format,
+                               description);
+       }
+
+       /**
+        * Rename the published document.
+        * 
+        * @param aPublication
+        *            the publication of the document
+        * @param title
+        *            the new document title
+        * @throws InvalidPropertyException
+        *             if the new title is empty
+        */
+       public void rename(Publication aPublication, String title)
+                       throws InvalidPropertyException {
+               getDocumentService().rename(aPublication.value(), title);
+       }
+
        /**
         * Get the projectElementService.
         * 
@@ -422,6 +480,7 @@ public class PublicationServiceImpl implements PublicationService {
 
        /**
         * Get the studyService.
+        * 
         * @return the studyService
         */
        public StudyService getStudyService() {
@@ -430,7 +489,9 @@ public class PublicationServiceImpl implements PublicationService {
 
        /**
         * Set the studyService.
-        * @param studyService the studyService to set
+        * 
+        * @param studyService
+        *            the studyService to set
         */
        public void setStudyService(StudyService studyService) {
                _studyService = studyService;
@@ -438,6 +499,7 @@ public class PublicationServiceImpl implements PublicationService {
 
        /**
         * Get the stepService.
+        * 
         * @return the stepService
         */
        public StepService getStepService() {
@@ -446,25 +508,68 @@ public class PublicationServiceImpl implements PublicationService {
 
        /**
         * Set the stepService.
-        * @param stepService the stepService to set
+        * 
+        * @param stepService
+        *            the stepService to set
         */
        public void setStepService(StepService stepService) {
                _stepService = stepService;
        }
 
        /**
-        * Get the documentTypeService.
-        * @return the documentTypeService
+        * Get the documentService.
+        * 
+        * @return the documentService
+        */
+       public DocumentService getDocumentService() {
+               return _documentService;
+       }
+
+       /**
+        * Set the documentService.
+        * 
+        * @param documentService
+        *            the documentService to set
+        */
+       public void setDocumentService(DocumentService documentService) {
+               _documentService = documentService;
+       }
+
+       /**
+        * Get the publicationDAO.
+        * 
+        * @return the publicationDAO
+        */
+       public PublicationDAO getPublicationDAO() {
+               return _publicationDAO;
+       }
+
+       /**
+        * Set the publicationDAO.
+        * 
+        * @param publicationDAO
+        *            the publicationDAO to set
         */
-       public DocumentTypeService getDocumentTypeService() {
-               return _documentTypeService;
+       public void setPublicationDAO(PublicationDAO publicationDAO) {
+               _publicationDAO = publicationDAO;
        }
 
        /**
-        * Set the documentTypeService.
-        * @param documentTypeService the documentTypeService to set
+        * Get the projectElementDAO.
+        * 
+        * @return the projectElementDAO
+        */
+       public ProjectElementDAO getProjectElementDAO() {
+               return _projectElementDAO;
+       }
+
+       /**
+        * Set the projectElementDAO.
+        * 
+        * @param projectElementDAO
+        *            the projectElementDAO to set
         */
-       public void setDocumentTypeService(DocumentTypeService documentTypeService) {
-               _documentTypeService = documentTypeService;
+       public void setProjectElementDAO(ProjectElementDAO projectElementDAO) {
+               _projectElementDAO = projectElementDAO;
        }
 }