Salome HOME
Creation of a new study from an existing one is implemented.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewStudyAction.java
index 8020aff1004dbe93397a96311fa4d625743c4127..2058b0881a14a8463403cbc29c083e4573e763aa 100644 (file)
@@ -1,17 +1,18 @@
 package org.splat.simer;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.ResourceBundle;
 
+import org.apache.commons.lang3.StringUtils;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.SimulationContext;
 import org.splat.dal.bo.som.SimulationContextType;
 import org.splat.dal.bo.som.Study;
-import org.splat.kernel.InvalidPropertyException;
-import org.splat.kernel.MissedPropertyException;
-import org.splat.kernel.MultiplyDefinedException;
+import org.splat.exception.BusinessException;
 import org.splat.service.ScenarioService;
 import org.splat.service.SimulationContextService;
+import org.splat.wapp.Constants;
 
 /**
  * Action for creation of a new study.
@@ -36,10 +37,13 @@ public class NewStudyAction extends Action {
         */
        private transient List<SimulationContext> _contelm = null;
        /**
-        * Project context.
+        * Selected project context string value.
         */
        private String _projectContext = null;
-
+       /**
+        * Selected project context id.
+        */
+       private long _projectContextId = -1;
        /**
         * Injected simulation context service.
         */
@@ -49,21 +53,6 @@ public class NewStudyAction extends Action {
         */
        private ScenarioService _scenarioService;
 
-       /**
-        * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
-        */
-       private String _menuProperty;
-
-       /**
-        * 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;
-
        // ==============================================================================================================================
        // Action methods
        // ==============================================================================================================================
@@ -79,16 +68,12 @@ public class NewStudyAction extends Action {
                _contelm = getSimulationContextService().getSimulationContextList();
 
                // set the default name of the new study
-               ResourceBundle locale = ResourceBundle.getBundle("labels",
-                               getApplicationSettings().getCurrentLocale());
-               _title = locale.getString("label.study") + " "
-                               + String.valueOf(number + 1);
+               if (StringUtils.isBlank(_title)) {
+                       _title = getText("label.study") + " " + (number + 1);
+               }
 
-               setMenuProperty("create");
-               setToolProperty("none");
-               setLeftMenuProperty("open");
-               initializationFullScreenContext(_menuProperty, _toolProperty,
-                               _leftMenuProperty);
+               initializationFullScreenContext(Constants.CREATE_MENU, Constants.NONE,
+                               Constants.OPEN);
 
                return SUCCESS;
        }
@@ -97,75 +82,103 @@ public class NewStudyAction extends Action {
         * Create a new study.
         * 
         * @return SUCCESS if the new study is created, INPUT if project context is not defined, ERROR if failed
-        * @throws InvalidPropertyException
-        *             if some property has invalid value
-        * @throws MultiplyDefinedException
-        *             if some property is defined several times
-        * @throws MissedPropertyException
-        *             if properties of the new study are invalid
+        * @throws BusinessException
+        *             if new study creation is failed
+        * @throws IOException
+        *             if file operations are failed
         */
-       public String doCreate() throws InvalidPropertyException,
-                       MissedPropertyException, MultiplyDefinedException {
-               String[] input = _projectContext.split(",");
-               int valid = Integer.valueOf(input[0]);
-               String value = ""; // input[1] if exists
-
-               Study.Properties sprop = new Study.Properties();
+       public String doCreate() throws BusinessException, IOException {
+               String res = SUCCESS;
 
                // Check arguments and creation of the study
-               if (valid == -1) {
+               if (_projectContextId == -1) {
                        SimulationContext.Properties cprop = new SimulationContext.Properties();
                        SimulationContextType product = getSimulationContextService()
                                        .selectType("product");
                        _contelm = getSimulationContextService()
                                        .selectSimulationContextsWhere(cprop.setType(product));
-                       return INPUT; // Title empty, simply wait for input without error message
-               }
-               if (valid == 0) {
-                       value = input[1].trim();
-                       if (value.length() == 0) {
-                               setMenuProperty("create");
-                               initializationScreenContext(_menuProperty);
-                               return INPUT; // No need to reinitialize the list of existing products
+                       // Title empty, simply wait for input without error message
+                       res = INPUT;
+               } else {
+                       String value; // if new a project context has to be created
+                       if (_projectContextId == 0) {
+                               value = _projectContext.trim();
+                               if (value.length() == 0) {
+                                       initializationScreenContext(Constants.CREATE_MENU);
+                                       // No need to reinitialize the list of existing products
+                                       res = INPUT;
+                               }
+                       } else {
+                               value = "";
                        }
-               }
-               sprop.setTitle(_title).setManager(getConnectedUser());
-               sprop.checkValidity();
-               sprop.disableCheck();
-               try {
-                       // Addition of a default scenario
-                       ResourceBundle locale = ResourceBundle.getBundle("labels",
-                                       getApplicationSettings().getCurrentLocale());
-                       Scenario.Properties oprop = new Scenario.Properties();
-                       oprop.setTitle(locale.getString("label.scenario") + " 1");
-
-                       // Addition of the entered project context
-                       SimulationContext.Properties cprop = new SimulationContext.Properties();
-                       if (valid == 0) { // Input of new project context
-                               cprop.setType(
-                                               getSimulationContextService().selectType("product"))
-                                               .setValue(value);
-                       } else { // Selection of existing project context
-                               cprop.setIndex(valid);
+                       if (SUCCESS.equals(res)) {
+                               Study.Properties sprop = new Study.Properties();
+
+                               sprop.setTitle(_title).setManager(getConnectedUser());
+                               sprop.checkValidity();
+                               sprop.disableCheck();
+                               try {
+                                       // Addition of a default scenario
+                                       ResourceBundle locale = ResourceBundle.getBundle("labels",
+                                                       getApplicationSettings().getCurrentLocale());
+                                       Scenario.Properties oprop = new Scenario.Properties();
+                                       oprop.setTitle(locale.getString("label.scenario") + " 1");
+
+                                       // Addition of the entered project context
+                                       SimulationContext.Properties cprop = new SimulationContext.Properties();
+                                       if (_projectContextId == 0) { // Input of new project context
+                                               SimulationContextType product = getSimulationContextService()
+                                                               .selectType("product");
+
+                                               SimulationContext testContext = getSimulationContextService()
+                                                               .selectSimulationContext(product, value);
+
+                                               if (testContext == null) {
+                                                       cprop.setType(
+                                                                       getSimulationContextService().selectType(
+                                                                                       "product")).setValue(value);
+                                               } else {
+                                                       cprop.setIndex(testContext.getIndex());
+                                               }
+                                       } else { // Selection of existing project context
+                                               cprop.setIndex(_projectContextId);
+                                       }
+                                       Study study = getScenarioService().createStudy(sprop,
+                                                       oprop, cprop);
+                                       // Update of the session
+                                       number += 1;
+                                       open(study); // Opens the study,
+
+                                       initializationFullScreenContext(Constants.STUDY_MENU,
+                                                       Constants.NONE, Constants.OPEN);
+
+                               } catch (BusinessException error) {
+                                       LOG.error("Unable to save the study, reason:", error);
+                                       setErrorCode("message.error.newstudy");
+                                       initializationScreenContext(Constants.NONE);
+                                       res = ERROR;
+                               }
                        }
-                       Study study = getScenarioService().createStudy(sprop, oprop, cprop);
-                       // Update of the session
-                       number += 1;
-                       open(study); // Opens the study,
-
-                       setMenuProperty("study");
-                       setToolProperty("none");
-                       setLeftMenuProperty("open");
-                       initializationFullScreenContext(_menuProperty, _toolProperty,
-                                       _leftMenuProperty);
-
-                       return SUCCESS;
-               } catch (Exception error) {
-                       LOG.error("Unable to save the study, reason:", error);
-                       setErrorCode("message.error.newstudy");
-                       setMenuProperty("none");
-                       initializationScreenContext(_menuProperty);
-                       return ERROR;
+               }
+               return res;
+       }
+
+       /**
+        * 
+        * {@inheritDoc}
+        * 
+        * @see com.opensymphony.xwork2.ActionSupport#validate()
+        */
+       @Override
+       public void validate() {
+
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("--- validate");
+               }
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("======> MKA test");
+                       LOG.debug(com.opensymphony.xwork2.ActionContext.getContext()
+                                       .getName());
                }
        }
 
@@ -260,59 +273,21 @@ public class NewStudyAction extends Action {
        }
 
        /**
-        * Get the menuProperty.
-        * 
-        * @return the menuProperty
-        */
-       public String getMenuProperty() {
-               return _menuProperty;
-       }
-
-       /**
-        * Set the menuProperty.
-        * 
-        * @param menuProperty
-        *            the menuProperty to set
-        */
-       public void setMenuProperty(final String menuProperty) {
-               this._menuProperty = menuProperty;
-       }
-
-       /**
-        * 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.
+        * Get the projectContextId.
         * 
-        * @return the leftMenuProperty
+        * @return the projectContextId
         */
-       public String getLeftMenuProperty() {
-               return _leftMenuProperty;
+       public long getProjectContextId() {
+               return _projectContextId;
        }
 
        /**
-        * Set the leftMenuProperty.
+        * Set the projectContextId.
         * 
-        * @param leftMenuProperty
-        *            the leftMenuProperty to set
+        * @param projectContextId
+        *            the projectContextId to set
         */
-       public void setLeftMenuProperty(final String leftMenuProperty) {
-               _leftMenuProperty = leftMenuProperty;
+       public void setProjectContextId(final long projectContextId) {
+               _projectContextId = projectContextId;
        }
 }
\ No newline at end of file