Salome HOME
Fix for mantis #0022093: To exclude the "In-Draft" state from drop-down list during...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / VersionDocumentAction.java
index 66778f3c936bb4a7bc3a463d2924b997497d8e3b..9bf95254904220d155609e52c3341f4d022a35f5 100644 (file)
@@ -2,11 +2,9 @@ package org.splat.simer;
 
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.Iterator;
 import java.util.List;
 import java.util.ResourceBundle;
 
@@ -17,13 +15,11 @@ import org.splat.dal.bo.som.ProgressState;
 import org.splat.dal.bo.som.Publication;
 import org.splat.dal.bo.som.UsedByRelation;
 import org.splat.dal.bo.som.UsesRelation;
+import org.splat.dal.bo.som.ValidationCycle;
+import org.splat.dal.bo.som.ValidationStep;
 import org.splat.kernel.InvalidPropertyException;
 import org.splat.manox.Reader;
 import org.splat.manox.Toolbox;
-import org.splat.service.PublicationService;
-import org.splat.service.StepService;
-import org.splat.service.technical.ProjectSettingsService;
-import org.splat.service.technical.RepositoryService;
 import org.splat.som.Revision;
 import org.splat.som.Step;
 import org.splat.wapp.Constants;
@@ -31,7 +27,7 @@ import org.splat.wapp.Constants;
 /**
  * Action for creating a new version of a document.
  */
-public class VersionDocumentAction extends UploadBaseNextAction {
+public class VersionDocumentAction extends BaseUploadDocumentAction {
 
        /**
         * Serial version ID.
@@ -55,33 +51,9 @@ public class VersionDocumentAction extends UploadBaseNextAction {
         */
        private String _description = null;
        /**
-        * Version number extracted from the imported file, if exist.
+        * Applicable document states.
         */
-       private String _version = "";
-       /**
-        * Date extracted from the imported file, if exist.
-        */
-       private String _date = "";
-       /**
-        * Injected project settings service.
-        */
-       private ProjectSettingsService _projectSettings;
-       /**
-        * Injected publication service.
-        */
-       private PublicationService _publicationService;
-       /**
-        * Injected step service.
-        */
-       private StepService _stepService;
-       /**
-        * Injected repository service.
-        */
-       private RepositoryService _repositoryService;
-
-       // ==============================================================================================================================
-       // Action methods
-       // ==============================================================================================================================
+       private transient final List<ProgressState> _documentStates = new ArrayList<ProgressState>();
 
        /**
         * Initialize the action form.
@@ -89,43 +61,48 @@ public class VersionDocumentAction extends UploadBaseNextAction {
         * @return SUCCESS if succeeded, ERROR if uploaded file is XML and we can't extract properties from it
         */
        public String doInitialize() {
+               File upfile = commonInitialize(Constants.TRUE);
 
-               if ("true".equals(getWriteAccess())) {
-                       setToolProperty(Constants.STUDY_MENU);
-               } else {
-                       setToolProperty(Constants.NONE);
-               }
-               initializationFullScreenContext(Constants.STUDY_MENU,
-                               Constants.STUDY_MENU, Constants.TRUE, getToolProperty(),
-                               Constants.STUDY_MENU);
-
-               User user = getConnectedUser();
-               File updir = getRepositoryService().getDownloadDirectory(user);
-               File upfile = new File(updir.getPath() + "/" + filename);
-
-               mystudy = getOpenStudy();
+               _mystudy = getOpenStudy();
+               _defuses = new ArrayList<Document>();
 
-               Publication tag = mystudy.getSelectedStep().getDocument(
+               Publication tag = _mystudy.getSelectedStep().getDocument(
                                Integer.valueOf(_index));
                Document doc = tag.value();
-               deftype = doc.getType();
-               docname = doc.getTitle();
-               defuses = new ArrayList<Document>();
+               _deftype = doc.getType();
+               _docname = doc.getTitle();
                _usedby = new ArrayList<Publication>();
 
                String res = SUCCESS;
                if (extractProperties(upfile, doc)) {
-                       setupDefaultUses(deftype);
+                       if (_deftype != null) {
+                               setupDefaultUses(_deftype);
+                       }
                        // Add additional documents used by the current version
-                       List<Relation> uses = doc.getRelations(UsesRelation.class);
-                       for (Iterator<Relation> i = uses.iterator(); i.hasNext();) {
-                               Document used = (Document) i.next().getTo();
-                               if (!defuses.contains(used)) {
-                                       defuses.add(used);
+                       for (Relation usesRel: doc.getRelations(UsesRelation.class)) {
+                               Document used = (Document) usesRel.getTo();
+                               if (!_defuses.contains(used)) {
+                                       _defuses.add(used);
                                }
                        }
+                       // Avoid recursive using of the document
+                       if (_defuses.contains(doc)) {
+                               _defuses.remove(doc);
+                       }
                        // Setup dependencies
                        _usedby.addAll(tag.getRelations(UsedByRelation.class));
+
+                       // Initialize applicable states list
+                       _documentStates.add(ProgressState.inWORK);
+                       if (_deftype != null) {
+                               // Check if the validation cycle of the document type can has a review state
+                               ValidationCycle cycle = getStudyService().getValidationCycleOf(
+                                               _mystudy.getMystudy(), _deftype);
+                               if ((cycle != null) && cycle.enables(ValidationStep.REVIEW)) {
+                                       _documentStates.add(ProgressState.inDRAFT);
+                               }
+                       }
+
                } else {
                        if (!(Constants.NONE.equals(getToolProperty()))) {
                                initializationFullScreenContext(Constants.STUDY_MENU,
@@ -163,11 +140,11 @@ public class VersionDocumentAction extends UploadBaseNextAction {
                                                        throw new InvalidPropertyException("version");
                                                }
                                                if (newver.isMinor()) {
-                                                       state = ProgressState.inWORK;
+                                                       _state = ProgressState.inWORK;
                                                } else {
-                                                       state = ProgressState.inDRAFT;
+                                                       _state = ProgressState.inDRAFT;
                                                }
-                                               _version = newver.toString();
+                                               setVersion(newver.toString());
                                        } catch (Exception e) {
                                                setErrorCode("message.error.version.mismatch");
                                                res = false;
@@ -175,21 +152,7 @@ public class VersionDocumentAction extends UploadBaseNextAction {
                                }
                                if (res) {
                                        _description = tool.extractProperty("history");
-                                       _date = tool.extractProperty("date");
-                                       if (_date == null) {
-                                               _date = "";
-                                       } else {
-                                               ResourceBundle locale = ResourceBundle.getBundle("som",
-                                                               getApplicationSettings().getCurrentLocale());
-                                               SimpleDateFormat check = new SimpleDateFormat(locale
-                                                               .getString("date.format"));
-                                               try {
-                                                       check.parse(_date);
-                                               } catch (ParseException e) {
-                                                       setErrorCode("message.error.format.date");
-                                                       res = false;
-                                               }
-                                       }
+                                       res = extractDate(tool);
                                }
                        } else {
                                setErrorCode("message.error.reference.mismatch");
@@ -209,34 +172,35 @@ public class VersionDocumentAction extends UploadBaseNextAction {
                initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
                                Constants.TRUE);
 
-               if (action == ToDo.cancel) {
+               if (_action == ToDo.cancel) {
                        res = "cancel";
                } else {
 
                        try {
                                // Getting user inputs
-                               mystudy = getOpenStudy();
+                               _mystudy = getOpenStudy();
                                User user = getConnectedUser();
-                               Step step = mystudy.getSelectedStep();
+                               Step step = _mystudy.getSelectedStep();
                                Date aDate = null;
-                               if (_date.length() > 0) {
+                               if (getDocumentDate().length() > 0) {
                                        ResourceBundle locale = ResourceBundle.getBundle("som",
                                                        getApplicationSettings().getCurrentLocale());
                                        SimpleDateFormat get = new SimpleDateFormat(locale
-                                                       .getString("date.format"));
-                                       aDate = get.parse(_date);
+                                                       .getString("date.format"), getApplicationSettings()
+                                                       .getCurrentLocale());
+                                       aDate = get.parse(getDocumentDate());
                                }
 
                                String[] listDocuses = null;
-                               if (docuses != null) {
-                                       listDocuses = docuses.split(",");
+                               if (_docuses != null) {
+                                       listDocuses = _docuses.split(",");
                                }
-                               getPublicationService().versionDocument(step, user, filename,
-                                               Integer.valueOf(_index), _version, _description, state,
-                                               aDate, listDocuses, _docusedby);
+                               getPublicationService().versionDocument(step, user, _fileName,
+                                               Integer.valueOf(_index), getVersion(), _description,
+                                               _state, aDate, listDocuses, _docusedby);
 
                                // Update of the open study
-                               mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
+                               _mystudy.setSelection(_mystudy.getSelection()); // Rebuilds the presentation
                                // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
 
                                res = SUCCESS;
@@ -260,10 +224,6 @@ public class VersionDocumentAction extends UploadBaseNextAction {
        // Getters and setters
        // ==============================================================================================================================
 
-       public String getDate() {
-               return _date;
-       }
-
        public List<Publication> getDependencies() {
                return _usedby;
        }
@@ -276,14 +236,6 @@ public class VersionDocumentAction extends UploadBaseNextAction {
                return _index;
        }
 
-       public String getVersion() {
-               return _version;
-       }
-
-       public void setDate(final String date) {
-               this._date = date;
-       }
-
        public void setDefaultDescription(final String summary) {
                if (this._description == null) {
                        this._description = summary;
@@ -302,85 +254,12 @@ public class VersionDocumentAction extends UploadBaseNextAction {
                this._docusedby = list;
        }
 
-       public void setVersion(final String value) {
-               this._version = value;
-       }
-
-       /**
-        * Get project settings.
-        * 
-        * @return Project settings service
-        */
-       private ProjectSettingsService getProjectSettings() {
-               return _projectSettings;
-       }
-
-       /**
-        * Set project settings service.
-        * 
-        * @param projectSettingsService
-        *            project settings service
-        */
-       public void setProjectSettings(
-                       final ProjectSettingsService projectSettingsService) {
-               _projectSettings = projectSettingsService;
-       }
-
-       /**
-        * Get the publicationService.
-        * 
-        * @return the publicationService
-        */
-       public PublicationService getPublicationService() {
-               return _publicationService;
-       }
-
-       /**
-        * Set the publicationService.
-        * 
-        * @param publicationService
-        *            the publicationService to set
-        */
-       public void setPublicationService(
-                       final PublicationService publicationService) {
-               _publicationService = publicationService;
-       }
-
-       /**
-        * Get the stepService.
-        * 
-        * @return the stepService
-        */
-       public StepService getStepService() {
-               return _stepService;
-       }
-
-       /**
-        * Set the stepService.
-        * 
-        * @param stepService
-        *            the stepService to set
-        */
-       public void setStepService(final StepService stepService) {
-               _stepService = stepService;
-       }
-
-       /**
-        * Get the repositoryService.
-        * 
-        * @return the repositoryService
-        */
-       public RepositoryService getRepositoryService() {
-               return _repositoryService;
-       }
-
        /**
-        * Set the repositoryService.
+        * Get the documentStates.
         * 
-        * @param repositoryService
-        *            the repositoryService to set
+        * @return the documentStates
         */
-       public void setRepositoryService(final RepositoryService repositoryService) {
-               _repositoryService = repositoryService;
+       public List<ProgressState> getDocumentStates() {
+               return _documentStates;
        }
 }
\ No newline at end of file