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.ScenarioService;
15 import org.splat.service.SimulationContextService;
16 import org.splat.service.StudyService;
18 public class NewStudyAction extends Action {
20 private String title = null;
21 private List<SimulationContext> contelm = null;
22 private String context = null;
24 private static int number = 0;
28 private static final long serialVersionUID = 693943641800113782L;
31 * The injected Study service.
33 private StudyService _studyService;
34 private SimulationContextService _simulationContextService;
36 * Injected scenario service.
38 private ScenarioService _scenarioService;
40 // ==============================================================================================================================
42 // ==============================================================================================================================
44 // Fill the values of the drop-down list.
45 public String doInitialize() {
47 // get the list of the simulation contexts of the study
48 contelm = getSimulationContextService().getSimulationContextList();
50 // set the default name of the new study
51 ResourceBundle locale = ResourceBundle.getBundle("labels",
52 ApplicationSettings.getCurrentLocale());
53 title = locale.getString("label.study") + " "
54 + String.valueOf(number + 1);
59 public String doCreate() throws Exception {
60 // -------------------------
61 String[] input = context.split(",");
62 int valid = Integer.valueOf(input[0]);
63 String value = ""; // input[1] if exists
65 Session session = Database.getSession();
66 Transaction transax = session.beginTransaction();
67 Study.Properties sprop = new Study.Properties();
69 // Check arguments and creation of the study
72 throw new Exception();
74 value = input[1].trim();
75 if (value.length() == 0)
76 return INPUT; // No need to reinitialize the list of existing products
78 sprop.setTitle(title).setManager(getConnectedUser());
79 sprop.checkValidity();
81 } catch (Exception error) {
82 SimulationContext.Properties cprop = new SimulationContext.Properties();
83 SimulationContextType product = getSimulationContextService()
84 .selectType("product");
85 contelm = getSimulationContextService()
86 .selectSimulationContextsWhere(cprop.setType(product));
88 return INPUT; // Title empty, simply wait for input without error message
91 Study study = getStudyService().createStudy(sprop);
93 // Addition of a default scenario
94 ResourceBundle locale = ResourceBundle.getBundle("labels",
95 ApplicationSettings.getCurrentLocale());
96 Scenario.Properties oprop = new Scenario.Properties();
97 oprop.setTitle(locale.getString("label.scenario") + " 1");
98 getScenarioService().addScenario(study, oprop);
100 // Addition of the entered project context
101 if (valid == 0) { // Input of new project context
102 SimulationContext.Properties cprop = new SimulationContext.Properties();
104 getSimulationContextService().selectType("product"))
106 getStudyService().addProjectContext(study, cprop);
107 } else { // Selection of existing project context
108 SimulationContext context = getSimulationContextService()
109 .selectSimulationContext(valid);
110 getStudyService().addProjectContext(study, context);
112 // Update of the session
114 open(study); // Opens the study,
117 } catch (Exception error) {
118 logger.error("Unable to save the study, reason:", error);
119 if (transax != null && transax.isActive()) {
120 // Second try-catch as the rollback could fail as well
123 } catch (HibernateException backerror) {
124 logger.debug("Error rolling back transaction", backerror);
131 // ==============================================================================================================================
132 // Getters and setters
133 // ==============================================================================================================================
135 public String getProjectContext() {
136 // ----------------------------------
140 public List<SimulationContext> getProjectContextValues() {
141 // ---------------------------------------------------------
145 public String getTitle() {
146 // ----------------------------
150 public void setProjectContext(String value) {
151 // --------------------------------------------
152 this.context = value;
155 public void setTitle(String value) {
156 // -----------------------------------
161 * Get the studyService.
163 * @return the studyService
165 public StudyService getStudyService() {
166 return _studyService;
170 * Set the studyService.
172 * @param studyService
173 * the studyService to set
175 public void setStudyService(StudyService studyService) {
176 _studyService = studyService;
180 * Get the simulationContextService.
182 * @return the simulationContextService
184 public SimulationContextService getSimulationContextService() {
185 return _simulationContextService;
189 * Set the simulationContextService.
191 * @param simulationContextService
192 * the simulationContextService to set
194 public void setSimulationContextService(
195 SimulationContextService simulationContextService) {
196 _simulationContextService = simulationContextService;
200 * Get the scenarioService.
201 * @return the scenarioService
203 public ScenarioService getScenarioService() {
204 return _scenarioService;
208 * Set the scenarioService.
209 * @param scenarioService the scenarioService to set
211 public void setScenarioService(ScenarioService scenarioService) {
212 _scenarioService = scenarioService;