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.service.technical.IndexServiceImpl;
36 import org.splat.som.Revision;
37 import org.splat.som.Step;
40 * Step service implementation.
41 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
43 public class StepServiceImpl implements StepService {
46 * Injected index service.
48 private IndexService _indexService;
50 * Injected document service.
52 private DocumentService _documentService;
54 * Injected simulation context service.
56 private SimulationContextService _simulationContextService;
58 public SimulationContext addSimulationContext(Step aStep,
59 SimulationContext.Properties dprop) throws MissedPropertyException,
60 InvalidPropertyException, MultiplyDefinedException,
62 // ----------------------------------------------------------------------------------
63 SimulationContext context = new SimulationContext(dprop.setStep(aStep
65 return addSimulationContext(aStep, context);
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())
74 Session session = Database.getSession();
75 IndexService lucin = getIndexService();
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) {
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
94 private void updateKnowledgeElementsIndex(Step aStep, IndexService lucin) {
95 // ------------------------------------------------------
97 if (aStep.getOwner() instanceof Scenario) {
98 scenarii = new Scenario[1];
99 scenarii[0] = (Scenario) aStep.getOwner();
101 scenarii = aStep.getOwnerStudy().getScenarii();
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
109 KnowledgeElement kelm = j.next();
112 scene.updateMyIndex(lucin);
114 } catch (Exception error) {
115 // logger.error("Unable to re-index Knowledge Elements, reason:", error);
119 public boolean removeSimulationContext (Step aStep, SimulationContext context) {
120 // ------------------------------------------------------------------
121 SimulationContext torem = aStep.getSimulationContext(context.getIndex());
122 Session session = Database.getSession();
124 if (torem == null) return false;
125 if (!aStep.getOwner().remove(torem)) return false;
127 aStep.getContex().remove(torem);
128 session.update(aStep.getOwner());
129 if (torem.isShared()) {
130 getSimulationContextService().release(torem);
131 session.update(torem);
133 session.delete(torem);
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);
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");
147 // Identification and save
148 newdoc.buildReferenceFrom(aStep.getOwnerStudy());
149 Database.getSession().save(newdoc);
151 return new Publication(newdoc, aStep.getOwner());
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;
159 Document slot = getDocumentService().selectDocument(refid, new Revision().toString());
160 if ( slot == null ) return null;
161 if (!slot.isUndefined()) return null; // Should not happen
163 getDocumentService().initialize(slot, dprop.setOwner(aStep.getOwnerStudy()));
164 return new Publication(slot, aStep.getOwner());
167 public Publication versionDocument (Step aStep, Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
168 // -----------------------------------------------------
169 return versionDocument(aStep, base, new Document.Properties());
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));
177 public Publication versionDocument (Step aStep, Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
178 // --------------------------------------------------------------------------------
179 Document previous = base.value();
181 dprop.setDocument(previous); // Initializes the Step property
182 if (dprop.getStep().getNumber() != aStep.getNumber()) throw new MismatchException();
184 if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
185 String summary = dprop.getDescription();
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);
194 if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
195 else newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
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);
206 return new Publication(newdoc, aStep.getOwner());
212 public IndexService getIndexService() {
213 return _indexService;
216 public void setIndexService(IndexService indexService) {
217 _indexService = indexService;
221 * Get the documentService.
222 * @return the documentService
224 public DocumentService getDocumentService() {
225 return _documentService;
229 * Set the documentService.
230 * @param documentService the documentService to set
232 public void setDocumentService(DocumentService documentService) {
233 _documentService = documentService;
237 * Get the simulationContextService.
238 * @return the simulationContextService
240 public SimulationContextService getSimulationContextService() {
241 return _simulationContextService;
245 * Set the simulationContextService.
246 * @param simulationContextService the simulationContextService to set
248 public void setSimulationContextService(
249 SimulationContextService simulationContextService) {
250 _simulationContextService = simulationContextService;