Salome HOME
Fix for some problems with definition uses.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / VersionDocumentAction.java
index d171ce1b65da1bba2cde1156795beaf2d35cfcd1..1eeaabe06b8dfeabc5f4be792f2d9b03391f5ce6 100644 (file)
@@ -2,35 +2,35 @@ 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;
-import java.util.Vector;
 
-import org.splat.kernel.InvalidPropertyException;
 import org.splat.dal.bo.kernel.Relation;
 import org.splat.dal.bo.kernel.User;
-import org.splat.manox.Reader;
-import org.splat.manox.Toolbox;
 import org.splat.dal.bo.som.Document;
 import org.splat.dal.bo.som.ProgressState;
-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.dal.bo.som.Publication;
-import org.splat.som.Revision;
-import org.splat.som.Step;
 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.StudyService;
+import org.splat.som.Revision;
+import org.splat.som.Step;
+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.
@@ -40,77 +40,23 @@ public class VersionDocumentAction extends UploadBaseNextAction {
        /**
         * Versioned document index.
         */
-       private String index = null;
+       private String _index = null;
        /**
         * List of publications which use the selected document.
         */
-       private List<Publication> usedby = null;
+       private transient List<Publication> _usedby = null;
        /**
         * List of selected impacted documents ids.
         */
-       private long[] docusedby = null;
+       private transient long[] _docusedby = null;
        /**
         * Summary of changes in the new version.
         */
-       private String summary = null;
-       /**
-        * Version number extracted from the imported file, if exist.
-        */
-       private String docver = "";
-       /**
-        * Date extracted from the imported file, if exist.
-        */
-       private String date = "";
+       private String _description = null;
        /**
-        * Injected project settings service.
+        * Applicable document states.
         */
-       private ProjectSettingsService _projectSettingsService;
-       /**
-        * Injected publication service.
-        */
-       private PublicationService _publicationService;
-       /**
-        * Injected step service.
-        */
-       private StepService _stepService;
-       /**
-        * Injected repository service.
-        */
-       private RepositoryService _repositoryService;
-       /**
-        * Value of the menu property. 
-        * It can be: none, create, open, study, knowledge, sysadmin, help.
-        */
-       private String _menuProperty;
-
-       /**
-        * Value of the title bar property. 
-        * It can be: study, knowledge.
-        */
-       private String _titleProperty;
-
-       /**
-        * Value of the tool bar property. 
-        * It can be: none, standard, study, back.
-        */
-       private String _toolProperty;
-       
-       /**
-        * Value of the left menu property. 
-        * It can be: open, study, knowledge, scenario.
-        */
-       private String _leftMenuProperty;
-
-       /**
-        * Property that indicates whether the current open study is editable or not.
-        * On the screen it looks like pen on the status icon, pop-up menu also can be called.
-        * It is necessary for correct building the title bar.
-        */
-       private String _editDisabledProperty = "false";
-
-       // ==============================================================================================================================
-       // Action methods
-       // ==============================================================================================================================
+       private transient final List<ProgressState> _documentStates = new ArrayList<ProgressState>();
 
        /**
         * Initialize the action form.
@@ -118,54 +64,76 @@ 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);
 
-               setMenuProperty("study");
-               setTitleProperty("study");
-               setEditDisabledProperty("true");
-               if ("true".equals(getWriteAccess())) {
-                       setToolProperty("study");
-               } else {
-                       setToolProperty("none");
-               }
-               setLeftMenuProperty("study");
-        initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-           
-               User user = getConnectedUser();
-               File updir = getRepositoryService().getDownloadDirectory(user);
-               File upfile = new File(updir.getPath() + "/" + filename);
-
-               mystudy = getOpenStudy();
-
-               Publication tag = mystudy.getSelectedStep().getDocument(
-                               Integer.valueOf(index));
+               _mystudy = getOpenStudy();
+               //updating relations of docs
+               _mystudy.updateCurrentStep();
+               _defuses = new ArrayList<Document>();
+               
+               Publication tag = _mystudy.getSelectedStep().getDocument(
+                               Integer.valueOf(_index));
                Document doc = tag.value();
-               deftype = doc.getType();
-               docname = doc.getTitle();
-               defuses = new Vector<Document>();
-               usedby = new Vector<Publication>();
+               _deftype = doc.getType();
+               _docname = doc.getTitle();
+               _usedby = new ArrayList<Publication>();
+
+               String res = SUCCESS;
+               if (extractProperties(upfile, doc)) {
+                       if (_deftype != null) {
+                               setupDefaultUses(_deftype);
+                       }
+                       // Add additional documents used by the current version
+                       for (Relation usesRel : doc.getRelations(UsesRelation.class)) {
+                               Document used = (Document) usesRel.getTo();
+                               Document lastVersion = getPublicationService().getLastVersion(used, tag.getOwner());
+                               if (lastVersion != null && !_defuses.contains(lastVersion)) {
+                                       _defuses.add(lastVersion);
+                               }
+                       }
+                       // Avoid recursive using of the document
+                       if (_defuses.contains(doc)) {
+                               _defuses.remove(doc);
+                       }
+                       
+                       // Avoid using of documents dependent on the current version of the document being versioned
+                       // (This case is possible only if both documents belong to the step of the same Project Element)
+                       List<Document> toDeleteFromDefuses = new ArrayList<Document>();
+                       getPublicationService().findSequenceUses(toDeleteFromDefuses, tag,
+                                       _defuses);
+                       for (Document document : toDeleteFromDefuses) {
+                               if (_defuses.contains(document)) {
+                                       _defuses.remove(document);
+                               }
+                       }
+                       
+                       // Setup dependencies
+                       _usedby.addAll(tag.getRelations(UsedByRelation.class));
 
-               if (!extractProperties(upfile, doc)) {
-                       if (!("none".equals(_toolProperty))) {
-                               setToolProperty("none");
-                               setLeftMenuProperty("study");
-                               initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
+                       // Initialize applicable states list
+                       if (tag.value().getProgressState() == ProgressState.EXTERN) {
+                               _documentStates.add(ProgressState.EXTERN);
+                       } else {
+                               _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);
+                                       }
+                               }
                        }
-                       return ERROR;
-               }
-               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);
-               }
-               // Setup dependencies
-               List<Publication> relist = tag.getRelations(UsedByRelation.class);
-               for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
-                       usedby.add(i.next());
+               } else {
+                       if (!(Constants.NONE.equals(getToolProperty()))) {
+                               initializationFullScreenContext(Constants.STUDY_MENU,
+                                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                                               Constants.STUDY_MENU);
+                       }
+                       res = ERROR;
                }
-               return SUCCESS;
+               return res;
        }
 
        /**
@@ -177,16 +145,13 @@ public class VersionDocumentAction extends UploadBaseNextAction {
         *            the document to version
         * @return true if succeeded or if the file is not XML, otherwise return false
         */
-       private boolean extractProperties(File upfile, Document doc) {
+       private boolean extractProperties(final File upfile, final Document doc) {
                boolean res = true;
                Reader tool = Toolbox.getReader(upfile);
                if (tool != null) {
                        String fileref = tool.extractProperty("reference");
                        String filever = tool.extractProperty("version");
-                       if (fileref != null && !doc.getReference().equals(fileref)) {
-                               setErrorCode("reference.mismatch");
-                               res = false;
-                       } else {
+                       if (fileref == null || doc.getReference().equals(fileref)) {
                                if (filever != null) {
                                        try {
                                                Revision.Format get = new Revision.Format(
@@ -197,34 +162,23 @@ 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;
                                                }
-                                               docver = newver.toString();
+                                               setVersion(newver.toString());
                                        } catch (Exception e) {
-                                               setErrorCode("version.mismatch");
+                                               setErrorCode("message.error.version.mismatch");
                                                res = false;
                                        }
                                }
                                if (res) {
-                                       summary = tool.extractProperty("history");
-                                       date = tool.extractProperty("date");
-                                       if (date != null) {
-                                               ResourceBundle locale = ResourceBundle.getBundle("som",
-                                                               getApplicationSettings().getCurrentLocale());
-                                               SimpleDateFormat check = new SimpleDateFormat(locale
-                                                               .getString("date.format"));
-                                               try {
-                                                       check.parse(date);
-                                               } catch (ParseException e) {
-                                                       setErrorCode("format.date");
-                                                       res = false;
-                                               }
-                                       } else {
-                                               date = "";
-                                       }
+                                       _description = tool.extractProperty("history");
+                                       res = extractDate(tool);
                                }
+                       } else {
+                               setErrorCode("message.error.reference.mismatch");
+                               res = false;
                        }
                }
                return res;
@@ -236,268 +190,97 @@ public class VersionDocumentAction extends UploadBaseNextAction {
         * @return SUCCESS - if succeeded, "cancel" - if canceled, ERROR - if failed
         */
        public String doVersion() {
-               setMenuProperty("study");
-               setTitleProperty("study");
-               setEditDisabledProperty("true");
-        initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
-           
-               if (action == ToDo.cancel)
-                       return "cancel";
-
-               try {
-                       // Getting user inputs
-                       mystudy = getOpenStudy();
-                       User user = getConnectedUser();
-                       Step step = mystudy.getSelectedStep();
-                       Date aDate = null;
-                       if (date.length() > 0) {
-                               ResourceBundle locale = ResourceBundle.getBundle("som",
-                                               getApplicationSettings().getCurrentLocale());
-                               SimpleDateFormat get = new SimpleDateFormat(locale
-                                               .getString("date.format"));
-                               aDate = get.parse(date);
-                       }
+               String res = ERROR;
+               initializationScreenContext(Constants.STUDY_MENU, Constants.STUDY_MENU,
+                               Constants.TRUE);
 
-                       String[] listDocuses = null;
-                       if (docuses != null) {
-                               listDocuses = docuses.split(",");
-                       }
-                       getPublicationService().versionDocument(step, user, filename,
-                                       Integer.valueOf(index), docver, summary, state, aDate,
-                                       listDocuses, docusedby);
+               if (_action == ToDo.cancel) {
+                       res = "cancel";
+               } else {
 
-                       // Update of the open study
-                       mystudy.setSelection(mystudy.getSelection()); // Rebuilds the presentation
-                       // TODO: Look is an optimization is possible (for example by updating the presentation of versioned document)
+                       try {
+                               // Getting user inputs
+                               _mystudy = getOpenStudy();
+                               User user = getConnectedUser();
+                               Step step = _mystudy.getSelectedStep();
+                               Date aDate = null;
+                               if (getDocumentDate().length() > 0) {
+                                       ResourceBundle locale = ResourceBundle.getBundle("som",
+                                                       getApplicationSettings().getCurrentLocale());
+                                       SimpleDateFormat get = new SimpleDateFormat(locale
+                                                       .getString("date.format"), getApplicationSettings()
+                                                       .getCurrentLocale());
+                                       aDate = get.parse(getDocumentDate());
+                               }
 
-                       return SUCCESS;
-               } catch (FileNotFoundException error) {
-                       LOG.error("Reason:", error);
-                       setErrorCode("import.file");
-               } catch (Exception error) {
-                       LOG.error("Reason:", error);
-                       setErrorCode("internal");
+                               String[] listDocuses = null;
+                               if (_docuses != null) {
+                                       listDocuses = _docuses.split(",");
+                               }
+                               getPublicationService().versionDocument(step, user, _fileName,
+                                               Integer.valueOf(_index), getVersion(), _description,
+                                               _state, aDate, listDocuses, _docusedby);
+
+                               // Update of the open study
+                               refreshStudy();
+
+                               res = SUCCESS;
+                       } catch (FileNotFoundException error) {
+                               LOG.error("Reason:", error);
+                               setErrorCode("message.error.import.file");
+                       } catch (Exception error) {
+                               LOG.error("Reason:", error);
+                               setErrorCode("message.error.internal");
+                       }
+                       if (!SUCCESS.equals(res)) {
+                               initializationFullScreenContext(Constants.STUDY_MENU,
+                                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                                               Constants.STUDY_MENU);
+                       }
                }
-               
-               setToolProperty("none");
-               
-               setLeftMenuProperty("study");
-               initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
-               
-               return ERROR;
+               return res;
        }
 
        // ==============================================================================================================================
        // Getters and setters
        // ==============================================================================================================================
 
-       public String getDate() {
-               return date;
-       }
-
        public List<Publication> getDependencies() {
-               return usedby;
+               return _usedby;
        }
 
        public String getDescription() {
-               return summary;
+               return _description;
        }
 
        public String getIndex() {
-               return index;
+               return _index;
        }
 
-       public String getVersion() {
-               return docver;
-       }
-
-       public void setDate(String date) {
-               this.date = date;
-       }
-
-       public void setDefaultDescription(String summary) {
-               if (this.summary == null)
-                       this.summary = summary;
-       }
-
-       public void setDescription(String summary) {
-               this.summary = summary;
-       }
-
-       public void setIndex(String index) {
-               this.index = index;
-       }
-
-       public void setUsedBy(long[] list) {
-               this.docusedby = list;
-       }
-
-       public void setVersion(String value) {
-               this.docver = value;
-       }
-
-       /**
-        * Get project settings.
-        * 
-        * @return Project settings service
-        */
-       private ProjectSettingsService getProjectSettings() {
-               return _projectSettingsService;
-       }
-
-       /**
-        * Set project settings service.
-        * 
-        * @param projectSettingsService
-        *            project settings service
-        */
-       public void setProjectSettings(ProjectSettingsService projectSettingsService) {
-               _projectSettingsService = projectSettingsService;
-       }
-
-       /**
-        * Get the publicationService.
-        * 
-        * @return the publicationService
-        */
-       public PublicationService getPublicationService() {
-               return _publicationService;
-       }
-
-       /**
-        * Set the publicationService.
-        * 
-        * @param publicationService
-        *            the publicationService to set
-        */
-       public void setPublicationService(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(StepService stepService) {
-               _stepService = stepService;
-       }
-
-       /**
-        * Get the repositoryService.
-        * 
-        * @return the repositoryService
-        */
-       public RepositoryService getRepositoryService() {
-               return _repositoryService;
-       }
-
-       /**
-        * Set the repositoryService.
-        * 
-        * @param repositoryService
-        *            the repositoryService to set
-        */
-       public void setRepositoryService(RepositoryService repositoryService) {
-               _repositoryService = repositoryService;
-       }
-
-       /**
-        * Get the menuProperty.
-        * 
-        * @return the menuProperty
-        */
-       public String getMenuProperty() {
-               return _menuProperty;
-       }
-
-       /**
-        * Set the menuProperty.
-        * 
-        * @param menuProperty
-        *            the menuProperty to set
-        */
-       public void setMenuProperty(String menuProperty) {
-               this._menuProperty = menuProperty;
+       public void setDefaultDescription(final String summary) {
+               if (this._description == null) {
+                       this._description = summary;
+               }
        }
 
-       /**
-        * Get the _titleProperty.
-        * 
-        * @return the _titleProperty
-        */
-       public String getTitleProperty() {
-               return _titleProperty;
+       public void setDescription(final String summary) {
+               this._description = summary;
        }
 
-       /**
-        * Set the _titleProperty.
-        * 
-        * @param titleProperty
-        *            the titleProperty to set
-        */
-       public void setTitleProperty(String titleProperty) {
-               _titleProperty = titleProperty;
+       public void setIndex(final String index) {
+               this._index = index;
        }
 
-       /**
-        * Get the _editDisabledProperty.
-        * 
-        * @return the _editDisabledProperty
-        */
-       public String getEditDisabledProperty() {
-               return _editDisabledProperty;
+       public void setUsedBy(final long[] list) {
+               this._docusedby = list;
        }
 
        /**
-        * Set the _editDisabledProperty.
+        * Get the documentStates.
         * 
-        * @param _editDisabledProperty
-        *            the _editDisabledProperty to set
-        */
-       public void setEditDisabledProperty(String _editDisabledProperty) {
-               this._editDisabledProperty = _editDisabledProperty;
-       }
-       
-       /**
-        * Get the toolProperty.
-        * @return the toolProperty
-        */
-       public String getToolProperty() {
-               return _toolProperty;
-       }
-
-       /**
-        * Set the toolProperty.
-        * @param toolProperty the toolProperty to set
-        */
-       public void setToolProperty(final String toolProperty) {
-               _toolProperty = toolProperty;
-       }
-       
-       /**
-        * Get the leftMenuProperty.
-        * @return the leftMenuProperty
-        */
-       public String getLeftMenuProperty() {
-               return _leftMenuProperty;
-       }
-
-       /**
-        * Set the leftMenuProperty.
-        * @param leftMenuProperty the leftMenuProperty to set
+        * @return the documentStates
         */
-       public void setLeftMenuProperty(final String leftMenuProperty) {
-               _leftMenuProperty = leftMenuProperty;
+       public List<ProgressState> getDocumentStates() {
+               return _documentStates;
        }
 }
\ No newline at end of file