Salome HOME
ebac98cabfad70920e8b35b7cf60f34274388c0a
[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.bo.som.Document.Properties;
34 import org.splat.dal.dao.som.Database;
35 import org.splat.kernel.InvalidPropertyException;
36 import org.splat.kernel.MismatchException;
37 import org.splat.kernel.MissedPropertyException;
38 import org.splat.kernel.MultiplyDefinedException;
39 import org.splat.kernel.NotApplicableException;
40 import org.splat.service.technical.IndexServiceImpl;
41 import org.splat.service.technical.ProjectSettingsService;
42
43
44 public class Step {
45         
46         private ProjectSettingsService.Step    step;
47     private ProjectElement          owner;
48     private List<SimulationContext> contex;
49         private List<Publication>       docums;
50     private User                    actor;     // Actor involved in operations on published documents and requiring a time-stamp
51
52 //  ==============================================================================================================================
53 //  Constructor
54 //  ==============================================================================================================================
55
56     public Step (ProjectSettingsService.Step step, ProjectElement owner) {
57 //  ----------------------------------------------------------------
58       this.step   = step;
59       this.owner  = owner;
60       this.contex = new Vector<SimulationContext>();
61       this.docums = new Vector<Publication>();
62       this.actor  = null;
63
64 //  Filtering of Simulation contexts, if exist
65       for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
66         SimulationContext adoc = i.next();
67         if (!adoc.isInto(this)) continue;
68         this.contex.add(adoc);
69       }
70 //  Filtering of Documents, if exist
71       for (Iterator<Publication> i=owner.PublicationIterator(); i.hasNext();) {
72         Publication mydoc = i.next();
73         if (!mydoc.value().isInto(this)) continue;
74         this.docums.add(mydoc);
75       }
76     }
77
78 //  ==============================================================================================================================
79 //  Public member functions
80 //  ==============================================================================================================================
81
82     public User getActor () {
83 //  -----------------------
84       return actor;
85     }
86
87     public List<Publication> getAllDocuments () {
88 //  -------------------------------------------
89       return  Collections.unmodifiableList(docums);
90     }
91     
92     public List<SimulationContext> getAllSimulationContexts () {
93 //  ----------------------------------------------------------
94       return  Collections.unmodifiableList(contex);
95     }
96
97     public Publication getDocument (int index) {
98 //  ------------------------------------------
99       for (Iterator<Publication> i=docums.iterator(); i.hasNext();) {
100         Publication found = i.next();                          // In a given study step,
101         if (found.value().getIndex() == index) return found;   // there is only one publication of a given document
102       }
103       return null;
104     }
105     
106     public int getNumber () {
107 //  -----------------------
108       return step.getNumber();
109     }
110
111     public ProjectElement getOwner () {
112 //  ---------------------------------
113       return owner;   // May be a Study or a Scenario
114     }
115
116     public Study getOwnerStudy () {
117 //  -----------------------------
118       if (owner instanceof Study) return  (Study)owner;
119       else                        return ((Scenario)owner).getOwnerStudy();
120     }
121
122     public String getPath () {
123 //  ------------------------
124       return step.getPath();
125     }
126
127     public List<Publication> getResultDocuments () {
128 //  ----------------------------------------------
129       List<Publication> result = new Vector<Publication>();
130       
131       if (!docums.isEmpty()) for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
132         Publication  content = i.next();
133         DocumentType type    = content.value().getType();
134         if (!type.isResultOf(this.getStep())) continue;
135         result.add(content);
136           }
137       return result;
138     }
139
140     public ProjectSettingsService.Step getStep () {
141 //  --------------------------------------
142       return step;
143     }
144
145     public SimulationContext getSimulationContext (int index) {
146 //  ---------------------------------------------------------
147       for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
148         SimulationContext myctex = i.next();
149         if (myctex.getIndex() == index) return myctex;
150       }
151       return null;
152     }
153
154     public List<SimulationContext> getSimulationContext (SimulationContextType type) {
155 //  --------------------------------------------------------------------------------
156       Vector<SimulationContext> result = new Vector<SimulationContext>();
157
158       for (Iterator<SimulationContext> i=owner.SimulationContextIterator(); i.hasNext();) {
159         SimulationContext myctex = i.next();
160         if (myctex.getType().equals(type)) result.add(myctex);
161       }
162       return result;
163     }
164
165     public List<DocumentType> getValidDocumentTypes () {
166 //  --------------------------------------------------
167       return Document.selectTypesOf(step);
168     }
169
170     public boolean isStarted () {
171 //  ---------------------------
172       if (!step.mayContain(KnowledgeElement.class)) return !docums.isEmpty();
173
174       List<KnowledgeElement> kelm = ((Scenario)owner).getAllKnowledgeElements();
175       if (kelm.isEmpty() && docums.isEmpty()) return false;
176       return true;
177     }
178
179     public boolean isFinished () {
180 //  ----------------------------
181       if (!step.mayContain(KnowledgeElement.class)) {   // Check if all result documents are approved
182         if (docums.isEmpty()) return false;
183         boolean result = false;
184         for (Iterator<Publication> i=docums.iterator(); i.hasNext(); ) {
185           Document     content = i.next().value();
186           DocumentType type    = content.getType();
187           if (!type.isResultOf(this.getStep())) continue;
188           if (content.getProgressState() == ProgressState.EXTERN) continue;
189           result = true;          // There is at least 1 non external result document
190           if (content.getProgressState() != ProgressState.APPROVED) return false;
191         }
192         return result;
193       }
194       else {                                            // Check if all existing knowledges are approved
195         List<KnowledgeElement> kelm  = ((Scenario)owner).getAllKnowledgeElements();
196         if (kelm.isEmpty()) return false;
197         for (Iterator<KnowledgeElement> i=kelm.iterator(); i.hasNext(); ) {
198           KnowledgeElement  content = i.next();
199           if (content.getProgressState() != ProgressState.APPROVED) return false;
200         }
201         return true;
202       }
203     }
204
205     public boolean mayContain (@SuppressWarnings("rawtypes") Class type) {
206 //  --------------------------------------------------------------------
207       return step.mayContain(type);
208     }
209
210     public boolean removeDocument (Publication doctag) {
211 //  --------------------------------------------------
212       Document     value   = doctag.value();
213       Publication  torem   = getDocument(value.getIndex());
214       Session      session = Database.getSession();
215
216       if (torem == null)        return false;
217
218       this.remove(torem);
219       session.update(owner);
220       if (!value.isPublished() && !value.isVersioned()) {         // The referenced document is no more used
221         Set<Relation>  links = value.getAllRelations();
222         List<Document> using = new Vector<Document>();
223         for (Iterator<Relation> i=links.iterator(); i.hasNext(); ) {
224           Relation link = i.next();
225           if (link.getClass().equals(ConvertsRelation.class)) {   // File conversion
226             session.delete(link.getTo());                         // The corresponding physical file is not removed from the vault
227           } else
228           if (link.getClass().equals(UsesRelation.class)) {       // Document dependency
229                 using.add((Document)link.getTo());
230           }
231         }
232         for (Iterator<Document> i=using.iterator(); i.hasNext(); ) {
233           i.next().removeRelation(UsedByRelation.class, value);
234         }
235         session.delete(value);                              // The corresponding physical file is not removed from the vault
236       }
237       return true;
238     }
239
240     public void setActor (User user) {
241 //  --------------------------------
242       actor = user;
243     }
244 //  ==============================================================================================================================
245 //  Protected member functions
246 //  ==============================================================================================================================
247
248     public boolean add (Publication newdoc) {
249 //  ------------------------------------------
250       if (!owner.add(newdoc)) return false;   // Updates the study in memory
251       docums.add(0, newdoc);                  // Updates this step
252       newdoc.value().hold();                  // Increments the configuration tag count of document
253 //    If not yet saved, the Publication MUST NOT be saved here, although this creates a temporary inconsistent state into the
254 //    database (it will be saved later by cascading the update of owner scenario).    
255       return true;
256     }
257
258     public boolean remove (Publication oldoc) {
259 //  --------------------------------------------
260       if (!owner.remove(oldoc)) return false; // Updates the study in memory
261       docums.remove(oldoc);                   // Updates this step
262       oldoc.value().release();                // Decrements the configuration tag count of document
263 //    The publication becoming orphan, it should automatically be removed from the database when updating of owner scenario.
264       return true;
265     }
266
267     public List<SimulationContext> getContex() {
268                 return contex;
269         }
270
271 }