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.splat.dal.dao.som.Database;
+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;
public class NewStudyAction extends Action {
- private String title = null;
+ 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);
+ private String context = null;
+
+ private static int number = 0;
+ private static final long serialVersionUID = 693943641800113782L;
+
+ private StudyService _studyService;
+
+ // ==============================================================================================================================
+ // 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
}
- else { // Selection of existing project context
- SimulationContext context = Database.selectSimulationContext(valid);
- study.addProjectContext(context);
+ 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);
+ }
+ // 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;
}
-// 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
+ // ==============================================================================================================================
+
+ 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;
+ }
+
+ /**
+ * Get the studyService.
+ *
+ * @return the studyService
+ */
+ public StudyService getStudyService() {
+ return _studyService;
+ }
+
+ /**
+ * Set the studyService.
+ *
+ * @param studyService
+ * the studyService to set
+ */
+ public void setStudyService(StudyService studyService) {
+ _studyService = studyService;
+ }
}
\ No newline at end of file