Salome HOME
353477ebf7f01266720ca822ad8aa8c3c725f166
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StepServiceImpl.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   06.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.service;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.hibernate.Session;
18 import org.splat.dal.bo.kernel.Relation;
19 import org.splat.dal.bo.som.Document;
20 import org.splat.dal.bo.som.KnowledgeElement;
21 import org.splat.dal.bo.som.Publication;
22 import org.splat.dal.bo.som.Scenario;
23 import org.splat.dal.bo.som.SimulationContext;
24 import org.splat.dal.bo.som.Study;
25 import org.splat.dal.bo.som.UsedByRelation;
26 import org.splat.dal.bo.som.UsesRelation;
27 import org.splat.dal.bo.som.VersionsRelation;
28 import org.splat.dal.dao.som.Database;
29 import org.splat.kernel.InvalidPropertyException;
30 import org.splat.kernel.MismatchException;
31 import org.splat.kernel.MissedPropertyException;
32 import org.splat.kernel.MultiplyDefinedException;
33 import org.splat.kernel.NotApplicableException;
34 import org.splat.service.technical.IndexService;
35 import org.splat.service.technical.IndexServiceImpl;
36 import org.splat.som.Revision;
37 import org.splat.som.Step;
38
39 /**
40  * @author RKV
41  * 
42  */
43 public class StepServiceImpl implements StepService {
44
45         private IndexService _indexService;
46         private DocumentService _documentService;
47
48         public SimulationContext addSimulationContext(Step aStep,
49                         SimulationContext.Properties dprop) throws MissedPropertyException,
50                         InvalidPropertyException, MultiplyDefinedException,
51                         RuntimeException {
52                 // ----------------------------------------------------------------------------------
53                 SimulationContext context = new SimulationContext(dprop.setStep(aStep
54                                 .getStep()));
55                 return addSimulationContext(aStep, context);
56         }
57
58         public SimulationContext addSimulationContext(Step aStep,
59                         SimulationContext context) {
60                 // -------------------------------------------------------------------------
61                 context.hold(); // Increments the reference count of simulation context
62                 if (aStep.getOwner().isSaved())
63                         try {
64                                 Session session = Database.getSession();
65                                 IndexService lucin = getIndexService();
66
67                                 if (!context.isSaved())
68                                         session.save(context);
69                                 aStep.getOwner().add(context);
70                                 aStep.getContex().add(context); // The context is also referenced from this (transient) Step
71                                 session.update(aStep.getOwner());
72                                 updateKnowledgeElementsIndex(aStep, lucin);
73                         } catch (Exception error) {
74                                 return null;
75                         }
76                 else { // Happens when copying a scenario
77                         aStep.getOwner().add(context);
78                         aStep.getContex().add(context); // The context is also referenced from this (transient) Step
79                         // In case of owner scenario, the Knowledge Element index will be updated later, when saving the scenario
80                 }
81                 return context;
82         }
83
84         private void updateKnowledgeElementsIndex(Step aStep, IndexService lucin) {
85                 // ------------------------------------------------------
86                 Scenario[] scenarii;
87                 if (aStep.getOwner() instanceof Scenario) {
88                         scenarii = new Scenario[1];
89                         scenarii[0] = (Scenario) aStep.getOwner();
90                 } else {
91                         scenarii = aStep.getOwnerStudy().getScenarii();
92                 }
93                 try {
94                         for (int i = 0; i < scenarii.length; i++) {
95                                 Scenario scene = scenarii[i];
96                                 List<KnowledgeElement> knelm = scene.getAllKnowledgeElements();
97                                 for (Iterator<KnowledgeElement> j = knelm.iterator(); j
98                                                 .hasNext();) {
99                                         KnowledgeElement kelm = j.next();
100                                         lucin.update(kelm);
101                                 }
102                                 scene.updateMyIndex(lucin);
103                         }
104                 } catch (Exception error) {
105                         // logger.error("Unable to re-index Knowledge Elements, reason:", error);
106                 }
107         }
108
109     public boolean removeSimulationContext (Step aStep, SimulationContext context) {
110 //  ------------------------------------------------------------------
111       SimulationContext  torem   = aStep.getSimulationContext(context.getIndex());
112       Session            session = Database.getSession();
113
114       if (torem == null)        return false;
115       if (!aStep.getOwner().remove(torem)) return false;
116
117       aStep.getContex().remove(torem);
118       session.update(aStep.getOwner());
119       if (torem.isShared()) {
120         torem.release();
121         session.update(torem);
122       } else {
123         session.delete(torem);
124       }
125       return true;
126     }
127
128     public Publication createDocument (Step aStep, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException {
129     //  -------------------------------------------------------------
130           Document  newdoc = new Document(dprop.setOwner(aStep.getOwner()).setStep(aStep.getStep()));
131           getDocumentService().generateDocumentId(newdoc, dprop);
132
133 //        Creation of the save directory      
134           File wdir = newdoc.getSaveDirectory();
135           if (!wdir.exists()) if (!wdir.mkdirs()) throw new IOException("Cannot create the repository vault directory");
136
137 //        Identification and save
138           newdoc.buildReferenceFrom(aStep.getOwnerStudy());
139           Database.getSession().save(newdoc);
140
141           return  new Publication(newdoc, aStep.getOwner());
142         }
143
144         public Publication assignDocument (Step aStep, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, NotApplicableException {
145     //  -------------------------------------------------------------
146           String refid = dprop.getReference();
147           if    (refid == null)    return null;
148
149           Document  slot = Database.selectDocument(refid, new Revision().toString());
150           if ( slot == null )      return null;
151           if (!slot.isUndefined()) return null;     // Should not happen
152
153           getDocumentService().initialize(slot, dprop.setOwner(aStep.getOwnerStudy()));
154           return  new Publication(slot, aStep.getOwner());
155         }
156
157         public Publication versionDocument (Step aStep, Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
158     //  -----------------------------------------------------
159           return versionDocument(aStep, base, new Document.Properties());
160         }
161
162         public Publication versionDocument (Step aStep, Publication base, String reason) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
163     //  --------------------------------------------------------------------
164           return versionDocument(aStep, base, new Document.Properties().setDescription(reason));
165         }
166
167         public Publication versionDocument (Step aStep, Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
168     //  --------------------------------------------------------------------------------
169           Document previous = base.value();
170           
171           dprop.setDocument(previous);        // Initializes the Step property
172           if (dprop.getStep().getNumber() != aStep.getNumber()) throw new MismatchException();
173
174           if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
175           String    summary = dprop.getDescription();
176
177 //        Creation of the document
178           Document  newdoc = new Document(dprop.setOwner(aStep.getOwner()).setStep(aStep.getStep()));
179           getDocumentService().generateDocumentId(newdoc, dprop);
180           newdoc.buildReferenceFrom(aStep.getOwner(), previous);
181           Database.getSession().save(newdoc);
182
183 //        Versioning
184           if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
185           else                 newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
186
187 //        Update of usedby relations, if exist
188           List<Relation> relist = previous.getRelations(UsedByRelation.class);
189           Study          scope  = aStep.getOwnerStudy();
190           for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
191             UsedByRelation relation  = (UsedByRelation)i.next();
192             Document       relatedoc = relation.getTo();
193             if (scope.shares(relatedoc)) relatedoc.addRelation( new UsesRelation(relatedoc, newdoc) );
194             else                         relation.moveTo(newdoc);
195           }
196           return  new Publication(newdoc, aStep.getOwner());
197         }
198
199         /**
200          * @return
201          */
202         public IndexService getIndexService() {
203                 return _indexService;
204         }
205
206         public void setIndexService(IndexService indexService) {
207                 _indexService = indexService;
208         }
209
210         /**
211          * Get the documentService.
212          * @return the documentService
213          */
214         public DocumentService getDocumentService() {
215                 return _documentService;
216         }
217
218         /**
219          * Set the documentService.
220          * @param documentService the documentService to set
221          */
222         public void setDocumentService(DocumentService documentService) {
223                 _documentService = documentService;
224         }
225 }