]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/NewStudyAction.java
Salome HOME
More business logic has been moved from BO to services. ServiceLocator is created...
[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.ScenarioService;
15 import org.splat.service.SimulationContextService;
16 import org.splat.service.StudyService;
17
18 public class NewStudyAction extends Action {
19
20         private String title = null;
21         private List<SimulationContext> contelm = null;
22         private String context = null;
23
24         private static int number = 0;
25         /**
26          * Serial version ID.
27          */
28         private static final long serialVersionUID = 693943641800113782L;
29
30         /**
31          * The injected Study service.
32          */
33         private StudyService _studyService;
34         private SimulationContextService _simulationContextService;
35         /**
36          * Injected scenario service.
37          */
38         private ScenarioService _scenarioService;
39
40         // ==============================================================================================================================
41         // Action methods
42         // ==============================================================================================================================
43
44         // Fill the values of the drop-down list.
45         public String doInitialize() {
46
47                 // get the list of the simulation contexts of the study
48                 contelm = getSimulationContextService().getSimulationContextList();
49
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);
55
56                 return SUCCESS;
57         }
58
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
64
65                 Session session = Database.getSession();
66                 Transaction transax = session.beginTransaction();
67                 Study.Properties sprop = new Study.Properties();
68
69                 // Check arguments and creation of the study
70                 try {
71                         if (valid == -1)
72                                 throw new Exception();
73                         if (valid == 0) {
74                                 value = input[1].trim();
75                                 if (value.length() == 0)
76                                         return INPUT; // No need to reinitialize the list of existing products
77                         }
78                         sprop.setTitle(title).setManager(getConnectedUser());
79                         sprop.checkValidity();
80                         sprop.disableCheck();
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));
87                         transax.commit();
88                         return INPUT; // Title empty, simply wait for input without error message
89                 }
90                 try {
91                         Study study = getStudyService().createStudy(sprop);
92
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);
99
100                         // Addition of the entered project context
101                         if (valid == 0) { // Input of new project context
102                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
103                                 cprop.setType(
104                                                 getSimulationContextService().selectType("product"))
105                                                 .setValue(value);
106                                 getStudyService().addProjectContext(study, cprop);
107                         } else { // Selection of existing project context
108                                 SimulationContext context = getSimulationContextService()
109                                                 .selectSimulationContext(valid);
110                                 getStudyService().addProjectContext(study, context);
111                         }
112                         // Update of the session
113                         number += 1;
114                         open(study); // Opens the study,
115                         transax.commit();
116                         return SUCCESS;
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
121                                 try {
122                                         transax.rollback();
123                                 } catch (HibernateException backerror) {
124                                         logger.debug("Error rolling back transaction", backerror);
125                                 }
126                         }
127                         return ERROR;
128                 }
129         }
130
131         // ==============================================================================================================================
132         // Getters and setters
133         // ==============================================================================================================================
134
135         public String getProjectContext() {
136                 // ----------------------------------
137                 return context;
138         }
139
140         public List<SimulationContext> getProjectContextValues() {
141                 // ---------------------------------------------------------
142                 return contelm;
143         }
144
145         public String getTitle() {
146                 // ----------------------------
147                 return title;
148         }
149
150         public void setProjectContext(String value) {
151                 // --------------------------------------------
152                 this.context = value;
153         }
154
155         public void setTitle(String value) {
156                 // -----------------------------------
157                 this.title = value;
158         }
159
160         /**
161          * Get the studyService.
162          * 
163          * @return the studyService
164          */
165         public StudyService getStudyService() {
166                 return _studyService;
167         }
168
169         /**
170          * Set the studyService.
171          * 
172          * @param studyService
173          *            the studyService to set
174          */
175         public void setStudyService(StudyService studyService) {
176                 _studyService = studyService;
177         }
178
179         /**
180          * Get the simulationContextService.
181          * 
182          * @return the simulationContextService
183          */
184         public SimulationContextService getSimulationContextService() {
185                 return _simulationContextService;
186         }
187
188         /**
189          * Set the simulationContextService.
190          * 
191          * @param simulationContextService
192          *            the simulationContextService to set
193          */
194         public void setSimulationContextService(
195                         SimulationContextService simulationContextService) {
196                 _simulationContextService = simulationContextService;
197         }
198
199         /**
200          * Get the scenarioService.
201          * @return the scenarioService
202          */
203         public ScenarioService getScenarioService() {
204                 return _scenarioService;
205         }
206
207         /**
208          * Set the scenarioService.
209          * @param scenarioService the scenarioService to set
210          */
211         public void setScenarioService(ScenarioService scenarioService) {
212                 _scenarioService = scenarioService;
213         }
214 }