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