Salome HOME
Refactoring: configuration files are moved into Siman web project.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / ValidationCycle.java
1 package org.splat.som;
2 /**
3  * Class defining the validation cycle applicable to documents of a given type.<br/>
4  * A validation cycle specifies the validation steps of documents complying with by this cycle, as well as the actors
5  * involved in such validations. Once past, each validation step is written down by a time-stamp attached to the validated
6  * document.<br/>
7  * <br/>
8  * The possible validation steps are Promotion, Review, Approval and Acceptance (or Refusal), all being optional,
9  * except Promotion.<br/>
10  * When enabled, Review and Approval involve one given user while Promotion and Acceptance have default actors defined by
11  * the application. The default actors are:
12  * <ul>
13  * <li>Promotion by either the author of the document or the responsible of study</li>
14  * <li>Acceptance by the customer, possibly represented by an internal user</li>
15  * </ul>
16  * Explicit Promotion and Acceptance actors can be defined, for example to force some documents to be published by the
17  * responsible of study only.<br/> 
18  * <br/>
19  * Default validation cycles are defined in the configuration workflow, the responsible of studies overriding them when necessary.
20  * They are attached to studies at a given document type.<br/>
21  * 
22  * @see Study#addValidationCycle(DocumentType,Properties)
23  * @see Study#getValidationCycleOf(DocumentType)
24  * @see Timestamp
25  * @author    Daniel Brunier-Coulin
26  * @copyright OPEN CASCADE 2012
27  */
28
29 import java.util.List;
30 import java.util.Vector;
31
32 import org.splat.kernel.InvalidPropertyException;
33 import org.splat.kernel.MissedPropertyException;
34 import org.splat.kernel.MultiplyDefinedException;
35 import org.splat.kernel.Persistent;
36 import org.splat.kernel.User;
37 import org.splat.kernel.UserDirectory;
38
39 public class ValidationCycle extends Persistent {
40
41         private  ValidationCycleRelation context;
42     private  DocumentType            mytype;      // Null if the referenced validation cycle is a default one
43         private  User                    publisher;
44     private  User                    reviewer;    // Null if no REVIEW validation step
45     private  User                    approver;    // Null if no APPROVAL validation step
46     private  User                    signatory;   // Null if no ACCEPTANCE validation step
47
48     public enum Actor {
49       manager,                                    // Responsible of study 
50       Nx1,                                        // N+1 manager of the responsible of study
51       Nx2,                                        // N+2 manager of the responsible of study
52       customer                                    // Customer
53     }
54
55 //  ==============================================================================================================================
56 //  Construction
57 //  ==============================================================================================================================
58     
59 //  Fields initialization class
60     public static class Properties extends Persistent.Properties {
61 //  ------------------------------------------------------------
62       DocumentType doctype   = null;
63       User         publisher = null;
64       User         reviewer  = null;
65       User         approver  = null;
66       User         signatory = null;
67
68 //  - Public services
69
70       public void clear () {
71         super.clear();
72         doctype   = null;
73         publisher = null;
74         reviewer  = null;
75         approver  = null;
76         signatory = null;
77       }
78 //  - Protected services
79
80       protected Properties setDocumentType (DocumentType type)
81       {
82         doctype = type;
83         return this;
84       }
85 //  - Properties setter
86
87       public Properties setActor (ValidationStep step, User actor)
88       {
89         if      (step == ValidationStep.PROMOTION) publisher = actor;
90         else if (step == ValidationStep.REVIEW)    reviewer  = actor;
91         else if (step == ValidationStep.APPROVAL)  approver  = actor;
92         else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = actor;
93         return this;
94       }
95 //  - Global validity check
96         
97       public void checkValidity() throws MissedPropertyException
98       { 
99         if (doctype == null) throw new MissedPropertyException("type");
100       }
101     }
102 //  Database fetch constructor
103     protected ValidationCycle () {
104     }
105 //  Internal constructors
106     protected ValidationCycle (Study from, ProjectSettings.ValidationCycle cycle) {
107 //  -----------------------------------------------------------------------------
108       Actor[]         actype = cycle.getActorTypes();
109       User.Properties uprop  = new User.Properties();
110
111       mytype  = Document.selectType(cycle.getName());     // Null in case of default validation cycle
112 //    context = new ValidationCycleRelation(from, this);
113       context = null;                                     // Validation cycle defined in the workflow
114       for (int i=0; i<actype.length; i++) {
115         User actor = null;
116         if (actype[i] != null)
117         try {
118           if (actype[i] == Actor.manager) {
119             actor = from.getAuthor();
120           } else
121           if (actype[i] == Actor.Nx1) {
122             List<User> manager = UserDirectory.selectUsersWhere(uprop.setOrganizationName("Nx1"));
123             if (manager.size() == 1) actor = manager.get(0);
124           } else
125           if (actype[i] == Actor.Nx2) {
126             List<User> manager = UserDirectory.selectUsersWhere(uprop.setOrganizationName("Nx2"));
127             if (manager.size() == 1) actor = manager.get(0);
128           } else {      /* Actor.customer */
129             actor = from.getAuthor();
130 //TODO: Get the customer of the study, if exists
131           }
132         } catch (Exception e) {      // Should not happen
133           actor = null;
134         }
135         if      (i == 0) reviewer  = actor;
136         else if (i == 1) approver  = actor;
137         else if (i == 2) signatory = actor;
138       }
139     }
140     protected ValidationCycle (Study from, Properties vprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
141 //  --------------------------------------------------------
142       super(vprop);                  // Throws one of the above exception if not valid
143       mytype    = vprop.doctype;
144       publisher = vprop.publisher;   // May be null
145       reviewer  = vprop.reviewer;    // May be null
146       approver  = vprop.approver;    // May be null
147       signatory = vprop.signatory;   // May be null
148       context   = new ValidationCycleRelation(from, this);
149     }
150
151 //  ==============================================================================================================================
152 //  Public member functions
153 //  ==============================================================================================================================
154 /**
155  * Checks if a given validation step is enabled in this validation cycle.
156  * 
157  * @param  step the validation step checked.
158  * @return true if the given validation step is enabled.
159  */
160     public boolean enables (ValidationStep step) {
161 //  -------------------------------------------
162       if      (step == ValidationStep.PROMOTION) return true;
163       else if (step == ValidationStep.REVIEW)    return (reviewer  != null);
164       else if (step == ValidationStep.APPROVAL)  return (approver  != null);
165       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) return (signatory != null);
166       return false;
167     }
168
169 /**
170  * Returns the user involved in a given step of this document validation cycle.
171  * When enabled, the Review and Approval steps have one explicit actor returned by this function. On the other hand,
172  * Promotion and Acceptance have default actors - they respectively are the author of the document or the responsible of study,
173  * and the customer or its representative internal user. In this context, a null user is returned.
174  * 
175  * @param  step the validation step
176  * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
177  * @see    #getAllActors()
178  * @see    #enables
179  */
180     public User getActor (ValidationStep step) {
181 //  -----------------------------------------
182       if      (step == ValidationStep.PROMOTION) return publisher;
183       else if (step == ValidationStep.REVIEW)    return reviewer;
184       else if (step == ValidationStep.APPROVAL)  return approver;
185       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) return signatory;
186       return null;
187     }
188
189 /**
190  * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding
191  * the default actors.
192  * 
193  * @return the users explicitly involved by the steps of this validation cycle
194  * @see    #getActor(ValidationStep)
195  * @see    #enables(ValidationStep)
196  */
197     public User[] getAllActors () {
198 //  -----------------------------
199       Vector<User> result = new Vector<User>();
200       if (publisher != null) result.add(publisher);
201       if (reviewer  != null) result.add(reviewer);
202       if (approver  != null) result.add(approver);
203       if (signatory != null) result.add(signatory);
204       return  result.toArray(new User[result.size()]);
205     }
206
207 /**
208  * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document
209  * type is not defined.
210  * 
211  * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
212  * @see    #isDefault()
213  */
214     public DocumentType getDocumentType () {
215 //  --------------------------------------
216       return mytype;                    // May be null
217     }
218
219 /**
220  * Checks if this validation cycle is assigned to a study. If not, it is common to all studies of the workflow in which it has
221  * been defined.
222  * 
223  * @return true if this validation cycle is assigned to a study.
224  */
225     public boolean isAssigned () {
226 //  ----------------------------
227       return (context != null);
228     }
229
230 /**
231  * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or
232  * built-in.<br/>
233  * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step
234  * other than Promotion.
235  * 
236  * @return true if this validation cycle is a default one.
237  * @see    #getDocumentType()
238  * @see    ProjectSettings#getNewValidationCycle()
239  */
240     public boolean isDefault () {
241 //  ---------------------------
242      return (mytype == null);
243     }
244
245 //  ==============================================================================================================================
246 //  Protected services
247 //  ==============================================================================================================================
248
249     protected ValidationCycleRelation getContext () {
250 //  -----------------------------------------------
251       return context;
252     }
253
254     protected void remove (ValidationStep step) {
255 //  ------------------------------------------
256       if      (step == ValidationStep.REVIEW)     reviewer  = null;
257       else if (step == ValidationStep.APPROVAL)   approver  = null;
258       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = null;
259       if (this.isSaved()) Database.getSession().update(this);
260     }
261
262     protected void resetActors (Properties vprop) {
263 //  ---------------------------------------------
264       publisher = vprop.publisher;   // May be null
265       reviewer  = vprop.reviewer;    // May be null
266       approver  = vprop.approver;    // May be null
267       signatory = vprop.signatory;   // May be null
268       if (this.isSaved()) Database.getSession().update(this);
269     }
270
271     protected void setActor (ValidationStep step, User actor) {
272 //  --------------------------------------------------------
273       if      (step == ValidationStep.PROMOTION) publisher = actor;
274       else if (step == ValidationStep.REVIEW)    reviewer  = actor;
275       else if (step == ValidationStep.APPROVAL)  approver  = actor;
276       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = actor;
277       if (this.isSaved()) Database.getSession().update(this);
278     }
279 }