Salome HOME
049f88f9835e5e0ea34caab8560864e6ef68b01f
[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                 if (mystate == ProgressState.inDRAFT) {
220                         if (publisher == null) {
221                                 if ((!_isauthor) && (!_user.equals(manager))
222                                                 && (!_user.equals(reviewer))) {
223                                         return false;
224                                 }
225                         } else if ((!_user.equals(publisher)) && (!_user.equals(reviewer))) {
226                                 return false;
227                         }
228                 } else if (mystate == ProgressState.inCHECK) {
229                         if (!_user.equals(approver)) {
230                                 return false;
231                         }
232                 } else {
233                         return false;
234                 }
235
236                 return true;
237         }
238
239         /**
240          * 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
241          * dependencies have previously been promoted. By default, both the author of the document and the responsible of study has right to
242          * promote such document. Otherwise, only the user involved in the Promotion step of validation cycle of the selected document has such
243          * right.
244          * 
245          * @return true if the user has right to promote the document.
246          * @see #canDemote()
247          * @see Publication#promote()
248          * @see ValidationCycle
249          */
250         public boolean canPromote() {
251                 User manager = _operand.getOwnerStudy().getAuthor();
252                 User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
253
254                 if (_operand.getProgressState() != ProgressState.inWORK) {
255                         if (_operand.getProgressState() == ProgressState.inDRAFT) {
256                                 return canReview();
257                         }
258                         return false;
259                 }
260                 if (publisher == null) {
261                         if (!_isauthor && !_user.equals(manager)) {
262                                 return false;
263                         }
264                 } else {
265                         if (!_user.equals(publisher)) {
266                                 return false;
267                         }
268                 }
269                 return getStudyService().canBePromoted(_operand);
270         }
271
272         /**
273          * Checks if the user has right to validate the selected document. Only the reviewer of the type of selected document has such right,
274          * providing that the document is being reviewed and all document dependencies have already been validated.
275          * 
276          * @return true if the user has right to validate the document
277          * @see #canUnvalidate()
278          * @see Publication#review()
279          * @see ValidationCycle
280          */
281         public boolean canReview() {
282                 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
283
284                 if (!_user.equals(reviewer)) {
285                         return false;
286                 }
287                 if (_operand.getProgressState() != ProgressState.inDRAFT) {
288                         return false;
289                 }
290
291                 return getStudyService().canBeReviewed(_operand);
292         }
293
294         // ==============================================================================================================================
295         // Getter
296         // ==============================================================================================================================
297
298         /**
299          * Get the selected study.
300          * 
301          * @return the selected study
302          */
303         public Study getOperand() {
304                 return _operand;
305         }
306
307         /**
308          * Get the study service.
309          * 
310          * @return the study service
311          */
312         private StudyService getStudyService() {
313                 return ServiceLocatorImpl.getInstance().getStudyService();
314         }
315
316 }