Salome HOME
Copyrights update 2015.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / Step.java
1 package org.splat.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012-2015
7  */
8
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.Iterator;
12 import java.util.List;
13
14 import org.splat.dal.bo.kernel.User;
15 import org.splat.dal.bo.som.Document;
16 import org.splat.dal.bo.som.DocumentType;
17 import org.splat.dal.bo.som.KnowledgeElement;
18 import org.splat.dal.bo.som.ProgressState;
19 import org.splat.dal.bo.som.ProjectElement;
20 import org.splat.dal.bo.som.Publication;
21 import org.splat.dal.bo.som.Scenario;
22 import org.splat.dal.bo.som.SimulationContext;
23 import org.splat.dal.bo.som.SimulationContextType;
24 import org.splat.dal.bo.som.Study;
25 import org.splat.service.technical.ProjectSettingsService;
26
27 /**
28  * The activity (step) instance of a project element.
29  */
30 public class Step {
31
32         /**
33          * The study workflow step presented by this step instance.
34          */
35         private transient final ProjectSettingsService.Step _step;
36         /**
37          * The parent project element containing this step.
38          */
39         private transient final ProjectElement _owner;
40         /**
41          * The list of applied simulation contexts.
42          */
43         private transient final List<SimulationContext> _contex;
44         /**
45          * The list of documents publications of this step instance.
46          */
47         private transient final List<Publication> _docums;
48         /**
49          * Actor involved in operations on published documents and requiring a time-stamp.
50          */
51         private User _actor;
52
53         // =========================================================================
54         // Constructor
55         // =========================================================================
56
57         /**
58          * The constructor of the study step instance.
59          * 
60          * @param step
61          *            the study workflow step to instantiate
62          * @param owner
63          *            the parent project element
64          */
65         public Step(final ProjectSettingsService.Step step,
66                         final ProjectElement owner) {
67                 this._step = step;
68                 this._owner = owner;
69                 this._contex = new ArrayList<SimulationContext>();
70                 this._docums = new ArrayList<Publication>();
71
72                 // Filtering of Simulation contexts, if exist
73                 for (Iterator<SimulationContext> i = owner.SimulationContextIterator(); i
74                                 .hasNext();) {
75                         SimulationContext adoc = i.next();
76                         if (adoc.isInto(this)) {
77                                 this._contex.add(adoc);
78                         }
79                 }
80                 // Filtering of Documents, if exist
81                 for (Iterator<Publication> i = owner.PublicationIterator(); i.hasNext();) {
82                         Publication mydoc = i.next();
83                         if (mydoc.value().isInto(this)) {
84                                 mydoc.setStep(this); // RKV
85                                 this._docums.add(mydoc);
86                         }
87                 }
88         }
89
90         // =========================================================================
91         // Public member functions
92         // =========================================================================
93
94         public User getActor() {
95                 return _actor;
96         }
97
98         public List<Publication> getAllDocuments() {
99                 return Collections.unmodifiableList(_docums);
100         }
101
102         /**
103          * Get the persistent collection of step documents.
104          * 
105          * @return the list of documents
106          */
107         public List<Publication> getDocuments() {
108                 return _docums;
109         }
110
111         public List<SimulationContext> getAllSimulationContexts() {
112                 return Collections.unmodifiableList(_contex);
113         }
114
115         /**
116          * Get document publication or null if it is not published in this step.
117          * 
118          * @param docId
119          *            document id
120          * @return document publication or null if not published in this step
121          */
122         public Publication getDocument(final long docId) {
123                 Publication found = null;
124                 for (Publication pub : _docums) {
125                         // In a given study step,
126                         if (pub.value().getIndex() == docId) {
127                                 found = pub; // there is only one publication of a given document
128                                 break;
129                         }
130                 }
131                 return found;
132         }
133
134         /**
135          * Get step number.
136          * 
137          * @return step number
138          */
139         public int getNumber() {
140                 return _step.getNumber();
141         }
142
143         /**
144          * Get step's parent project element.
145          * 
146          * @return parent project element
147          */
148         public ProjectElement getOwner() {
149                 return _owner; // May be a Study or a Scenario
150         }
151
152         /**
153          * Get parent study.
154          * 
155          * @return the parent study
156          */
157         public Study getOwnerStudy() {
158                 return _owner.getOwnerStudy();
159         }
160
161         /**
162          * Get data path for this step.
163          * 
164          * @return the data path
165          */
166         public String getPath() {
167                 return _step.getPath();
168         }
169
170         /**
171          * Get all documents publications which are results of this step according to documents types.
172          * 
173          * @return list of the step result documents publications
174          */
175         public List<Publication> getResultDocuments() {
176                 List<Publication> result = new ArrayList<Publication>();
177
178                 if (!_docums.isEmpty()) {
179                         for (Publication content : _docums) {
180                                 DocumentType type = content.value().getType();
181                                 if (type.isResultOf(this.getStep())) {
182                                         result.add(content);
183                                 }
184                         }
185                 }
186                 return result;
187         }
188
189         /**
190          * Get the abstract workflow step of this step instance.
191          * 
192          * @return the abstract workflow step
193          */
194         public ProjectSettingsService.Step getStep() {
195                 return _step;
196         }
197
198         /**
199          * Get an applied simulation context by its id or null.
200          * 
201          * @param contextId
202          *            the simulation context id
203          * @return simulation context or null if it is not applied to this step
204          */
205         public SimulationContext getSimulationContext(final long contextId) {
206                 SimulationContext found = null;
207                 for (Iterator<SimulationContext> i = _owner.SimulationContextIterator(); i
208                                 .hasNext();) {
209                         SimulationContext myctex = i.next();
210                         if (myctex.getIndex() == contextId) {
211                                 found = myctex;
212                                 break;
213                         }
214                 }
215                 return found;
216         }
217
218         /**
219          * Get applied simulation contexts of the given type.
220          * 
221          * @param type
222          *            the simulation context type
223          * @return the list of applied simulation contexts
224          */
225         public List<SimulationContext> getSimulationContext(
226                         final SimulationContextType type) {
227                 List<SimulationContext> result = new ArrayList<SimulationContext>();
228
229                 for (Iterator<SimulationContext> i = _owner.SimulationContextIterator(); i
230                                 .hasNext();) {
231                         SimulationContext myctex = i.next();
232                         if (myctex.getType().equals(type)) {
233                                 result.add(myctex);
234                         }
235                 }
236                 return result;
237         }
238
239         /**
240          * Check if the step is started, i.e. it contains a knowledge if applicable or at least one document.
241          * 
242          * @return true if the step is started, otherwise false
243          */
244         public boolean isStarted() {
245                 boolean res = _step.mayContain(KnowledgeElement.class);
246                 if (res) {
247                         List<KnowledgeElement> kelm = ((Scenario) _owner)
248                                         .getAllKnowledgeElements();
249                         res = (!kelm.isEmpty() || !_docums.isEmpty());
250                 } else {
251                         res = (!_docums.isEmpty());
252                 }
253                 return res;
254         }
255
256         /**
257          * Check if the step is finished, i.e. all knowledges are approved if any,<BR>
258          * there is at least one non external result document and all non external <BR>
259          * result documents are approved.
260          * 
261          * @return true if the step is finished, otherwise false
262          */
263         public boolean isFinished() {
264                 boolean result = false;
265                 if (_step.mayContain(KnowledgeElement.class)) {
266                         // Check if all existing knowledges are approved
267                         List<KnowledgeElement> kelm = ((Scenario) _owner)
268                                         .getAllKnowledgeElements();
269                         if (!kelm.isEmpty()) {
270                                 result = true;
271                                 for (KnowledgeElement content : kelm) {
272                                         if (content.getProgressState() != ProgressState.APPROVED) {
273                                                 result = false;
274                                                 break;
275                                         }
276                                 }
277                         }
278                 } else {
279                         // Check if all result documents are approved
280                         for (Publication pub : _docums) {
281                                 Document content = pub.value();
282                                 DocumentType type = content.getType();
283                                 if (type.isResultOf(this.getStep())
284                                                 && (content.getProgressState() != ProgressState.EXTERN)) {
285                                         // There is at least 1 non external result document
286                                         result = (content.getProgressState() == ProgressState.APPROVED);
287                                         if (!result) {
288                                                 break; // not approved result is found
289                                         }
290                                 }
291                         }
292                 }
293                 return result;
294         }
295
296         /**
297          * Check if data of the given type are applicable to this step.
298          * 
299          * @param type
300          *            the data type to check
301          * @return true if the type is applicable to the step, otherwise false
302          */
303         @SuppressWarnings("unchecked")
304         public boolean mayContain(final Class type) {
305                 return _step.mayContain(type);
306         }
307
308         /**
309          * Set an actor involved in operations on published documents and requiring a time-stamp.
310          * 
311          * @param user
312          *            the actor to set
313          */
314         public void setActor(final User user) {
315                 _actor = user;
316         }
317
318         /**
319          * Get the list of simulation contexts applied to this step instance.
320          * 
321          * @return the list of applied simulation contexts
322          */
323         public List<SimulationContext> getContex() {
324                 return _contex;
325         }
326
327 }