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