]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman/src/org/splat/simer/NewStudyAction.java
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 499ffef35a4be7038d18d2ec96b021cf78b605f4..2058b0881a14a8463403cbc29c083e4573e763aa 100644 (file)
@@ -1,14 +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.exception.BusinessException;
 import org.splat.service.ScenarioService;
 import org.splat.service.SimulationContextService;
+import org.splat.wapp.Constants;
 
 /**
  * Action for creation of a new study.
@@ -27,16 +31,19 @@ public class NewStudyAction extends Action {
        /**
         * Title of the new study.
         */
-       private String title = null;
+       private String _title = null;
        /**
         * List of available project contexts for selection for the new study.
         */
-       private List<SimulationContext> contelm = null;
+       private transient List<SimulationContext> _contelm = null;
        /**
-        * Project context.
+        * Selected project context string value.
         */
-       private String context = null;
-
+       private String _projectContext = null;
+       /**
+        * Selected project context id.
+        */
+       private long _projectContextId = -1;
        /**
         * Injected simulation context service.
         */
@@ -46,11 +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;
-
        // ==============================================================================================================================
        // Action methods
        // ==============================================================================================================================
@@ -63,16 +65,15 @@ public class NewStudyAction extends Action {
        public String doInitialize() {
 
                // get the list of the simulation contexts of the study
-               contelm = getSimulationContextService().getSimulationContextList();
+               _contelm = getSimulationContextService().getSimulationContextList();
 
                // set the default name of the new study
-               ResourceBundle locale = ResourceBundle.getBundle("labels",
-                               ApplicationSettings.getCurrentLocale());
-               title = locale.getString("label.study") + " "
-                               + String.valueOf(number + 1);
+               if (StringUtils.isBlank(_title)) {
+                       _title = getText("label.study") + " " + (number + 1);
+               }
 
-               setMenuProperty("create");
-               initializationScreenContext(_menuProperty);
+               initializationFullScreenContext(Constants.CREATE_MENU, Constants.NONE,
+                               Constants.OPEN);
 
                return SUCCESS;
        }
@@ -81,66 +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 Exception
-        *             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 Exception {
-               String[] input = context.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()
+                       _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",
-                                       ApplicationSettings.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");
-                       initializationScreenContext(_menuProperty);
-
-                       return SUCCESS;
-               } catch (Exception error) {
-                       logger.error("Unable to save the study, reason:", error);
-                       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());
                }
        }
 
@@ -150,42 +188,49 @@ public class NewStudyAction extends Action {
 
        /**
         * Get the selected project context for the new study.
+        * 
         * @return the selected project context
         */
        public String getProjectContext() {
-               return context;
+               return _projectContext;
        }
 
        /**
         * Get the list of available project contexts.
+        * 
         * @return the list of context values
         */
        public List<SimulationContext> getProjectContextValues() {
-               return contelm;
+               return _contelm;
        }
 
        /**
         * Get the title of the new study.
+        * 
         * @return the title
         */
        public String getTitle() {
-               return title;
+               return _title;
        }
 
        /**
         * Set the project context for the new study.
-        * @param value the project context value.
+        * 
+        * @param value
+        *            the project context value.
         */
-       public void setProjectContext(String value) {
-               this.context = value;
+       public void setProjectContext(final String value) {
+               this._projectContext = value;
        }
 
        /**
         * Set the title of the new study.
-        * @param value the title to set
+        * 
+        * @param value
+        *            the title to set
         */
-       public void setTitle(String value) {
-               this.title = value;
+       public void setTitle(final String value) {
+               this._title = value;
        }
 
        /**
@@ -204,7 +249,7 @@ public class NewStudyAction extends Action {
         *            the simulationContextService to set
         */
        public void setSimulationContextService(
-                       SimulationContextService simulationContextService) {
+                       final SimulationContextService simulationContextService) {
                _simulationContextService = simulationContextService;
        }
 
@@ -223,26 +268,26 @@ public class NewStudyAction extends Action {
         * @param scenarioService
         *            the scenarioService to set
         */
-       public void setScenarioService(ScenarioService scenarioService) {
+       public void setScenarioService(final ScenarioService scenarioService) {
                _scenarioService = scenarioService;
        }
 
        /**
-        * Get the menuProperty.
+        * Get the projectContextId.
         * 
-        * @return the menuProperty
+        * @return the projectContextId
         */
-       public String getMenuProperty() {
-               return _menuProperty;
+       public long getProjectContextId() {
+               return _projectContextId;
        }
 
        /**
-        * Set the menuProperty.
+        * Set the projectContextId.
         * 
-        * @param menuProperty
-        *            the menuProperty to set
+        * @param projectContextId
+        *            the projectContextId to set
         */
-       public void setMenuProperty(String menuProperty) {
-               this._menuProperty = menuProperty;
+       public void setProjectContextId(final long projectContextId) {
+               _projectContextId = projectContextId;
        }
 }
\ No newline at end of file