Salome HOME
Fix for a study creation with a new simulation context when no "product" context...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewStudyAction.java
1 package org.splat.simer;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.ResourceBundle;
6
7 import org.apache.commons.lang3.StringUtils;
8 import org.splat.dal.bo.som.Scenario;
9 import org.splat.dal.bo.som.SimulationContext;
10 import org.splat.dal.bo.som.SimulationContextType;
11 import org.splat.dal.bo.som.Study;
12 import org.splat.exception.BusinessException;
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          * Selected project context string value.
41          */
42         private String _projectContext = null;
43         /**
44          * Selected project context id.
45          */
46         private long _projectContextId = -1;
47         /**
48          * Injected simulation context service.
49          */
50         private SimulationContextService _simulationContextService;
51         /**
52          * Injected scenario service.
53          */
54         private ScenarioService _scenarioService;
55
56         // ==============================================================================================================================
57         // Action methods
58         // ==============================================================================================================================
59
60         /**
61          * Fill the values of the drop-down list, and initialize a menu.
62          * 
63          * @return SUCCESS
64          */
65         public String doInitialize() {
66
67                 // get the list of the simulation contexts of the study
68                 _contelm = getSimulationContextService().getSimulationContextList();
69
70                 // set the default name of the new study
71                 if (StringUtils.isBlank(_title)) {
72                         _title = getText("label.study") + " " + (number + 1);
73                 }
74
75                 initializationFullScreenContext(Constants.CREATE_MENU, Constants.NONE,
76                                 Constants.OPEN);
77
78                 return SUCCESS;
79         }
80
81         /**
82          * Create a new study.
83          * 
84          * @return SUCCESS if the new study is created, INPUT if project context is not defined, ERROR if failed
85          * @throws BusinessException
86          *             if new study creation is failed
87          * @throws IOException
88          *             if file operations are failed
89          */
90         public String doCreate() throws BusinessException, IOException {
91                 String res = SUCCESS;
92
93                 // Check arguments and creation of the study
94                 if (_projectContextId == -1) {
95                         SimulationContext.Properties cprop = new SimulationContext.Properties();
96                         SimulationContextType product = getSimulationContextService()
97                                         .selectType("product");
98                         _contelm = getSimulationContextService()
99                                         .selectSimulationContextsWhere(cprop.setType(product));
100                         // Title empty, simply wait for input without error message
101                         res = INPUT;
102                 } else {
103                         String value; // if a new project context has to be created
104                         if (_projectContextId == 0) {
105                                 value = _projectContext.trim();
106                                 if (value.length() == 0) {
107                                         initializationScreenContext(Constants.CREATE_MENU);
108                                         // No need to reinitialize the list of existing products
109                                         res = INPUT;
110                                 }
111                         } else {
112                                 value = "";
113                         }
114                         if (SUCCESS.equals(res)) {
115                                 Study.Properties sprop = new Study.Properties();
116
117                                 sprop.setTitle(_title).setManager(getConnectedUser());
118                                 sprop.checkValidity();
119                                 sprop.disableCheck();
120                                 try {
121                                         // Addition of a default scenario
122                                         ResourceBundle locale = ResourceBundle.getBundle("labels",
123                                                         getApplicationSettings().getCurrentLocale());
124                                         Scenario.Properties oprop = new Scenario.Properties();
125                                         oprop.setTitle(locale.getString("label.scenario") + " 1");
126
127                                         // Addition of the entered project context
128                                         SimulationContext.Properties cprop = new SimulationContext.Properties();
129                                         if (_projectContextId == 0) { // Input of new project context
130                                                 SimulationContextType product = getSimulationContextService()
131                                                                 .selectType("product");
132
133                                                 SimulationContext testContext = getSimulationContextService()
134                                                                 .selectSimulationContext(product, value);
135
136                                                 if (testContext == null) {
137                                                         cprop.setType(
138                                                                         getSimulationContextService().selectType(
139                                                                                         "product")).setValue(value);
140                                                 } else {
141                                                         cprop.setIndex(testContext.getIndex());
142                                                 }
143                                         } else { // Selection of existing project context
144                                                 cprop.setIndex(_projectContextId);
145                                         }
146                                         Study study = getScenarioService().createStudy(sprop,
147                                                         oprop, cprop);
148                                         // Update of the session
149                                         number += 1;
150                                         open(study); // Opens the study,
151
152                                         initializationFullScreenContext(Constants.STUDY_MENU,
153                                                         Constants.NONE, Constants.OPEN);
154
155                                 } catch (BusinessException error) {
156                                         LOG.error("Unable to save the study, reason:", error);
157                                         setErrorCode("message.error.newstudy");
158                                         initializationScreenContext(Constants.NONE);
159                                         res = ERROR;
160                                 }
161                         }
162                 }
163                 return res;
164         }
165
166         /**
167          * 
168          * {@inheritDoc}
169          * 
170          * @see com.opensymphony.xwork2.ActionSupport#validate()
171          */
172         @Override
173         public void validate() {
174
175                 if (LOG.isDebugEnabled()) {
176                         LOG.debug("--- validate");
177                 }
178                 if (LOG.isDebugEnabled()) {
179                         LOG.debug("======> MKA test");
180                         LOG.debug(com.opensymphony.xwork2.ActionContext.getContext()
181                                         .getName());
182                 }
183         }
184
185         // ==============================================================================================================================
186         // Getters and setters
187         // ==============================================================================================================================
188
189         /**
190          * Get the selected project context for the new study.
191          * 
192          * @return the selected project context
193          */
194         public String getProjectContext() {
195                 return _projectContext;
196         }
197
198         /**
199          * Get the list of available project contexts.
200          * 
201          * @return the list of context values
202          */
203         public List<SimulationContext> getProjectContextValues() {
204                 return _contelm;
205         }
206
207         /**
208          * Get the title of the new study.
209          * 
210          * @return the title
211          */
212         public String getTitle() {
213                 return _title;
214         }
215
216         /**
217          * Set the project context for the new study.
218          * 
219          * @param value
220          *            the project context value.
221          */
222         public void setProjectContext(final String value) {
223                 this._projectContext = value;
224         }
225
226         /**
227          * Set the title of the new study.
228          * 
229          * @param value
230          *            the title to set
231          */
232         public void setTitle(final String value) {
233                 this._title = value;
234         }
235
236         /**
237          * Get the simulationContextService.
238          * 
239          * @return the simulationContextService
240          */
241         public SimulationContextService getSimulationContextService() {
242                 return _simulationContextService;
243         }
244
245         /**
246          * Set the simulationContextService.
247          * 
248          * @param simulationContextService
249          *            the simulationContextService to set
250          */
251         public void setSimulationContextService(
252                         final SimulationContextService simulationContextService) {
253                 _simulationContextService = simulationContextService;
254         }
255
256         /**
257          * Get the scenarioService.
258          * 
259          * @return the scenarioService
260          */
261         public ScenarioService getScenarioService() {
262                 return _scenarioService;
263         }
264
265         /**
266          * Set the scenarioService.
267          * 
268          * @param scenarioService
269          *            the scenarioService to set
270          */
271         public void setScenarioService(final ScenarioService scenarioService) {
272                 _scenarioService = scenarioService;
273         }
274
275         /**
276          * Get the projectContextId.
277          * 
278          * @return the projectContextId
279          */
280         public long getProjectContextId() {
281                 return _projectContextId;
282         }
283
284         /**
285          * Set the projectContextId.
286          * 
287          * @param projectContextId
288          *            the projectContextId to set
289          */
290         public void setProjectContextId(final long projectContextId) {
291                 _projectContextId = projectContextId;
292         }
293 }