Salome HOME
4eb98458093e55a95bf396afdc4ced3e1dbfd527
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / DocumentRights.java
1 package org.splat.som;
2 /**
3  * Class providing services for checking the rights related to operations on a given document.
4  * These rights are partially driven by the validation cycle associated to documents.
5  * 
6  * @see ValidationCycle
7  * @author    Daniel Brunier-Coulin
8  * @copyright OPEN CASCADE 2012
9  */
10 //TODO: Review this rights in the following contexts:
11 //      - Document shared by several scenarios
12 //      - Document out-dated following a modification of a document it uses
13
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.splat.kernel.Relation;
18 import org.splat.kernel.User;
19
20
21 public class DocumentRights {
22
23     private User             user;
24     private Publication      operand;
25     private ValidationCycle  cycle;
26     private boolean          isauthor;     // True if the user is author of the document
27
28 //  ==============================================================================================================================
29 //  Constructors
30 //  ==============================================================================================================================
31
32     public DocumentRights (User user, Publication tag) {
33 //  --------------------------------------------------
34       this.user     = user;
35       this.operand  = tag;
36       this.cycle    = operand.getOwnerStudy().getValidationCycleOf(operand.value().getType());
37       this.isauthor = operand.value().getAuthor().equals(user);
38 //TODO: all contributors of the given document (when supported) must also behave as author
39     }
40     protected DocumentRights (Publication tag) {
41 //  ------------------------------------------
42       this.user     = operand.value().getAuthor();
43       this.operand  = tag;
44       this.cycle    = operand.getOwnerStudy().getValidationCycleOf(operand.value().getType());
45       this.isauthor = true;           // In order to ignore the author state in the context of any user
46 //TODO: all contributors of the given document (when supported) must also behave as author
47     }
48
49 //  ==============================================================================================================================
50 //  Public member functions
51 //  ==============================================================================================================================
52
53 /**
54  * Checks if the user has right to accept the modifications of documents depending on the selected document.
55  * This operation applies to out-dated documents following a modification of documents they use.
56  * Only the author of the document has such right.
57  * 
58  * @return true if the user has right to accept the modifications of dependencies of the document.
59  * @see    Publication#accept()
60  */
61     public boolean canAccept () {
62 //  ---------------------------
63       if (!isauthor) return false;
64       return operand.isOutdated();
65     }
66
67 /**
68  * Checks if the user has right to approve the selected document.
69  * Only the approver of the type of selected document has such right, providing that the document is candidate for approval and
70  * all document dependencies have already been approved.
71  * 
72  * @return true if the user has right to approve the document.
73  * @see    Publication#approve()
74  * @see    ValidationCycle
75  */
76     public boolean canApprove () {
77 //  ----------------------------
78       User  approver = cycle.getActor(ValidationStep.APPROVAL);             // May be null if not approvable
79
80       if (!user.equals(approver)) return false;
81       if (operand.getProgressState() != ProgressState.inCHECK) return false;
82       
83       List<Relation>            use = operand.value().getRelations(UsesRelation.class);
84       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
85         Document      depend = (Document)i.next().getTo();
86         ProgressState state  = depend.getProgressState();
87         if (state == ProgressState.EXTERN)   continue;                      // External documents do not follow this progress state
88         if (state != ProgressState.APPROVED) return false;
89       }
90       return true;
91     }
92
93 /**
94  * Checks if the user has right to attach a file to the selected document.
95  * Both, the author, during the elaboration of the document, and the reviewer of the document, during the review process,
96  * have such right.
97  * 
98  * @return true if the user has right to attach a file to the document.
99  * @see    Publication#attach(String)
100  * @see    Publication#attach(String, String)
101  */
102     public boolean canAttach () {
103 //  ---------------------------
104       User           manager  = operand.getOwnerStudy().getAuthor();
105       User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
106       ProgressState  state    = operand.value().getProgressState();
107
108       if (state == ProgressState.inWORK) return (isauthor);
109       else                               return (isauthor || user.equals(manager) || user.equals(reviewer));
110     }
111
112 /**
113  * Checks if the user has right to demote the selected document.
114  * A document can be demoted providing that it is In-Draft or In-Check and all documents using it have previously been demoted.
115  * In-Draft documents can be demoted by default by both, the author of the document and the responsible of study, while
116  * documents in approval process can be demoted by their approver only.
117  * 
118  * @return true if the user has right to demote the document.
119  * @see    #canInvalidate()
120  * @see    #canPromote()
121  * @see    Publication#demote()
122  * @see    ValidationCycle
123  */
124     public boolean canDemote () {
125 //  ---------------------------
126       User           manager   = operand.getOwnerStudy().getAuthor();
127       User           publisher = cycle.getActor(ValidationStep.PROMOTION);  // Null if the default users are involved
128       User           approver  = cycle.getActor(ValidationStep.APPROVAL);   // May be null if not approvable
129       ProgressState  mystate   = operand.value().getProgressState();
130
131       if (mystate == ProgressState.inDRAFT) {
132         if (publisher == null) { if (!isauthor && !user.equals(manager)) return false;
133         } else                   if (!user.equals(publisher))            return false;
134       } else
135       if (mystate == ProgressState.inCHECK) {
136         if (!user.equals(approver))                                      return false;
137       } else                                                             return false;
138       
139       List<Relation>            use = operand.value().getRelations(UsedByRelation.class);
140       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
141         Document      depend = (Document)i.next().getTo();
142         ProgressState state  = depend.getProgressState();
143         if (mystate == ProgressState.inDRAFT &&  state != ProgressState.inWORK)  return false;
144         if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) return false;
145       }
146       return true;
147     }
148
149 /**
150  * Checks if the user has right to check-out the selected document for editing.
151  * In-Work documents can be checked-out by both, the author of the document and the responsible of study, while
152  * documents In-Draft can be checked-out by the reviewer only.
153  * 
154  * @return true if the user has right to edit the document.
155  */
156     public boolean canEdit () {
157 //  -------------------------
158       User           manager  = operand.getOwnerStudy().getAuthor();
159       User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
160       ProgressState  state    = operand.value().getProgressState();
161
162 //TODO: Should be restricted by the application if no editor available
163       if (state == ProgressState.inWORK) {
164         if (isauthor || user.equals(manager)) return true;
165       } else
166       if (state == ProgressState.inDRAFT) {
167         if (user.equals(reviewer))            return true;
168       }
169       return false;
170     }
171
172 /**
173  * Checks if the user has right to promote the selected document.
174  * A document can be promoted providing that it is In-Work and all its dependencies have previously been promoted.
175  * By default, both the author of the document and the responsible of study has right to promote such document. Otherwise,
176  * only the user involved in the Promotion step of validation cycle of the selected document has such right.
177  * 
178  * @return true if the user has right to promote the document.
179  * @see    #canDemote()
180  * @see    Publication#promote()
181  * @see    ValidationCycle
182  */
183     public boolean canPromote () {
184 //  ----------------------------
185       User manager   = operand.getOwnerStudy().getAuthor();
186       User publisher = cycle.getActor(ValidationStep.PROMOTION);            // Null if the default users are involved
187
188       if (operand.getProgressState() != ProgressState.inWORK)          return false;
189       if (publisher == null) { if (!isauthor && !user.equals(manager)) return false;
190       } else {                 if (!user.equals(publisher))            return false;
191       }
192       List<Relation>            use = operand.value().getRelations(UsesRelation.class);
193       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
194         Document      depend = (Document)i.next().getTo();
195         ProgressState state  = depend.getProgressState();
196         if (state == ProgressState.EXTERN) continue;                        // External documents do not follow this progress state
197         if (state == ProgressState.inWORK) return false;
198       }
199       return true;
200     }
201
202 /**
203  * Checks if the user has right to remove the history of the selected document, if exists.
204  * Only the responsible of the study have such right.
205  * 
206  * @return true if the user has right to purge the document.
207  */
208     public boolean canPurge () {
209 //  --------------------------
210       User      manager = operand.getOwnerStudy().getAuthor();
211       Document  doc     = operand.value();
212       
213       if (!user.equals(manager))                                return false;
214       if (doc.isShared())                                       return false;
215       if (doc.getFirstRelation(VersionsRelation.class) == null) return false;
216       return true;
217     }
218
219 /**
220  * Checks if the user has right to remove the selected document from the study.
221  * Both, the author of the document and the responsible of the study, have such right, providing that:
222  * - the document is neither in review or in approval process
223  * - the document is not used by any other document
224  * 
225  * @return true if the user has right to remove the document.
226  * @see    Step#removeDocument(Publication)
227  */
228     public boolean canRemove () {
229 //  ---------------------------
230       User          manager = operand.getOwnerStudy().getAuthor();
231       ProgressState state   = operand.getProgressState();
232
233       if (!isauthor && !user.equals(manager))                             return false;
234       if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
235
236       List<Publication> using = operand.getRelations(UsedByRelation.class);
237       return (using.size() == 0);
238     }
239
240 /**
241  * Checks if the user has right to rename the selected document.
242  * Only the author of the document has such right, providing that the document is neither in review nor in approval process.
243  * 
244  * @return true if the user has right to rename the document.
245  * @see    Publication#rename(String)
246  */
247     public boolean canRename () {
248 //  ---------------------------
249       ProgressState state = operand.getProgressState();
250
251       if (!isauthor) return false;     // In case of external document, the author is the one who has imported the document.
252       if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
253       return (!operand.value().isShared());
254     }
255
256 /**
257  * Checks if the user has right to replace the source file of the selected document.
258  * Both, the author of the document and the responsible of study has such right, providing that the document is neither in review
259  * nor in approval process.
260  * 
261  * @return true if the user has right to replace the document.
262  */
263     public boolean canReplace () {
264 //  ----------------------------
265       User          manager = operand.getOwnerStudy().getAuthor();
266       ProgressState state   = operand.getProgressState();
267
268       if (!isauthor && !user.equals(manager)) return false;                 // Supposed to work also in case of external document.
269       if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
270       return !operand.value().isShared();
271     }
272
273 /**
274  * Checks if the user has right to validate the selected document.
275  * Only the reviewer of the type of selected document has such right, providing that the document is being reviewed and
276  * all document dependencies have already been validated.
277  * 
278  * @return true if the user has right to validate the document
279  * @see    #canUnvalidate()
280  * @see    Publication#review()
281  * @see    ValidationCycle
282  */
283     public boolean canReview () {
284 //  ---------------------------
285       User  reviewer = cycle.getActor(ValidationStep.REVIEW);               // May be null if not reviewable
286
287       if (!user.equals(reviewer))                              return false;
288       if (operand.getProgressState() != ProgressState.inDRAFT) return false;
289       
290       List<Relation>            use = operand.value().getRelations(UsesRelation.class);
291       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
292         Document      depend = (Document)i.next().getTo();
293         ProgressState state  = depend.getProgressState();
294         if (state == ProgressState.EXTERN) continue;                        // External documents do not follow this progress state
295         if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) return false;
296       }
297       return true;
298     }
299
300 /**
301  * Checks if the user has right to undo the validation operation of the selected document.
302  * Both, the author and the reviewer of a validated document, have such right.
303  * 
304  * @return true if the user has right to undo the validation operation
305  * @see    #canDemote()
306  * @see    #canReview()
307  * @see    Publication#invalidate()
308  * @see    ValidationCycle
309  */
310     public boolean canInvalidate () {
311 //  -------------------------------
312       User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
313       ProgressState  mystate  = operand.value().getProgressState();
314
315       if (mystate != ProgressState.inCHECK)    return false;
316       if (!isauthor && !user.equals(reviewer)) return false;
317         
318       List<Relation>            use = operand.value().getRelations(UsedByRelation.class);
319       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
320         Document      depend = (Document)i.next().getTo();
321         ProgressState state  = depend.getProgressState();
322         if (mystate == ProgressState.inDRAFT &&  state != ProgressState.inWORK)  return false;
323         if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) return false;
324       }
325       return true;
326     }
327
328 /**
329  * Checks if the user has right to version the selected document.
330  * In-Work documents can be versioned by both, the author of the document and the responsible of study, while
331  * documents In-Draft can be versioned by the reviewer only.
332  * Additionally, Approved documents can also be versioned by their author in order to enter in a new modification validation cycle.
333  * 
334  * @return true if the user has right to version the document.
335  * @see    Step#versionDocument(Publication)
336  * @see    Step#versionDocument(Publication, Document.Properties)
337  * @see    Step#versionDocument(Publication, String)
338  */
339     public boolean canVersion () {
340 //  ----------------------------
341       User           manager  = operand.getOwnerStudy().getAuthor();
342       User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
343       ProgressState  state    = operand.value().getProgressState();
344
345       if (state == ProgressState.inWORK) {
346         if (isauthor || user.equals(manager)) return true;
347       } else
348       if (state == ProgressState.inDRAFT) {
349         if (user.equals(reviewer))            return true;
350       } else
351       if (state == ProgressState.APPROVED) {
352         if (isauthor)                         return true;
353       }
354       return false;
355  }
356
357 //  ==============================================================================================================================
358 //  Getter
359 //  ==============================================================================================================================
360
361 /**
362  * Returns the document subject of checks according to this user rights.
363  * 
364  * @return the document subject of checks.
365  */
366     public Document getOperand () {
367 //  -----------------------------
368       return operand.value();
369     }
370 }