Salome HOME
StudyService is no more used in the NewStudyAction.
[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         // Action methods
56         // ==============================================================================================================================
57
58         /**
59          * Fill the values of the drop-down list, and initialize a menu.
60          * 
61          * @return SUCCESS
62          */
63         public String doInitialize() {
64
65                 // get the list of the simulation contexts of the study
66                 contelm = getSimulationContextService().getSimulationContextList();
67
68                 // set the default name of the new study
69                 ResourceBundle locale = ResourceBundle.getBundle("labels",
70                                 ApplicationSettings.getCurrentLocale());
71                 title = locale.getString("label.study") + " "
72                                 + String.valueOf(number + 1);
73
74                 setMenuProperty("create");
75                 initializationScreenContext(_menuProperty);
76
77                 return SUCCESS;
78         }
79
80         /**
81          * Create a new study.
82          * 
83          * @return SUCCESS if the new study is created, INPUT if project context is not defined, ERROR if failed
84          * @throws Exception
85          *             if properties of the new study are invalid
86          */
87         public String doCreate() throws Exception {
88                 String[] input = context.split(",");
89                 int valid = Integer.valueOf(input[0]);
90                 String value = ""; // input[1] if exists
91
92                 Study.Properties sprop = new Study.Properties();
93
94                 // Check arguments and creation of the study
95                 if (valid == -1) {
96                         SimulationContext.Properties cprop = new SimulationContext.Properties();
97                         SimulationContextType product = getSimulationContextService()
98                                         .selectType("product");
99                         contelm = getSimulationContextService()
100                                         .selectSimulationContextsWhere(cprop.setType(product));
101                         return INPUT; // Title empty, simply wait for input without error message
102                 }
103                 if (valid == 0) {
104                         value = input[1].trim();
105                         if (value.length() == 0) {
106                                 setMenuProperty("create");
107                                 initializationScreenContext(_menuProperty);
108                                 return INPUT; // No need to reinitialize the list of existing products
109                         }
110                 }
111                 sprop.setTitle(title).setManager(getConnectedUser());
112                 sprop.checkValidity();
113                 sprop.disableCheck();
114                 try {
115                         // Addition of a default scenario
116                         ResourceBundle locale = ResourceBundle.getBundle("labels",
117                                         ApplicationSettings.getCurrentLocale());
118                         Scenario.Properties oprop = new Scenario.Properties();
119                         oprop.setTitle(locale.getString("label.scenario") + " 1");
120
121                         // Addition of the entered project context
122                         SimulationContext.Properties cprop = new SimulationContext.Properties();
123                         if (valid == 0) { // Input of new project context
124                                 cprop.setType(
125                                                 getSimulationContextService().selectType("product"))
126                                                 .setValue(value);
127                         } else { // Selection of existing project context
128                                 cprop.setIndex(valid);
129                         }
130                         Study study = getScenarioService().createStudy(sprop, oprop, cprop);
131                         // Update of the session
132                         number += 1;
133                         open(study); // Opens the study,
134
135                         setMenuProperty("study");
136                         initializationScreenContext(_menuProperty);
137
138                         return SUCCESS;
139                 } catch (Exception error) {
140                         logger.error("Unable to save the study, reason:", error);
141                         setMenuProperty("none");
142                         initializationScreenContext(_menuProperty);
143                         return ERROR;
144                 }
145         }
146
147         // ==============================================================================================================================
148         // Getters and setters
149         // ==============================================================================================================================
150
151         /**
152          * Get the selected project context for the new study.
153          * @return the selected project context
154          */
155         public String getProjectContext() {
156                 return context;
157         }
158
159         /**
160          * Get the list of available project contexts.
161          * @return the list of context values
162          */
163         public List<SimulationContext> getProjectContextValues() {
164                 return contelm;
165         }
166
167         /**
168          * Get the title of the new study.
169          * @return the title
170          */
171         public String getTitle() {
172                 return title;
173         }
174
175         /**
176          * Set the project context for the new study.
177          * @param value the project context value.
178          */
179         public void setProjectContext(String value) {
180                 this.context = value;
181         }
182
183         /**
184          * Set the title of the new study.
185          * @param value the title to set
186          */
187         public void setTitle(String value) {
188                 this.title = value;
189         }
190
191         /**
192          * Get the simulationContextService.
193          * 
194          * @return the simulationContextService
195          */
196         public SimulationContextService getSimulationContextService() {
197                 return _simulationContextService;
198         }
199
200         /**
201          * Set the simulationContextService.
202          * 
203          * @param simulationContextService
204          *            the simulationContextService to set
205          */
206         public void setSimulationContextService(
207                         SimulationContextService simulationContextService) {
208                 _simulationContextService = simulationContextService;
209         }
210
211         /**
212          * Get the scenarioService.
213          * 
214          * @return the scenarioService
215          */
216         public ScenarioService getScenarioService() {
217                 return _scenarioService;
218         }
219
220         /**
221          * Set the scenarioService.
222          * 
223          * @param scenarioService
224          *            the scenarioService to set
225          */
226         public void setScenarioService(ScenarioService scenarioService) {
227                 _scenarioService = scenarioService;
228         }
229
230         /**
231          * Get the menuProperty.
232          * 
233          * @return the menuProperty
234          */
235         public String getMenuProperty() {
236                 return _menuProperty;
237         }
238
239         /**
240          * Set the menuProperty.
241          * 
242          * @param menuProperty
243          *            the menuProperty to set
244          */
245         public void setMenuProperty(String menuProperty) {
246                 this._menuProperty = menuProperty;
247         }
248 }