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.som.Database;
10 import org.splat.som.Scenario;
11 import org.splat.som.SimulationContext;
12 import org.splat.som.SimulationContextType;
13 import org.splat.som.Study;
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 // ==============================================================================================================================
27 // ==============================================================================================================================
29 public String doInitialize () {
30 // -----------------------------
31 Session connex = Database.getSession();
32 Transaction transax = connex.beginTransaction();
34 SimulationContext.Properties cprop = new SimulationContext.Properties();
35 SimulationContextType product = SimulationContext.selectType("product");
36 ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
38 contelm = Database.selectSimulationContextsWhere(cprop.setType(product));
39 title = locale.getString("label.study") + " " + String.valueOf(number + 1);
44 public String doCreate () throws Exception {
45 // -------------------------
46 String[] input = context.split(",");
47 int valid = Integer.valueOf(input[0]);
48 String value = ""; // input[1] if exists
50 Session session = Database.getSession();
51 Transaction transax = session.beginTransaction();
52 Study.Properties sprop = new Study.Properties();
54 // Check arguments and creation of the study
56 if (valid == -1) throw new Exception();
58 value = input[1].trim();
59 if (value.length() == 0) return INPUT; // No need to reinitialize the list of existing products
61 sprop.setTitle(title).setManager(getConnectedUser());
62 sprop.checkValidity();
65 catch (Exception error) {
66 SimulationContext.Properties cprop = new SimulationContext.Properties();
67 SimulationContextType product = SimulationContext.selectType("product");
68 contelm = Database.selectSimulationContextsWhere(cprop.setType(product));
70 return INPUT; // Title empty, simply wait for input without error message
73 Study study = Database.createStudy(sprop);
75 // Addition of a default scenario
76 ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
77 Scenario.Properties oprop = new Scenario.Properties();
78 oprop.setTitle(locale.getString("label.scenario") + " 1");
79 study.addScenario(oprop);
81 // Addition of the entered project context
82 if (valid == 0) { // Input of new project context
83 SimulationContext.Properties cprop = new SimulationContext.Properties();
84 cprop.setType(SimulationContext.selectType("product")).setValue(value);
85 study.addProjectContext(cprop);
87 else { // Selection of existing project context
88 SimulationContext context = Database.selectSimulationContext(valid);
89 study.addProjectContext(context);
91 // Update of the session
93 open(study); // Opens the study,
97 catch (Exception error) {
98 logger.error("Unable to save the study, reason:", error);
99 if (transax != null && transax.isActive()) {
100 // Second try-catch as the rollback could fail as well
103 } catch (HibernateException backerror) {
104 logger.debug("Error rolling back transaction", backerror);
111 // ==============================================================================================================================
112 // Getters and setters
113 // ==============================================================================================================================
115 public String getProjectContext () {
116 // ----------------------------------
119 public List<SimulationContext> getProjectContextValues () {
120 // ---------------------------------------------------------
123 public String getTitle () {
124 // ----------------------------
128 public void setProjectContext (String value) {
129 // --------------------------------------------
130 this.context = value;
132 public void setTitle (String value) {
133 // -----------------------------------