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.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;
39 * Step service implementation.
40 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
42 public class StepServiceImpl implements StepService {
45 * Injected index service.
47 private IndexService _indexService;
49 * Injected document service.
51 private DocumentService _documentService;
53 * Injected simulation context service.
55 private SimulationContextService _simulationContextService;
57 public SimulationContext addSimulationContext(Step aStep,
58 SimulationContext.Properties dprop) throws MissedPropertyException,
59 InvalidPropertyException, MultiplyDefinedException,
61 SimulationContext context = new SimulationContext(dprop.setStep(aStep
63 return addSimulationContext(aStep, context);
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())
71 Session session = Database.getSession();
72 IndexService lucin = getIndexService();
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) {
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
91 private void updateKnowledgeElementsIndex(Step aStep, IndexService lucin) {
92 // ------------------------------------------------------
94 if (aStep.getOwner() instanceof Scenario) {
95 scenarii = new Scenario[1];
96 scenarii[0] = (Scenario) aStep.getOwner();
98 scenarii = aStep.getOwnerStudy().getScenarii();
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
106 KnowledgeElement kelm = j.next();
109 scene.updateMyIndex(lucin);
111 } catch (Exception error) {
112 // logger.error("Unable to re-index Knowledge Elements, reason:", error);
116 public boolean removeSimulationContext (Step aStep, SimulationContext context) {
117 // ------------------------------------------------------------------
118 SimulationContext torem = aStep.getSimulationContext(context.getIndex());
119 Session session = Database.getSession();
121 if (torem == null) return false;
122 if (!aStep.getOwner().remove(torem)) return false;
124 aStep.getContex().remove(torem);
125 session.update(aStep.getOwner());
126 if (torem.isShared()) {
127 getSimulationContextService().release(torem);
128 session.update(torem);
130 session.delete(torem);
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);
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");
144 // Identification and save
145 newdoc.buildReferenceFrom(aStep.getOwnerStudy());
146 Database.getSession().save(newdoc);
148 return new Publication(newdoc, aStep.getOwner());
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;
156 Document slot = getDocumentService().selectDocument(refid, new Revision().toString());
157 if ( slot == null ) return null;
158 if (!slot.isUndefined()) return null; // Should not happen
160 getDocumentService().initialize(slot, dprop.setOwner(aStep.getOwnerStudy()));
161 return new Publication(slot, aStep.getOwner());
164 public Publication versionDocument (Step aStep, Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
165 // -----------------------------------------------------
166 return versionDocument(aStep, base, new Document.Properties());
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));
174 public Publication versionDocument (Step aStep, Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
175 // --------------------------------------------------------------------------------
176 Document previous = base.value();
178 dprop.setDocument(previous); // Initializes the Step property
179 if (dprop.getStep().getNumber() != aStep.getNumber()) throw new MismatchException();
181 if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
182 String summary = dprop.getDescription();
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);
191 if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
192 else newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
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);
203 return new Publication(newdoc, aStep.getOwner());
209 public IndexService getIndexService() {
210 return _indexService;
213 public void setIndexService(IndexService indexService) {
214 _indexService = indexService;
218 * Get the documentService.
219 * @return the documentService
221 public DocumentService getDocumentService() {
222 return _documentService;
226 * Set the documentService.
227 * @param documentService the documentService to set
229 public void setDocumentService(DocumentService documentService) {
230 _documentService = documentService;
234 * Get the simulationContextService.
235 * @return the simulationContextService
237 public SimulationContextService getSimulationContextService() {
238 return _simulationContextService;
242 * Set the simulationContextService.
243 * @param simulationContextService the simulationContextService to set
245 public void setSimulationContextService(
246 SimulationContextService simulationContextService) {
247 _simulationContextService = simulationContextService;