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