package org.splat.som;
+
/**
* Class defining the default rights related to operations on studies.
* On the contrary of documents, a study cannot directly be reviewed or approved. It is reviewed or approved through
import org.splat.dal.bo.kernel.User;
import org.splat.dal.bo.som.ProgressState;
+import org.splat.dal.bo.som.Publication;
import org.splat.dal.bo.som.Study;
+import org.splat.dal.bo.som.ValidationCycle;
+import org.splat.dal.bo.som.ValidationStep;
import org.splat.service.ServiceLocatorImpl;
+import org.splat.service.StudyService;
-
+/**
+ * The class representing user rights for a selected study.
+ */
public class StudyRights {
- private final transient User _user;
- private final transient Study _operand;
- private transient boolean _author = false; // For optimizing
- public StudyRights (final User user, final Study study) {
-// -------------------------------------------
- this._user = user;
- this._operand = study;
-
- if (_operand != null && _operand.getAuthor() != null) {
- this._author = _operand.getAuthor().equals(user); // user may be null
- }
- }
- public StudyRights (final Study study) {
-// --------------------------------
- this._user = study.getAuthor();
- this._operand = study;
- this._author = true; // In order to ignore the author in this context
- }
-
-// ==============================================================================================================================
-// Public member functions
-// ==============================================================================================================================
-
- public boolean canAddScenario () {
-// --------------------------------
- if (_operand.getProgressState() != ProgressState.inWORK && _operand.getProgressState() != ProgressState.inDRAFT) {
- return false;
- }
- return ServiceLocatorImpl.getInstance().getStudyService().isStaffedBy(_operand, _user);
- }
+ /**
+ * The connected user.
+ */
+ private final transient User _user;
+ /**
+ * The selected study.
+ */
+ private final transient Study _operand;
+ /**
+ * True if the current user is the author of the selected study.
+ */
+ private transient boolean _isauthor = false; // For optimizing
+ /**
+ * The study validation cycle according to the validation cycle of a study result document.
+ */
+ private transient final ValidationCycle _cycle;
-/**
- * Checks if the user has right to edit the description of the study.
- * All actors of the study have such right, including the author, contributors, reviewers and approvers.
- *
- * @return true if the user has right to edit the description.
- */
- public boolean canEditDescription () {
-// ------------------------------------
- return (_operand.getAuthor().equals(_user) || ServiceLocatorImpl.getInstance().getStudyService().hasActor(_operand, _user));
- }
+ /**
+ * The constructor.
+ *
+ * @param user
+ * the current user
+ * @param study
+ * the selected study
+ */
+ public StudyRights(final User user, final Study study) {
+ this._user = user;
+ this._operand = study;
+ this._cycle = getStudyService().getValidationCycleOf(_operand,
+ getStudyService().getStudyResultType(_operand));
+
+ if (_operand != null && _operand.getAuthor() != null) {
+ this._isauthor = _operand.getAuthor().equals(user); // user may be null
+ }
+ }
+
+ /**
+ * The constructor for the case when the user is the author of the study.
+ *
+ * @param study
+ * the selected study
+ */
+ public StudyRights(final Study study) {
+ this._user = study.getAuthor();
+ this._operand = study;
+ this._isauthor = true; // In order to ignore the author in this context
+ this._cycle = getStudyService().getValidationCycleOf(_operand,
+ getStudyService().getStudyResultType(_operand));
+ }
+
+ // ==============================================================================================================================
+ // Public member functions
+ // ==============================================================================================================================
+
+ /**
+ * Check if the user can add a new scenario to the study.
+ *
+ * @return true if the user can add a new scenario to the study
+ */
+ public boolean canAddScenario() {
+ return (_operand.getProgressState() == ProgressState.inWORK || _operand
+ .getProgressState() == ProgressState.inDRAFT)
+ && getStudyService().isStaffedBy(_operand, _user);
+ }
+
+ /**
+ * Checks if the user has right to edit the description of the study. <BR>
+ * All actors of the study have such right, including the author, contributors,<BR>
+ * reviewers and approvers.
+ *
+ * @return true if the user has right to edit the description.
+ */
+ public boolean canEditDescription() {
+ return (_operand.getAuthor().equals(_user) || getStudyService()
+ .hasActor(_operand, _user));
+ }
- public boolean canEditProperties () {
-// -----------------------------------
- return _author;
- }
+ /**
+ * Check if the user can configure the study.
+ *
+ * @return true if the user can configure the study.
+ */
+ public boolean canEditProperties() {
+ return _isauthor;
+ }
/**
* 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
* @return true if the user has right to edit the description.
*/
public boolean canPublish() {
- // ----------------------------
- if (_operand.getProgressState() == ProgressState.APPROVED
- && !_operand.isPublic()/* && "knowledgineer".equals(_user.getRole().getName()) */) {
- return true;
- }
- return false;
+ return (_operand.getProgressState() == ProgressState.APPROVED && !_operand
+ .isPublic()/* && "knowledgineer".equals(_user.getRole().getName()) */);
}
/**
* @return true if the user has right to edit the description.
*/
public boolean canProtect() {
- // ----------------------------
- if (_operand.getProgressState() == ProgressState.APPROVED
- && _operand.isPublic()/* && "knowledgineer".equals(_user.getRole().getName()) */) {
- return true;
+ return (_operand.getProgressState() == ProgressState.APPROVED && _operand
+ .isPublic()/* && "knowledgineer".equals(_user.getRole().getName()) */);
+ }
+
+ /**
+ * Check if the user can remove old versions.
+ *
+ * @return true if the user can remove old versions
+ */
+ public boolean canPurge() {
+ return (_isauthor && _operand.isVersioned());
+ }
+
+ /**
+ * Check if the user can remove the study.
+ *
+ * @return true if the user can remove the study
+ */
+ public boolean canRemove() {
+ return (_operand.getProgressState() == ProgressState.inWORK || _operand
+ .getProgressState() == ProgressState.inDRAFT)
+ && _isauthor;
+ }
+
+ /**
+ * Check if the user can version the study.
+ *
+ * @return true if the user can version the study
+ */
+ public boolean canVersion() {
+ return (_operand.getProgressState() == ProgressState.inWORK || _operand
+ .getProgressState() == ProgressState.inDRAFT)
+ && getStudyService().isStaffedBy(_operand, _user);
+ }
+
+ /**
+ * Can the given study be marked as reference or not.
+ *
+ * @return true/false.
+ */
+ public boolean canMarkStudyAsReference() {
+ return (_operand.getProgressState() == ProgressState.APPROVED /* && "knowledgineer".equals(_user.getRole().getName()) */);
+ }
+
+ /**
+ * Can the given study be unmarked as reference or not.
+ *
+ * @return true/false.
+ */
+ public boolean canRemoveStudyAsReference() {
+ return (_operand.getProgressState() == ProgressState.TEMPLATE /* && "knowledgineer".equals(_user.getRole().getName()) */);
+ }
+
+ // ==========================================================================
+ // Operations from document validation cycle
+ // ==========================================================================
+
+ /**
+ * 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
+ */
+ public boolean canApprove() {
+ User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
+ return (_user.equals(approver))
+ && getStudyService().canBeApproved(_operand);
+ }
+
+ /**
+ * 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
+ */
+ public boolean canDemote() {
+ User manager = _operand.getOwnerStudy().getAuthor();
+ User publisher = _cycle.getActor(ValidationStep.PROMOTION); // Null if the default users are involved
+ User reviewer = _cycle.getActor(ValidationStep.REVIEW); // May be null if not reviewable
+ User approver = _cycle.getActor(ValidationStep.APPROVAL); // May be null if not approvable
+ ProgressState mystate = _operand.getProgressState();
+
+ if (mystate == ProgressState.inDRAFT) {
+ if (publisher == null) {
+ if ((!_isauthor) && (!_user.equals(manager))
+ && (!_user.equals(reviewer))) {
+ return false;
+ }
+ } else if ((!_user.equals(publisher)) && (!_user.equals(reviewer))) {
+ return false;
+ }
+ } else if (mystate == ProgressState.inCHECK) {
+ if (!_user.equals(approver)) {
+ return false;
+ }
+ } else {
+ return false;
}
- return false;
- }
-
- public boolean canPurge () {
-// --------------------------
- if (!_author) {
- return false;
- }
- return _operand.isVersioned();
- }
-
- public boolean canRemove () {
-// ---------------------------
- if (_operand.getProgressState() != ProgressState.inWORK && _operand.getProgressState() != ProgressState.inDRAFT) {
- return false;
- }
- return _author;
- }
-
- public boolean canVersion () {
-// ----------------------------
- if (_operand.getProgressState() != ProgressState.inWORK && _operand.getProgressState() != ProgressState.inDRAFT) {
- return false;
- }
- return ServiceLocatorImpl.getInstance().getStudyService().isStaffedBy(_operand, _user);
- }
-
- /**
- * Can the given study be marked as reference or not.
- * @return true/false.
- */
- public boolean canMarkStudyAsReference() {
-
- if (_operand.getProgressState() == ProgressState.APPROVED /*&& "knowledgineer".equals(_user.getRole().getName())*/) {
- return true;
- }
- return false;
- }
-
- /**
- * Can the given study be unmarked as reference or not.
- * @return true/false.
- */
- public boolean canRemoveStudyAsReference() {
-
- if (_operand.getProgressState() == ProgressState.TEMPLATE /*&& "knowledgineer".equals(_user.getRole().getName())*/) {
- return true;
- }
- return false;
- }
-
-// ==============================================================================================================================
-// Getter
-// ==============================================================================================================================
-
- public Study getOperand () {
-// --------------------------
- return _operand;
- }
+
+ return true;
+ }
+
+ /**
+ * 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
+ * dependencies have previously been promoted. By default, both the author of the document and the responsible of study has right to
+ * promote such document. Otherwise, only the user involved in the Promotion step of validation cycle of the selected document has such
+ * right.
+ *
+ * @return true if the user has right to promote the document.
+ * @see #canDemote()
+ * @see Publication#promote()
+ * @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) {
+ if (_operand.getProgressState() == ProgressState.inDRAFT) {
+ return canReview();
+ }
+ return false;
+ }
+ if (publisher == null) {
+ if (!_isauthor && !_user.equals(manager)) {
+ return false;
+ }
+ } else {
+ if (!_user.equals(publisher)) {
+ return false;
+ }
+ }
+ return getStudyService().canBePromoted(_operand);
+ }
+
+ /**
+ * Checks if the user has right to validate the selected document. Only the reviewer of the type of selected document has such right,
+ * providing that the document is being reviewed and all document dependencies have already been validated.
+ *
+ * @return true if the user has right to validate the document
+ * @see #canUnvalidate()
+ * @see Publication#review()
+ * @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;
+ }
+
+ return getStudyService().canBeReviewed(_operand);
+ }
+
+ // ==============================================================================================================================
+ // Getter
+ // ==============================================================================================================================
+
+ /**
+ * Get the selected study.
+ *
+ * @return the selected study
+ */
+ public Study getOperand() {
+ return _operand;
+ }
+
+ /**
+ * Get the study service.
+ *
+ * @return the study service
+ */
+ private StudyService getStudyService() {
+ return ServiceLocatorImpl.getInstance().getStudyService();
+ }
+
}
\ No newline at end of file