Salome HOME
Fix for the bug: versioning of a document with uses relations => crash.
[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 //  Public member functions
155 //  ==============================================================================================================================
156 /**
157  * Checks if a given validation step is enabled in this validation cycle.
158  * 
159  * @param  step the validation step checked.
160  * @return true if the given validation step is enabled.
161  */
162     public boolean enables (final ValidationStep step) {
163       if      (step == ValidationStep.PROMOTION) {
164                 return true;
165         } else if (step == ValidationStep.REVIEW) {
166                 return (reviewer  != null);
167         } else if (step == ValidationStep.APPROVAL) {
168                 return (approver  != null);
169         } else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) {
170                 return (signatory != null);
171         }
172       return false;
173     }
174
175 /**
176  * Returns the user involved in a given step of this document validation cycle.
177  * When enabled, the Review and Approval steps have one explicit actor returned by this function. On the other hand,
178  * Promotion and Acceptance have default actors - they respectively are the author of the document or the responsible of study,
179  * and the customer or its representative internal user. In this context, a null user is returned.
180  * 
181  * @param  step the validation step
182  * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
183  * @see    #getAllActors()
184  * @see    #enables
185  */
186     public User getActor (final ValidationStep step) {
187       if      (step == ValidationStep.PROMOTION) {
188                 return publisher;
189         } else if (step == ValidationStep.REVIEW) {
190                 return reviewer;
191         } else if (step == ValidationStep.APPROVAL) {
192                 return approver;
193         } else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) {
194                 return signatory;
195         }
196       return null;
197     }
198
199 /**
200  * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding
201  * the default actors.
202  * 
203  * @return the users explicitly involved by the steps of this validation cycle
204  * @see    #getActor(ValidationStep)
205  * @see    #enables(ValidationStep)
206  */
207     public User[] getAllActors () {
208 //  -----------------------------
209       Vector<User> result = new Vector<User>();
210       if (publisher != null) {
211                 result.add(publisher);
212         }
213       if (reviewer  != null) {
214                 result.add(reviewer);
215         }
216       if (approver  != null) {
217                 result.add(approver);
218         }
219       if (signatory != null) {
220                 result.add(signatory);
221         }
222       return  result.toArray(new User[result.size()]);
223     }
224
225 /**
226  * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document
227  * type is not defined.
228  * 
229  * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
230  * @see    #isDefault()
231  */
232     public DocumentType getDocumentType () {
233           return mytype;                    // May be null
234         }
235
236     /**
237      * Set a document type for the validation cycle.
238      * @param aType the document type
239      */
240     public void setDocumentType (final DocumentType aType) {
241           mytype = aType;                    // May be null
242     }
243
244 /**
245  * 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
246  * been defined.
247  * 
248  * @return true if this validation cycle is assigned to a study.
249  */
250     public boolean isAssigned () {
251       return (context != null);
252     }
253
254 /**
255  * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or
256  * built-in.<br/>
257  * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step
258  * other than Promotion.
259  * 
260  * @return true if this validation cycle is a default one.
261  * @see    #getDocumentType()
262  */
263     public boolean isDefault () {
264      return (mytype == null);
265     }
266
267     public ValidationCycleRelation getContext () {
268 //  -----------------------------------------------
269       return context;
270     }
271         /**
272          * Set the publisher.
273          * @param publisher the publisher to set
274          */
275         public void setPublisher(final User publisher) {
276                 this.publisher = publisher;
277         }
278         /**
279          * Set the reviewer.
280          * @param reviewer the reviewer to set
281          */
282         public void setReviewer(final User reviewer) {
283                 this.reviewer = reviewer;
284         }
285         /**
286          * Set the approver.
287          * @param approver the approver to set
288          */
289         public void setApprover(final User approver) {
290                 this.approver = approver;
291         }
292         /**
293          * Set the signatory.
294          * @param signatory the signatory to set
295          */
296         public void setSignatory(final User signatory) {
297                 this.signatory = signatory;
298         }
299 }