Salome HOME
Fixed: adding a knowledge element (but there is still a double addition), opening...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StepServiceImpl.java
index dda84d66b00fb482af8be20e3e6570853aabce50..353477ebf7f01266720ca822ad8aa8c3c725f166 100644 (file)
@@ -9,19 +9,31 @@
 
 package org.splat.service;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 
 import org.hibernate.Session;
+import org.splat.dal.bo.kernel.Relation;
+import org.splat.dal.bo.som.Document;
 import org.splat.dal.bo.som.KnowledgeElement;
+import org.splat.dal.bo.som.Publication;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.SimulationContext;
+import org.splat.dal.bo.som.Study;
+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.Database;
 import org.splat.kernel.InvalidPropertyException;
+import org.splat.kernel.MismatchException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
+import org.splat.kernel.NotApplicableException;
 import org.splat.service.technical.IndexService;
 import org.splat.service.technical.IndexServiceImpl;
+import org.splat.som.Revision;
 import org.splat.som.Step;
 
 /**
@@ -31,6 +43,7 @@ import org.splat.som.Step;
 public class StepServiceImpl implements StepService {
 
        private IndexService _indexService;
+       private DocumentService _documentService;
 
        public SimulationContext addSimulationContext(Step aStep,
                        SimulationContext.Properties dprop) throws MissedPropertyException,
@@ -112,6 +125,77 @@ public class StepServiceImpl implements StepService {
       return true;
     }
 
+    public Publication createDocument (Step aStep, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException {
+    //  -------------------------------------------------------------
+          Document  newdoc = new Document(dprop.setOwner(aStep.getOwner()).setStep(aStep.getStep()));
+          getDocumentService().generateDocumentId(newdoc, dprop);
+
+//        Creation of the save directory      
+          File wdir = newdoc.getSaveDirectory();
+         if (!wdir.exists()) if (!wdir.mkdirs()) throw new IOException("Cannot create the repository vault directory");
+
+//        Identification and save
+          newdoc.buildReferenceFrom(aStep.getOwnerStudy());
+          Database.getSession().save(newdoc);
+
+          return  new Publication(newdoc, aStep.getOwner());
+        }
+
+        public Publication assignDocument (Step aStep, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, NotApplicableException {
+    //  -------------------------------------------------------------
+          String refid = dprop.getReference();
+          if    (refid == null)    return null;
+
+          Document  slot = Database.selectDocument(refid, new Revision().toString());
+          if ( slot == null )      return null;
+          if (!slot.isUndefined()) return null;     // Should not happen
+
+          getDocumentService().initialize(slot, dprop.setOwner(aStep.getOwnerStudy()));
+          return  new Publication(slot, aStep.getOwner());
+        }
+
+        public Publication versionDocument (Step aStep, Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
+    //  -----------------------------------------------------
+          return versionDocument(aStep, base, new Document.Properties());
+        }
+
+        public Publication versionDocument (Step aStep, Publication base, String reason) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
+    //  --------------------------------------------------------------------
+          return versionDocument(aStep, base, new Document.Properties().setDescription(reason));
+        }
+
+        public Publication versionDocument (Step aStep, Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
+    //  --------------------------------------------------------------------------------
+          Document previous = base.value();
+          
+          dprop.setDocument(previous);        // Initializes the Step property
+          if (dprop.getStep().getNumber() != aStep.getNumber()) throw new MismatchException();
+
+          if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
+          String    summary = dprop.getDescription();
+
+//        Creation of the document
+          Document  newdoc = new Document(dprop.setOwner(aStep.getOwner()).setStep(aStep.getStep()));
+          getDocumentService().generateDocumentId(newdoc, dprop);
+          newdoc.buildReferenceFrom(aStep.getOwner(), previous);
+          Database.getSession().save(newdoc);
+
+//        Versioning
+          if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
+          else                 newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
+
+//        Update of usedby relations, if exist
+          List<Relation> relist = previous.getRelations(UsedByRelation.class);
+          Study          scope  = aStep.getOwnerStudy();
+          for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
+            UsedByRelation relation  = (UsedByRelation)i.next();
+            Document       relatedoc = relation.getTo();
+            if (scope.shares(relatedoc)) relatedoc.addRelation( new UsesRelation(relatedoc, newdoc) );
+            else                         relation.moveTo(newdoc);
+          }
+          return  new Publication(newdoc, aStep.getOwner());
+        }
+
        /**
         * @return
         */
@@ -122,4 +206,20 @@ public class StepServiceImpl implements StepService {
        public void setIndexService(IndexService indexService) {
                _indexService = indexService;
        }
+
+       /**
+        * 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;
+       }
 }