Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / Step.java
1 package org.splat.som;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Set;
13 import java.util.Vector;
14 import java.util.Iterator;
15
16 import org.hibernate.Session;
17 import org.splat.dal.bo.kernel.Relation;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.dal.bo.som.ConvertsRelation;
20 import org.splat.dal.bo.som.Document;
21 import org.splat.dal.bo.som.DocumentType;
22 import org.splat.dal.bo.som.KnowledgeElement;
23 import org.splat.dal.bo.som.ProgressState;
24 import org.splat.dal.bo.som.ProjectElement;
25 import org.splat.dal.bo.som.Publication;
26 import org.splat.dal.bo.som.Scenario;
27 import org.splat.dal.bo.som.SimulationContext;
28 import org.splat.dal.bo.som.SimulationContextType;
29 import org.splat.dal.bo.som.Study;
30 import org.splat.dal.bo.som.UsedByRelation;
31 import org.splat.dal.bo.som.UsesRelation;
32 import org.splat.dal.bo.som.VersionsRelation;
33 import org.splat.dal.dao.som.Database;
34 import org.splat.kernel.InvalidPropertyException;
35 import org.splat.kernel.MismatchException;
36 import org.splat.kernel.MissedPropertyException;
37 import org.splat.kernel.MultiplyDefinedException;
38 import org.splat.kernel.NotApplicableException;
39 import org.splat.service.technical.IndexServiceImpl;
40 import org.splat.service.technical.ProjectSettingsService;
41
42
43 public class Step {
44         
45         private ProjectSettingsService.Step    step;
46     private ProjectElement          owner;
47     private List<SimulationContext> contex;
48         private List<Publication>       docums;
49     private User                    actor;     // Actor involved in operations on published documents and requiring a time-stamp
50
51 //  ==============================================================================================================================
52 //  Constructor
53 //  ==============================================================================================================================
54
55     public Step (ProjectSettingsService.Step step, ProjectElement owner) {
56 //  ----------------------------------------------------------------
57       this.step   = step;
58       this.owner  = owner;
59       this.contex = new Vector<SimulationContext>();
60       this.docums = new Vector<Publication>();
61       this.actor  = null;
62
63 //  Filtering of Simulation contexts, if exist
64       for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
65         SimulationContext adoc = i.next();
66         if (!adoc.isInto(this)) continue;
67         this.contex.add(adoc);
68       }
69 //  Filtering of Documents, if exist
70       for (Iterator<Publication> i=owner.PublicationIterator(); i.hasNext();) {
71         Publication mydoc = i.next();
72         if (!mydoc.value().isInto(this)) continue;
73         this.docums.add(mydoc);
74       }
75     }
76
77 //  ==============================================================================================================================
78 //  Public member functions
79 //  ==============================================================================================================================
80
81     public Publication createDocument (Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException {
82 //  -------------------------------------------------------------
83       Document  newdoc = new Document(dprop.setOwner(owner).setStep(step));
84
85 //    Creation of the save directory      
86       File wdir = newdoc.getSaveDirectory();
87           if (!wdir.exists()) if (!wdir.mkdirs()) throw new IOException("Cannot create the repository vault directory");
88
89 //    Identification and save
90       newdoc.buildReferenceFrom(getOwnerStudy());
91       Database.getSession().save(newdoc);
92
93       return  new Publication(newdoc, owner);
94     }
95
96     public Publication assignDocument (Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, NotApplicableException {
97 //  -------------------------------------------------------------
98       String refid = dprop.getReference();
99       if    (refid == null)    return null;
100
101       Document  slot = Database.selectDocument(refid, new Revision().toString());
102       if ( slot == null )      return null;
103       if (!slot.isUndefined()) return null;     // Should not happen
104
105       slot.initialize(dprop.setOwner(getOwnerStudy()));
106       return  new Publication(slot, owner);
107     }
108
109     public Publication versionDocument (Publication base) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
110 //  -----------------------------------------------------
111       return versionDocument(base, new Document.Properties());
112     }
113
114     public Publication versionDocument (Publication base, String reason) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
115 //  --------------------------------------------------------------------
116       return versionDocument(base, new Document.Properties().setDescription(reason));
117     }
118
119     public Publication versionDocument (Publication base, Document.Properties dprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, IOException, MismatchException {
120 //  --------------------------------------------------------------------------------
121       Document previous = base.value();
122       
123       dprop.setDocument(previous);        // Initializes the Step property
124       if (dprop.getStep().getNumber() != this.step.getNumber()) throw new MismatchException();
125
126       if (dprop.getAuthor() == null) dprop.setAuthor(previous.getAuthor());
127       String    summary = dprop.getDescription();
128
129 //    Creation of the document
130       Document  newdoc = new Document(dprop.setOwner(owner).setStep(step));
131       newdoc.buildReferenceFrom(getOwner(), previous);
132       Database.getSession().save(newdoc);
133
134 //    Versioning
135       if (summary == null) newdoc.addRelation( new VersionsRelation(newdoc, previous) );
136       else                 newdoc.addRelation( new VersionsRelation(newdoc, previous, summary) );
137
138 //    Update of usedby relations, if exist
139       List<Relation> relist = previous.getRelations(UsedByRelation.class);
140       Study          scope  = getOwnerStudy();
141       for (Iterator<Relation> i=relist.iterator(); i.hasNext();) {
142         UsedByRelation relation  = (UsedByRelation)i.next();
143         Document       relatedoc = relation.getTo();
144         if (scope.shares(relatedoc)) relatedoc.addRelation( new UsesRelation(relatedoc, newdoc) );
145         else                         relation.moveTo(newdoc);
146       }
147       return  new Publication(newdoc, owner);
148     }
149
150     public User getActor () {
151 //  -----------------------
152       return actor;
153     }
154
155     public List<Publication> getAllDocuments () {
156 //  -------------------------------------------
157       return  Collections.unmodifiableList(docums);
158     }
159     
160     public List<SimulationContext> getAllSimulationContexts () {
161 //  ----------------------------------------------------------
162       return  Collections.unmodifiableList(contex);
163     }
164
165     public Publication getDocument (int index) {
166 //  ------------------------------------------
167       for (Iterator<Publication> i=docums.iterator(); i.hasNext();) {
168         Publication found = i.next();                          // In a given study step,
169         if (found.value().getIndex() == index) return found;   // there is only one publication of a given document
170       }
171       return null;
172     }
173     
174     public int getNumber () {
175 //  -----------------------
176       return step.getNumber();
177     }
178
179     public ProjectElement getOwner () {
180 //  ---------------------------------
181       return owner;   // May be a Study or a Scenario
182     }
183
184     public Study getOwnerStudy () {
185 //  -----------------------------
186       if (owner instanceof Study) return  (Study)owner;
187       else                        return ((Scenario)owner).getOwnerStudy();
188     }
189
190     public String getPath () {
191 //  ------------------------
192       return step.getPath();
193     }
194
195     public List<Publication> getResultDocuments () {
196 //  ----------------------------------------------
197       List<Publication> result = new Vector<Publication>();
198       
199       if (!docums.isEmpty()) for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
200         Publication  content = i.next();
201         DocumentType type    = content.value().getType();
202         if (!type.isResultOf(this.getStep())) continue;
203         result.add(content);
204           }
205       return result;
206     }
207
208     public ProjectSettingsService.Step getStep () {
209 //  --------------------------------------
210       return step;
211     }
212
213     public SimulationContext getSimulationContext (int index) {
214 //  ---------------------------------------------------------
215       for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
216         SimulationContext myctex = i.next();
217         if (myctex.getIndex() == index) return myctex;
218       }
219       return null;
220     }
221
222     public List<SimulationContext> getSimulationContext (SimulationContextType type) {
223 //  --------------------------------------------------------------------------------
224       Vector<SimulationContext> result = new Vector<SimulationContext>();
225
226       for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
227         SimulationContext myctex = i.next();
228         if (myctex.getType().equals(type)) result.add(myctex);
229       }
230       return result;
231     }
232
233     public List<DocumentType> getValidDocumentTypes () {
234 //  --------------------------------------------------
235       return Document.selectTypesOf(step);
236     }
237
238     public boolean isStarted () {
239 //  ---------------------------
240       if (!step.mayContain(KnowledgeElement.class)) return !docums.isEmpty();
241
242       List<KnowledgeElement> kelm = ((Scenario)owner).getAllKnowledgeElements();
243       if (kelm.isEmpty() && docums.isEmpty()) return false;
244       return true;
245     }
246
247     public boolean isFinished () {
248 //  ----------------------------
249       if (!step.mayContain(KnowledgeElement.class)) {   // Check if all result documents are approved
250         if (docums.isEmpty()) return false;
251         boolean result = false;
252         for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
253           Document     content = i.next().value();
254           DocumentType type    = content.getType();
255           if (!type.isResultOf(this.getStep())) continue;
256           if (content.getProgressState() == ProgressState.EXTERN) continue;
257           result = true;          // There is at least 1 non external result document
258           if (content.getProgressState() != ProgressState.APPROVED) return false;
259         }
260         return result;
261       }
262       else {                                            // Check if all existing knowledges are approved
263         List<KnowledgeElement> kelm  = ((Scenario)owner).getAllKnowledgeElements();
264         if (kelm.isEmpty()) return false;
265         for (Iterator<KnowledgeElement> i=kelm.iterator(); i.hasNext(); ) {
266           KnowledgeElement  content = i.next();
267           if (content.getProgressState() != ProgressState.APPROVED) return false;
268         }
269         return true;
270       }
271     }
272
273     public boolean mayContain (@SuppressWarnings("rawtypes") Class type) {
274 //  --------------------------------------------------------------------
275       return step.mayContain(type);
276     }
277
278     public boolean removeDocument (Publication doctag) {
279 //  --------------------------------------------------
280       Document     value   = doctag.value();
281       Publication  torem   = getDocument(value.getIndex());
282       Session      session = Database.getSession();
283
284       if (torem == null)        return false;
285
286       this.remove(torem);
287       session.update(owner);
288       if (!value.isPublished() && !value.isVersioned()) {         // The referenced document is no more used
289         Set<Relation>  links = value.getAllRelations();
290         List<Document> using = new Vector<Document>();
291         for (Iterator<Relation> i=links.iterator(); i.hasNext(); ) {
292           Relation link = i.next();
293           if (link.getClass().equals(ConvertsRelation.class)) {   // File conversion
294             session.delete(link.getTo());                         // The corresponding physical file is not removed from the vault
295           } else
296           if (link.getClass().equals(UsesRelation.class)) {       // Document dependency
297                 using.add((Document)link.getTo());
298           }
299         }
300         for (Iterator<Document> i=using.iterator(); i.hasNext(); ) {
301           i.next().removeRelation(UsedByRelation.class, value);
302         }
303         session.delete(value);                              // The corresponding physical file is not removed from the vault
304       }
305       return true;
306     }
307
308     public void setActor (User user) {
309 //  --------------------------------
310       actor = user;
311     }
312 //  ==============================================================================================================================
313 //  Protected member functions
314 //  ==============================================================================================================================
315
316     public boolean add (Publication newdoc) {
317 //  ------------------------------------------
318       if (!owner.add(newdoc)) return false;   // Updates the study in memory
319       docums.add(0, newdoc);                  // Updates this step
320       newdoc.value().hold();                  // Increments the configuration tag count of document
321 //    If not yet saved, the Publication MUST NOT be saved here, although this creates a temporary inconsistent state into the
322 //    database (it will be saved later by cascading the update of owner scenario).    
323       return true;
324     }
325
326     public boolean remove (Publication oldoc) {
327 //  --------------------------------------------
328       if (!owner.remove(oldoc)) return false; // Updates the study in memory
329       docums.remove(oldoc);                   // Updates this step
330       oldoc.value().release();                // Decrements the configuration tag count of document
331 //    The publication becoming orphan, it should automatically be removed from the database when updating of owner scenario.
332       return true;
333     }
334
335     public List<SimulationContext> getContex() {
336                 return contex;
337         }
338
339 }