Salome HOME
e869784cefaf14a89a618f5f48c5d58495d1a48d
[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                 int roleIndex = _user.getRole().getName().indexOf("knowledgineer");             
120                 return (_operand.getProgressState() == ProgressState.APPROVED && !_operand
121                                 .isPublic() && (roleIndex > -1));
122         }
123
124         /**
125          * 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
126          * have such right.
127          * 
128          * @return true if the user has right to edit the description.
129          */
130         public boolean canProtect() {
131                 int roleIndex = _user.getRole().getName().indexOf("knowledgineer");
132                 return (_operand.getProgressState() == ProgressState.APPROVED && _operand
133                                 .isPublic() && (roleIndex > -1));
134         }
135
136         /**
137          * Check if the user can remove old versions.
138          * 
139          * @return true if the user can remove old versions
140          */
141         public boolean canPurge() {
142                 return (_isauthor && _operand.isVersioned());
143         }
144
145         /**
146          * Check if the user can remove the study.
147          * 
148          * @return true if the user can remove the study
149          */
150         public boolean canRemove() {
151                 return (_operand.getProgressState() == ProgressState.inWORK || _operand
152                                 .getProgressState() == ProgressState.inDRAFT)
153                                 && _isauthor;
154         }
155         
156         /**
157          * Check if the user can remove the selected scenario.
158          * 
159          * @return true if the user can remove the scenario
160          */
161         public boolean canRemoveScenario() {
162                 return (_operand.getProgressState() == ProgressState.inWORK 
163                         || _operand.getProgressState() == ProgressState.inDRAFT)
164                         && _isauthor && _operand.getScenariiList().size() > 1;
165         }
166
167         /**
168          * Check if the user can version the study.
169          * 
170          * @return true if the user can version the study
171          */
172         public boolean canVersion() {
173                 return (_operand.getProgressState() == ProgressState.inWORK || _operand
174                                 .getProgressState() == ProgressState.inDRAFT)
175                                 && getStudyService().isStaffedBy(_operand, _user);
176         }
177
178         /**
179          * Can the given study be marked as reference or not.
180          * 
181          * @return true/false.
182          */
183         public boolean canMarkStudyAsReference() {
184                 int roleIndex = _user.getRole().getName().indexOf("knowledgineer");
185                 return (_operand.getProgressState() == ProgressState.APPROVED  && (roleIndex > -1));
186         }
187
188         /**
189          * Can the given study be unmarked as reference or not.
190          * 
191          * @return true/false.
192          */
193         public boolean canRemoveStudyAsReference() {
194                 int roleIndex = _user.getRole().getName().indexOf("knowledgineer");
195                 return (_operand.getProgressState() == ProgressState.TEMPLATE  && (roleIndex > -1));
196         }
197
198         // ==========================================================================
199         // Operations from document validation cycle
200         // ==========================================================================
201
202         /**
203          * Checks if the user has right to approve the selected document. Only the approver of the type of selected document has such right,
204          * providing that the document is candidate for approval and all document dependencies have already been approved.
205          * 
206          * @return true if the user has right to approve the document.
207          * @see Publication#approve()
208          * @see ValidationCycle
209          */
210         public boolean canApprove() {
211                 User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
212                 return (_user.equals(approver) || _isauthor)
213                                 && getStudyService().canBeApproved(_operand);
214         }
215
216         /**
217          * 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
218          * all documents using it have previously been demoted. In-Draft documents can be demoted by default by both, the author of the document
219          * and the responsible of study, while documents in approval process can be demoted by their approver only.
220          * 
221          * @return true if the user has right to demote the document.
222          * @see #canInvalidate()
223          * @see #canPromote()
224          * @see Publication#demote()
225          * @see ValidationCycle
226          */
227         public boolean canDemote() {
228                 User manager = _operand.getOwnerStudy().getAuthor();
229                 User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
230                 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
231                 User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
232                 ProgressState mystate = _operand.getProgressState();
233
234                 boolean res = (mystate == ProgressState.inDRAFT
235                                 && (_user.equals(publisher) || _user.equals(reviewer)
236                                                 || _user.equals(manager) || (reviewer == null)
237                                                 && _user.equals(approver)) || mystate == ProgressState.inCHECK
238                                 && (_user.equals(approver) || _user.equals(reviewer) || (reviewer == null)
239                                                 && (_user.equals(publisher) || _user.equals(manager))));
240
241                 return res;
242         }
243
244         /**
245          * 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
246          * dependencies have previously been promoted. By default, both the author of the document and the responsible of study has right to
247          * promote such document. Otherwise, only the user involved in the Promotion step of validation cycle of the selected document has such
248          * right.
249          * 
250          * @return true if the user has right to promote the document.
251          * @see #canDemote()
252          * @see Publication#promote()
253          * @see ValidationCycle
254          */
255         public boolean canPromote() {
256                 User manager = _operand.getOwnerStudy().getAuthor();
257                 User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
258
259                 if (_operand.getProgressState() != ProgressState.inWORK) {
260                         if (_operand.getProgressState() == ProgressState.inDRAFT) {
261                                 return canReview();
262                         }
263                         return false;
264                 }
265                 if (publisher == null) {
266                         if (!_isauthor && !_user.equals(manager)) {
267                                 return false;
268                         }
269                 } else {
270                         if (!_user.equals(publisher)) {
271                                 return false;
272                         }
273                 }
274                 return getStudyService().canBePromoted(_operand);
275         }
276
277         /**
278          * Checks if the user has right to validate the selected document. Only the reviewer of the type of selected document has such right,
279          * providing that the document is being reviewed and all document dependencies have already been validated.
280          * 
281          * @return true if the user has right to validate the document
282          * @see #canUnvalidate()
283          * @see Publication#review()
284          * @see ValidationCycle
285          */
286         public boolean canReview() {
287                 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
288                 boolean res = false;
289
290                 if (((reviewer == null) && ((_operand.getProgressState() == ProgressState.inWORK) || (_operand
291                                 .getProgressState() == ProgressState.inDRAFT)))
292                                 || (_user.equals(reviewer) && (_operand.getProgressState() == ProgressState.inDRAFT))) {
293                         res = getStudyService().canBeReviewed(_operand);
294                 }
295
296                 return res;
297         }
298         
299         /**
300          * Checks if the user has right to rename a scenario of the study.
301          * 
302          * @return true if user in an author, contributor or a validation cycle member of the study.
303          */
304         public boolean canRenameScenario() {
305                 return _isauthor || getStudyService().hasActor(_operand, _user);
306         }
307
308         // ==============================================================================================================================
309         // Getter
310         // ==============================================================================================================================
311
312         /**
313          * Get the selected study.
314          * 
315          * @return the selected study
316          */
317         public Study getOperand() {
318                 return _operand;
319         }
320
321         /**
322          * Get the study service.
323          * 
324          * @return the study service
325          */
326         private StudyService getStudyService() {
327                 return ServiceLocatorImpl.getInstance().getStudyService();
328         }
329
330 }