Salome HOME
Modifications to respect PMD rules.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / DocumentRights.java
index 599a32c48a55283ec7c148b2e89b476101d63ca0..dfe1cf4e004d1c613df3db044f4d72173bf9eb6e 100644 (file)
@@ -29,30 +29,29 @@ import org.splat.service.ServiceLocatorImpl;
 
 public class DocumentRights {
 
-    private User             user;
-    private Publication      operand;
-    private ValidationCycle  cycle;
-    private boolean          isauthor;     // True if the user is author of the document
+    private transient final User            _user;
+    private transient Publication           _operand;
+    private transient final ValidationCycle _cycle;
+    private transient boolean               _isauthor;     // True if the user is author of the document
 
 //  ==============================================================================================================================
 //  Constructors
 //  ==============================================================================================================================
 
-    public DocumentRights (User user, Publication tag) {
-//  --------------------------------------------------
-      this.user     = user;
-      this.operand  = tag;
+    public DocumentRights (final User user, final Publication tag) {
+      this._user     = user;
+      this._operand  = tag;
 //RKV      this.cycle    = operand.getOwnerStudy().getValidationCycleOf(operand.value().getType());
-      this.cycle    = ServiceLocatorImpl.getInstance().getStudyService().getValidationCycleOf(operand.getOwnerStudy(), operand.value().getType());
-      this.isauthor = operand.value().getAuthor().equals(user);
+      this._cycle    = ServiceLocatorImpl.getInstance().getStudyService().getValidationCycleOf(_operand.getOwnerStudy(), _operand.value().getType());
+      this._isauthor = _operand.value().getAuthor().equals(user);
 //TODO: all contributors of the given document (when supported) must also behave as author
     }
-    protected DocumentRights (Publication tag) {
-//  ------------------------------------------
-      this.user     = operand.value().getAuthor();
-      this.operand  = tag;
-      this.cycle    = ServiceLocatorImpl.getInstance().getStudyService().getValidationCycleOf(operand.getOwnerStudy(), operand.value().getType());
-      this.isauthor = true;           // In order to ignore the author state in the context of any user
+    protected DocumentRights (final Publication tag) {
+      this._operand  = tag;
+      this._user     = _operand.value().getAuthor();
+      this._operand  = tag;
+      this._cycle    = ServiceLocatorImpl.getInstance().getStudyService().getValidationCycleOf(_operand.getOwnerStudy(), _operand.value().getType());
+      this._isauthor = true;           // In order to ignore the author state in the context of any user
 //TODO: all contributors of the given document (when supported) must also behave as author
     }
 
@@ -68,90 +67,101 @@ public class DocumentRights {
  * @return true if the user has right to accept the modifications of dependencies of the document.
  * @see    Publication#accept()
  */
-    public boolean canAccept () {
-//  ---------------------------
-      if (!isauthor) return false;
-      return operand.isOutdated();
-    }
+    public boolean canAccept() {
+               return _isauthor && _operand.isOutdated();
+       }
 
 /**
- * Checks if the user has right to approve the selected document.
- * Only the approver of the type of selected document has such right, providing that the document is candidate for approval and
- * all document dependencies have already been approved.
+ * Checks if the user has right to approve the selected document. Only the approver of the type of selected document has such right,
+ * providing that the document is candidate for approval and all document dependencies have already been approved.
  * 
  * @return true if the user has right to approve the document.
- * @see    Publication#approve()
- * @see    ValidationCycle
+ * @see Publication#approve()
+ * @see ValidationCycle
  */
-    public boolean canApprove () {
-//  ----------------------------
-      User  approver = cycle.getActor(ValidationStep.APPROVAL);             // May be null if not approvable
-
-      if (!user.equals(approver)) return false;
-      if (operand.getProgressState() != ProgressState.inCHECK) return false;
-      
-      List<Relation>            use = operand.value().getRelations(UsesRelation.class);
-      for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
-       Document      depend = (Document)i.next().getTo();
-       ProgressState state  = depend.getProgressState();
-       if (state == ProgressState.EXTERN)   continue;                      // External documents do not follow this progress state
-       if (state != ProgressState.APPROVED) return false;
-      }
-      return true;
-    }
+    public boolean canApprove() {
+               User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
+               boolean res = (_user.equals(approver))
+                               && (_operand.getProgressState() == ProgressState.inCHECK);
+               if (res) {
+                       List<Relation> use = _operand.value().getRelations(
+                                       UsesRelation.class);
+                       for (Iterator<Relation> i = use.iterator(); i.hasNext();) {
+                               Document depend = (Document) i.next().getTo();
+                               ProgressState state = depend.getProgressState();
+                               if (state == ProgressState.EXTERN) {
+                                       continue; // External documents do not follow this progress state
+                               }
+                               if (state != ProgressState.APPROVED) {
+                                       res = false;
+                                       break;
+                               }
+                       }
+               }
+               return res;
+       }
 
 /**
- * Checks if the user has right to attach a file to the selected document.
- * Both, the author, during the elaboration of the document, and the reviewer of the document, during the review process,
- * have such right.
+ * Checks if the user has right to attach a file to the selected document. Both, the author, during the elaboration of the document, and the
+ * reviewer of the document, during the review process, have such right.
  * 
  * @return true if the user has right to attach a file to the document.
- * @see    Publication#attach(String)
- * @see    Publication#attach(String, String)
+ * @see Publication#attach(String)
+ * @see Publication#attach(String, String)
  */
-    public boolean canAttach () {
-//  ---------------------------
-      User           manager  = operand.getOwnerStudy().getAuthor();
-      User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
-      ProgressState  state    = operand.value().getProgressState();
-
-      if (state == ProgressState.inWORK) return (isauthor);
-      else                               return (isauthor || user.equals(manager) || user.equals(reviewer));
-    }
+    public boolean canAttach() {
+               User manager = _operand.getOwnerStudy().getAuthor();
+               User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
+               ProgressState state = _operand.value().getProgressState();
+
+               return _isauthor
+                               || ((state != ProgressState.inWORK) && (_user.equals(manager) || _user
+                                               .equals(reviewer)));
+       }
 
 /**
- * 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 all documents using it have previously been demoted.
- * In-Draft documents can be demoted by default by both, the author of the document and the responsible of study, while
- * documents in approval process can be demoted by their approver only.
+ * 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 all
+ * documents using it have previously been demoted. In-Draft documents can be demoted by default by both, the author of the document and the
+ * responsible of study, while documents in approval process can be demoted by their approver only.
  * 
  * @return true if the user has right to demote the document.
- * @see    #canInvalidate()
- * @see    #canPromote()
- * @see    Publication#demote()
- * @see    ValidationCycle
+ * @see #canInvalidate()
+ * @see #canPromote()
+ * @see Publication#demote()
+ * @see ValidationCycle
  */
     public boolean canDemote () {
-//  ---------------------------
-      User           manager   = operand.getOwnerStudy().getAuthor();
-      User           publisher = cycle.getActor(ValidationStep.PROMOTION);  // Null if the default users are involved
-      User           approver  = cycle.getActor(ValidationStep.APPROVAL);   // May be null if not approvable
-      ProgressState  mystate   = operand.value().getProgressState();
+      User           manager   = _operand.getOwnerStudy().getAuthor();
+      User           publisher = _cycle.getActor(ValidationStep.PROMOTION);  // Null if the default users are involved
+      User           approver  = _cycle.getActor(ValidationStep.APPROVAL);   // May be null if not approvable
+      ProgressState  mystate   = _operand.value().getProgressState();
 
       if (mystate == ProgressState.inDRAFT) {
-        if (publisher == null) { if (!isauthor && !user.equals(manager)) return false;
-        } else                   if (!user.equals(publisher))            return false;
+        if (publisher == null) { if (!_isauthor && !_user.equals(manager)) {
+                       return false;
+               }
+        } else                   if (!_user.equals(publisher)) {
+                       return false;
+               }
       } else
       if (mystate == ProgressState.inCHECK) {
-       if (!user.equals(approver))                                      return false;
-      } else                                                             return false;
+       if (!_user.equals(approver)) {
+                       return false;
+               }
+      } else {
+               return false;
+       }
       
-      List<Relation>            use = operand.value().getRelations(UsedByRelation.class);
+      List<Relation>            use = _operand.value().getRelations(UsedByRelation.class);
       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
        Document      depend = (Document)i.next().getTo();
        ProgressState state  = depend.getProgressState();
-       if (mystate == ProgressState.inDRAFT &&  state != ProgressState.inWORK)  return false;
-       if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) return false;
+       if (mystate == ProgressState.inDRAFT &&  state != ProgressState.inWORK) {
+                       return false;
+               }
+       if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) {
+                       return false;
+               }
       }
       return true;
     }
@@ -164,17 +174,20 @@ public class DocumentRights {
  * @return true if the user has right to edit the document.
  */
     public boolean canEdit () {
-//  -------------------------
-      User           manager  = operand.getOwnerStudy().getAuthor();
-      User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
-      ProgressState  state    = operand.value().getProgressState();
+      User           manager  = _operand.getOwnerStudy().getAuthor();
+      User           reviewer = _cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
+      ProgressState  state    = _operand.value().getProgressState();
 
 //TODO: Should be restricted by the application if no editor available
       if (state == ProgressState.inWORK) {
-        if (isauthor || user.equals(manager)) return true;
+        if (_isauthor || _user.equals(manager)) {
+                       return true;
+               }
       } else
       if (state == ProgressState.inDRAFT) {
-        if (user.equals(reviewer))            return true;
+        if (_user.equals(reviewer)) {
+                       return true;
+               }
       }
       return false;
     }
@@ -191,20 +204,29 @@ public class DocumentRights {
  * @see    ValidationCycle
  */
     public boolean canPromote () {
-//  ----------------------------
-      User manager   = operand.getOwnerStudy().getAuthor();
-      User publisher = cycle.getActor(ValidationStep.PROMOTION);            // Null if the default users are involved
-
-      if (operand.getProgressState() != ProgressState.inWORK)          return false;
-      if (publisher == null) { if (!isauthor && !user.equals(manager)) return false;
-      } else {                 if (!user.equals(publisher))            return false;
+      User manager   = _operand.getOwnerStudy().getAuthor();
+      User publisher = _cycle.getActor(ValidationStep.PROMOTION);            // Null if the default users are involved
+
+      if (_operand.getProgressState() != ProgressState.inWORK) {
+               return false;
+       }
+      if (publisher == null) { if (!_isauthor && !_user.equals(manager)) {
+               return false;
+       }
+      } else {                 if (!_user.equals(publisher)) {
+               return false;
+       }
       }
-      List<Relation>            use = operand.value().getRelations(UsesRelation.class);
+      List<Relation>            use = _operand.value().getRelations(UsesRelation.class);
       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
        Document      depend = (Document)i.next().getTo();
        ProgressState state  = depend.getProgressState();
-       if (state == ProgressState.EXTERN) continue;                        // External documents do not follow this progress state
-       if (state == ProgressState.inWORK) return false;
+       if (state == ProgressState.EXTERN) {
+                       continue;                        // External documents do not follow this progress state
+               }
+       if (state == ProgressState.inWORK) {
+                       return false;
+               }
       }
       return true;
     }
@@ -216,13 +238,18 @@ public class DocumentRights {
  * @return true if the user has right to purge the document.
  */
     public boolean canPurge () {
-//  --------------------------
-      User      manager = operand.getOwnerStudy().getAuthor();
-      Document  doc     = operand.value();
+      User      manager = _operand.getOwnerStudy().getAuthor();
+      Document  doc     = _operand.value();
       
-      if (!user.equals(manager))                                return false;
-      if (doc.isShared())                                       return false;
-      if (doc.getFirstRelation(VersionsRelation.class) == null) return false;
+      if (!_user.equals(manager)) {
+               return false;
+       }
+      if (doc.isShared()) {
+               return false;
+       }
+      if (doc.getFirstRelation(VersionsRelation.class) == null) {
+               return false;
+       }
       return true;
     }
 
@@ -236,15 +263,18 @@ public class DocumentRights {
  * @see    Step#removeDocument(Publication)
  */
     public boolean canRemove () {
-//  ---------------------------
-      User          manager = operand.getOwnerStudy().getAuthor();
-      ProgressState state   = operand.getProgressState();
-
-      if (!isauthor && !user.equals(manager))                             return false;
-      if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
-
-      List<Publication> using = operand.getRelations(UsedByRelation.class);
-      return (using.size() == 0);
+      User          manager = _operand.getOwnerStudy().getAuthor();
+      ProgressState state   = _operand.getProgressState();
+
+      if (!_isauthor && !_user.equals(manager)) {
+               return false;
+       }
+      if (state != ProgressState.inWORK && state != ProgressState.EXTERN) {
+               return false;
+       }
+
+      List<Publication> using = _operand.getRelations(UsedByRelation.class);
+      return using.isEmpty();
     }
 
 /**
@@ -255,12 +285,15 @@ public class DocumentRights {
  * @see    Publication#rename(String)
  */
     public boolean canRename () {
-//  ---------------------------
-      ProgressState state = operand.getProgressState();
-
-      if (!isauthor) return false;     // In case of external document, the author is the one who has imported the document.
-      if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
-      return (!operand.value().isShared());
+      ProgressState state = _operand.getProgressState();
+
+      if (!_isauthor) {
+               return false;     // In case of external document, the author is the one who has imported the document.
+       }
+      if (state != ProgressState.inWORK && state != ProgressState.EXTERN) {
+               return false;
+       }
+      return (!_operand.value().isShared());
     }
 
 /**
@@ -271,13 +304,16 @@ public class DocumentRights {
  * @return true if the user has right to replace the document.
  */
     public boolean canReplace () {
-//  ----------------------------
-      User          manager = operand.getOwnerStudy().getAuthor();
-      ProgressState state   = operand.getProgressState();
-
-      if (!isauthor && !user.equals(manager)) return false;                 // Supposed to work also in case of external document.
-      if (state != ProgressState.inWORK && state != ProgressState.EXTERN) return false;
-      return !operand.value().isShared();
+      User          manager = _operand.getOwnerStudy().getAuthor();
+      ProgressState state   = _operand.getProgressState();
+
+      if (!_isauthor && !_user.equals(manager)) {
+               return false;                 // Supposed to work also in case of external document.
+       }
+      if (state != ProgressState.inWORK && state != ProgressState.EXTERN) {
+               return false;
+       }
+      return !_operand.value().isShared();
     }
 
 /**
@@ -291,18 +327,25 @@ public class DocumentRights {
  * @see    ValidationCycle
  */
     public boolean canReview () {
-//  ---------------------------
-      User  reviewer = cycle.getActor(ValidationStep.REVIEW);               // May be null if not reviewable
-
-      if (!user.equals(reviewer))                              return false;
-      if (operand.getProgressState() != ProgressState.inDRAFT) return false;
+      User  reviewer = _cycle.getActor(ValidationStep.REVIEW);               // May be null if not reviewable
+
+      if (!_user.equals(reviewer)) {
+               return false;
+       }
+      if (_operand.getProgressState() != ProgressState.inDRAFT) {
+               return false;
+       }
       
-      List<Relation>            use = operand.value().getRelations(UsesRelation.class);
+      List<Relation>            use = _operand.value().getRelations(UsesRelation.class);
       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
        Document      depend = (Document)i.next().getTo();
        ProgressState state  = depend.getProgressState();
-       if (state == ProgressState.EXTERN) continue;                        // External documents do not follow this progress state
-       if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) return false;
+       if (state == ProgressState.EXTERN) {
+                       continue;                        // External documents do not follow this progress state
+               }
+       if (state == ProgressState.inWORK || state == ProgressState.inDRAFT) {
+                       return false;
+               }
       }
       return true;
     }
@@ -318,19 +361,26 @@ public class DocumentRights {
  * @see    ValidationCycle
  */
     public boolean canInvalidate () {
-//  -------------------------------
-      User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
-      ProgressState  mystate  = operand.value().getProgressState();
-
-      if (mystate != ProgressState.inCHECK)    return false;
-      if (!isauthor && !user.equals(reviewer)) return false;
+      User           reviewer = _cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
+      ProgressState  mystate  = _operand.value().getProgressState();
+
+      if (mystate != ProgressState.inCHECK) {
+               return false;
+       }
+      if (!_isauthor && !_user.equals(reviewer)) {
+               return false;
+       }
         
-      List<Relation>            use = operand.value().getRelations(UsedByRelation.class);
+      List<Relation>            use = _operand.value().getRelations(UsedByRelation.class);
       for (Iterator<Relation> i=use.iterator(); i.hasNext();) {
         Document      depend = (Document)i.next().getTo();
         ProgressState state  = depend.getProgressState();
-        if (mystate == ProgressState.inDRAFT &&  state != ProgressState.inWORK)  return false;
-        if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) return false;
+        if (mystate == ProgressState.inDRAFT &&  state != ProgressState.inWORK) {
+                       return false;
+               }
+        if (mystate == ProgressState.inCHECK && (state != ProgressState.inDRAFT && state != ProgressState.inWORK)) {
+                       return false;
+               }
       }
       return true;
     }
@@ -347,19 +397,24 @@ public class DocumentRights {
  * @see    Step#versionDocument(Publication, String)
  */
     public boolean canVersion () {
-//  ----------------------------
-      User           manager  = operand.getOwnerStudy().getAuthor();
-      User           reviewer = cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
-      ProgressState  state    = operand.value().getProgressState();
+      User           manager  = _operand.getOwnerStudy().getAuthor();
+      User           reviewer = _cycle.getActor(ValidationStep.REVIEW);      // May be null if not reviewable
+      ProgressState  state    = _operand.value().getProgressState();
 
       if (state == ProgressState.inWORK) {
-        if (isauthor || user.equals(manager)) return true;
+        if (_isauthor || _user.equals(manager)) {
+                       return true;
+               }
       } else
       if (state == ProgressState.inDRAFT) {
-        if (user.equals(reviewer))            return true;
+        if (_user.equals(reviewer)) {
+                       return true;
+               }
       } else
       if (state == ProgressState.APPROVED) {
-       if (isauthor)                         return true;
+       if (_isauthor) {
+                       return true;
+               }
       }
       return false;
  }
@@ -374,7 +429,6 @@ public class DocumentRights {
  * @return the document subject of checks.
  */
     public Document getOperand () {
-//  -----------------------------
-      return operand.value();
+      return _operand.value();
     }
 }
\ No newline at end of file