Salome HOME
Modifications to respect PMD rules.
[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.splat.dal.bo.som.Scenario;
7 import org.splat.dal.bo.som.SimulationContext;
8 import org.splat.dal.bo.som.SimulationContextType;
9 import org.splat.dal.bo.som.Study;
10 import org.splat.kernel.InvalidPropertyException;
11 import org.splat.kernel.MissedPropertyException;
12 import org.splat.kernel.MultiplyDefinedException;
13 import org.splat.service.ScenarioService;
14 import org.splat.service.SimulationContextService;
15
16 /**
17  * Action for creation of a new study.
18  */
19 public class NewStudyAction extends Action {
20
21         /**
22          * Serial version ID.
23          */
24         private static final long serialVersionUID = 693943641800113782L;
25         /**
26          * Sequential number of the new study for appending to its default title.
27          */
28         private static int number = 0;
29
30         /**
31          * Title of the new study.
32          */
33         private String _title = null;
34         /**
35          * List of available project contexts for selection for the new study.
36          */
37         private transient List<SimulationContext> _contelm = null;
38         /**
39          * Project context.
40          */
41         private String _projectContext = null;
42
43         /**
44          * Injected simulation context service.
45          */
46         private SimulationContextService _simulationContextService;
47         /**
48          * Injected scenario service.
49          */
50         private ScenarioService _scenarioService;
51
52         /**
53          * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
54          */
55         private String _menuProperty;
56
57         /**
58          * Value of the tool bar property. It can be: none, standard, study, back.
59          */
60         private String _toolProperty;
61
62         /**
63          * Value of the left menu property. It can be: open, study, knowledge, scenario.
64          */
65         private String _leftMenuProperty;
66
67         // ==============================================================================================================================
68         // Action methods
69         // ==============================================================================================================================
70
71         /**
72          * Fill the values of the drop-down list, and initialize a menu.
73          * 
74          * @return SUCCESS
75          */
76         public String doInitialize() {
77
78                 // get the list of the simulation contexts of the study
79                 _contelm = getSimulationContextService().getSimulationContextList();
80
81                 // set the default name of the new study
82                 ResourceBundle locale = ResourceBundle.getBundle("labels",
83                                 getApplicationSettings().getCurrentLocale());
84                 _title = locale.getString("label.study") + " "
85                                 + String.valueOf(number + 1);
86
87                 setMenuProperty("create");
88                 setToolProperty("none");
89                 setLeftMenuProperty("open");
90                 initializationFullScreenContext(_menuProperty, _toolProperty,
91                                 _leftMenuProperty);
92
93                 return SUCCESS;
94         }
95
96         /**
97          * Create a new study.
98          * 
99          * @return SUCCESS if the new study is created, INPUT if project context is not defined, ERROR if failed
100          * @throws InvalidPropertyException
101          *             if some property has invalid value
102          * @throws MultiplyDefinedException
103          *             if some property is defined several times
104          * @throws MissedPropertyException
105          *             if properties of the new study are invalid
106          */
107         public String doCreate() throws InvalidPropertyException,
108                         MissedPropertyException, MultiplyDefinedException {
109                 String[] input = _projectContext.split(",");
110                 int valid = Integer.valueOf(input[0]);
111                 String value = ""; // input[1] if exists
112
113                 Study.Properties sprop = new Study.Properties();
114
115                 // Check arguments and creation of the study
116                 if (valid == -1) {
117                         SimulationContext.Properties cprop = new SimulationContext.Properties();
118                         SimulationContextType product = getSimulationContextService()
119                                         .selectType("product");
120                         _contelm = getSimulationContextService()
121                                         .selectSimulationContextsWhere(cprop.setType(product));
122                         return INPUT; // Title empty, simply wait for input without error message
123                 }
124                 if (valid == 0) {
125                         value = input[1].trim();
126                         if (value.length() == 0) {
127                                 setMenuProperty("create");
128                                 initializationScreenContext(_menuProperty);
129                                 return INPUT; // No need to reinitialize the list of existing products
130                         }
131                 }
132                 sprop.setTitle(_title).setManager(getConnectedUser());
133                 sprop.checkValidity();
134                 sprop.disableCheck();
135                 try {
136                         // Addition of a default scenario
137                         ResourceBundle locale = ResourceBundle.getBundle("labels",
138                                         getApplicationSettings().getCurrentLocale());
139                         Scenario.Properties oprop = new Scenario.Properties();
140                         oprop.setTitle(locale.getString("label.scenario") + " 1");
141
142                         // Addition of the entered project context
143                         SimulationContext.Properties cprop = new SimulationContext.Properties();
144                         if (valid == 0) { // Input of new project context
145                                 cprop.setType(
146                                                 getSimulationContextService().selectType("product"))
147                                                 .setValue(value);
148                         } else { // Selection of existing project context
149                                 cprop.setIndex(valid);
150                         }
151                         Study study = getScenarioService().createStudy(sprop, oprop, cprop);
152                         // Update of the session
153                         number += 1;
154                         open(study); // Opens the study,
155
156                         setMenuProperty("study");
157                         setToolProperty("none");
158                         setLeftMenuProperty("open");
159                         initializationFullScreenContext(_menuProperty, _toolProperty,
160                                         _leftMenuProperty);
161
162                         return SUCCESS;
163                 } catch (Exception error) {
164                         LOG.error("Unable to save the study, reason:", error);
165                         setMenuProperty("none");
166                         initializationScreenContext(_menuProperty);
167                         return ERROR;
168                 }
169         }
170
171         // ==============================================================================================================================
172         // Getters and setters
173         // ==============================================================================================================================
174
175         /**
176          * Get the selected project context for the new study.
177          * 
178          * @return the selected project context
179          */
180         public String getProjectContext() {
181                 return _projectContext;
182         }
183
184         /**
185          * Get the list of available project contexts.
186          * 
187          * @return the list of context values
188          */
189         public List<SimulationContext> getProjectContextValues() {
190                 return _contelm;
191         }
192
193         /**
194          * Get the title of the new study.
195          * 
196          * @return the title
197          */
198         public String getTitle() {
199                 return _title;
200         }
201
202         /**
203          * Set the project context for the new study.
204          * 
205          * @param value
206          *            the project context value.
207          */
208         public void setProjectContext(final String value) {
209                 this._projectContext = value;
210         }
211
212         /**
213          * Set the title of the new study.
214          * 
215          * @param value
216          *            the title to set
217          */
218         public void setTitle(final String value) {
219                 this._title = value;
220         }
221
222         /**
223          * Get the simulationContextService.
224          * 
225          * @return the simulationContextService
226          */
227         public SimulationContextService getSimulationContextService() {
228                 return _simulationContextService;
229         }
230
231         /**
232          * Set the simulationContextService.
233          * 
234          * @param simulationContextService
235          *            the simulationContextService to set
236          */
237         public void setSimulationContextService(
238                         final SimulationContextService simulationContextService) {
239                 _simulationContextService = simulationContextService;
240         }
241
242         /**
243          * Get the scenarioService.
244          * 
245          * @return the scenarioService
246          */
247         public ScenarioService getScenarioService() {
248                 return _scenarioService;
249         }
250
251         /**
252          * Set the scenarioService.
253          * 
254          * @param scenarioService
255          *            the scenarioService to set
256          */
257         public void setScenarioService(final ScenarioService scenarioService) {
258                 _scenarioService = scenarioService;
259         }
260
261         /**
262          * Get the menuProperty.
263          * 
264          * @return the menuProperty
265          */
266         public String getMenuProperty() {
267                 return _menuProperty;
268         }
269
270         /**
271          * Set the menuProperty.
272          * 
273          * @param menuProperty
274          *            the menuProperty to set
275          */
276         public void setMenuProperty(final String menuProperty) {
277                 this._menuProperty = menuProperty;
278         }
279
280         /**
281          * Get the toolProperty.
282          * 
283          * @return the toolProperty
284          */
285         public String getToolProperty() {
286                 return _toolProperty;
287         }
288
289         /**
290          * Set the toolProperty.
291          * 
292          * @param toolProperty
293          *            the toolProperty to set
294          */
295         public void setToolProperty(final String toolProperty) {
296                 _toolProperty = toolProperty;
297         }
298
299         /**
300          * Get the leftMenuProperty.
301          * 
302          * @return the leftMenuProperty
303          */
304         public String getLeftMenuProperty() {
305                 return _leftMenuProperty;
306         }
307
308         /**
309          * Set the leftMenuProperty.
310          * 
311          * @param leftMenuProperty
312          *            the leftMenuProperty to set
313          */
314         public void setLeftMenuProperty(final String leftMenuProperty) {
315                 _leftMenuProperty = leftMenuProperty;
316         }
317 }