Salome HOME
StepServiceImpl doesn't use Database class anymore.
[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.splat.dal.bo.kernel.Relation;
18 import org.splat.dal.bo.som.Document;
19 import org.splat.dal.bo.som.KnowledgeElement;
20 import org.splat.dal.bo.som.Publication;
21 import org.splat.dal.bo.som.Scenario;
22 import org.splat.dal.bo.som.SimulationContext;
23 import org.splat.dal.bo.som.Study;
24 import org.splat.dal.bo.som.UsedByRelation;
25 import org.splat.dal.bo.som.UsesRelation;
26 import org.splat.dal.bo.som.VersionsRelation;
27 import org.splat.dal.dao.som.DocumentDAO;
28 import org.splat.dal.dao.som.ProjectElementDAO;
29 import org.splat.dal.dao.som.SimulationContextDAO;
30 import org.splat.kernel.InvalidPropertyException;
31 import org.splat.kernel.MismatchException;
32 import org.splat.kernel.MissedPropertyException;
33 import org.splat.kernel.MultiplyDefinedException;
34 import org.splat.kernel.NotApplicableException;
35 import org.splat.log.AppLogger;
36 import org.splat.service.technical.IndexService;
37 import org.splat.som.Revision;
38 import org.splat.som.Step;
39 import org.springframework.transaction.annotation.Transactional;
40
41 /**
42  * Step service implementation.
43  * 
44  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
45  */
46 public class StepServiceImpl implements StepService {
47
48         /**
49          * logger for the service.
50          */
51         public final static AppLogger logger = AppLogger
52                         .getLogger(StepServiceImpl.class);
53         /**
54          * Injected index service.
55          */
56         private IndexService _indexService;
57         /**
58          * Injected document service.
59          */
60         private DocumentService _documentService;
61         /**
62          * Injected document DAO.
63          */
64         private DocumentDAO _documentDAO;
65         /**
66          * Injected simulation context service.
67          */
68         private SimulationContextService _simulationContextService;
69         /**
70          * Injected simulation context DAO.
71          */
72         private SimulationContextDAO _simulationContextDAO;
73         /**
74          * Injected project element DAO.
75          */
76         private ProjectElementDAO _projectElementDAO;
77
78         /**
79          * {@inheritDoc}
80          * 
81          * @see org.splat.service.StepService#addSimulationContext(org.splat.som.Step, org.splat.dal.bo.som.SimulationContext.Properties)
82          */
83         public SimulationContext addSimulationContext(Step aStep,
84                         SimulationContext.Properties dprop) throws MissedPropertyException,
85                         InvalidPropertyException, MultiplyDefinedException,
86                         RuntimeException {
87                 SimulationContext context = new SimulationContext(dprop.setStep(aStep
88                                 .getStep()));
89                 return addSimulationContext(aStep, context);
90         }
91
92         /**
93          * {@inheritDoc}
94          * 
95          * @see org.splat.service.StepService#addSimulationContext(org.splat.som.Step, org.splat.dal.bo.som.SimulationContext)
96          */
97         @Transactional
98         public SimulationContext addSimulationContext(Step aStep,
99                         SimulationContext context) {
100                 getSimulationContextService().hold(context); // Increments the reference count of simulation context
101                 if (aStep.getOwner().isSaved())
102                         try {
103                                 if (!context.isSaved())
104                                         getSimulationContextDAO().create(context);
105                                 aStep.getOwner().add(context);
106                                 aStep.getContex().add(context); // The context is also referenced from this (transient) Step
107                                 getProjectElementDAO().update(aStep.getOwner());
108                                 updateKnowledgeElementsIndex(aStep);
109                         } catch (Exception error) {
110                                 return null;
111                         }
112                 else { // Happens when copying a scenario
113                         aStep.getOwner().add(context);
114                         aStep.getContex().add(context); // The context is also referenced from this (transient) Step
115                         // In case of owner scenario, the Knowledge Element index will be updated later, when saving the scenario
116                 }
117                 return context;
118         }
119
120         /**
121          * Update lucene index of knowledge elements of a scenario or a study which the given step is related to.
122          * 
123          * @param aStep
124          *            the step (activity)
125          */
126         private void updateKnowledgeElementsIndex(Step aStep) {
127                 Scenario[] scenarii;
128                 if (aStep.getOwner() instanceof Scenario) {
129                         scenarii = new Scenario[1];
130                         scenarii[0] = (Scenario) aStep.getOwner();
131                 } else {
132                         scenarii = aStep.getOwnerStudy().getScenarii();
133                 }
134                 try {
135                         for (int i = 0; i < scenarii.length; i++) {
136                                 Scenario scene = scenarii[i];
137                                 List<KnowledgeElement> knelm = scene.getAllKnowledgeElements();
138                                 for (Iterator<KnowledgeElement> j = knelm.iterator(); j
139                                                 .hasNext();) {
140                                         KnowledgeElement kelm = j.next();
141                                         getIndexService().update(kelm);
142                                 }
143                                 updateScenarioIndex(scene);
144                         }
145                 } catch (Exception error) {
146                         logger.error("Unable to re-index Knowledge Elements, reason:", error);
147                 }
148         }
149
150         /**
151          * Update lucene index for knowledge elements of the scenario.
152          * 
153          * @param scene
154          *            the scenario
155          * @throws IOException
156          *             if can't update lucene index
157          */
158         private void updateScenarioIndex(Scenario scene) throws IOException {
159                 if (scene.getUcase() == null) {
160                         for (Iterator<KnowledgeElement> i = scene.getKnowledgeElements()
161                                         .iterator(); i.hasNext();) {
162                                 KnowledgeElement kelm = i.next();
163                                 if (!kelm.getType().equals("usecase"))
164                                         continue;
165                                 scene.setUcase(kelm);
166                                 break;
167                         }
168                 }
169                 getIndexService().update(scene.getUcase());
170         }
171
172         /**
173          * {@inheritDoc}
174          * 
175          * @see org.splat.service.StepService#removeSimulationContext(org.splat.som.Step, org.splat.dal.bo.som.SimulationContext)
176          */
177         @Transactional
178         public boolean removeSimulationContext(Step aStep, SimulationContext context) {
179                 boolean isOk = false;
180                 SimulationContext torem = aStep
181                                 .getSimulationContext(context.getIndex());
182
183                 if ((torem != null) && (aStep.getOwner().remove(torem))) {
184
185                         aStep.getContex().remove(torem);
186                         getProjectElementDAO().update(aStep.getOwner());
187                         if (torem.isShared()) {
188                                 getSimulationContextService().release(torem);
189                                 getSimulationContextDAO().update(torem);
190                         } else {
191                                 getSimulationContextDAO().delete(torem);
192                         }
193                         isOk = true;
194                 }
195                 return isOk;
196         }
197
198         /**
199          * {@inheritDoc}
200          * 
201          * @see org.splat.service.StepService#createDocument(org.splat.som.Step, org.splat.dal.bo.som.Document.Properties)
202          */
203         @Transactional
204         public Publication createDocument(Step aStep, Document.Properties dprop)
205                         throws MissedPropertyException, InvalidPropertyException,
206                         MultiplyDefinedException, IOException {
207                 Document newdoc = new Document(dprop.setOwner(aStep.getOwner())
208                                 .setStep(aStep.getStep()));
209                 getDocumentService().generateDocumentId(newdoc, dprop);
210
211                 // Creation of the save directory
212                 File wdir = getDocumentService().getSaveDirectory(newdoc);
213                 if (!wdir.exists())
214                         if (!wdir.mkdirs())
215                                 throw new IOException(
216                                                 "Cannot create the repository vault directory");
217
218                 // Identification and save
219                 newdoc.buildReferenceFrom(aStep.getOwnerStudy());
220                 getDocumentDAO().create(newdoc);
221
222                 return new Publication(newdoc, aStep.getOwner());
223         }
224
225         /**
226          * {@inheritDoc}
227          * 
228          * @see org.splat.service.StepService#assignDocument(org.splat.som.Step, org.splat.dal.bo.som.Document.Properties)
229          */
230         public Publication assignDocument(Step aStep, Document.Properties dprop)
231                         throws MissedPropertyException, InvalidPropertyException,
232                         NotApplicableException {
233                 String refid = dprop.getReference();
234                 if (refid == null)
235                         return null;
236
237                 Document slot = getDocumentService().selectDocument(refid,
238                                 new Revision().toString());
239                 if (slot == null)
240                         return null;
241                 if (!slot.isUndefined())
242                         return null; // Should not happen
243
244                 getDocumentService().initialize(slot,
245                                 dprop.setOwner(aStep.getOwnerStudy()));
246                 return new Publication(slot, aStep.getOwner());
247         }
248
249         /**
250          * @param aStep
251          * @param base
252          * @return
253          * @throws MissedPropertyException
254          * @throws InvalidPropertyException
255          * @throws MultiplyDefinedException
256          * @throws IOException
257          * @throws MismatchException
258          */
259         public Publication versionDocument(Step aStep, Publication base)
260                         throws MissedPropertyException, InvalidPropertyException,
261                         MultiplyDefinedException, IOException, MismatchException {
262                 return versionDocument(aStep, base, new Document.Properties());
263         }
264
265         /**
266          * @param aStep
267          * @param base
268          * @param reason
269          * @return
270          * @throws MissedPropertyException
271          * @throws InvalidPropertyException
272          * @throws MultiplyDefinedException
273          * @throws IOException
274          * @throws MismatchException
275          */
276         public Publication versionDocument(Step aStep, Publication base,
277                         String reason) throws MissedPropertyException,
278                         InvalidPropertyException, MultiplyDefinedException, IOException,
279                         MismatchException {
280                 return versionDocument(aStep, base, new Document.Properties()
281                                 .setDescription(reason));
282         }
283
284         /**
285          * {@inheritDoc}
286          * 
287          * @see org.splat.service.StepService#versionDocument(org.splat.som.Step, org.splat.dal.bo.som.Publication,
288          *      org.splat.dal.bo.som.Document.Properties)
289          */
290         @Transactional
291         public Publication versionDocument(Step aStep, Publication base,
292                         Document.Properties dprop) throws MissedPropertyException,
293                         InvalidPropertyException, MultiplyDefinedException, IOException,
294                         MismatchException {
295                 Document previous = base.value();
296
297                 dprop.setDocument(previous); // Initializes the Step property
298                 if (dprop.getStep().getNumber() != aStep.getNumber())
299                         throw new MismatchException();
300
301                 if (dprop.getAuthor() == null)
302                         dprop.setAuthor(previous.getAuthor());
303                 String summary = dprop.getDescription();
304
305                 // Creation of the document
306                 Document newdoc = new Document(dprop.setOwner(aStep.getOwner())
307                                 .setStep(aStep.getStep()));
308                 getDocumentService().generateDocumentId(newdoc, dprop);
309                 newdoc.buildReferenceFrom(aStep.getOwner(), previous);
310                 getDocumentDAO().create(newdoc);
311
312                 // Versioning
313                 if (summary == null)
314                         newdoc.addRelation(new VersionsRelation(newdoc, previous));
315                 else
316                         newdoc.addRelation(new VersionsRelation(newdoc, previous, summary));
317
318                 // Update of usedby relations, if exist
319                 List<Relation> relist = previous.getRelations(UsedByRelation.class);
320                 Study scope = aStep.getOwnerStudy();
321                 for (Iterator<Relation> i = relist.iterator(); i.hasNext();) {
322                         UsedByRelation relation = (UsedByRelation) i.next();
323                         Document relatedoc = relation.getTo();
324                         if (scope.shares(relatedoc))
325                                 relatedoc.addRelation(new UsesRelation(relatedoc, newdoc));
326                         else
327                                 relation.moveTo(newdoc);
328                 }
329                 return new Publication(newdoc, aStep.getOwner());
330         }
331
332         /**
333          * Get the documentService.
334          * 
335          * @return the documentService
336          */
337         public DocumentService getDocumentService() {
338                 return _documentService;
339         }
340
341         /**
342          * Set the documentService.
343          * 
344          * @param documentService
345          *            the documentService to set
346          */
347         public void setDocumentService(DocumentService documentService) {
348                 _documentService = documentService;
349         }
350
351         /**
352          * Get the simulationContextService.
353          * 
354          * @return the simulationContextService
355          */
356         public SimulationContextService getSimulationContextService() {
357                 return _simulationContextService;
358         }
359
360         /**
361          * Set the simulationContextService.
362          * 
363          * @param simulationContextService
364          *            the simulationContextService to set
365          */
366         public void setSimulationContextService(
367                         SimulationContextService simulationContextService) {
368                 _simulationContextService = simulationContextService;
369         }
370
371         /**
372          * Get the documentDAO.
373          * 
374          * @return the documentDAO
375          */
376         public DocumentDAO getDocumentDAO() {
377                 return _documentDAO;
378         }
379
380         /**
381          * Set the documentDAO.
382          * 
383          * @param documentDAO
384          *            the documentDAO to set
385          */
386         public void setDocumentDAO(DocumentDAO documentDAO) {
387                 _documentDAO = documentDAO;
388         }
389
390         /**
391          * Get the simulationContextDAO.
392          * 
393          * @return the simulationContextDAO
394          */
395         public SimulationContextDAO getSimulationContextDAO() {
396                 return _simulationContextDAO;
397         }
398
399         /**
400          * Set the simulationContextDAO.
401          * 
402          * @param simulationContextDAO
403          *            the simulationContextDAO to set
404          */
405         public void setSimulationContextDAO(
406                         SimulationContextDAO simulationContextDAO) {
407                 _simulationContextDAO = simulationContextDAO;
408         }
409
410         /**
411          * Get the projectElementDAO.
412          * 
413          * @return the projectElementDAO
414          */
415         public ProjectElementDAO getProjectElementDAO() {
416                 return _projectElementDAO;
417         }
418
419         /**
420          * Set the projectElementDAO.
421          * 
422          * @param projectElementDAO
423          *            the projectElementDAO to set
424          */
425         public void setProjectElementDAO(ProjectElementDAO projectElementDAO) {
426                 _projectElementDAO = projectElementDAO;
427         }
428
429         /**
430          * Get the indexService.
431          * 
432          * @return the indexService
433          */
434         public IndexService getIndexService() {
435                 return _indexService;
436         }
437
438         /**
439          * Set the indexService.
440          * 
441          * @param indexService
442          *            the indexService to set
443          */
444         public void setIndexService(IndexService indexService) {
445                 _indexService = indexService;
446         }
447 }