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;
29 private StudyService _studyService;
31 // ==============================================================================================================================
33 // ==============================================================================================================================
35 //Fill the values of the drop-down list.
36 public String doInitialize () {
38 //get the list of the simulation contexts of the study
39 contelm = _studyService.getSimulationContextList();
41 //set the default name of the new study
42 ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
43 title = locale.getString("label.study") + " " + String.valueOf(number + 1);
48 public String doCreate() throws Exception {
49 // -------------------------
50 String[] input = context.split(",");
51 int valid = Integer.valueOf(input[0]);
52 String value = ""; // input[1] if exists
54 Session session = Database.getSession();
55 Transaction transax = session.beginTransaction();
56 Study.Properties sprop = new Study.Properties();
58 // Check arguments and creation of the study
61 throw new Exception();
63 value = input[1].trim();
64 if (value.length() == 0)
65 return INPUT; // No need to reinitialize the list of existing products
67 sprop.setTitle(title).setManager(getConnectedUser());
68 sprop.checkValidity();
70 } catch (Exception error) {
71 SimulationContext.Properties cprop = new SimulationContext.Properties();
72 SimulationContextType product = SimulationContext
73 .selectType("product");
74 contelm = Database.selectSimulationContextsWhere(cprop
77 return INPUT; // Title empty, simply wait for input without error message
80 Study study = getStudyService().createStudy(sprop);
82 // Addition of a default scenario
83 ResourceBundle locale = ResourceBundle.getBundle("labels",
84 ApplicationSettings.getCurrentLocale());
85 Scenario.Properties oprop = new Scenario.Properties();
86 oprop.setTitle(locale.getString("label.scenario") + " 1");
87 getStudyService().addScenario(study, oprop);
89 // Addition of the entered project context
90 if (valid == 0) { // Input of new project context
91 SimulationContext.Properties cprop = new SimulationContext.Properties();
92 cprop.setType(SimulationContext.selectType("product"))
94 getStudyService().addProjectContext(study, cprop);
95 } else { // Selection of existing project context
96 SimulationContext context = Database
97 .selectSimulationContext(valid);
98 getStudyService().addProjectContext(study, context);
100 // Update of the session
102 open(study); // Opens the study,
105 } catch (Exception error) {
106 logger.error("Unable to save the study, reason:", error);
107 if (transax != null && transax.isActive()) {
108 // Second try-catch as the rollback could fail as well
111 } catch (HibernateException backerror) {
112 logger.debug("Error rolling back transaction", backerror);
119 // ==============================================================================================================================
120 // Getters and setters
121 // ==============================================================================================================================
123 public String getProjectContext() {
124 // ----------------------------------
128 public List<SimulationContext> getProjectContextValues() {
129 // ---------------------------------------------------------
133 public String getTitle() {
134 // ----------------------------
138 public void setProjectContext(String value) {
139 // --------------------------------------------
140 this.context = value;
143 public void setTitle(String value) {
144 // -----------------------------------
149 * Get the studyService.
151 * @return the studyService
153 public StudyService getStudyService() {
154 return _studyService;
158 * Set the studyService.
160 * @param studyService
161 * the studyService to set
163 public void setStudyService(StudyService studyService) {
164 _studyService = studyService;