]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/som/ValidationCycle.java
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 / 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.List;
30 import java.util.Vector;
31
32 import org.splat.dal.bo.kernel.Persistent;
33 import org.splat.dal.bo.kernel.User;
34 import org.splat.dal.dao.som.Database;
35 import org.splat.kernel.InvalidPropertyException;
36 import org.splat.kernel.MissedPropertyException;
37 import org.splat.kernel.MultiplyDefinedException;
38 import org.splat.kernel.UserDirectory;
39 import org.splat.service.technical.ProjectSettingsServiceImpl;
40
41 public class ValidationCycle extends Persistent {
42
43         private  ValidationCycleRelation context;
44     private  DocumentType            mytype;      // Null if the referenced validation cycle is a default one
45         private  User                    publisher;
46     private  User                    reviewer;    // Null if no REVIEW validation step
47     private  User                    approver;    // Null if no APPROVAL validation step
48     private  User                    signatory;   // Null if no ACCEPTANCE validation step
49
50     public enum Actor {
51       manager,                                    // Responsible of study 
52       Nx1,                                        // N+1 manager of the responsible of study
53       Nx2,                                        // N+2 manager of the responsible of study
54       customer                                    // Customer
55     }
56
57 //  ==============================================================================================================================
58 //  Construction
59 //  ==============================================================================================================================
60     
61 //  Fields initialization class
62     public static class Properties extends Persistent.Properties {
63 //  ------------------------------------------------------------
64       DocumentType doctype   = null;
65       User         publisher = null;
66       User         reviewer  = null;
67       User         approver  = null;
68       User         signatory = null;
69
70 //  - Public services
71
72       public void clear () {
73         super.clear();
74         doctype   = null;
75         publisher = null;
76         reviewer  = null;
77         approver  = null;
78         signatory = null;
79       }
80 //  - Protected services
81
82       public Properties setDocumentType (DocumentType type)
83       {
84         doctype = type;
85         return this;
86       }
87 //  - Properties setter
88
89       public Properties setActor (ValidationStep step, User actor)
90       {
91         if      (step == ValidationStep.PROMOTION) publisher = actor;
92         else if (step == ValidationStep.REVIEW)    reviewer  = actor;
93         else if (step == ValidationStep.APPROVAL)  approver  = actor;
94         else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = actor;
95         return this;
96       }
97 //  - Global validity check
98         
99       public void checkValidity() throws MissedPropertyException
100       { 
101         if (doctype == null) throw new MissedPropertyException("type");
102       }
103     }
104 //  Database fetch constructor
105     protected ValidationCycle () {
106     }
107 //  Internal constructors
108     protected ValidationCycle (Study from, ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle) {
109 //  -----------------------------------------------------------------------------
110       Actor[]         actype = cycle.getActorTypes();
111       User.Properties uprop  = new User.Properties();
112
113       mytype  = Document.selectType(cycle.getName());     // Null in case of default validation cycle
114 //    context = new ValidationCycleRelation(from, this);
115       context = null;                                     // Validation cycle defined in the workflow
116       for (int i=0; i<actype.length; i++) {
117         User actor = null;
118         if (actype[i] != null)
119         try {
120           if (actype[i] == Actor.manager) {
121             actor = from.getAuthor();
122           } else
123           if (actype[i] == Actor.Nx1) {
124             List<User> manager = UserDirectory.selectUsersWhere(uprop.setOrganizationName("Nx1"));
125             if (manager.size() == 1) actor = manager.get(0);
126           } else
127           if (actype[i] == Actor.Nx2) {
128             List<User> manager = UserDirectory.selectUsersWhere(uprop.setOrganizationName("Nx2"));
129             if (manager.size() == 1) actor = manager.get(0);
130           } else {      /* Actor.customer */
131             actor = from.getAuthor();
132 //TODO: Get the customer of the study, if exists
133           }
134         } catch (Exception e) {      // Should not happen
135           actor = null;
136         }
137         if      (i == 0) reviewer  = actor;
138         else if (i == 1) approver  = actor;
139         else if (i == 2) signatory = actor;
140       }
141     }
142     public ValidationCycle (Study from, Properties vprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
143 //  --------------------------------------------------------
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 (ValidationStep step) {
163 //  -------------------------------------------
164       if      (step == ValidationStep.PROMOTION) return true;
165       else if (step == ValidationStep.REVIEW)    return (reviewer  != null);
166       else if (step == ValidationStep.APPROVAL)  return (approver  != null);
167       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) return (signatory != null);
168       return false;
169     }
170
171 /**
172  * Returns the user involved in a given step of this document validation cycle.
173  * When enabled, the Review and Approval steps have one explicit actor returned by this function. On the other hand,
174  * Promotion and Acceptance have default actors - they respectively are the author of the document or the responsible of study,
175  * and the customer or its representative internal user. In this context, a null user is returned.
176  * 
177  * @param  step the validation step
178  * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
179  * @see    #getAllActors()
180  * @see    #enables
181  */
182     public User getActor (ValidationStep step) {
183 //  -----------------------------------------
184       if      (step == ValidationStep.PROMOTION) return publisher;
185       else if (step == ValidationStep.REVIEW)    return reviewer;
186       else if (step == ValidationStep.APPROVAL)  return approver;
187       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) return signatory;
188       return null;
189     }
190
191 /**
192  * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding
193  * the default actors.
194  * 
195  * @return the users explicitly involved by the steps of this validation cycle
196  * @see    #getActor(ValidationStep)
197  * @see    #enables(ValidationStep)
198  */
199     public User[] getAllActors () {
200 //  -----------------------------
201       Vector<User> result = new Vector<User>();
202       if (publisher != null) result.add(publisher);
203       if (reviewer  != null) result.add(reviewer);
204       if (approver  != null) result.add(approver);
205       if (signatory != null) result.add(signatory);
206       return  result.toArray(new User[result.size()]);
207     }
208
209 /**
210  * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document
211  * type is not defined.
212  * 
213  * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
214  * @see    #isDefault()
215  */
216     public DocumentType getDocumentType () {
217 //  --------------------------------------
218       return mytype;                    // May be null
219     }
220
221 /**
222  * 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
223  * been defined.
224  * 
225  * @return true if this validation cycle is assigned to a study.
226  */
227     public boolean isAssigned () {
228 //  ----------------------------
229       return (context != null);
230     }
231
232 /**
233  * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or
234  * built-in.<br/>
235  * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step
236  * other than Promotion.
237  * 
238  * @return true if this validation cycle is a default one.
239  * @see    #getDocumentType()
240  * @see    ProjectSettingsServiceImpl#getNewValidationCycle()
241  */
242     public boolean isDefault () {
243 //  ---------------------------
244      return (mytype == null);
245     }
246
247 //  ==============================================================================================================================
248 //  Protected services
249 //  ==============================================================================================================================
250
251     public ValidationCycleRelation getContext () {
252 //  -----------------------------------------------
253       return context;
254     }
255
256     protected void remove (ValidationStep step) {
257 //  ------------------------------------------
258       if      (step == ValidationStep.REVIEW)     reviewer  = null;
259       else if (step == ValidationStep.APPROVAL)   approver  = null;
260       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = null;
261       if (this.isSaved()) Database.getSession().update(this);
262     }
263
264     public void resetActors (Properties vprop) {
265 //  ---------------------------------------------
266       publisher = vprop.publisher;   // May be null
267       reviewer  = vprop.reviewer;    // May be null
268       approver  = vprop.approver;    // May be null
269       signatory = vprop.signatory;   // May be null
270       if (this.isSaved()) Database.getSession().update(this);
271     }
272
273     protected void setActor (ValidationStep step, User actor) {
274 //  --------------------------------------------------------
275       if      (step == ValidationStep.PROMOTION) publisher = actor;
276       else if (step == ValidationStep.REVIEW)    reviewer  = actor;
277       else if (step == ValidationStep.APPROVAL)  approver  = actor;
278       else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = actor;
279       if (this.isSaved()) Database.getSession().update(this);
280     }
281 }