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.
8 * @author Daniel Brunier-Coulin
9 * @copyright OPEN CASCADE 2012
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;
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;
31 * The class defining user rights to the document.
33 public class DocumentRights {
38 private transient final User _user;
40 * The document publication.
42 private transient Publication _operand;
44 * The document validation cycle.
46 private transient final ValidationCycle _cycle;
48 * True if the user is author of the document.
50 private final transient boolean _isauthor;
52 // ==============================================================================================================================
54 // ==============================================================================================================================
62 * the document publication
64 public DocumentRights(final User user, final Publication 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
75 * The constructor in the case when the user is an author of the document.
78 * the document publication
80 protected DocumentRights(final Publication tag) {
82 this._user = _operand.value().getAuthor();
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
90 // ==============================================================================================================================
91 // Public member functions
92 // ==============================================================================================================================
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.
98 * @return true if the user has right to accept the modifications of dependencies of the document.
99 * @see Publication#accept()
101 public boolean canAccept() {
102 return _isauthor && _operand.isOutdated();
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.
109 * @return true if the user has right to approve the document.
110 * @see Publication#approve()
111 * @see ValidationCycle
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);
118 List<Relation> use = _operand.value().getRelations(
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
126 if (state != ProgressState.APPROVED) {
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.
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)
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();
149 || ((state != ProgressState.inWORK) && (_user.equals(manager) || _user
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.
158 * @return true if the user has right to demote the document.
159 * @see #canInvalidate()
161 * @see Publication#demote()
162 * @see ValidationCycle
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();
171 if (mystate == ProgressState.inDRAFT) {
172 if (publisher == null) {
173 if ((!_isauthor) && (!_user.equals(manager))
174 && (!_user.equals(reviewer))) {
177 } else if ((!_user.equals(publisher)) && (!_user.equals(reviewer))) {
180 } else if (mystate == ProgressState.inCHECK) {
181 if (!_user.equals(approver)) {
188 List<Relation> use = _operand.value()
189 .getRelations(UsedByRelation.class);
190 for (Iterator<Relation> i = use.iterator(); i.hasNext();) {
191 Document depend = (Document) i.next().getTo();
192 ProgressState state = depend.getProgressState();
193 if (mystate == ProgressState.inDRAFT
194 && state != ProgressState.inWORK) {
197 if (mystate == ProgressState.inCHECK
198 && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) {
206 * 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
207 * of the document and the responsible of study, while documents In-Draft can be checked-out by the reviewer only.
209 * @return true if the user has right to edit the document.
211 public boolean canEdit() {
212 User manager = _operand.getOwnerStudy().getAuthor();
213 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
214 ProgressState state = _operand.value().getProgressState();
216 // TODO: Should be restricted by the application if no editor available
217 if (state == ProgressState.inWORK) {
218 if (_isauthor || _user.equals(manager)) {
221 } else if (state == ProgressState.inDRAFT) {
222 if (_user.equals(reviewer)) {
230 * 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
231 * dependencies have previously been promoted. By default, both the author of the document and the responsible of study has right to
232 * promote such document. Otherwise, only the user involved in the Promotion step of validation cycle of the selected document has such
235 * @return true if the user has right to promote the document.
237 * @see Publication#promote()
238 * @see ValidationCycle
240 public boolean canPromote() {
241 User manager = _operand.getOwnerStudy().getAuthor();
242 User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
244 if (_operand.getProgressState() != ProgressState.inWORK) {
245 if (_operand.getProgressState() == ProgressState.inDRAFT) {
250 if (publisher == null) {
251 if (!_isauthor && !_user.equals(manager)) {
255 if (!_user.equals(publisher)) {
259 List<Relation> use = _operand.value().getRelations(UsesRelation.class);
260 for (Iterator<Relation> i = use.iterator(); i.hasNext();) {
261 Document depend = (Document) i.next().getTo();
262 ProgressState state = depend.getProgressState();
263 if (state == ProgressState.EXTERN) {
264 continue; // External documents do not follow this progress state
266 if (state == ProgressState.inWORK) {
274 * Checks if the user has right to remove the history of the selected document, if exists. Only the responsible of the study have such
277 * @return true if the user has right to purge the document.
279 public boolean canPurge() {
280 User manager = _operand.getOwnerStudy().getAuthor();
281 Document doc = _operand.value();
283 if (!_user.equals(manager)) {
286 if (doc.isShared()) {
289 if (doc.getFirstRelation(VersionsRelation.class) == null) {
296 * Checks if the user has right to remove the selected document from the study. Both, the author of the document and the responsible of
297 * the study, have such right, providing that: - the document is neither in review or in approval process - the document is not used by
300 * @return true if the user has right to remove the document.
301 * @see Step#removeDocument(Publication)
303 public boolean canRemove() {
304 User manager = _operand.getOwnerStudy().getAuthor();
305 ProgressState state = _operand.getProgressState();
307 if (!_isauthor && !_user.equals(manager)) {
310 if (state != ProgressState.inWORK && state != ProgressState.EXTERN) {
314 List<Publication> using = _operand.getRelations(UsedByRelation.class);
315 return using.isEmpty();
319 * Checks if the user has right to rename the selected document. Only the author of the document has such right, providing that the
320 * document is neither in review nor in approval process.
322 * @return true if the user has right to rename the document.
323 * @see Publication#rename(String)
325 public boolean canRename() {
326 ProgressState state = _operand.getProgressState();
329 return false; // In case of external document, the author is the one who has imported the document.
331 if (state != ProgressState.inWORK && state != ProgressState.EXTERN) {
334 return (!_operand.value().isShared());
338 * Checks if the user has right to replace the source file of the selected document. Both, the author of the document and the
339 * responsible of study has such right, providing that the document is neither in review nor in approval process.
341 * @return true if the user has right to replace the document.
343 public boolean canReplace() {
344 User manager = _operand.getOwnerStudy().getAuthor();
345 ProgressState state = _operand.getProgressState();
347 if (!_isauthor && !_user.equals(manager)) {
348 return false; // Supposed to work also in case of external document.
350 if (state != ProgressState.inWORK && state != ProgressState.EXTERN) {
353 return !_operand.value().isShared();
357 * Checks if the user has right to validate the selected document. Only the reviewer of the type of selected document has such right,
358 * providing that the document is being reviewed and all document dependencies have already been validated.
360 * @return true if the user has right to validate the document
361 * @see #canUnvalidate()
362 * @see Publication#review()
363 * @see ValidationCycle
365 public boolean canReview() {
366 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
368 if (!_user.equals(reviewer)) {
371 if (_operand.getProgressState() != ProgressState.inDRAFT) {
375 List<Relation> use = _operand.value().getRelations(UsesRelation.class);
376 for (Iterator<Relation> i = use.iterator(); i.hasNext();) {
377 Document depend = (Document) i.next().getTo();
378 ProgressState state = depend.getProgressState();
379 if (state == ProgressState.EXTERN) {
380 continue; // External documents do not follow this progress state
382 if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) {
390 * Checks if the user has right to undo the validation operation of the selected document. Both, the author and the reviewer of a
391 * validated document, have such right.
393 * @return true if the user has right to undo the validation operation
396 * @see Publication#invalidate()
397 * @see ValidationCycle
399 public boolean canInvalidate() {
400 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
401 ProgressState mystate = _operand.value().getProgressState();
403 if (mystate != ProgressState.inCHECK) {
406 if (!_isauthor && !_user.equals(reviewer)) {
410 List<Relation> use = _operand.value()
411 .getRelations(UsedByRelation.class);
412 for (Iterator<Relation> i = use.iterator(); i.hasNext();) {
413 Document depend = (Document) i.next().getTo();
414 ProgressState state = depend.getProgressState();
415 if (mystate == ProgressState.inDRAFT
416 && state != ProgressState.inWORK) {
419 if (mystate == ProgressState.inCHECK
420 && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) {
428 * Checks if the user has right to version the selected document. In-Work documents can be versioned by both, the author of the document
429 * and the responsible of study, while documents In-Draft can be versioned by the reviewer only. Additionally, Approved documents can
430 * also be versioned by their author in order to enter in a new modification validation cycle.
432 * @return true if the user has right to version the document.
433 * @see Step#versionDocument(Publication)
434 * @see Step#versionDocument(Publication, Document.Properties)
435 * @see Step#versionDocument(Publication, String)
437 public boolean canVersion() {
438 User manager = _operand.getOwnerStudy().getAuthor();
439 User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
440 ProgressState state = _operand.value().getProgressState();
442 if (state == ProgressState.inWORK) {
443 if (_isauthor || _user.equals(manager)) {
446 } else if (state == ProgressState.inDRAFT) {
447 if (_user.equals(reviewer)) {
450 } else if (state == ProgressState.APPROVED) {
455 } else if (state == ProgressState.EXTERN) {
456 if (_isauthor || _user.equals(manager)) {
464 // ==============================================================================================================================
466 // ==============================================================================================================================
469 * Returns the document subject of checks according to this user rights.
471 * @return the document subject of checks.
473 public Document getOperand() {
474 return _operand.value();
478 * Get the study service.
480 * @return the study service
482 private StudyService getStudyService() {
483 return ServiceLocatorImpl.getInstance().getStudyService();