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.service.StudyService;
+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;
+ /**
+ * 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 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 StudyService _studyService;
+ 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.
- public String doInitialize () {
-
- //get the list of the simulation contexts of the study
- contelm = _studyService.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);
-
- 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();
+ /**
+ * 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
- 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 = SimulationContext
+ SimulationContextType product = getSimulationContextService()
.selectType("product");
- contelm = Database.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");
- getStudyService().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(SimulationContext.selectType("product"))
- .setValue(value);
- getStudyService().addProjectContext(study, cprop);
- } else { // Selection of existing project context
- SimulationContext context = Database
- .selectSimulationContext(valid);
- getStudyService().addProjectContext(study, context);
+ _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 = "";
}
- // 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());
}
}
// 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;
+ return _title;
}
- public void setProjectContext(String value) {
- // --------------------------------------------
- this.context = value;
+ /**
+ * Set the project context for the new study.
+ *
+ * @param value
+ * the project context value.
+ */
+ public void setProjectContext(final String value) {
+ this._projectContext = value;
}
- public void setTitle(String value) {
- // -----------------------------------
- this.title = 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 studyService.
+ * Get the projectContextId.
*
- * @return the studyService
+ * @return the projectContextId
*/
- public StudyService getStudyService() {
- return _studyService;
+ public long getProjectContextId() {
+ return _projectContextId;
}
/**
- * Set the studyService.
+ * Set the projectContextId.
*
- * @param studyService
- * the studyService to set
+ * @param projectContextId
+ * the projectContextId to set
*/
- public void setStudyService(StudyService studyService) {
- _studyService = studyService;
+ public void setProjectContextId(final long projectContextId) {
+ _projectContextId = projectContextId;
}
}
\ No newline at end of file