Salome HOME
327241fbf50da461661f34c3b5b8a2b110ef940a
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / ValidationCycle.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * Class defining the validation cycle applicable to documents of a given type.<br/>
5  * A validation cycle specifies the validation steps of documents complying with by this cycle, as well as the actors
6  * involved in such validations. Once past, each validation step is written down by a time-stamp attached to the validated
7  * document.<br/>
8  * <br/>
9  * The possible validation steps are Promotion, Review, Approval and Acceptance (or Refusal), all being optional,
10  * except Promotion.<br/>
11  * When enabled, Review and Approval involve one given user while Promotion and Acceptance have default actors defined by
12  * the application. The default actors are:
13  * <ul>
14  * <li>Promotion by either the author of the document or the responsible of study</li>
15  * <li>Acceptance by the customer, possibly represented by an internal user</li>
16  * </ul>
17  * Explicit Promotion and Acceptance actors can be defined, for example to force some documents to be published by the
18  * responsible of study only.<br/> 
19  * <br/>
20  * Default validation cycles are defined in the configuration workflow, the responsible of studies overriding them when necessary.
21  * They are attached to studies at a given document type.<br/>
22  * 
23  * @see Study#addValidationCycle(DocumentType,Properties)
24  * @see Study#getValidationCycleOf(DocumentType)
25  * @see Timestamp
26  * @author    Daniel Brunier-Coulin
27  * @copyright OPEN CASCADE 2012-2014
28  */
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.splat.dal.bo.kernel.Persistent;
34 import org.splat.dal.bo.kernel.User;
35 import org.splat.kernel.InvalidPropertyException;
36 import org.splat.kernel.MissedPropertyException;
37 import org.splat.kernel.MultiplyDefinedException;
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
53                 // Customer
54         }
55
56         // ==============================================================================================================================
57         // Construction
58         // ==============================================================================================================================
59
60         // Fields initialization class
61         public static class Properties extends Persistent.Properties {
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                 @Override
71                 public void clear() {
72                         super.clear();
73                         doctype = null;
74                         publisher = null;
75                         reviewer = null;
76                         approver = null;
77                         signatory = null;
78                 }
79
80                 // - Protected services
81
82                 public Properties setDocumentType(final DocumentType type) {
83                         doctype = type;
84                         return this;
85                 }
86
87                 // - Properties setter
88
89                 public Properties setActor(final ValidationStep step, final User actor) {
90                         if (step == ValidationStep.PROMOTION) {
91                                 publisher = actor;
92                         } else if (step == ValidationStep.REVIEW) {
93                                 reviewer = actor;
94                         } else if (step == ValidationStep.APPROVAL) {
95                                 approver = actor;
96                         } else if (step == ValidationStep.ACCEPTANCE
97                                         || step == ValidationStep.REFUSAL) {
98                                 signatory = actor;
99                         }
100                         return this;
101                 }
102
103                 // - Global validity check
104
105                 public void checkValidity() throws MissedPropertyException {
106                         if (doctype == null) {
107                                 throw new MissedPropertyException("type");
108                         }
109                 }
110
111                 /**
112                  * Get the publisher.
113                  * 
114                  * @return the publisher
115                  */
116                 public User getPublisher() {
117                         return publisher;
118                 }
119
120                 /**
121                  * Get the reviewer.
122                  * 
123                  * @return the reviewer
124                  */
125                 public User getReviewer() {
126                         return reviewer;
127                 }
128
129                 /**
130                  * Get the approver.
131                  * 
132                  * @return the approver
133                  */
134                 public User getApprover() {
135                         return approver;
136                 }
137
138                 /**
139                  * Get the signatory.
140                  * 
141                  * @return the signatory
142                  */
143                 public User getSignatory() {
144                         return signatory;
145                 }
146         }
147
148         // Database fetch constructor
149         public ValidationCycle() {
150                 super();
151         }
152
153         public ValidationCycle(final Study from, final Properties vprop)
154                         throws MissedPropertyException, InvalidPropertyException,
155                         MultiplyDefinedException {
156                 super(vprop); // Throws one of the above exception if not valid
157                 mytype = vprop.doctype;
158                 publisher = vprop.publisher; // May be null
159                 reviewer = vprop.reviewer; // May be null
160                 approver = vprop.approver; // May be null
161                 signatory = vprop.signatory; // May be null
162                 context = new ValidationCycleRelation(from, this);
163         }
164
165         /**
166          * Create a copy of the given cycle for the given study.
167          * 
168          * @param study
169          *            the owner study
170          * @return the cloned validation cycle
171          */
172         public ValidationCycle clone(final Study study) {
173                 ValidationCycle res = new ValidationCycle();
174                 res.mytype = this.mytype;
175                 res.publisher = this.publisher; // May be null
176                 res.reviewer = this.reviewer; // May be null
177                 res.approver = this.approver; // May be null
178                 res.signatory = this.signatory; // May be null
179                 res.context = new ValidationCycleRelation(study, res);
180                 return res;
181         }
182
183         // ==============================================================================================================================
184         // Public member functions
185         // ==============================================================================================================================
186         /**
187          * Checks if a given validation step is enabled in this validation cycle.
188          * 
189          * @param step
190          *            the validation step checked.
191          * @return true if the given validation step is enabled.
192          */
193         public boolean enables(final ValidationStep step) {
194                 boolean res = false;
195                 if (step == ValidationStep.PROMOTION) {
196                         res = true;
197                 } else if (step == ValidationStep.REVIEW) {
198                         res = (reviewer != null);
199                 } else if (step == ValidationStep.APPROVAL) {
200                         res = (approver != null);
201                 } else if (step == ValidationStep.ACCEPTANCE
202                                 || step == ValidationStep.REFUSAL) {
203                         res = (signatory != null);
204                 }
205                 return res;
206         }
207
208         /**
209          * Returns the user involved in a given step of this document validation cycle. When enabled, the Review and Approval steps have one
210          * explicit actor returned by this function. On the other hand, Promotion and Acceptance have default actors - they respectively are the
211          * author of the document or the responsible of study, and the customer or its representative internal user. In this context, a null
212          * user is returned.
213          * 
214          * @param step
215          *            the validation step
216          * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
217          * @see #getAllActors()
218          * @see #enables
219          */
220         public User getActor(final ValidationStep step) {
221                 User res = null;
222                 if (step == ValidationStep.PROMOTION) {
223                         res = publisher;
224                 } else if (step == ValidationStep.REVIEW) {
225                         res = reviewer;
226                 } else if (step == ValidationStep.APPROVAL) {
227                         res = approver;
228                 } else if (step == ValidationStep.ACCEPTANCE
229                                 || step == ValidationStep.REFUSAL) {
230                         res = signatory;
231                 }
232                 return res;
233         }
234
235         /**
236          * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding the default
237          * actors.
238          * 
239          * @return the users explicitly involved by the steps of this validation cycle
240          * @see #getActor(ValidationStep)
241          * @see #enables(ValidationStep)
242          */
243         public User[] getAllActors() {
244                 List<User> result = new ArrayList<User>();
245                 if (publisher != null) {
246                         result.add(publisher);
247                 }
248                 if (reviewer != null) {
249                         result.add(reviewer);
250                 }
251                 if (approver != null) {
252                         result.add(approver);
253                 }
254                 if (signatory != null) {
255                         result.add(signatory);
256                 }
257                 return result.toArray(new User[result.size()]);
258         }
259
260         /**
261          * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document type is not
262          * defined.
263          * 
264          * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
265          * @see #isDefault()
266          */
267         public DocumentType getDocumentType() {
268                 return mytype; // May be null
269         }
270
271         /**
272          * Set a document type for the validation cycle.
273          * 
274          * @param aType
275          *            the document type
276          */
277         public void setDocumentType(final DocumentType aType) {
278                 mytype = aType; // May be null
279         }
280
281         /**
282          * 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 been
283          * defined.
284          * 
285          * @return true if this validation cycle is assigned to a study.
286          */
287         public boolean isAssigned() {
288                 return (context != null);
289         }
290
291         /**
292          * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or built-in.<br/>
293          * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step other
294          * than Promotion.
295          * 
296          * @return true if this validation cycle is a default one.
297          * @see #getDocumentType()
298          */
299         public boolean isDefault() {
300                 return (mytype == null);
301         }
302
303         public ValidationCycleRelation getContext() {
304                 return context;
305         }
306
307         /**
308          * Set the publisher.
309          * 
310          * @param publisher
311          *            the publisher to set
312          */
313         public void setPublisher(final User publisher) {
314                 this.publisher = publisher;
315         }
316
317         /**
318          * Set the reviewer.
319          * 
320          * @param reviewer
321          *            the reviewer to set
322          */
323         public void setReviewer(final User reviewer) {
324                 this.reviewer = reviewer;
325         }
326
327         /**
328          * Set the approver.
329          * 
330          * @param approver
331          *            the approver to set
332          */
333         public void setApprover(final User approver) {
334                 this.approver = approver;
335         }
336
337         /**
338          * Set the signatory.
339          * 
340          * @param signatory
341          *            the signatory to set
342          */
343         public void setSignatory(final User signatory) {
344                 this.signatory = signatory;
345         }
346 }