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