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;
43 public class StepServiceImpl implements StepService {
45 private IndexService _indexService;
46 private DocumentService _documentService;
48 public SimulationContext addSimulationContext(Step aStep,
49 SimulationContext.Properties dprop) throws MissedPropertyException,
50 InvalidPropertyException, MultiplyDefinedException,
52 // ----------------------------------------------------------------------------------
53 SimulationContext context = new SimulationContext(dprop.setStep(aStep
55 return addSimulationContext(aStep, context);
58 public SimulationContext addSimulationContext(Step aStep,
59 SimulationContext context) {
60 // -------------------------------------------------------------------------
61 context.hold(); // Increments the reference count of simulation context
62 if (aStep.getOwner().isSaved())
64 Session session = Database.getSession();
65 IndexService lucin = getIndexService();
67 if (!context.isSaved())
68 session.save(context);
69 aStep.getOwner().add(context);
70 aStep.getContex().add(context); // The context is also referenced from this (transient) Step
71 session.update(aStep.getOwner());
72 updateKnowledgeElementsIndex(aStep, lucin);
73 } catch (Exception error) {
76 else { // Happens when copying a scenario
77 aStep.getOwner().add(context);
78 aStep.getContex().add(context); // The context is also referenced from this (transient) Step
79 // In case of owner scenario, the Knowledge Element index will be updated later, when saving the scenario
84 private void updateKnowledgeElementsIndex(Step aStep, IndexService lucin) {
85 // ------------------------------------------------------
87 if (aStep.getOwner() instanceof Scenario) {
88 scenarii = new Scenario[1];
89 scenarii[0] = (Scenario) aStep.getOwner();
91 scenarii = aStep.getOwnerStudy().getScenarii();
94 for (int i = 0; i < scenarii.length; i++) {
95 Scenario scene = scenarii[i];
96 List<KnowledgeElement> knelm = scene.getAllKnowledgeElements();
97 for (Iterator<KnowledgeElement> j = knelm.iterator(); j
99 KnowledgeElement kelm = j.next();
102 scene.updateMyIndex(lucin);
104 } catch (Exception error) {
105 // logger.error("Unable to re-index Knowledge Elements, reason:", error);
109 public boolean removeSimulationContext (Step aStep, SimulationContext context) {
110 // ------------------------------------------------------------------
111 SimulationContext torem = aStep.getSimulationContext(context.getIndex());
112 Session session = Database.getSession();
114 if (torem == null) return false;
115 if (!aStep.getOwner().remove(torem)) return false;
117 aStep.getContex().remove(torem);
118 session.update(aStep.getOwner());
119 if (torem.isShared()) {
121 session.update(torem);
123 session.delete(torem);
128 public Publication createDocument (Step aStep, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException {
129 // -------------------------------------------------------------
130 Document newdoc = new Document(dprop.setOwner(aStep.getOwner()).setStep(aStep.getStep()));
131 getDocumentService().generateDocumentId(newdoc, dprop);
133 // Creation of the save directory
134 File wdir = newdoc.getSaveDirectory();
135 if (!wdir.exists()) if (!wdir.mkdirs()) throw new IOException("Cannot create the repository vault directory");
137 // Identification and save
138 newdoc.buildReferenceFrom(aStep.getOwnerStudy());
139 Database.getSession().save(newdoc);
141 return new Publication(newdoc, aStep.getOwner());
144 public Publication assignDocument (Step aStep, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, NotApplicableException {
145 // -------------------------------------------------------------
146 String refid = dprop.getReference();
147 if (refid == null) return null;
149 Document slot = Database.selectDocument(refid, new Revision().toString());
150 if ( slot == null ) return null;
151 if (!slot.isUndefined()) return null; // Should not happen
153 getDocumentService().initialize(slot, dprop.setOwner(aStep.getOwnerStudy()));
154 return new Publication(slot, aStep.getOwner());
157 public Publication versionDocument (Step aStep, Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
158 // -----------------------------------------------------
159 return versionDocument(aStep, base, new Document.Properties());
162 public Publication versionDocument (Step aStep, Publication base, String reason) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
163 // --------------------------------------------------------------------
164 return versionDocument(aStep, base, new Document.Properties().setDescription(reason));
167 public Publication versionDocument (Step aStep, Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
168 // --------------------------------------------------------------------------------
169 Document previous = base.value();
171 dprop.setDocument(previous); // Initializes the Step property
172 if (dprop.getStep().getNumber() != aStep.getNumber()) throw new MismatchException();
174 if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
175 String summary = dprop.getDescription();
177 // Creation of the document
178 Document newdoc = new Document(dprop.setOwner(aStep.getOwner()).setStep(aStep.getStep()));
179 getDocumentService().generateDocumentId(newdoc, dprop);
180 newdoc.buildReferenceFrom(aStep.getOwner(), previous);
181 Database.getSession().save(newdoc);
184 if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
185 else newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
187 // Update of usedby relations, if exist
188 List<Relation> relist = previous.getRelations(UsedByRelation.class);
189 Study scope = aStep.getOwnerStudy();
190 for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
191 UsedByRelation relation = (UsedByRelation)i.next();
192 Document relatedoc = relation.getTo();
193 if (scope.shares(relatedoc)) relatedoc.addRelation( new UsesRelation(relatedoc, newdoc) );
194 else relation.moveTo(newdoc);
196 return new Publication(newdoc, aStep.getOwner());
202 public IndexService getIndexService() {
203 return _indexService;
206 public void setIndexService(IndexService indexService) {
207 _indexService = indexService;
211 * Get the documentService.
212 * @return the documentService
214 public DocumentService getDocumentService() {
215 return _documentService;
219 * Set the documentService.
220 * @param documentService the documentService to set
222 public void setDocumentService(DocumentService documentService) {
223 _documentService = documentService;