Salome HOME
5c67d1e10f03e2c8ecc8ddafdf15223e918257a6
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / ValidationCycle.java
1 package org.splat.dal.bo.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.Vector;
30
31 import org.splat.dal.bo.kernel.Persistent;
32 import org.splat.dal.bo.kernel.User;
33 import org.splat.kernel.InvalidPropertyException;
34 import org.splat.kernel.MissedPropertyException;
35 import org.splat.kernel.MultiplyDefinedException;
36
37 public class ValidationCycle extends Persistent {
38
39         private  ValidationCycleRelation context;
40     private  DocumentType            mytype;      // Null if the referenced validation cycle is a default one
41         private  User                    publisher;
42     private  User                    reviewer;    // Null if no REVIEW validation step
43     private  User                    approver;    // Null if no APPROVAL validation step
44     private  User                    signatory;   // Null if no ACCEPTANCE validation step
45
46     public enum Actor {
47       manager,                                    // Responsible of study 
48       Nx1,                                        // N+1 manager of the responsible of study
49       Nx2,                                        // N+2 manager of the responsible of study
50       customer                                    // Customer
51     }
52
53 //  ==============================================================================================================================
54 //  Construction
55 //  ==============================================================================================================================
56     
57 //  Fields initialization class
58     public static class Properties extends Persistent.Properties {
59 //  ------------------------------------------------------------
60       DocumentType doctype   = null;
61       User         publisher = null;
62       User         reviewer  = null;
63       User         approver  = null;
64       User         signatory = null;
65
66 //  - Public services
67
68       @Override
69         public void clear () {
70         super.clear();
71         doctype   = null;
72         publisher = null;
73         reviewer  = null;
74         approver  = null;
75         signatory = null;
76       }
77 //  - Protected services
78
79       public Properties setDocumentType (final DocumentType type)
80       {
81         doctype = type;
82         return this;
83       }
84 //  - Properties setter
85
86       public Properties setActor (final ValidationStep step, final User actor)
87       {
88         if      (step == ValidationStep.PROMOTION) {
89                         publisher = actor;
90                 } else if (step == ValidationStep.REVIEW) {
91                         reviewer  = actor;
92                 } else if (step == ValidationStep.APPROVAL) {
93                         approver  = actor;
94                 } else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) {
95                         signatory = actor;
96                 }
97         return this;
98       }
99 //  - Global validity check
100         
101       public void checkValidity() throws MissedPropertyException
102       { 
103         if (doctype == null) {
104                         throw new MissedPropertyException("type");
105                 }
106       }
107
108         /**
109          * Get the publisher.
110          * @return the publisher
111          */
112         public User getPublisher() {
113                 return publisher;
114         }
115
116         /**
117          * Get the reviewer.
118          * @return the reviewer
119          */
120         public User getReviewer() {
121                 return reviewer;
122         }
123
124         /**
125          * Get the approver.
126          * @return the approver
127          */
128         public User getApprover() {
129                 return approver;
130         }
131
132         /**
133          * Get the signatory.
134          * @return the signatory
135          */
136         public User getSignatory() {
137                 return signatory;
138         }
139     }
140 //  Database fetch constructor
141     public ValidationCycle () {
142     }
143     public ValidationCycle (final Study from, final Properties vprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
144         super(vprop);                  // Throws one of the above exception if not valid
145         mytype    = vprop.doctype;
146         publisher = vprop.publisher;   // May be null
147         reviewer  = vprop.reviewer;    // May be null
148         approver  = vprop.approver;    // May be null
149         signatory = vprop.signatory;   // May be null
150         context   = new ValidationCycleRelation(from, this);
151       }
152
153     /**
154      * Create a copy of the given cycle for the given study.
155      * @param study the owner study
156      * @param src the original validation cycle
157      */
158     public ValidationCycle (final Study study, final ValidationCycle src) {
159         super();                  // Throws one of the above exception if not valid
160         mytype    = src.getDocumentType();
161         publisher = src.publisher;   // May be null
162         reviewer  = src.reviewer;    // May be null
163         approver  = src.approver;    // May be null
164         signatory = src.signatory;   // May be null
165         context   = new ValidationCycleRelation(study, this);
166       }
167
168 //  ==============================================================================================================================
169 //  Public member functions
170 //  ==============================================================================================================================
171 /**
172  * Checks if a given validation step is enabled in this validation cycle.
173  * 
174  * @param  step the validation step checked.
175  * @return true if the given validation step is enabled.
176  */
177     public boolean enables (final ValidationStep step) {
178       if      (step == ValidationStep.PROMOTION) {
179                 return true;
180         } else if (step == ValidationStep.REVIEW) {
181                 return (reviewer  != null);
182         } else if (step == ValidationStep.APPROVAL) {
183                 return (approver  != null);
184         } else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) {
185                 return (signatory != null);
186         }
187       return false;
188     }
189
190 /**
191  * Returns the user involved in a given step of this document validation cycle.
192  * When enabled, the Review and Approval steps have one explicit actor returned by this function. On the other hand,
193  * Promotion and Acceptance have default actors - they respectively are the author of the document or the responsible of study,
194  * and the customer or its representative internal user. In this context, a null user is returned.
195  * 
196  * @param  step the validation step
197  * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
198  * @see    #getAllActors()
199  * @see    #enables
200  */
201     public User getActor (final ValidationStep step) {
202       if      (step == ValidationStep.PROMOTION) {
203                 return publisher;
204         } else if (step == ValidationStep.REVIEW) {
205                 return reviewer;
206         } else if (step == ValidationStep.APPROVAL) {
207                 return approver;
208         } else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) {
209                 return signatory;
210         }
211       return null;
212     }
213
214 /**
215  * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding
216  * the default actors.
217  * 
218  * @return the users explicitly involved by the steps of this validation cycle
219  * @see    #getActor(ValidationStep)
220  * @see    #enables(ValidationStep)
221  */
222     public User[] getAllActors () {
223 //  -----------------------------
224       Vector<User> result = new Vector<User>();
225       if (publisher != null) {
226                 result.add(publisher);
227         }
228       if (reviewer  != null) {
229                 result.add(reviewer);
230         }
231       if (approver  != null) {
232                 result.add(approver);
233         }
234       if (signatory != null) {
235                 result.add(signatory);
236         }
237       return  result.toArray(new User[result.size()]);
238     }
239
240 /**
241  * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document
242  * type is not defined.
243  * 
244  * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
245  * @see    #isDefault()
246  */
247     public DocumentType getDocumentType () {
248           return mytype;                    // May be null
249         }
250
251     /**
252      * Set a document type for the validation cycle.
253      * @param aType the document type
254      */
255     public void setDocumentType (final DocumentType aType) {
256           mytype = aType;                    // May be null
257     }
258
259 /**
260  * 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
261  * been defined.
262  * 
263  * @return true if this validation cycle is assigned to a study.
264  */
265     public boolean isAssigned () {
266       return (context != null);
267     }
268
269 /**
270  * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or
271  * built-in.<br/>
272  * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step
273  * other than Promotion.
274  * 
275  * @return true if this validation cycle is a default one.
276  * @see    #getDocumentType()
277  */
278     public boolean isDefault () {
279      return (mytype == null);
280     }
281
282     public ValidationCycleRelation getContext () {
283 //  -----------------------------------------------
284       return context;
285     }
286         /**
287          * Set the publisher.
288          * @param publisher the publisher to set
289          */
290         public void setPublisher(final User publisher) {
291                 this.publisher = publisher;
292         }
293         /**
294          * Set the reviewer.
295          * @param reviewer the reviewer to set
296          */
297         public void setReviewer(final User reviewer) {
298                 this.reviewer = reviewer;
299         }
300         /**
301          * Set the approver.
302          * @param approver the approver to set
303          */
304         public void setApprover(final User approver) {
305                 this.approver = approver;
306         }
307         /**
308          * Set the signatory.
309          * @param signatory the signatory to set
310          */
311         public void setSignatory(final User signatory) {
312                 this.signatory = signatory;
313         }
314 }