1 package org.splat.simer;
4 import java.util.ResourceBundle;
6 import org.hibernate.HibernateException;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.splat.dal.dao.som.Database;
10 import org.splat.dal.bo.som.Scenario;
11 import org.splat.dal.bo.som.SimulationContext;
12 import org.splat.dal.bo.som.SimulationContextType;
13 import org.splat.dal.bo.som.Study;
14 import org.splat.service.StudyService;
16 public class NewStudyAction extends Action {
18 private String title = null;
19 private List<SimulationContext> contelm = null;
20 private String context = null;
22 private static int number = 0;
23 private static final long serialVersionUID = 693943641800113782L;
25 private StudyService _studyService;
27 // ==============================================================================================================================
29 // ==============================================================================================================================
31 public String doInitialize() {
32 // -----------------------------
33 Session connex = Database.getSession();
34 Transaction transax = connex.beginTransaction();
36 SimulationContext.Properties cprop = new SimulationContext.Properties();
37 SimulationContextType product = SimulationContext.selectType("product");
38 ResourceBundle locale = ResourceBundle.getBundle("labels",
39 ApplicationSettings.getCurrentLocale());
42 .selectSimulationContextsWhere(cprop.setType(product));
43 title = locale.getString("label.study") + " "
44 + String.valueOf(number + 1);
49 public String doCreate() throws Exception {
50 // -------------------------
51 String[] input = context.split(",");
52 int valid = Integer.valueOf(input[0]);
53 String value = ""; // input[1] if exists
55 Session session = Database.getSession();
56 Transaction transax = session.beginTransaction();
57 Study.Properties sprop = new Study.Properties();
59 // Check arguments and creation of the study
62 throw new Exception();
64 value = input[1].trim();
65 if (value.length() == 0)
66 return INPUT; // No need to reinitialize the list of existing products
68 sprop.setTitle(title).setManager(getConnectedUser());
69 sprop.checkValidity();
71 } catch (Exception error) {
72 SimulationContext.Properties cprop = new SimulationContext.Properties();
73 SimulationContextType product = SimulationContext
74 .selectType("product");
75 contelm = Database.selectSimulationContextsWhere(cprop
78 return INPUT; // Title empty, simply wait for input without error message
81 Study study = getStudyService().createStudy(sprop);
83 // Addition of a default scenario
84 ResourceBundle locale = ResourceBundle.getBundle("labels",
85 ApplicationSettings.getCurrentLocale());
86 Scenario.Properties oprop = new Scenario.Properties();
87 oprop.setTitle(locale.getString("label.scenario") + " 1");
88 getStudyService().addScenario(study, oprop);
90 // Addition of the entered project context
91 if (valid == 0) { // Input of new project context
92 SimulationContext.Properties cprop = new SimulationContext.Properties();
93 cprop.setType(SimulationContext.selectType("product"))
95 getStudyService().addProjectContext(study, cprop);
96 } else { // Selection of existing project context
97 SimulationContext context = Database
98 .selectSimulationContext(valid);
99 getStudyService().addProjectContext(study, context);
101 // Update of the session
103 open(study); // Opens the study,
106 } catch (Exception error) {
107 logger.error("Unable to save the study, reason:", error);
108 if (transax != null && transax.isActive()) {
109 // Second try-catch as the rollback could fail as well
112 } catch (HibernateException backerror) {
113 logger.debug("Error rolling back transaction", backerror);
120 // ==============================================================================================================================
121 // Getters and setters
122 // ==============================================================================================================================
124 public String getProjectContext() {
125 // ----------------------------------
129 public List<SimulationContext> getProjectContextValues() {
130 // ---------------------------------------------------------
134 public String getTitle() {
135 // ----------------------------
139 public void setProjectContext(String value) {
140 // --------------------------------------------
141 this.context = value;
144 public void setTitle(String value) {
145 // -----------------------------------
150 * Get the studyService.
152 * @return the studyService
154 public StudyService getStudyService() {
155 return _studyService;
159 * Set the studyService.
161 * @param studyService
162 * the studyService to set
164 public void setStudyService(StudyService studyService) {
165 _studyService = studyService;