]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/som/StudyRights.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 / StudyRights.java
1 package org.splat.som;
2
3 /**
4  * Class defining the default rights related to operations on studies.
5  * On the contrary of documents, a study cannot directly be reviewed or approved. It is reviewed or approved through
6  * its final report.
7  * 
8  * @author    Daniel Brunier-Coulin
9  * @copyright OPEN CASCADE 2012
10  */
11
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.dal.bo.som.ProgressState;
14 import org.splat.dal.bo.som.Publication;
15 import org.splat.dal.bo.som.Study;
16 import org.splat.dal.bo.som.ValidationCycle;
17 import org.splat.dal.bo.som.ValidationStep;
18 import org.splat.service.ServiceLocatorImpl;
19 import org.splat.service.StudyService;
20
21 /**
22  * The class representing user rights for a selected study.
23  */
24 public class StudyRights {
25
26         /**
27          * The connected user.
28          */
29         private final transient User _user;
30         /**
31          * The selected study.
32          */
33         private final transient Study _operand;
34         /**
35          * True if the current user is the author of the selected study.
36          */
37         private transient boolean _isauthor = false; // For optimizing
38         /**
39          * The study validation cycle according to the validation cycle of a study result document.
40          */
41         private transient final ValidationCycle _cycle;
42
43         /**
44          * The constructor.
45          * 
46          * @param user
47          *            the current user
48          * @param study
49          *            the selected study
50          */
51         public StudyRights(final User user, final Study study) {
52                 this._user = user;
53                 this._operand = study;
54                 this._cycle = getStudyService().getValidationCycleOf(_operand,
55                                 getStudyService().getStudyResultType(_operand));
56
57                 if (_operand != null && _operand.getAuthor() != null) {
58                         this._isauthor = _operand.getAuthor().equals(user); // user may be null
59                 }
60         }
61
62         /**
63          * The constructor for the case when the user is the author of the study.
64          * 
65          * @param study
66          *            the selected study
67          */
68         public StudyRights(final Study study) {
69                 this._user = study.getAuthor();
70                 this._operand = study;
71                 this._isauthor = true; // In order to ignore the author in this context
72                 this._cycle = getStudyService().getValidationCycleOf(_operand,
73                                 getStudyService().getStudyResultType(_operand));
74         }
75
76         // ==============================================================================================================================
77         // Public member functions
78         // ==============================================================================================================================
79
80         /**
81          * Check if the user can add a new scenario to the study.
82          * 
83          * @return true if the user can add a new scenario to the study
84          */
85         public boolean canAddScenario() {
86                 return (_operand.getProgressState() == ProgressState.inWORK || _operand
87                                 .getProgressState() == ProgressState.inDRAFT)
88                                 && getStudyService().isStaffedBy(_operand, _user);
89         }
90
91         /**
92          * Checks if the user has right to edit the description of the study. <BR>
93          * All actors of the study have such right, including the author, contributors,<BR>
94          * reviewers and approvers.
95          * 
96          * @return true if the user has right to edit the description.
97          */
98         public boolean canEditDescription() {
99                 return (_operand.getAuthor().equals(_user) || getStudyService()
100                                 .hasActor(_operand, _user));
101         }
102
103         /**
104          * Check if the user can configure the study.
105          * 
106          * @return true if the user can configure the study.
107          */
108         public boolean canEditProperties() {
109                 return _isauthor;
110         }
111
112         /**
113          * Checks if the user has right to move the study from the Private to the Public area of the repository. Only the author of the study
114          * have such right.
115          * 
116          * @return true if the user has right to edit the description.
117          */
118         public boolean canPublish() {
119                 return (_operand.getProgressState() == ProgressState.APPROVED && !_operand
120                                 .isPublic()/* && "knowledgineer".equals(_user.getRole().getName()) */);
121         }
122
123         /**
124          * Checks if the user has right to move the study from the Public to the Private area of the repository. Only the author of the study
125          * have such right.
126          * 
127          * @return true if the user has right to edit the description.
128          */
129         public boolean canProtect() {
130                 return (_operand.getProgressState() == ProgressState.APPROVED && _operand
131                                 .isPublic()/* && "knowledgineer".equals(_user.getRole().getName()) */);
132         }
133
134         /**
135          * Check if the user can remove old versions.
136          * 
137          * @return true if the user can remove old versions
138          */
139         public boolean canPurge() {
140                 return (_isauthor && _operand.isVersioned());
141         }
142
143         /**
144          * Check if the user can remove the study.
145          * 
146          * @return true if the user can remove the study
147          */
148         public boolean canRemove() {
149                 return (_operand.getProgressState() == ProgressState.inWORK || _operand
150                                 .getProgressState() == ProgressState.inDRAFT)
151                                 && _isauthor;
152         }
153
154         /**
155          * Check if the user can version the study.
156          * 
157          * @return true if the user can version the study
158          */
159         public boolean canVersion() {
160                 return (_operand.getProgressState() == ProgressState.inWORK || _operand
161                                 .getProgressState() == ProgressState.inDRAFT)
162                                 && getStudyService().isStaffedBy(_operand, _user);
163         }
164
165         /**
166          * Can the given study be marked as reference or not.
167          * 
168          * @return true/false.
169          */
170         public boolean canMarkStudyAsReference() {
171                 return (_operand.getProgressState() == ProgressState.APPROVED /* && "knowledgineer".equals(_user.getRole().getName()) */);
172         }
173
174         /**
175          * Can the given study be unmarked as reference or not.
176          * 
177          * @return true/false.
178          */
179         public boolean canRemoveStudyAsReference() {
180                 return (_operand.getProgressState() == ProgressState.TEMPLATE /* && "knowledgineer".equals(_user.getRole().getName()) */);
181         }
182
183         // ==========================================================================
184         // Operations from document validation cycle
185         // ==========================================================================
186
187         /**
188          * Checks if the user has right to approve the selected document. Only the approver of the type of selected document has such right,
189          * providing that the document is candidate for approval and all document dependencies have already been approved.
190          * 
191          * @return true if the user has right to approve the document.
192          * @see Publication#approve()
193          * @see ValidationCycle
194          */
195         public boolean canApprove() {
196                 User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
197                 return (_user.equals(approver))
198                                 && getStudyService().canBeApproved(_operand);
199         }
200
201         /**
202          * 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
203          * all documents using it have previously been demoted. In-Draft documents can be demoted by default by both, the author of the document
204          * and the responsible of study, while documents in approval process can be demoted by their approver only.
205          * 
206          * @return true if the user has right to demote the document.
207          * @see #canInvalidate()
208          * @see #canPromote()
209          * @see Publication#demote()
210          * @see ValidationCycle
211          */
212         public boolean canDemote() {
213                 User manager = _operand.getOwnerStudy().getAuthor();
214                 User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
215                 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
216                 User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
217                 ProgressState mystate = _operand.getProgressState();
218
219                 boolean res = (((mystate != ProgressState.inDRAFT || publisher != null
220                                 || _isauthor || _user.equals(manager))
221                                 && (publisher == null || _user.equals(publisher)) || _user
222                                 .equals(reviewer))
223
224                                 && (mystate != ProgressState.inCHECK
225
226                                 || ((approver == null || _user.equals(approver) || _user
227                                                 .equals(reviewer))
228
229                                 && (approver != null || _user.equals(publisher) || (reviewer == null || _user
230                                                 .equals(reviewer))
231                                                 && (reviewer != null || _user.equals(manager)))))
232
233                 && (mystate == ProgressState.inCHECK || mystate == ProgressState.inDRAFT));
234
235                 return res;
236         }
237
238         /**
239          * 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
240          * dependencies have previously been promoted. By default, both the author of the document and the responsible of study has right to
241          * promote such document. Otherwise, only the user involved in the Promotion step of validation cycle of the selected document has such
242          * right.
243          * 
244          * @return true if the user has right to promote the document.
245          * @see #canDemote()
246          * @see Publication#promote()
247          * @see ValidationCycle
248          */
249         public boolean canPromote() {
250                 User manager = _operand.getOwnerStudy().getAuthor();
251                 User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
252
253                 if (_operand.getProgressState() != ProgressState.inWORK) {
254                         if (_operand.getProgressState() == ProgressState.inDRAFT) {
255                                 return canReview();
256                         }
257                         return false;
258                 }
259                 if (publisher == null) {
260                         if (!_isauthor && !_user.equals(manager)) {
261                                 return false;
262                         }
263                 } else {
264                         if (!_user.equals(publisher)) {
265                                 return false;
266                         }
267                 }
268                 return getStudyService().canBePromoted(_operand);
269         }
270
271         /**
272          * Checks if the user has right to validate the selected document. Only the reviewer of the type of selected document has such right,
273          * providing that the document is being reviewed and all document dependencies have already been validated.
274          * 
275          * @return true if the user has right to validate the document
276          * @see #canUnvalidate()
277          * @see Publication#review()
278          * @see ValidationCycle
279          */
280         public boolean canReview() {
281                 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
282                 boolean res = false;
283
284                 if (((reviewer == null) && ((_operand.getProgressState() == ProgressState.inWORK) || (_operand
285                                 .getProgressState() == ProgressState.inDRAFT)))
286                                 || (_user.equals(reviewer) && (_operand.getProgressState() == ProgressState.inDRAFT))) {
287                         res = getStudyService().canBeReviewed(_operand);
288                 }
289
290                 return res;
291         }
292
293         // ==============================================================================================================================
294         // Getter
295         // ==============================================================================================================================
296
297         /**
298          * Get the selected study.
299          * 
300          * @return the selected study
301          */
302         public Study getOperand() {
303                 return _operand;
304         }
305
306         /**
307          * Get the study service.
308          * 
309          * @return the study service
310          */
311         private StudyService getStudyService() {
312                 return ServiceLocatorImpl.getInstance().getStudyService();
313         }
314
315 }