Salome HOME
Copyrights update 2015.
[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-2015
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.LinkedList;
14 import java.util.List;
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.InvalidPropertyException;
22 import org.splat.kernel.MissedPropertyException;
23 import org.splat.kernel.MultiplyDefinedException;
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 scenarios.
54          */
55         private final List<Scenario> scenarii = new LinkedList<Scenario>();
56         /**
57          * Persistent study version property.
58          */
59         private String version;
60         /**
61          * Persistent history property. It is a number of studies versioning this one, if any.
62          */
63         private int history;
64         
65         /**
66          * Persistent property. Flag: marked the given study as reference or not.
67          */
68         private int markreference;
69
70         // Transient fields
71         /**
72          * Transient list of contributors.
73          */
74         private transient final List<User> contributor = new Vector<User>();
75         /**
76          * Transient map of document types to validation cycles.
77          */
78         private transient final Map<String, ValidationCycle> validactor = new HashMap<String, ValidationCycle>();
79         /**
80          * Transient set of all actors of the study, i.d. contributors and validation cycles participants.
81          */
82         private transient final Set<User> actor = new HashSet<User>();
83
84         // ==============================================================================================================================
85         // Construction
86         // ==============================================================================================================================
87
88         /**
89          * Fields initialization class.
90          */
91         public static class Properties extends Persistent.Properties {
92                 private String sid = null; // Search criterion only
93                 private String title = null;
94                 private String summary = null;
95                 private User manager = null;
96                 private User actor = null; // Search criterion only
97                 private Visibility visibility = null; // Search criterion only
98                 private ProgressState state = null; // Search criterion only
99                 private Date date = null;
100                 private List<SimulationContext> context = new Vector<SimulationContext>(); // Search criterion only
101
102                 // - Public services
103
104                 @Override
105                 public void clear() {
106                         super.clear();
107                         sid = null;
108                         title = null;
109                         summary = null;
110                         manager = null;
111                         actor = null;
112                         visibility = null;
113                         state = null;
114                         date = null;
115                         context = new Vector<SimulationContext>(); // as clear() may generate side effects
116                 }
117
118                 public Properties copy() {
119                         Properties copy = new Properties();
120                         copy.sid = this.sid;
121                         copy.title = this.title;
122                         copy.summary = this.summary;
123                         copy.manager = this.manager;
124                         copy.actor = this.actor;
125                         copy.visibility = this.visibility;
126                         copy.state = this.state;
127                         copy.date = this.date;
128                         copy.context = this.context;
129                         return copy;
130                 }
131
132                 // - Protected services
133
134                 public User getActor() {
135                         return actor;
136                 }
137
138                 public User getManager() {
139                         return manager;
140                 }
141
142                 public ProgressState getProgressState() {
143                         return state;
144                 }
145
146                 public String getReference() {
147                         return sid;
148                 }
149
150                 public List<SimulationContext> getSimulationContexts() {
151                         return context;
152                 }
153
154                 public String getTitle() {
155                         return title;
156                 }
157
158                 public String getSummary() {
159                         return summary;
160                 }
161
162                 public Visibility getVisibility() {
163                         return visibility;
164                 }
165
166                 // - Property setters
167
168                 // For building a search query
169                 public Properties setActor(final User actor) {
170                         this.actor = actor;
171                         return this;
172                 }
173
174                 public Properties setDate(final Date date) {
175                         this.date = date;
176                         return this;
177                 }
178
179                 public Properties setDescription(final String summary) {
180                         if (summary.length() > 0) {
181                                 this.summary = summary;
182                         }
183                         return this;
184                 }
185
186                 public Properties setManager(final User user) {
187                         this.manager = user;
188                         return this;
189                 }
190
191                 // For building a search query
192                 public Properties setReference(final String sid)
193                                 throws InvalidPropertyException {
194                         if (sid.length() == 0) {
195                                 throw new InvalidPropertyException("reference");
196                         }
197                         this.sid = sid;
198                         return this;
199                 }
200
201                 // For building a search query
202                 public Properties setSimulationContexts(
203                                 final List<SimulationContext> context) {
204                         this.context = context;
205                         return this;
206                 }
207
208                 // For building a search query
209                 public Properties setState(final ProgressState state) {
210                         this.state = state;
211                         return this;
212                 }
213
214                 public Properties setTitle(final String title)
215                                 throws InvalidPropertyException {
216                         if (title.length() == 0) {
217                                 throw new InvalidPropertyException("title");
218                         }
219                         this.title = title;
220                         return this;
221                 }
222
223                 // For building a search query
224                 public Properties setVisibility(final Visibility area) {
225                         this.visibility = area;
226                         return this;
227                 }
228
229                 // - Global validity check
230
231                 @Override
232                 public void checkValidity() throws MissedPropertyException,
233                                 InvalidPropertyException, MultiplyDefinedException {
234                         if (title == null) {
235                                 throw new MissedPropertyException("title");
236                         }
237                         if (manager == null) {
238                                 throw new MissedPropertyException("manager");
239                         }
240                 }
241         }
242
243         /**
244          * Database fetch constructor.
245          */
246         protected Study() {
247                 super();
248                 contributor.clear();
249                 validactor.clear();
250                 actor.clear();
251         }
252
253         /**
254          * Constructor from properties.
255          * 
256          * @param sprop
257          *            study properties
258          * @throws MissedPropertyException
259          *             if some mandatory property is missed
260          * @throws InvalidPropertyException
261          *             if some property has invalid value
262          * @throws MultiplyDefinedException
263          *             if some property is defined several times
264          */
265         public Study(final Properties sprop) throws MissedPropertyException,
266                         InvalidPropertyException, MultiplyDefinedException {
267                 super(sprop); // Throws one of the above exception if not valid
268                 sid = sprop.sid; // Reset after save
269                 title = sprop.title; // Inherited attribute
270                 manager = sprop.manager;
271                 docount = 0;
272                 history = 0;
273                 // RKV scenarii = new LinkedList<Scenario>();
274                 visibility = Visibility.PRIVATE;
275                 state = ProgressState.inWORK;
276                 markreference = 0;
277
278                 credate = sprop.date; // Inherited attribute
279                 if (credate == null) {
280                         Calendar current = Calendar.getInstance();
281                         credate = current.getTime(); // Today
282                 }
283                 lasdate = credate; // Inherited attribute
284                 version = new Revision().incrementAs(state).toString();
285
286                 if (sprop.summary != null) {
287                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
288                 }
289
290                 contributor.clear();
291                 validactor.clear();
292                 actor.clear();
293         }
294
295         public ProgressState getProgressState() {
296                 return state;
297         }
298
299         /**
300          * Returns the global unique reference of this study. The study reference is common to all versions of the study (versioning a study
301          * does not change its reference). The form of this study reference is defined in the configuration of the application server - see the
302          * SOM XML customization file.
303          */
304         public String getReference() {
305                 return sid;
306         }
307
308         public void setReference(final String aReference) {
309                 sid = aReference;
310         }
311
312         /**
313          * Get persistent list of study scenarios as an array.
314          * 
315          * @return array of scenarios
316          */
317         public Scenario[] getScenarii() {
318                 return scenarii.toArray(new Scenario[scenarii.size()]);
319         }
320
321         /**
322          * Get persistent list of study scenarios.
323          * 
324          * @return the list of scenarios
325          */
326         public List<Scenario> getScenariiList() {
327                 return scenarii;
328         }
329
330         public String getVersion() {
331                 return version;
332         }
333
334         public Visibility getVisibility() {
335                 return visibility;
336         }
337
338         /**
339          * Checks whether this study is in the Public or the Reference area of the repository.
340          * 
341          * @return true if the study is public.
342          * @see #moveToPublic()
343          * @see #moveToReference()
344          */
345         public boolean isPublic() {
346                 return (visibility != Visibility.PRIVATE);
347         }
348
349         public boolean isVersioned() {
350                 return (history > 0);
351         }
352
353         /**
354          * Check if the document is shared by scenarios of the study.
355          * 
356          * @param doc
357          *            the document to check
358          * @return true if the document is published in more then one scenario of the study
359          */
360         public boolean shares(final Document doc) {
361                 Scenario[] scene = this.getScenarii(); // If shared from within the study, the document is shared by the scenarios
362                 int counter = 0;
363                 boolean res = false;
364
365                 for (int i = 0; i < scene.length; i++) {
366                         if (scene[i].publishes(doc)) {
367                                 if (counter == 1) {
368                                         res = true;
369                                         break;
370                                 }
371                                 counter += 1;
372                         }
373                 }
374                 return res;
375         }
376
377         /**
378          * Get the last numerical index of study documents.
379          * 
380          * @return the last value of the index part of study documents reference
381          */
382         public int getLastLocalIndex() {
383                 return docount;
384         }
385
386         /**
387          * Set visibility of the study.
388          * 
389          * @param aVisibility
390          *            a study visibility to set
391          */
392         public void setVisibility(final Visibility aVisibility) {
393                 visibility = aVisibility;
394         }
395
396         /**
397          * Set the progress state for the study.
398          * 
399          * @param aState
400          *            a study progress state to set
401          */
402         public void setProgressState(final ProgressState aState) {
403                 state = aState;
404         }
405
406         /**
407          * Set the study version.
408          * 
409          * @param aVersion
410          *            the version
411          */
412         public void setVersion(final String aVersion) {
413                 version = aVersion;
414         }
415
416         /**
417          * Set total number of documents of this study, including versions. It is stored in the persistent property docount.
418          * 
419          * @param anIndex
420          *            a number of study documents
421          */
422         public void setLastLocalIndex(final int anIndex) {
423                 docount = anIndex;
424         }
425
426         /**
427          * Get the transient map of document types to validation cycles.
428          * 
429          * @return map of validation cycles
430          */
431         public Map<String, ValidationCycle> getValidationCycles() {
432                 return validactor;
433         }
434
435         /**
436          * Get the transient list of study contributors.
437          * 
438          * @return the List of User objects
439          */
440         public List<User> getContributor() {
441                 return contributor;
442         }
443
444         /**
445          * Get the transient set of all study actors (contributors and validation cycles participants).
446          * 
447          * @return the set of User objects
448          */
449         public Set<User> getActor() {
450                 return actor;
451         }
452
453         /**
454          * {@inheritDoc}
455          * 
456          * @see org.splat.dal.bo.som.ProjectElement#getOwnerStudy()
457          */
458         @Override
459         public Study getOwnerStudy() {
460                 return this;
461         }
462         
463         /**
464          * Get the markreference.
465          * @return the markreference
466          */
467         public int getMarkreference() {
468                 return markreference;
469         }
470
471         /**
472          * Set the markreference.
473          * @param markreference the markreference to set
474          */
475         public void setMarkreference(final int markreference) {
476                 this.markreference = markreference;
477         }
478         
479         /** 
480          * {@inheritDoc}
481          * @see org.splat.dal.bo.som.ProjectElement#evict()
482          */
483         @Override
484         public void evict() {
485                 super.evict();
486                 // Evict all attributes of the persistent object
487                 Set<Scenario> tmpSet = new HashSet<Scenario>();
488                 tmpSet.addAll(scenarii);
489                 scenarii.clear();
490                 // Evict publications
491                 for (Scenario rel : tmpSet) {
492                         if (rel.isSaved()) { // to avoid recursive evict
493                                 rel.evict();
494                         }
495                         scenarii.add(rel);
496                 }
497         }
498 }