1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
13 import java.io.IOException;
14 import java.util.Iterator;
15 import java.util.List;
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;
42 * Step service implementation.
44 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
46 public class StepServiceImpl implements StepService {
49 * logger for the service.
51 public final static AppLogger logger = AppLogger
52 .getLogger(StepServiceImpl.class);
54 * Injected index service.
56 private IndexService _indexService;
58 * Injected document service.
60 private DocumentService _documentService;
62 * Injected document DAO.
64 private DocumentDAO _documentDAO;
66 * Injected simulation context service.
68 private SimulationContextService _simulationContextService;
70 * Injected simulation context DAO.
72 private SimulationContextDAO _simulationContextDAO;
74 * Injected project element DAO.
76 private ProjectElementDAO _projectElementDAO;
81 * @see org.splat.service.StepService#addSimulationContext(org.splat.som.Step, org.splat.dal.bo.som.SimulationContext.Properties)
83 public SimulationContext addSimulationContext(Step aStep,
84 SimulationContext.Properties dprop) throws MissedPropertyException,
85 InvalidPropertyException, MultiplyDefinedException,
87 SimulationContext context = new SimulationContext(dprop.setStep(aStep
89 return addSimulationContext(aStep, context);
95 * @see org.splat.service.StepService#addSimulationContext(org.splat.som.Step, org.splat.dal.bo.som.SimulationContext)
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())
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) {
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
121 * Update lucene index of knowledge elements of a scenario or a study which the given step is related to.
124 * the step (activity)
126 private void updateKnowledgeElementsIndex(Step aStep) {
128 if (aStep.getOwner() instanceof Scenario) {
129 scenarii = new Scenario[1];
130 scenarii[0] = (Scenario) aStep.getOwner();
132 scenarii = aStep.getOwnerStudy().getScenarii();
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
140 KnowledgeElement kelm = j.next();
141 getIndexService().update(kelm);
143 updateScenarioIndex(scene);
145 } catch (Exception error) {
146 logger.error("Unable to re-index Knowledge Elements, reason:", error);
151 * Update lucene index for knowledge elements of the scenario.
155 * @throws IOException
156 * if can't update lucene index
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"))
165 scene.setUcase(kelm);
169 getIndexService().update(scene.getUcase());
175 * @see org.splat.service.StepService#removeSimulationContext(org.splat.som.Step, org.splat.dal.bo.som.SimulationContext)
178 public boolean removeSimulationContext(Step aStep, SimulationContext context) {
179 boolean isOk = false;
180 SimulationContext torem = aStep
181 .getSimulationContext(context.getIndex());
183 if ((torem != null) && (aStep.getOwner().remove(torem))) {
185 aStep.getContex().remove(torem);
186 getProjectElementDAO().update(aStep.getOwner());
187 if (torem.isShared()) {
188 getSimulationContextService().release(torem);
189 getSimulationContextDAO().update(torem);
191 getSimulationContextDAO().delete(torem);
201 * @see org.splat.service.StepService#createDocument(org.splat.som.Step, org.splat.dal.bo.som.Document.Properties)
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);
211 // Creation of the save directory
212 File wdir = getDocumentService().getSaveDirectory(newdoc);
215 throw new IOException(
216 "Cannot create the repository vault directory");
218 // Identification and save
219 newdoc.buildReferenceFrom(aStep.getOwnerStudy());
220 getDocumentDAO().create(newdoc);
222 return new Publication(newdoc, aStep.getOwner());
228 * @see org.splat.service.StepService#assignDocument(org.splat.som.Step, org.splat.dal.bo.som.Document.Properties)
230 public Publication assignDocument(Step aStep, Document.Properties dprop)
231 throws MissedPropertyException, InvalidPropertyException,
232 NotApplicableException {
233 String refid = dprop.getReference();
237 Document slot = getDocumentService().selectDocument(refid,
238 new Revision().toString());
241 if (!slot.isUndefined())
242 return null; // Should not happen
244 getDocumentService().initialize(slot,
245 dprop.setOwner(aStep.getOwnerStudy()));
246 return new Publication(slot, aStep.getOwner());
253 * @throws MissedPropertyException
254 * @throws InvalidPropertyException
255 * @throws MultiplyDefinedException
256 * @throws IOException
257 * @throws MismatchException
259 public Publication versionDocument(Step aStep, Publication base)
260 throws MissedPropertyException, InvalidPropertyException,
261 MultiplyDefinedException, IOException, MismatchException {
262 return versionDocument(aStep, base, new Document.Properties());
270 * @throws MissedPropertyException
271 * @throws InvalidPropertyException
272 * @throws MultiplyDefinedException
273 * @throws IOException
274 * @throws MismatchException
276 public Publication versionDocument(Step aStep, Publication base,
277 String reason) throws MissedPropertyException,
278 InvalidPropertyException, MultiplyDefinedException, IOException,
280 return versionDocument(aStep, base, new Document.Properties()
281 .setDescription(reason));
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)
291 public Publication versionDocument(Step aStep, Publication base,
292 Document.Properties dprop) throws MissedPropertyException,
293 InvalidPropertyException, MultiplyDefinedException, IOException,
295 Document previous = base.value();
297 dprop.setDocument(previous); // Initializes the Step property
298 if (dprop.getStep().getNumber() != aStep.getNumber())
299 throw new MismatchException();
301 if (dprop.getAuthor() == null)
302 dprop.setAuthor(previous.getAuthor());
303 String summary = dprop.getDescription();
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);
314 newdoc.addRelation(new VersionsRelation(newdoc, previous));
316 newdoc.addRelation(new VersionsRelation(newdoc, previous, summary));
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));
327 relation.moveTo(newdoc);
329 return new Publication(newdoc, aStep.getOwner());
333 * Get the documentService.
335 * @return the documentService
337 public DocumentService getDocumentService() {
338 return _documentService;
342 * Set the documentService.
344 * @param documentService
345 * the documentService to set
347 public void setDocumentService(DocumentService documentService) {
348 _documentService = documentService;
352 * Get the simulationContextService.
354 * @return the simulationContextService
356 public SimulationContextService getSimulationContextService() {
357 return _simulationContextService;
361 * Set the simulationContextService.
363 * @param simulationContextService
364 * the simulationContextService to set
366 public void setSimulationContextService(
367 SimulationContextService simulationContextService) {
368 _simulationContextService = simulationContextService;
372 * Get the documentDAO.
374 * @return the documentDAO
376 public DocumentDAO getDocumentDAO() {
381 * Set the documentDAO.
384 * the documentDAO to set
386 public void setDocumentDAO(DocumentDAO documentDAO) {
387 _documentDAO = documentDAO;
391 * Get the simulationContextDAO.
393 * @return the simulationContextDAO
395 public SimulationContextDAO getSimulationContextDAO() {
396 return _simulationContextDAO;
400 * Set the simulationContextDAO.
402 * @param simulationContextDAO
403 * the simulationContextDAO to set
405 public void setSimulationContextDAO(
406 SimulationContextDAO simulationContextDAO) {
407 _simulationContextDAO = simulationContextDAO;
411 * Get the projectElementDAO.
413 * @return the projectElementDAO
415 public ProjectElementDAO getProjectElementDAO() {
416 return _projectElementDAO;
420 * Set the projectElementDAO.
422 * @param projectElementDAO
423 * the projectElementDAO to set
425 public void setProjectElementDAO(ProjectElementDAO projectElementDAO) {
426 _projectElementDAO = projectElementDAO;
430 * Get the indexService.
432 * @return the indexService
434 public IndexService getIndexService() {
435 return _indexService;
439 * Set the indexService.
441 * @param indexService
442 * the indexService to set
444 public void setIndexService(IndexService indexService) {
445 _indexService = indexService;