]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/NewStudyAction.java
Salome HOME
The draft of the "Copy from existing study" action is added. The YACS step is introdu...
[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                                 + (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 res = SUCCESS;
93                 String[] input = _projectContext.split(",");
94                 int valid = Integer.valueOf(input[0]);
95
96                 // Check arguments and creation of the study
97                 if (valid == -1) {
98                         SimulationContext.Properties cprop = new SimulationContext.Properties();
99                         SimulationContextType product = getSimulationContextService()
100                                         .selectType("product");
101                         _contelm = getSimulationContextService()
102                                         .selectSimulationContextsWhere(cprop.setType(product));
103                         // Title empty, simply wait for input without error message
104                         res = INPUT;
105                 } else {
106                         String value; // input[1] if exists
107                         if (valid == 0) {
108                                 value = input[1].trim();
109                                 if (value.length() == 0) {
110                                         initializationScreenContext(Constants.CREATE_MENU);
111                                         // No need to reinitialize the list of existing products
112                                         res = INPUT;
113                                 }
114                         } else {
115                                 value = "";
116                         }
117                         if (SUCCESS.equals(res)) {
118                                 Study.Properties sprop = new Study.Properties();
119
120                                 sprop.setTitle(_title).setManager(getConnectedUser());
121                                 sprop.checkValidity();
122                                 sprop.disableCheck();
123                                 try {
124                                         // Addition of a default scenario
125                                         ResourceBundle locale = ResourceBundle.getBundle("labels",
126                                                         getApplicationSettings().getCurrentLocale());
127                                         Scenario.Properties oprop = new Scenario.Properties();
128                                         oprop.setTitle(locale.getString("label.scenario") + " 1");
129                 
130                                         // Addition of the entered project context
131                                         SimulationContext.Properties cprop = new SimulationContext.Properties();
132                                         if (valid == 0) { // Input of new project context
133                                                 SimulationContextType product = getSimulationContextService()
134                                                                 .selectType("product");
135                                                 
136                                                 SimulationContext testContext = getSimulationContextService().selectSimulationContext(product, value);
137                                                 
138                                                 if (testContext == null) {
139                                                 cprop.setType(
140                                                                 getSimulationContextService().selectType("product"))
141                                                                 .setValue(value);
142                                                 } else {
143                                                         cprop.setIndex(testContext.getIndex());
144                                                 }
145                                         } else { // Selection of existing project context
146                                                 cprop.setIndex(valid);
147                                         }
148                                         Study study = getScenarioService().createStudy(sprop, oprop, cprop);
149                                         // Update of the session
150                                         number += 1;
151                                         open(study); // Opens the study,
152                 
153                                         initializationFullScreenContext(Constants.STUDY_MENU,
154                                                         Constants.NONE, Constants.OPEN);
155                 
156                                 } catch (Exception error) {
157                                         LOG.error("Unable to save the study, reason:", error);
158                                         setErrorCode("message.error.newstudy");
159                                         initializationScreenContext(Constants.NONE);
160                                         res = ERROR;
161                                 }
162                         }
163                 }
164                 return res;
165         }
166         
167         /**
168          * 
169          * {@inheritDoc}
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().getName());
181                 }
182         }
183
184         // ==============================================================================================================================
185         // Getters and setters
186         // ==============================================================================================================================
187
188         /**
189          * Get the selected project context for the new study.
190          * 
191          * @return the selected project context
192          */
193         public String getProjectContext() {
194                 return _projectContext;
195         }
196
197         /**
198          * Get the list of available project contexts.
199          * 
200          * @return the list of context values
201          */
202         public List<SimulationContext> getProjectContextValues() {
203                 return _contelm;
204         }
205
206         /**
207          * Get the title of the new study.
208          * 
209          * @return the title
210          */
211         public String getTitle() {
212                 return _title;
213         }
214
215         /**
216          * Set the project context for the new study.
217          * 
218          * @param value
219          *            the project context value.
220          */
221         public void setProjectContext(final String value) {
222                 this._projectContext = value;
223         }
224
225         /**
226          * Set the title of the new study.
227          * 
228          * @param value
229          *            the title to set
230          */
231         public void setTitle(final String value) {
232                 this._title = value;
233         }
234
235         /**
236          * Get the simulationContextService.
237          * 
238          * @return the simulationContextService
239          */
240         public SimulationContextService getSimulationContextService() {
241                 return _simulationContextService;
242         }
243
244         /**
245          * Set the simulationContextService.
246          * 
247          * @param simulationContextService
248          *            the simulationContextService to set
249          */
250         public void setSimulationContextService(
251                         final SimulationContextService simulationContextService) {
252                 _simulationContextService = simulationContextService;
253         }
254
255         /**
256          * Get the scenarioService.
257          * 
258          * @return the scenarioService
259          */
260         public ScenarioService getScenarioService() {
261                 return _scenarioService;
262         }
263
264         /**
265          * Set the scenarioService.
266          * 
267          * @param scenarioService
268          *            the scenarioService to set
269          */
270         public void setScenarioService(final ScenarioService scenarioService) {
271                 _scenarioService = scenarioService;
272         }
273 }