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