Salome HOME
1fd88f4b560c76a4bd8c021330a0306cc273f649
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / Study.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012
7  */
8
9 import java.util.Calendar;
10 import java.util.Date;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.LinkedList;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.Vector;
18
19 import org.splat.dal.bo.kernel.Persistent;
20 import org.splat.dal.bo.kernel.User;
21 import org.splat.kernel.MultiplyDefinedException;
22 import org.splat.kernel.InvalidPropertyException;
23 import org.splat.kernel.MissedPropertyException;
24 import org.splat.som.Revision;
25
26 /**
27  * Siman Study persistent object.
28  */
29 public class Study extends ProjectElement {
30
31         // Persistent fields
32         /**
33          * Persistent sid property. It is an external unique reference in a format conform to the configuration pattern.
34          */
35         private String sid;
36         /**
37          * Persistent docount property. It is a total number of documents of this study, including versions.
38          */
39         private int docount;
40         /**
41          * Persistent state property. It is a study progress state.
42          * 
43          * @see org.splat.dal.bo.som.ProgressState enumeration
44          */
45         private ProgressState state;
46         /**
47          * Persistent visibility property.
48          * 
49          * @see org.splat.dal.bo.som.Visibility enumeration
50          */
51         private Visibility visibility;
52         /**
53          * Persistent list of study scenarii.
54          */
55         private List<Scenario> scenarii;
56         private String version;
57         /**
58          * Persistent history property. It is a number of studies versioning this one, if any.
59          */
60         private int history;
61
62         // Transient fields
63         /**
64          * Transient list of contributors.
65          */
66         private transient List<User> contributor = new Vector<User>(); // Shortcut to contributors
67         /**
68          * Transient map of document types to validation cycles.
69          */
70         private transient Map<String, ValidationCycle> validactor = new HashMap<String, ValidationCycle>(); // Shortcut to validation cycles
71         /**
72          * Transient set of all actors of the study, i.d. contributors and validation cycles participants.
73          */
74         private transient Set<User> actor = new HashSet<User>(); // Summary of above actors
75
76         // ==============================================================================================================================
77         // Construction
78         // ==============================================================================================================================
79
80         /**
81          * Fields initialization class.
82          */
83         public static class Properties extends Persistent.Properties {
84                 // ------------------------------------------------------------
85                 private String sid = null; // Search criterion only
86                 private String title = null;
87                 private String summary = null;
88                 private User manager = null;
89                 private User actor = null; // Search criterion only
90                 private Visibility visibility = null; // Search criterion only
91                 private ProgressState state = null; // Search criterion only
92                 private Date date = null;
93                 private List<SimulationContext> context = new Vector<SimulationContext>(); // Search criterion only
94
95                 // - Public services
96
97                 public void clear() {
98                         super.clear();
99                         sid = null;
100                         title = null;
101                         summary = null;
102                         manager = null;
103                         actor = null;
104                         visibility = null;
105                         state = null;
106                         date = null;
107                         context = new Vector<SimulationContext>(); // as clear() may generate side effects
108                 }
109
110                 public Properties copy() {
111                         Properties copy = new Properties();
112                         copy.sid = this.sid;
113                         copy.title = this.title;
114                         copy.summary = this.summary;
115                         copy.manager = this.manager;
116                         copy.actor = this.actor;
117                         copy.visibility = this.visibility;
118                         copy.state = this.state;
119                         copy.date = this.date;
120                         copy.context = this.context;
121                         return copy;
122                 }
123
124                 // - Protected services
125
126                 public User getActor() {
127                         return actor;
128                 }
129
130                 public User getManager() {
131                         return manager;
132                 }
133
134                 public ProgressState getProgressState() {
135                         return state;
136                 }
137
138                 public String getReference() {
139                         return sid;
140                 }
141
142                 public List<SimulationContext> getSimulationContexts() {
143                         return context;
144                 }
145
146                 public String getTitle() {
147                         return title;
148                 }
149
150                 public String getSummary() {
151                         return summary;
152                 }
153
154                 public Visibility getVisibility() {
155                         return visibility;
156                 }
157
158                 // - Property setters
159
160                 // For building a search query
161                 public Properties setActor(User actor) {
162                         this.actor = actor;
163                         return this;
164                 }
165
166                 public Properties setDate(Date date) {
167                         this.date = date;
168                         return this;
169                 }
170
171                 public Properties setDescription(String summary) {
172                         if (summary.length() > 0)
173                                 this.summary = summary;
174                         return this;
175                 }
176
177                 public Properties setManager(User user) {
178                         this.manager = user;
179                         return this;
180                 }
181
182                 // For building a search query
183                 public Properties setReference(String sid)
184                                 throws InvalidPropertyException {
185                         if (sid.length() == 0)
186                                 throw new InvalidPropertyException("reference");
187                         this.sid = sid;
188                         return this;
189                 }
190
191                 // For building a search query
192                 public Properties setSimulationContexts(List<SimulationContext> context) {
193                         this.context = context;
194                         return this;
195                 }
196
197                 // For building a search query
198                 public Properties setState(ProgressState state) {
199                         this.state = state;
200                         return this;
201                 }
202
203                 public Properties setTitle(String title)
204                                 throws InvalidPropertyException {
205                         if (title.length() == 0)
206                                 throw new InvalidPropertyException("title");
207                         this.title = title;
208                         return this;
209                 }
210
211                 // For building a search query
212                 public Properties setVisibility(Visibility area) {
213                         this.visibility = area;
214                         return this;
215                 }
216
217                 // - Global validity check
218
219                 public void checkValidity() throws MissedPropertyException,
220                                 InvalidPropertyException, MultiplyDefinedException {
221                         if (title == null)
222                                 throw new MissedPropertyException("title");
223                         if (manager == null)
224                                 throw new MissedPropertyException("manager");
225                 }
226         }
227
228         // Database fetch constructor
229         protected Study() {
230                 contributor.clear();
231                 validactor.clear();
232                 actor.clear();
233         }
234
235         // Internal constructor
236         public Study(Properties sprop) throws MissedPropertyException,
237                         InvalidPropertyException, MultiplyDefinedException {
238                 // ----------------------------------
239                 super(sprop); // Throws one of the above exception if not valid
240                 sid = sprop.sid; // Reset after save
241                 title = sprop.title; // Inherited attribute
242                 manager = sprop.manager;
243                 docount = 0;
244                 history = 0;
245                 scenarii = new LinkedList<Scenario>();
246                 visibility = Visibility.PRIVATE;
247                 state = ProgressState.inWORK;
248
249                 credate = sprop.date; // Inherited attribute
250                 if (credate == null) {
251                         Calendar current = Calendar.getInstance();
252                         credate = current.getTime(); // Today
253                 }
254                 lasdate = credate; // Inherited attribute
255                 version = new Revision().incrementAs(state).toString();
256
257                 if (sprop.summary != null)
258                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
259
260                 contributor.clear();
261                 validactor.clear();
262                 actor.clear();
263         }
264
265         public ProgressState getProgressState() {
266                 return state;
267         }
268
269         /**
270          * Returns the global unique reference of this study. The study reference is common to all versions of the study (versioning a study
271          * does not change its reference). The form of this study reference is defined in the configuration of the application server - see the
272          * SOM XML customization file.
273          */
274         public String getReference() {
275                 return sid;
276         }
277
278         public void setReference(String aReference) {
279                 sid = aReference;
280         }
281
282         public Scenario[] getScenarii() {
283                 // --------------------------------
284                 return scenarii.toArray(new Scenario[scenarii.size()]);
285         }
286
287         public List<Scenario> getScenariiList() {
288                 // --------------------------------
289                 return scenarii;
290         }
291
292         public String getVersion() {
293                 // ---------------------------
294                 return version;
295         }
296
297         public Visibility getVisibility() {
298                 // ----------------------------------
299                 return visibility;
300         }
301
302         /**
303          * Checks whether this study is in the Public or the Reference area of the repository.
304          * 
305          * @return true if the study is public.
306          * @see #moveToPublic()
307          * @see #moveToReference()
308          */
309         public boolean isPublic() {
310                 // --------------------------
311                 return (visibility != Visibility.PRIVATE);
312         }
313
314         public boolean isVersioned() {
315                 // -----------------------------
316                 return (history > 0);
317         }
318
319         public boolean shares(Document doc) {
320                 // ------------------------------------
321                 Scenario[] scene = this.getScenarii(); // If shared from within the study, the document is shared by the scenarios
322                 int counter = 0;
323
324                 for (int i = 0; i < scene.length; i++) {
325                         if (!scene[i].publishes(doc))
326                                 continue;
327                         if (counter == 1)
328                                 return true;
329                         counter += 1;
330                 }
331                 return false;
332         }
333
334         public int getLastLocalIndex() {
335                 // ----------------------------------
336                 return docount;
337         }
338
339         /**
340          * @param aVisibility
341          *            a study visibility to set
342          */
343         public void setVisibility(Visibility aVisibility) {
344                 visibility = aVisibility;
345         }
346
347         /**
348          * @param aState
349          *            a study progress state to set
350          */
351         public void setProgressState(ProgressState aState) {
352                 state = aState;
353         }
354
355         /**
356          * @param aVersion
357          */
358         public void setVersion(String aVersion) {
359                 version = aVersion;
360         }
361
362         /**
363          * Set total number of documents of this study, including versions. It is stored in the persistent property docount.
364          * 
365          * @param anIndex
366          *            a number of study documents
367          */
368         public void setLastLocalIndex(int anIndex) {
369                 docount = anIndex;
370         }
371
372         /**
373          * Get the transient map of document types to validation cycles.
374          * 
375          * @return map of validation cycles
376          */
377         public Map<String, ValidationCycle> getValidationCycles() {
378                 return validactor;
379         }
380
381         /**
382          * Get the transient list of study contributors.
383          * 
384          * @return the List of User objects
385          */
386         public List<User> getContributor() {
387                 return contributor;
388         }
389
390         /**
391          * Get the transient set of all study actors (contributors and validation cycles participants).
392          * 
393          * @return the set of User objects
394          */
395         public Set<User> getActor() {
396                 return actor;
397         }
398 }