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