Salome HOME
Default document types mappings are moved from application settings (my.xml) to proje...
[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.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         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 final List<User> contributor = new Vector<User>();
67         /**
68          * Transient map of document types to validation cycles.
69          */
70         private transient final Map<String, ValidationCycle> validactor = new HashMap<String, ValidationCycle>(); 
71         /**
72          * Transient set of all actors of the study, i.d. contributors and validation cycles participants.
73          */
74         private transient final Set<User> actor = new HashSet<User>();
75
76         // ==============================================================================================================================
77         // Construction
78         // ==============================================================================================================================
79
80         /**
81          * Fields initialization class.
82          */
83         public static class Properties extends Persistent.Properties {
84                 private String sid = null; // Search criterion only
85                 private String title = null;
86                 private String summary = null;
87                 private User manager = null;
88                 private User actor = null; // Search criterion only
89                 private Visibility visibility = null; // Search criterion only
90                 private ProgressState state = null; // Search criterion only
91                 private Date date = null;
92                 private List<SimulationContext> context = new Vector<SimulationContext>(); // Search criterion only
93
94                 // - Public services
95
96                 @Override
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(final User actor) {
162                         this.actor = actor;
163                         return this;
164                 }
165
166                 public Properties setDate(final Date date) {
167                         this.date = date;
168                         return this;
169                 }
170
171                 public Properties setDescription(final String summary) {
172                         if (summary.length() > 0) {
173                                 this.summary = summary;
174                         }
175                         return this;
176                 }
177
178                 public Properties setManager(final User user) {
179                         this.manager = user;
180                         return this;
181                 }
182
183                 // For building a search query
184                 public Properties setReference(final String sid)
185                                 throws InvalidPropertyException {
186                         if (sid.length() == 0) {
187                                 throw new InvalidPropertyException("reference");
188                         }
189                         this.sid = sid;
190                         return this;
191                 }
192
193                 // For building a search query
194                 public Properties setSimulationContexts(
195                                 final List<SimulationContext> context) {
196                         this.context = context;
197                         return this;
198                 }
199
200                 // For building a search query
201                 public Properties setState(final ProgressState state) {
202                         this.state = state;
203                         return this;
204                 }
205
206                 public Properties setTitle(final String title)
207                                 throws InvalidPropertyException {
208                         if (title.length() == 0) {
209                                 throw new InvalidPropertyException("title");
210                         }
211                         this.title = title;
212                         return this;
213                 }
214
215                 // For building a search query
216                 public Properties setVisibility(final Visibility area) {
217                         this.visibility = area;
218                         return this;
219                 }
220
221                 // - Global validity check
222
223                 public void checkValidity() throws MissedPropertyException,
224                                 InvalidPropertyException, MultiplyDefinedException {
225                         if (title == null) {
226                                 throw new MissedPropertyException("title");
227                         }
228                         if (manager == null) {
229                                 throw new MissedPropertyException("manager");
230                         }
231                 }
232         }
233
234         /**
235          * Database fetch constructor.
236          */
237         protected Study() {
238                 super();
239                 contributor.clear();
240                 validactor.clear();
241                 actor.clear();
242         }
243
244         /**
245          * Constructor from properties.
246          * 
247          * @param sprop
248          *            study properties
249          * @throws MissedPropertyException
250          *             if some mandatory property is missed
251          * @throws InvalidPropertyException
252          *             if some property has invalid value
253          * @throws MultiplyDefinedException
254          *             if some property is defined several times
255          */
256         public Study(final Properties sprop) throws MissedPropertyException,
257                         InvalidPropertyException, MultiplyDefinedException {
258                 super(sprop); // Throws one of the above exception if not valid
259                 sid = sprop.sid; // Reset after save
260                 title = sprop.title; // Inherited attribute
261                 manager = sprop.manager;
262                 docount = 0;
263                 history = 0;
264                 // RKV scenarii = new LinkedList<Scenario>();
265                 visibility = Visibility.PRIVATE;
266                 state = ProgressState.inWORK;
267
268                 credate = sprop.date; // Inherited attribute
269                 if (credate == null) {
270                         Calendar current = Calendar.getInstance();
271                         credate = current.getTime(); // Today
272                 }
273                 lasdate = credate; // Inherited attribute
274                 version = new Revision().incrementAs(state).toString();
275
276                 if (sprop.summary != null) {
277                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
278                 }
279
280                 contributor.clear();
281                 validactor.clear();
282                 actor.clear();
283         }
284
285         public ProgressState getProgressState() {
286                 return state;
287         }
288
289         /**
290          * Returns the global unique reference of this study. The study reference is common to all versions of the study (versioning a study
291          * does not change its reference). The form of this study reference is defined in the configuration of the application server - see the
292          * SOM XML customization file.
293          */
294         public String getReference() {
295                 return sid;
296         }
297
298         public void setReference(final String aReference) {
299                 sid = aReference;
300         }
301
302         /**
303          * Get persistent list of study scenarios as an array.
304          * 
305          * @return array of scenarios
306          */
307         public Scenario[] getScenarii() {
308                 return scenarii.toArray(new Scenario[scenarii.size()]);
309         }
310
311         /**
312          * Get persistent list of study scenarios.
313          * 
314          * @return the list of scenarios
315          */
316         public List<Scenario> getScenariiList() {
317                 return scenarii;
318         }
319
320         public String getVersion() {
321                 return version;
322         }
323
324         public Visibility getVisibility() {
325                 return visibility;
326         }
327
328         /**
329          * Checks whether this study is in the Public or the Reference area of the repository.
330          * 
331          * @return true if the study is public.
332          * @see #moveToPublic()
333          * @see #moveToReference()
334          */
335         public boolean isPublic() {
336                 return (visibility != Visibility.PRIVATE);
337         }
338
339         public boolean isVersioned() {
340                 return (history > 0);
341         }
342
343         /**
344          * Check if the document is shared by scenarios of the study.
345          * 
346          * @param doc
347          *            the document to check
348          * @return true if the document is published in more then one scenario of the study
349          */
350         public boolean shares(final Document doc) {
351                 Scenario[] scene = this.getScenarii(); // If shared from within the study, the document is shared by the scenarios
352                 int counter = 0;
353                 boolean res = false;
354
355                 for (int i = 0; i < scene.length; i++) {
356                         if (scene[i].publishes(doc)) {
357                                 if (counter == 1) {
358                                         res = true;
359                                         break;
360                                 }
361                                 counter += 1;
362                         }
363                 }
364                 return res;
365         }
366
367         public int getLastLocalIndex() {
368                 return docount;
369         }
370
371         /**
372          * @param aVisibility
373          *            a study visibility to set
374          */
375         public void setVisibility(final Visibility aVisibility) {
376                 visibility = aVisibility;
377         }
378
379         /**
380          * @param aState
381          *            a study progress state to set
382          */
383         public void setProgressState(final ProgressState aState) {
384                 state = aState;
385         }
386
387         /**
388          * @param aVersion
389          */
390         public void setVersion(final String aVersion) {
391                 version = aVersion;
392         }
393
394         /**
395          * Set total number of documents of this study, including versions. It is stored in the persistent property docount.
396          * 
397          * @param anIndex
398          *            a number of study documents
399          */
400         public void setLastLocalIndex(final int anIndex) {
401                 docount = anIndex;
402         }
403
404         /**
405          * Get the transient map of document types to validation cycles.
406          * 
407          * @return map of validation cycles
408          */
409         public Map<String, ValidationCycle> getValidationCycles() {
410                 return validactor;
411         }
412
413         /**
414          * Get the transient list of study contributors.
415          * 
416          * @return the List of User objects
417          */
418         public List<User> getContributor() {
419                 return contributor;
420         }
421
422         /**
423          * Get the transient set of all study actors (contributors and validation cycles participants).
424          * 
425          * @return the set of User objects
426          */
427         public Set<User> getActor() {
428                 return actor;
429         }
430 }