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 ca76f5e7a9cdce521bede632109b914d1af91ead..2058b0881a14a8463403cbc29c083e4573e763aa 100644 (file)
 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.som.Database;
-import org.splat.som.Scenario;
-import org.splat.som.SimulationContext;
-import org.splat.som.SimulationContextType;
-import org.splat.som.Study;
-
+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.
+ */
 public class NewStudyAction extends Action {
 
-    private String                  title   = null;
-       private List<SimulationContext> contelm = null;
-    private String                  context = null;
-    
-    private static       int        number           = 0;
-       private static final long       serialVersionUID = 693943641800113782L;
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------      
-      Session      connex  = Database.getSession();
-         Transaction  transax = connex.beginTransaction();       
-
-         SimulationContext.Properties cprop   = new SimulationContext.Properties();
-      SimulationContextType        product = SimulationContext.selectType("product");
-      ResourceBundle               locale  = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
-         
-      contelm = Database.selectSimulationContextsWhere(cprop.setType(product));
-      title   = locale.getString("label.study") + " " + String.valueOf(number + 1);
-         transax.commit();
-      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();
-
-//    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) {
-        SimulationContext.Properties cprop   = new SimulationContext.Properties();
-        SimulationContextType        product = SimulationContext.selectType("product");
-        contelm = Database.selectSimulationContextsWhere(cprop.setType(product));
-        transax.commit();
-               return INPUT;        // Title empty, simply wait for input without error message
-         }
-         try {
-        Study study = Database.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");
-               study.addScenario(oprop);
-               
-//      Addition of the entered project context
-               if (valid == 0) {     // Input of new project context
-             SimulationContext.Properties cprop = new SimulationContext.Properties();
-                 cprop.setType(SimulationContext.selectType("product")).setValue(value);
-          study.addProjectContext(cprop);
+       /**
+        * 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;
+
+       /**
+        * 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 SimulationContextService _simulationContextService;
+       /**
+        * Injected scenario service.
+        */
+       private ScenarioService _scenarioService;
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       /**
+        * 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();
+
+               // set the default name of the new study
+               if (StringUtils.isBlank(_title)) {
+                       _title = getText("label.study") + " " + (number + 1);
+               }
+
+               initializationFullScreenContext(Constants.CREATE_MENU, Constants.NONE,
+                               Constants.OPEN);
+
+               return SUCCESS;
+       }
+
+       /**
+        * 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
+               if (_projectContextId == -1) {
+                       SimulationContext.Properties cprop = new SimulationContext.Properties();
+                       SimulationContextType product = getSimulationContextService()
+                                       .selectType("product");
+                       _contelm = getSimulationContextService()
+                                       .selectSimulationContextsWhere(cprop.setType(product));
+                       // 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 = "";
+                       }
+                       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;
+                               }
+                       }
+               }
+               return res;
+       }
+
+       /**
+        * 
+        * {@inheritDoc}
+        * 
+        * @see com.opensymphony.xwork2.ActionSupport#validate()
+        */
+       @Override
+       public void validate() {
+
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("--- validate");
                }
-               else {                // Selection of existing project context
-          SimulationContext context = Database.selectSimulationContext(valid);
-          study.addProjectContext(context);
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("======> MKA test");
+                       LOG.debug(com.opensymphony.xwork2.ActionContext.getContext()
+                                       .getName());
                }
-//      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
-          try {
-               transax.rollback();
-          } catch (HibernateException backerror) {
-            logger.debug("Error rolling back transaction", backerror);
-          }
-        }
-        return ERROR;
-      }
        }
 
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-
-    public String getProjectContext () {
-//  ----------------------------------
-      return context;
-    }
-    public List<SimulationContext> getProjectContextValues () {
-//  ---------------------------------------------------------
-      return contelm;
-    }
-    public String getTitle () {
-//  ----------------------------       
-      return title;
-    }
-
-    public void setProjectContext (String value) {
-//  --------------------------------------------
-      this.context = value;
-    }
-    public void setTitle (String value) {
-//  -----------------------------------        
-      this.title = value;
-    }
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       /**
+        * Get the selected project context for the new study.
+        * 
+        * @return the selected project context
+        */
+       public String getProjectContext() {
+               return _projectContext;
+       }
+
+       /**
+        * Get the list of available project contexts.
+        * 
+        * @return the list of context values
+        */
+       public List<SimulationContext> getProjectContextValues() {
+               return _contelm;
+       }
+
+       /**
+        * Get the title of the new study.
+        * 
+        * @return the title
+        */
+       public String getTitle() {
+               return _title;
+       }
+
+       /**
+        * Set the project context for the new study.
+        * 
+        * @param value
+        *            the project context value.
+        */
+       public void setProjectContext(final String value) {
+               this._projectContext = value;
+       }
+
+       /**
+        * Set the title of the new study.
+        * 
+        * @param value
+        *            the title to set
+        */
+       public void setTitle(final String value) {
+               this._title = value;
+       }
+
+       /**
+        * Get the simulationContextService.
+        * 
+        * @return the simulationContextService
+        */
+       public SimulationContextService getSimulationContextService() {
+               return _simulationContextService;
+       }
+
+       /**
+        * Set the simulationContextService.
+        * 
+        * @param simulationContextService
+        *            the simulationContextService to set
+        */
+       public void setSimulationContextService(
+                       final SimulationContextService simulationContextService) {
+               _simulationContextService = simulationContextService;
+       }
+
+       /**
+        * Get the scenarioService.
+        * 
+        * @return the scenarioService
+        */
+       public ScenarioService getScenarioService() {
+               return _scenarioService;
+       }
+
+       /**
+        * Set the scenarioService.
+        * 
+        * @param scenarioService
+        *            the scenarioService to set
+        */
+       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