Salome HOME
Creation of a new study from an existing one is implemented.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / ValidationCycle.java
index 8803d52e3799926583b07e4529ac16f471eaace4..f08829bea1d5acc4455e653f6126e0ab01931ece 100644 (file)
@@ -1,4 +1,5 @@
 package org.splat.dal.bo.som;
+
 /**
  * Class defining the validation cycle applicable to documents of a given type.<br/>
  * A validation cycle specifies the validation steps of documents complying with by this cycle, as well as the actors
@@ -26,256 +27,320 @@ package org.splat.dal.bo.som;
  * @copyright OPEN CASCADE 2012
  */
 
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Vector;
 
 import org.splat.dal.bo.kernel.Persistent;
 import org.splat.dal.bo.kernel.User;
-import org.splat.dal.dao.som.Database;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
-import org.splat.kernel.UserDirectory;
-import org.splat.service.technical.ProjectSettingsServiceImpl;
 
 public class ValidationCycle extends Persistent {
 
-       private  ValidationCycleRelation context;
-    private  DocumentType            mytype;      // Null if the referenced validation cycle is a default one
-       private  User                    publisher;
-    private  User                    reviewer;    // Null if no REVIEW validation step
-    private  User                    approver;    // Null if no APPROVAL validation step
-    private  User                    signatory;   // Null if no ACCEPTANCE validation step
-
-    public enum Actor {
-      manager,                                    // Responsible of study 
-      Nx1,                                        // N+1 manager of the responsible of study
-      Nx2,                                        // N+2 manager of the responsible of study
-      customer                                    // Customer
-    }
-
-//  ==============================================================================================================================
-//  Construction
-//  ==============================================================================================================================
-    
-//  Fields initialization class
-    public static class Properties extends Persistent.Properties {
-//  ------------------------------------------------------------
-      DocumentType doctype   = null;
-      User         publisher = null;
-      User         reviewer  = null;
-      User         approver  = null;
-      User         signatory = null;
-
-//  - Public services
-
-      public void clear () {
-        super.clear();
-        doctype   = null;
-        publisher = null;
-        reviewer  = null;
-        approver  = null;
-        signatory = null;
-      }
-//  - Protected services
-
-      public Properties setDocumentType (DocumentType type)
-      {
-        doctype = type;
-       return this;
-      }
-//  - Properties setter
-
-      public Properties setActor (ValidationStep step, User actor)
-      {
-        if      (step == ValidationStep.PROMOTION) publisher = actor;
-        else if (step == ValidationStep.REVIEW)    reviewer  = actor;
-        else if (step == ValidationStep.APPROVAL)  approver  = actor;
-        else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = actor;
-        return this;
-      }
-//  - Global validity check
-        
-      public void checkValidity() throws MissedPropertyException
-      { 
-        if (doctype == null) throw new MissedPropertyException("type");
-      }
-    }
-//  Database fetch constructor
-    protected ValidationCycle () {
-    }
-//  Internal constructors
-    protected ValidationCycle (Study from, ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle) {
-//  -----------------------------------------------------------------------------
-      Actor[]         actype = cycle.getActorTypes();
-      User.Properties uprop  = new User.Properties();
-
-      mytype  = Document.selectType(cycle.getName());     // Null in case of default validation cycle
-//    context = new ValidationCycleRelation(from, this);
-      context = null;                                     // Validation cycle defined in the workflow
-      for (int i=0; i<actype.length; i++) {
-        User actor = null;
-        if (actype[i] != null)
-        try {
-          if (actype[i] == Actor.manager) {
-            actor = from.getAuthor();
-          } else
-          if (actype[i] == Actor.Nx1) {
-            List<User> manager = UserDirectory.selectUsersWhere(uprop.setOrganizationName("Nx1"));
-            if (manager.size() == 1) actor = manager.get(0);
-          } else
-          if (actype[i] == Actor.Nx2) {
-            List<User> manager = UserDirectory.selectUsersWhere(uprop.setOrganizationName("Nx2"));
-            if (manager.size() == 1) actor = manager.get(0);
-          } else {      /* Actor.customer */
-            actor = from.getAuthor();
-//TODO: Get the customer of the study, if exists
-          }
-        } catch (Exception e) {      // Should not happen
-          actor = null;
-        }
-        if      (i == 0) reviewer  = actor;
-        else if (i == 1) approver  = actor;
-        else if (i == 2) signatory = actor;
-      }
-    }
-    public ValidationCycle (Study from, Properties vprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
-//  --------------------------------------------------------
-      super(vprop);                  // Throws one of the above exception if not valid
-      mytype    = vprop.doctype;
-      publisher = vprop.publisher;   // May be null
-      reviewer  = vprop.reviewer;    // May be null
-      approver  = vprop.approver;    // May be null
-      signatory = vprop.signatory;   // May be null
-      context   = new ValidationCycleRelation(from, this);
-    }
-
-//  ==============================================================================================================================
-//  Public member functions
-//  ==============================================================================================================================
-/**
- * Checks if a given validation step is enabled in this validation cycle.
- * 
- * @param  step the validation step checked.
- * @return true if the given validation step is enabled.
- */
-    public boolean enables (ValidationStep step) {
-//  -------------------------------------------
-      if      (step == ValidationStep.PROMOTION) return true;
-      else if (step == ValidationStep.REVIEW)    return (reviewer  != null);
-      else if (step == ValidationStep.APPROVAL)  return (approver  != null);
-      else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) return (signatory != null);
-      return false;
-    }
+       private ValidationCycleRelation context;
+       private DocumentType mytype; // Null if the referenced validation cycle is a default one
+       private User publisher;
+       private User reviewer; // Null if no REVIEW validation step
+       private User approver; // Null if no APPROVAL validation step
+       private User signatory; // Null if no ACCEPTANCE validation step
 
-/**
- * Returns the user involved in a given step of this document validation cycle.
- * When enabled, the Review and Approval steps have one explicit actor returned by this function. On the other hand,
- * Promotion and Acceptance have default actors - they respectively are the author of the document or the responsible of study,
- * and the customer or its representative internal user. In this context, a null user is returned.
- * 
- * @param  step the validation step
- * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
- * @see    #getAllActors()
- * @see    #enables
- */
-    public User getActor (ValidationStep step) {
-//  -----------------------------------------
-      if      (step == ValidationStep.PROMOTION) return publisher;
-      else if (step == ValidationStep.REVIEW)    return reviewer;
-      else if (step == ValidationStep.APPROVAL)  return approver;
-      else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) return signatory;
-      return null;
-    }
+       public enum Actor {
+               manager, // Responsible of study
+               Nx1, // N+1 manager of the responsible of study
+               Nx2, // N+2 manager of the responsible of study
+               customer
+               // Customer
+       }
 
-/**
- * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding
- * the default actors.
- * 
- * @return the users explicitly involved by the steps of this validation cycle
- * @see    #getActor(ValidationStep)
- * @see    #enables(ValidationStep)
- */
-    public User[] getAllActors () {
-//  -----------------------------
-      Vector<User> result = new Vector<User>();
-      if (publisher != null) result.add(publisher);
-      if (reviewer  != null) result.add(reviewer);
-      if (approver  != null) result.add(approver);
-      if (signatory != null) result.add(signatory);
-      return  result.toArray(new User[result.size()]);
-    }
+       // ==============================================================================================================================
+       // Construction
+       // ==============================================================================================================================
 
-/**
- * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document
- * type is not defined.
- * 
- * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
- * @see    #isDefault()
- */
-    public DocumentType getDocumentType () {
-//  --------------------------------------
-      return mytype;                    // May be null
-    }
+       // Fields initialization class
+       public static class Properties extends Persistent.Properties {
+               DocumentType doctype = null;
+               User publisher = null;
+               User reviewer = null;
+               User approver = null;
+               User signatory = null;
 
-/**
- * Checks if this validation cycle is assigned to a study. If not, it is common to all studies of the workflow in which it has
- * been defined.
- * 
- * @return true if this validation cycle is assigned to a study.
- */
-    public boolean isAssigned () {
-//  ----------------------------
-      return (context != null);
-    }
+               // - Public services
 
-/**
- * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or
- * built-in.<br/>
- * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step
- * other than Promotion.
- * 
- * @return true if this validation cycle is a default one.
- * @see    #getDocumentType()
- * @see    ProjectSettingsServiceImpl#getNewValidationCycle()
- */
-    public boolean isDefault () {
-//  ---------------------------
-     return (mytype == null);
-    }
-
-//  ==============================================================================================================================
-//  Protected services
-//  ==============================================================================================================================
-
-    public ValidationCycleRelation getContext () {
-//  -----------------------------------------------
-      return context;
-    }
-
-    protected void remove (ValidationStep step) {
-//  ------------------------------------------
-      if      (step == ValidationStep.REVIEW)     reviewer  = null;
-      else if (step == ValidationStep.APPROVAL)   approver  = null;
-      else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = null;
-      if (this.isSaved()) Database.getSession().update(this);
-    }
-
-    public void resetActors (Properties vprop) {
-//  ---------------------------------------------
-      publisher = vprop.publisher;   // May be null
-      reviewer  = vprop.reviewer;    // May be null
-      approver  = vprop.approver;    // May be null
-      signatory = vprop.signatory;   // May be null
-      if (this.isSaved()) Database.getSession().update(this);
-    }
-
-    protected void setActor (ValidationStep step, User actor) {
-//  --------------------------------------------------------
-      if      (step == ValidationStep.PROMOTION) publisher = actor;
-      else if (step == ValidationStep.REVIEW)    reviewer  = actor;
-      else if (step == ValidationStep.APPROVAL)  approver  = actor;
-      else if (step == ValidationStep.ACCEPTANCE || step == ValidationStep.REFUSAL) signatory = actor;
-      if (this.isSaved()) Database.getSession().update(this);
-    }
+               @Override
+               public void clear() {
+                       super.clear();
+                       doctype = null;
+                       publisher = null;
+                       reviewer = null;
+                       approver = null;
+                       signatory = null;
+               }
+
+               // - Protected services
+
+               public Properties setDocumentType(final DocumentType type) {
+                       doctype = type;
+                       return this;
+               }
+
+               // - Properties setter
+
+               public Properties setActor(final ValidationStep step, final User actor) {
+                       if (step == ValidationStep.PROMOTION) {
+                               publisher = actor;
+                       } else if (step == ValidationStep.REVIEW) {
+                               reviewer = actor;
+                       } else if (step == ValidationStep.APPROVAL) {
+                               approver = actor;
+                       } else if (step == ValidationStep.ACCEPTANCE
+                                       || step == ValidationStep.REFUSAL) {
+                               signatory = actor;
+                       }
+                       return this;
+               }
+
+               // - Global validity check
+
+               public void checkValidity() throws MissedPropertyException {
+                       if (doctype == null) {
+                               throw new MissedPropertyException("type");
+                       }
+               }
+
+               /**
+                * Get the publisher.
+                * 
+                * @return the publisher
+                */
+               public User getPublisher() {
+                       return publisher;
+               }
+
+               /**
+                * Get the reviewer.
+                * 
+                * @return the reviewer
+                */
+               public User getReviewer() {
+                       return reviewer;
+               }
+
+               /**
+                * Get the approver.
+                * 
+                * @return the approver
+                */
+               public User getApprover() {
+                       return approver;
+               }
+
+               /**
+                * Get the signatory.
+                * 
+                * @return the signatory
+                */
+               public User getSignatory() {
+                       return signatory;
+               }
+       }
+
+       // Database fetch constructor
+       public ValidationCycle() {
+               super();
+       }
+
+       public ValidationCycle(final Study from, final Properties vprop)
+                       throws MissedPropertyException, InvalidPropertyException,
+                       MultiplyDefinedException {
+               super(vprop); // Throws one of the above exception if not valid
+               mytype = vprop.doctype;
+               publisher = vprop.publisher; // May be null
+               reviewer = vprop.reviewer; // May be null
+               approver = vprop.approver; // May be null
+               signatory = vprop.signatory; // May be null
+               context = new ValidationCycleRelation(from, this);
+       }
+
+       /**
+        * Create a copy of the given cycle for the given study.
+        * 
+        * @param study
+        *            the owner study
+        * @return the cloned validation cycle
+        */
+       public ValidationCycle clone(final Study study) {
+               ValidationCycle res = new ValidationCycle();
+               res.mytype = this.mytype;
+               res.publisher = this.publisher; // May be null
+               res.reviewer = this.reviewer; // May be null
+               res.approver = this.approver; // May be null
+               res.signatory = this.signatory; // May be null
+               res.context = new ValidationCycleRelation(study, res);
+               return res;
+       }
+
+       // ==============================================================================================================================
+       // Public member functions
+       // ==============================================================================================================================
+       /**
+        * Checks if a given validation step is enabled in this validation cycle.
+        * 
+        * @param step
+        *            the validation step checked.
+        * @return true if the given validation step is enabled.
+        */
+       public boolean enables(final ValidationStep step) {
+               boolean res = false;
+               if (step == ValidationStep.PROMOTION) {
+                       res = true;
+               } else if (step == ValidationStep.REVIEW) {
+                       res = (reviewer != null);
+               } else if (step == ValidationStep.APPROVAL) {
+                       res = (approver != null);
+               } else if (step == ValidationStep.ACCEPTANCE
+                               || step == ValidationStep.REFUSAL) {
+                       res = (signatory != null);
+               }
+               return res;
+       }
+
+       /**
+        * Returns the user involved in a given step of this document validation cycle. When enabled, the Review and Approval steps have one
+        * explicit actor returned by this function. On the other hand, Promotion and Acceptance have default actors - they respectively are the
+        * author of the document or the responsible of study, and the customer or its representative internal user. In this context, a null
+        * user is returned.
+        * 
+        * @param step
+        *            the validation step
+        * @return the user involved by the given step or null if the step is disabled or the actors are the default ones
+        * @see #getAllActors()
+        * @see #enables
+        */
+       public User getActor(final ValidationStep step) {
+               User res = null;
+               if (step == ValidationStep.PROMOTION) {
+                       res = publisher;
+               } else if (step == ValidationStep.REVIEW) {
+                       res = reviewer;
+               } else if (step == ValidationStep.APPROVAL) {
+                       res = approver;
+               } else if (step == ValidationStep.ACCEPTANCE
+                               || step == ValidationStep.REFUSAL) {
+                       res = signatory;
+               }
+               return res;
+       }
+
+       /**
+        * Returns all explicit actors of this document validation cycle, that is, actors of enabled validation cycles, excluding the default
+        * actors.
+        * 
+        * @return the users explicitly involved by the steps of this validation cycle
+        * @see #getActor(ValidationStep)
+        * @see #enables(ValidationStep)
+        */
+       public User[] getAllActors() {
+               List<User> result = new ArrayList<User>();
+               if (publisher != null) {
+                       result.add(publisher);
+               }
+               if (reviewer != null) {
+                       result.add(reviewer);
+               }
+               if (approver != null) {
+                       result.add(approver);
+               }
+               if (signatory != null) {
+                       result.add(signatory);
+               }
+               return result.toArray(new User[result.size()]);
+       }
+
+       /**
+        * Return the document type to which this validation cycle applies. If this validation cycle is a default one, the document type is not
+        * defined.
+        * 
+        * @return the document type involved by this validation cycle, or null if this validation cycle is a default one.
+        * @see #isDefault()
+        */
+       public DocumentType getDocumentType() {
+               return mytype; // May be null
+       }
+
+       /**
+        * Set a document type for the validation cycle.
+        * 
+        * @param aType
+        *            the document type
+        */
+       public void setDocumentType(final DocumentType aType) {
+               mytype = aType; // May be null
+       }
+
+       /**
+        * Checks if this validation cycle is assigned to a study. If not, it is common to all studies of the workflow in which it has been
+        * defined.
+        * 
+        * @return true if this validation cycle is assigned to a study.
+        */
+       public boolean isAssigned() {
+               return (context != null);
+       }
+
+       /**
+        * Checks if this validation cycle is a default one, either specific to a Study or defined in the configuration workflow or built-in.<br/>
+        * Default validation cycle are not attached to any document type. As for built-in ones, they doesn't include any validation step other
+        * than Promotion.
+        * 
+        * @return true if this validation cycle is a default one.
+        * @see #getDocumentType()
+        */
+       public boolean isDefault() {
+               return (mytype == null);
+       }
+
+       public ValidationCycleRelation getContext() {
+               return context;
+       }
+
+       /**
+        * Set the publisher.
+        * 
+        * @param publisher
+        *            the publisher to set
+        */
+       public void setPublisher(final User publisher) {
+               this.publisher = publisher;
+       }
+
+       /**
+        * Set the reviewer.
+        * 
+        * @param reviewer
+        *            the reviewer to set
+        */
+       public void setReviewer(final User reviewer) {
+               this.reviewer = reviewer;
+       }
+
+       /**
+        * Set the approver.
+        * 
+        * @param approver
+        *            the approver to set
+        */
+       public void setApprover(final User approver) {
+               this.approver = approver;
+       }
+
+       /**
+        * Set the signatory.
+        * 
+        * @param signatory
+        *            the signatory to set
+        */
+       public void setSignatory(final User signatory) {
+               this.signatory = signatory;
+       }
 }
\ No newline at end of file