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.SimulationContextService;
15 import org.splat.service.StudyService;
17 public class NewStudyAction extends Action {
19 private String title = null;
20 private List<SimulationContext> contelm = null;
21 private String context = null;
23 private static int number = 0;
27 private static final long serialVersionUID = 693943641800113782L;
30 * The injected Study service.
32 private StudyService _studyService;
33 private SimulationContextService _simulationContextService;
35 // ==============================================================================================================================
37 // ==============================================================================================================================
39 // Fill the values of the drop-down list.
40 public String doInitialize() {
42 // get the list of the simulation contexts of the study
43 contelm = getSimulationContextService().getSimulationContextList();
45 // set the default name of the new study
46 ResourceBundle locale = ResourceBundle.getBundle("labels",
47 ApplicationSettings.getCurrentLocale());
48 title = locale.getString("label.study") + " "
49 + String.valueOf(number + 1);
54 public String doCreate() throws Exception {
55 // -------------------------
56 String[] input = context.split(",");
57 int valid = Integer.valueOf(input[0]);
58 String value = ""; // input[1] if exists
60 Session session = Database.getSession();
61 Transaction transax = session.beginTransaction();
62 Study.Properties sprop = new Study.Properties();
64 // Check arguments and creation of the study
67 throw new Exception();
69 value = input[1].trim();
70 if (value.length() == 0)
71 return INPUT; // No need to reinitialize the list of existing products
73 sprop.setTitle(title).setManager(getConnectedUser());
74 sprop.checkValidity();
76 } catch (Exception error) {
77 SimulationContext.Properties cprop = new SimulationContext.Properties();
78 SimulationContextType product = getSimulationContextService()
79 .selectType("product");
80 contelm = getSimulationContextService()
81 .selectSimulationContextsWhere(cprop.setType(product));
83 return INPUT; // Title empty, simply wait for input without error message
86 Study study = getStudyService().createStudy(sprop);
88 // Addition of a default scenario
89 ResourceBundle locale = ResourceBundle.getBundle("labels",
90 ApplicationSettings.getCurrentLocale());
91 Scenario.Properties oprop = new Scenario.Properties();
92 oprop.setTitle(locale.getString("label.scenario") + " 1");
93 getStudyService().addScenario(study, oprop);
95 // Addition of the entered project context
96 if (valid == 0) { // Input of new project context
97 SimulationContext.Properties cprop = new SimulationContext.Properties();
99 getSimulationContextService().selectType("product"))
101 getStudyService().addProjectContext(study, cprop);
102 } else { // Selection of existing project context
103 SimulationContext context = getSimulationContextService()
104 .selectSimulationContext(valid);
105 getStudyService().addProjectContext(study, context);
107 // Update of the session
109 open(study); // Opens the study,
112 } catch (Exception error) {
113 logger.error("Unable to save the study, reason:", error);
114 if (transax != null && transax.isActive()) {
115 // Second try-catch as the rollback could fail as well
118 } catch (HibernateException backerror) {
119 logger.debug("Error rolling back transaction", backerror);
126 // ==============================================================================================================================
127 // Getters and setters
128 // ==============================================================================================================================
130 public String getProjectContext() {
131 // ----------------------------------
135 public List<SimulationContext> getProjectContextValues() {
136 // ---------------------------------------------------------
140 public String getTitle() {
141 // ----------------------------
145 public void setProjectContext(String value) {
146 // --------------------------------------------
147 this.context = value;
150 public void setTitle(String value) {
151 // -----------------------------------
156 * Get the studyService.
158 * @return the studyService
160 public StudyService getStudyService() {
161 return _studyService;
165 * Set the studyService.
167 * @param studyService
168 * the studyService to set
170 public void setStudyService(StudyService studyService) {
171 _studyService = studyService;
175 * Get the simulationContextService.
177 * @return the simulationContextService
179 public SimulationContextService getSimulationContextService() {
180 return _simulationContextService;
184 * Set the simulationContextService.
186 * @param simulationContextService
187 * the simulationContextService to set
189 public void setSimulationContextService(
190 SimulationContextService simulationContextService) {
191 _simulationContextService = simulationContextService;