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 82755c5d540a0f444b075835030f7165750c337c..2058b0881a14a8463403cbc29c083e4573e763aa 100644 (file)
@@ -1,36 +1,52 @@
 package org.splat.simer;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.ResourceBundle;
 
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.splat.dal.dao.som.Database;
+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.service.StudyService;
+import org.splat.wapp.Constants;
 
+/**
+ * Action for creation of a new study.
+ */
 public class NewStudyAction extends Action {
 
-       private String title = null;
-       private List<SimulationContext> contelm = null;
-       private String context = null;
-
-       private static int number = 0;
        /**
         * Serial version ID.
         */
        private static final long serialVersionUID = 693943641800113782L;
+       /**
+        * Sequential number of the new study for appending to its default title.
+        */
+       private static int number = 0;
 
        /**
-        * The injected Study service.
+        * Title of the new study.
+        */
+       private String _title = null;
+       /**
+        * List of available project contexts for selection for the new study.
+        */
+       private transient List<SimulationContext> _contelm = null;
+       /**
+        * Selected project context string value.
+        */
+       private String _projectContext = null;
+       /**
+        * Selected project context id.
+        */
+       private long _projectContextId = -1;
+       /**
+        * Injected simulation context service.
         */
-       private StudyService _studyService;
        private SimulationContextService _simulationContextService;
        /**
         * Injected scenario service.
@@ -41,90 +57,128 @@ public class NewStudyAction extends Action {
        // Action methods
        // ==============================================================================================================================
 
-       // Fill the values of the drop-down list.
+       /**
+        * Fill the values of the drop-down list, and initialize a menu.
+        * 
+        * @return SUCCESS
+        */
        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);
+               }
+
+               initializationFullScreenContext(Constants.CREATE_MENU, Constants.NONE,
+                               Constants.OPEN);
 
                return SUCCESS;
        }
 
-       public String doCreate() throws Exception {
-               // -------------------------
-               String[] input = context.split(",");
-               int valid = Integer.valueOf(input[0]);
-               String value = ""; // input[1] if exists
-
-               Session session = Database.getSession();
-               Transaction transax = session.beginTransaction();
-               Study.Properties sprop = new Study.Properties();
+       /**
+        * Create a new study.
+        * 
+        * @return SUCCESS if the new study is created, INPUT if project context is not defined, ERROR if failed
+        * @throws BusinessException
+        *             if new study creation is failed
+        * @throws IOException
+        *             if file operations are failed
+        */
+       public String doCreate() throws BusinessException, IOException {
+               String res = SUCCESS;
 
                // Check arguments and creation of the study
-               try {
-                       if (valid == -1)
-                               throw new Exception();
-                       if (valid == 0) {
-                               value = input[1].trim();
-                               if (value.length() == 0)
-                                       return INPUT; // No need to reinitialize the list of existing products
-                       }
-                       sprop.setTitle(title).setManager(getConnectedUser());
-                       sprop.checkValidity();
-                       sprop.disableCheck();
-               } catch (Exception error) {
+               if (_projectContextId == -1) {
                        SimulationContext.Properties cprop = new SimulationContext.Properties();
                        SimulationContextType product = getSimulationContextService()
                                        .selectType("product");
-                       contelm = getSimulationContextService()
+                       _contelm = getSimulationContextService()
                                        .selectSimulationContextsWhere(cprop.setType(product));
-                       transax.commit();
-                       return INPUT; // Title empty, simply wait for input without error message
-               }
-               try {
-                       Study study = getStudyService().createStudy(sprop);
-
-                       // 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");
-                       getScenarioService().addScenario(study, oprop);
-
-                       // Addition of the entered project context
-                       if (valid == 0) { // Input of new project context
-                               SimulationContext.Properties cprop = new SimulationContext.Properties();
-                               cprop.setType(
-                                               getSimulationContextService().selectType("product"))
-                                               .setValue(value);
-                               getStudyService().addProjectContext(study, cprop);
-                       } else { // Selection of existing project context
-                               SimulationContext context = getSimulationContextService()
-                                               .selectSimulationContext(valid);
-                               getStudyService().addProjectContext(study, context);
+                       // 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 = "";
                        }
-                       // Update of the session
-                       number += 1;
-                       open(study); // Opens the study,
-                       transax.commit();
-                       return SUCCESS;
-               } catch (Exception error) {
-                       logger.error("Unable to save the study, reason:", error);
-                       if (transax != null && transax.isActive()) {
-                               // Second try-catch as the rollback could fail as well
+                       if (SUCCESS.equals(res)) {
+                               Study.Properties sprop = new Study.Properties();
+
+                               sprop.setTitle(_title).setManager(getConnectedUser());
+                               sprop.checkValidity();
+                               sprop.disableCheck();
                                try {
-                                       transax.rollback();
-                               } catch (HibernateException backerror) {
-                                       logger.debug("Error rolling back transaction", backerror);
+                                       // 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;
                                }
                        }
-                       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());
                }
        }
 
@@ -132,48 +186,51 @@ public class NewStudyAction extends Action {
        // Getters and setters
        // ==============================================================================================================================
 
+       /**
+        * 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;
-       }
-
-       public void setProjectContext(String value) {
-               // --------------------------------------------
-               this.context = value;
-       }
-
-       public void setTitle(String value) {
-               // -----------------------------------
-               this.title = value;
+               return _title;
        }
 
        /**
-        * Get the studyService.
+        * Set the project context for the new study.
         * 
-        * @return the studyService
+        * @param value
+        *            the project context value.
         */
-       public StudyService getStudyService() {
-               return _studyService;
+       public void setProjectContext(final String value) {
+               this._projectContext = value;
        }
 
        /**
-        * Set the studyService.
+        * Set the title of the new study.
         * 
-        * @param studyService
-        *            the studyService to set
+        * @param value
+        *            the title to set
         */
-       public void setStudyService(StudyService studyService) {
-               _studyService = studyService;
+       public void setTitle(final String value) {
+               this._title = value;
        }
 
        /**
@@ -192,12 +249,13 @@ public class NewStudyAction extends Action {
         *            the simulationContextService to set
         */
        public void setSimulationContextService(
-                       SimulationContextService simulationContextService) {
+                       final SimulationContextService simulationContextService) {
                _simulationContextService = simulationContextService;
        }
 
        /**
         * Get the scenarioService.
+        * 
         * @return the scenarioService
         */
        public ScenarioService getScenarioService() {
@@ -206,9 +264,30 @@ public class NewStudyAction extends Action {
 
        /**
         * Set the scenarioService.
-        * @param scenarioService the scenarioService to set
+        * 
+        * @param scenarioService
+        *            the scenarioService to set
         */
-       public void setScenarioService(ScenarioService scenarioService) {
+       public void setScenarioService(final ScenarioService scenarioService) {
                _scenarioService = scenarioService;
        }
+
+       /**
+        * Get the projectContextId.
+        * 
+        * @return the projectContextId
+        */
+       public long getProjectContextId() {
+               return _projectContextId;
+       }
+
+       /**
+        * Set the projectContextId.
+        * 
+        * @param projectContextId
+        *            the projectContextId to set
+        */
+       public void setProjectContextId(final long projectContextId) {
+               _projectContextId = projectContextId;
+       }
 }
\ No newline at end of file