Salome HOME
Create new scenario from existing one is improved
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditSimulationContextAction.java
1 package org.splat.simer;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import org.splat.dal.bo.som.ProjectElement;
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.SimulationContextService;
11 import org.splat.service.SimulationContextTypeService;
12 import org.splat.service.StepService;
13 import org.splat.som.Step;
14
15 /**
16  * Action for simulation context selection or input.
17  */
18 public class EditSimulationContextAction extends DisplayStudyStepAction {
19
20         /**
21          * Serialization version id.
22          */
23         private static final long serialVersionUID = -641719644024601042L;
24
25         /**
26          * List of involved context types.
27          */
28         private transient List<SimulationContextType> _contype = null;
29         /**
30          * List of simulation context values of the selected type.
31          */
32         private transient List<SimulationContext> _contelm = null;
33         /**
34          * Context type, if selected.
35          */
36         private transient String _selectype = null;
37         /**
38          * Context type, if newed.
39          */
40         private transient String _newtype = null;
41         /**
42          * Corresponding context type object.
43          */
44         private transient SimulationContextType _type = null;
45         /**
46          * Context value.
47          */
48         private transient String _value = null;
49         /**
50          * Injected step service.
51          */
52         private StepService _stepService;
53         /**
54          * Injected simulation context service.
55          */
56         private SimulationContextService _simulationContextService;
57         /**
58          * Injected simulation context type service.
59          */
60         private SimulationContextTypeService _simulationContextTypeService;
61
62         // ==============================================================================================================================
63         // Action methods
64         // ==============================================================================================================================
65
66         /**
67          * Initialize context input/selection action.
68          * 
69          * @return "create" if there is no involved context types or "select" otherwise
70          */
71         public String doInitialize() {
72
73                 String res;
74                 _openStudy = getOpenStudy();
75                 _contype = getInvolvedContexts();
76
77                 setMenu();
78
79                 if (_contype.isEmpty()) {
80                         res = "create";
81                         setAction("newContext");
82                 } else {
83                         res = "select";
84                         setAction("selectContext");
85                 }
86                 return res;
87         }
88
89         /**
90          * Select or create a simulation context. If a type has been selected then initialize the list of context values.
91          * 
92          * @return "set" if context type is selected or "create" otherwise
93          */
94         public String doSelectContext() {
95                 String res = "set";
96                 _openStudy = getOpenStudy();
97
98                 setMenu();
99
100                 int typid = Integer.valueOf(_selectype);
101                 if (typid == 0) {
102                         res = "create";
103                         setAction("newContext");
104                 } else {
105
106                         SimulationContext.Properties cprop = new SimulationContext.Properties();
107                         _type = getSimulationContextService().selectType(typid);
108                         _newtype = _type.getName();
109                         _contype = getInvolvedContexts();
110                         _contelm = getSimulationContextService()
111                                         .selectSimulationContextsWhere(cprop.setType(_type));
112
113                         setAction("setContext");
114                         setIndex(String.valueOf(getContextType().getIndex()));
115                 }
116
117                 return res;
118         }
119
120         /**
121          * Create new context. Add the created context to the step and reindex the study. <BR>
122          * If type or value are undefined then return INPUT.
123          * 
124          * @return SUCCESS if added or ERROR or INPUT if failed
125          */
126         public String doCreateContext() {
127                 String res = SUCCESS;
128                 try {
129                         _openStudy = getOpenStudy();
130
131                         setMenu();
132
133                         if (_newtype.length() == 0 || _value.length() == 0) {
134                                 res = INPUT;
135                         } else {
136
137                                 Step step = _openStudy.getSelectedStep();
138                                 ProjectElement owner = step.getOwner();
139
140                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
141                                 SimulationContext contex = null;
142                                 _type = getSimulationContextTypeService().createType(_newtype,
143                                                 step.getStep());
144                                 cprop.setType(_type).setValue(_value);
145                                 if (owner instanceof Study) {
146                                         contex = getStudyService().addProjectContext(
147                                                         ((Study) owner), cprop); // Re-indexes knowledges and the study
148                                 } else {
149                                         contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
150                                 }
151
152                                 _openStudy.add(contex);
153                         }
154                 } catch (RuntimeException saverror) {
155                         LOG.error("Reason:", saverror);
156                         res = ERROR;
157                 } catch (Exception error) {
158                         setAction("newContext");
159                         res = INPUT;
160                 }
161                 return res;
162         }
163
164         /**
165          * Remove the selected simulation context from the study step.
166          * 
167          * @return SUCCESS if removed or ERROR if failed
168          */
169         public String doDeleteContext() {
170
171                 String res = SUCCESS;
172                 setMenu();
173
174                 try {
175                         _openStudy = getOpenStudy();
176
177                         Step step = _openStudy.getSelectedStep();
178                         ProjectElement owner = step.getOwner();
179                         SimulationContext context = step.getSimulationContext(Integer
180                                         .valueOf(_myindex));
181                         if (owner instanceof Study) {
182                                 getStudyService()
183                                                 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
184                         } else {
185                                 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
186                         }
187
188                         _openStudy.remove(context);
189                 } catch (RuntimeException saverror) {
190                         LOG.error("Reason:", saverror);
191                         res = ERROR;
192                 }
193                 return res;
194         }
195
196         /**
197          * Add the selected context to the study step and reindex the study. <BR>
198          * If type or value are undefined then return INPUT.
199          * 
200          * @return SUCCESS if added or ERROR or INPUT if failed
201          */
202         public String doSetContext() {
203                 String res = SUCCESS;
204                 String[] input = _value.split(",");
205
206                 setMenu();
207
208                 try {
209                         _openStudy = getOpenStudy();
210
211                         Step step = _openStudy.getSelectedStep();
212                         ProjectElement owner = step.getOwner();
213                         SimulationContext contex = null;
214
215                         if (input.length == 1
216                                         || (input.length == 2 && input[1].equals(" "))) {
217                                 // Setting an existing simulation context identified by value (input = rid," ")
218                                 int valid = Integer.valueOf(input[0]);
219                                 contex = getSimulationContextService().selectSimulationContext(
220                                                 valid);
221                                 if (owner instanceof Study) {
222                                         getStudyService()
223                                                         .addProjectContext(((Study) owner), contex);
224                                 } else {
225                                         getStepService().addSimulationContext(step, contex);
226                                 }
227                         } else {
228                                 // Setting a new simulation context value (input = 0,"new context value")
229                                 int typid = Integer.valueOf(_selectype);
230                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
231                                 cprop.setType(getSimulationContextService().selectType(typid))
232                                                 .setValue(input[1].trim());
233                                 if (owner instanceof Study) {
234                                         contex = getStudyService().addProjectContext(
235                                                         ((Study) owner), cprop); // Re-indexes knowledges and the study
236                                 } else {
237                                         contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
238                                 }
239                         }
240                         _openStudy.add(contex);
241                         _contype = getInvolvedContexts();
242
243                         setAction("setContext");
244                         setIndex(String.valueOf(getContextType().getIndex()));
245
246                 } catch (RuntimeException saverror) {
247                         LOG.error("Reason:", saverror);
248                         res = ERROR;
249                 } catch (Exception error) {
250                         _value = input[0];
251                         setAction("setContext");
252                         res = INPUT;
253                 }
254                 return res;
255         }
256
257         // ==============================================================================================================================
258         // Getters and setters
259         // ==============================================================================================================================
260
261         /**
262          * Get context type.
263          * 
264          * @return Corresponding context type object
265          */
266         public SimulationContextType getContextType() {
267                 return _type;
268         }
269
270         /**
271          * Get the new context type to be created.
272          * 
273          * @return Context type, if newed
274          */
275         public String getContextName() {
276                 return _newtype;
277         }
278
279         /**
280          * Get the list of involved context types.
281          * 
282          * @return List of involved context types
283          */
284         public List<SimulationContextType> getSimulationContextTypes() {
285                 return _contype;
286         }
287
288         /**
289          * Get the list of simulation context values of the selected type.
290          * 
291          * @return List of simulation context values of the selected type
292          */
293         public List<SimulationContext> getSimulationContextValues() {
294                 return _contelm;
295         }
296
297         /**
298          * Get the selected context type.
299          * 
300          * @param type
301          *            Context type, if selected
302          */
303         public void setContextType(final String type) {
304                 this._selectype = type;
305         }
306
307         /**
308          * Set the context value.
309          * 
310          * @param value
311          *            context value
312          */
313         public void setContextValue(final String value) {
314                 this._value = value;
315         }
316
317         /**
318          * Set the new context type.
319          * 
320          * @param name
321          *            the new context type
322          */
323         public void setNewType(final String name) {
324                 this._newtype = name;
325         }
326
327         // ==============================================================================================================================
328         // Private service
329         // ==============================================================================================================================
330
331         /**
332          * Get the list of context types involved in the current study step.
333          * 
334          * @return the list of context types
335          */
336         private List<SimulationContextType> getInvolvedContexts() {
337                 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
338                                 .setStep(_openStudy.getSelectedStep().getStep());
339                 List<SimulationContextType> contype = getSimulationContextService()
340                                 .selectTypesWhere(sprop);
341
342                 if (!contype.isEmpty()) {
343                         // Ordering by alphabetical order of localized context types
344                         SimulationContextType[] types = contype
345                                         .toArray(new SimulationContextType[contype.size()]);
346                         ContextTypeComparator compare = new ContextTypeComparator();
347                         boolean state = types[0].isApproved();
348                         int from = 0;
349                         int to = 0;
350                         while (to < types.length - 1) {
351                                 to += 1;
352                                 if (types[to].isApproved() == state) {
353                                         continue;
354                                 }
355
356                                 if (to > from + 1) {
357                                         Arrays.sort(types, from, to, compare);
358                                 }
359                                 state ^= state;
360                                 from = to;
361                         }
362                         if (to > from) {
363                                 Arrays.sort(types, from, to + 1, compare);
364                         }
365                         contype = Arrays.asList(types);
366                 }
367                 return contype;
368         }
369
370         /**
371          * Get the simulationContextService.
372          * 
373          * @return the simulationContextService
374          */
375         public SimulationContextService getSimulationContextService() {
376                 return _simulationContextService;
377         }
378
379         /**
380          * Set the simulationContextService.
381          * 
382          * @param simulationContextService
383          *            the simulationContextService to set
384          */
385         public void setSimulationContextService(
386                         final SimulationContextService simulationContextService) {
387                 _simulationContextService = simulationContextService;
388         }
389
390         /**
391          * Get the simulationContextTypeService.
392          * 
393          * @return the simulationContextTypeService
394          */
395         public SimulationContextTypeService getSimulationContextTypeService() {
396                 return _simulationContextTypeService;
397         }
398
399         /**
400          * Set the simulationContextTypeService.
401          * 
402          * @param simulationContextTypeService
403          *            the simulationContextTypeService to set
404          */
405         public void setSimulationContextTypeService(
406                         final SimulationContextTypeService simulationContextTypeService) {
407                 _simulationContextTypeService = simulationContextTypeService;
408         }
409
410         /**
411          * Get the stepService.
412          * 
413          * @return the stepService
414          */
415         public StepService getStepService() {
416                 return _stepService;
417         }
418
419         /**
420          * Set the stepService.
421          * 
422          * @param stepService
423          *            the stepService to set
424          */
425         public void setStepService(final StepService stepService) {
426                 _stepService = stepService;
427         }
428 }