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