Salome HOME
05da635e63b716178bdd23be896c30ae9086390b
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewStudyAction.java
1 package org.splat.simer;
2
3 import java.util.List;
4 import java.util.ResourceBundle;
5
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;
16
17 public class NewStudyAction extends Action {
18
19         private String title = null;
20         private List<SimulationContext> contelm = null;
21         private String context = null;
22
23         private static int number = 0;
24         /**
25          * Serial version ID.
26          */
27         private static final long serialVersionUID = 693943641800113782L;
28
29         /**
30          * The injected Study service.
31          */
32         private StudyService _studyService;
33         private SimulationContextService _simulationContextService;
34
35         // ==============================================================================================================================
36         // Action methods
37         // ==============================================================================================================================
38
39         // Fill the values of the drop-down list.
40         public String doInitialize() {
41
42                 // get the list of the simulation contexts of the study
43                 contelm = getSimulationContextService().getSimulationContextList();
44
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);
50
51                 return SUCCESS;
52         }
53
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
59
60                 Session session = Database.getSession();
61                 Transaction transax = session.beginTransaction();
62                 Study.Properties sprop = new Study.Properties();
63
64                 // Check arguments and creation of the study
65                 try {
66                         if (valid == -1)
67                                 throw new Exception();
68                         if (valid == 0) {
69                                 value = input[1].trim();
70                                 if (value.length() == 0)
71                                         return INPUT; // No need to reinitialize the list of existing products
72                         }
73                         sprop.setTitle(title).setManager(getConnectedUser());
74                         sprop.checkValidity();
75                         sprop.disableCheck();
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));
82                         transax.commit();
83                         return INPUT; // Title empty, simply wait for input without error message
84                 }
85                 try {
86                         Study study = getStudyService().createStudy(sprop);
87
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);
94
95                         // Addition of the entered project context
96                         if (valid == 0) { // Input of new project context
97                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
98                                 cprop.setType(
99                                                 getSimulationContextService().selectType("product"))
100                                                 .setValue(value);
101                                 getStudyService().addProjectContext(study, cprop);
102                         } else { // Selection of existing project context
103                                 SimulationContext context = getSimulationContextService()
104                                                 .selectSimulationContext(valid);
105                                 getStudyService().addProjectContext(study, context);
106                         }
107                         // Update of the session
108                         number += 1;
109                         open(study); // Opens the study,
110                         transax.commit();
111                         return SUCCESS;
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
116                                 try {
117                                         transax.rollback();
118                                 } catch (HibernateException backerror) {
119                                         logger.debug("Error rolling back transaction", backerror);
120                                 }
121                         }
122                         return ERROR;
123                 }
124         }
125
126         // ==============================================================================================================================
127         // Getters and setters
128         // ==============================================================================================================================
129
130         public String getProjectContext() {
131                 // ----------------------------------
132                 return context;
133         }
134
135         public List<SimulationContext> getProjectContextValues() {
136                 // ---------------------------------------------------------
137                 return contelm;
138         }
139
140         public String getTitle() {
141                 // ----------------------------
142                 return title;
143         }
144
145         public void setProjectContext(String value) {
146                 // --------------------------------------------
147                 this.context = value;
148         }
149
150         public void setTitle(String value) {
151                 // -----------------------------------
152                 this.title = value;
153         }
154
155         /**
156          * Get the studyService.
157          * 
158          * @return the studyService
159          */
160         public StudyService getStudyService() {
161                 return _studyService;
162         }
163
164         /**
165          * Set the studyService.
166          * 
167          * @param studyService
168          *            the studyService to set
169          */
170         public void setStudyService(StudyService studyService) {
171                 _studyService = studyService;
172         }
173
174         /**
175          * Get the simulationContextService.
176          * 
177          * @return the simulationContextService
178          */
179         public SimulationContextService getSimulationContextService() {
180                 return _simulationContextService;
181         }
182
183         /**
184          * Set the simulationContextService.
185          * 
186          * @param simulationContextService
187          *            the simulationContextService to set
188          */
189         public void setSimulationContextService(
190                         SimulationContextService simulationContextService) {
191                 _simulationContextService = simulationContextService;
192         }
193 }