Salome HOME
939d5d1ba1d4d2929ee83facb038721f913382db
[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.service.SimulationContextService;
10 import org.splat.service.SimulationContextTypeService;
11 import org.splat.service.StepService;
12 import org.splat.service.StudyService;
13 import org.splat.som.Step;
14 import org.splat.dal.bo.som.Study;
15
16 public class EditSimulationContextAction extends DisplayStudyStepAction {
17
18         private List<SimulationContextType> contype = null;
19         private List<SimulationContext> contelm = null;
20         private String selectype = null; // Context type, if selected
21         private String newtype = null; // Context type, if newed
22         private SimulationContextType type = null; // Corresponding context type object
23         private String value = null; // Context value
24         /**
25          * Injected study service.
26          */
27         private StudyService _studyService;
28         /**
29          * Injected step service.
30          */
31         private StepService _stepService;
32         /**
33          * Injected simulation context service.
34          */
35         private SimulationContextService _simulationContextService;
36         /**
37          * Injected simulation context type service.
38          */
39         private SimulationContextTypeService _simulationContextTypeService;
40         
41         /**
42          * Value of the menu property. 
43          * It can be: none, create, open, study, knowledge, sysadmin, help.
44          */
45         private String _menuProperty;
46
47         /**
48          * Get the stepService.
49          * 
50          * @return the stepService
51          */
52         public StepService getStepService() {
53                 return _stepService;
54         }
55
56         /**
57          * Set the stepService.
58          * 
59          * @param stepService
60          *            the stepService to set
61          */
62         public void setStepService(StepService stepService) {
63                 _stepService = stepService;
64         }
65
66         private static final long serialVersionUID = -641719644024601042L;
67
68         // ==============================================================================================================================
69         // Action methods
70         // ==============================================================================================================================
71
72         public String doInitialize() {
73
74                 mystudy = getOpenStudy();
75                 contype = getInvolvedContexts();
76                 
77                 setMenuProperty("study");
78                 initializationScreenContext(_menuProperty);
79
80                 if (contype.isEmpty())
81                         return "create";
82                 else
83                         return "select";
84         }
85
86         public String doSelectContext() {
87                 mystudy = getOpenStudy();
88                 
89                 setMenuProperty("study");
90                 initializationScreenContext(_menuProperty);
91                 
92                 int typid = Integer.valueOf(selectype);
93                 if (typid == 0)
94                         return "create";
95
96                 SimulationContext.Properties cprop = new SimulationContext.Properties();
97                 type = getSimulationContextService().selectType(typid);
98                 newtype = type.getName();
99                 contype = getInvolvedContexts();
100                 contelm = getSimulationContextService()
101                                 .selectSimulationContextsWhere(cprop.setType(type));
102
103                 return "set";
104         }
105
106         public String doCreateContext() {
107                 // --------------------------------
108                 try {
109                         mystudy = getOpenStudy();
110                         
111                         setMenuProperty("study");
112                         initializationScreenContext(_menuProperty);
113                         
114                         if (newtype.length() == 0 || value.length() == 0)
115                                 return INPUT;
116
117                         Step step = mystudy.getSelectedStep();
118                         ProjectElement owner = step.getOwner();
119
120                         SimulationContext.Properties cprop = new SimulationContext.Properties();
121                         SimulationContext contex = null;
122                         type = getSimulationContextTypeService().createType(newtype,
123                                         step.getStep());
124                         cprop.setType(type).setValue(value);
125                         if (owner instanceof Study)
126                                 contex = getStudyService().addProjectContext(((Study) owner),
127                                                 cprop); // Re-indexes knowledges and the study
128                         else
129                                 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
130
131                         mystudy.add(contex);
132                         return SUCCESS;
133                 } catch (RuntimeException saverror) {
134                         logger.error("Reason:", saverror);
135                         return ERROR;
136                 } catch (Exception error) {
137                         return INPUT;
138                 }
139         }
140
141         public String doDeleteContext() {
142                 
143                 setMenuProperty("study");
144                 initializationScreenContext(_menuProperty);
145                 
146                 try {
147                         mystudy = getOpenStudy();
148
149                         Step step = mystudy.getSelectedStep();
150                         ProjectElement owner = step.getOwner();
151                         SimulationContext context = step.getSimulationContext(Integer
152                                         .valueOf(myindex));
153                         if (owner instanceof Study)
154                                 getStudyService()
155                                                 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
156                         else
157                                 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
158
159                         mystudy.remove(context);
160                         return SUCCESS;
161                 } catch (RuntimeException saverror) {
162                         logger.error("Reason:", saverror);
163                         return ERROR;
164                 }
165         }
166
167         public String doSetContext() {
168                 // -----------------------------
169                 String[] input = value.split(",");
170 //              Session connex = Database.getCurSession();
171 //              Transaction transax = connex.beginTransaction();
172                 
173                 setMenuProperty("study");
174                 initializationScreenContext(_menuProperty);
175                 
176                 try {
177                         mystudy = getOpenStudy();
178
179                         Step step = mystudy.getSelectedStep();
180                         ProjectElement owner = step.getOwner();
181                         SimulationContext contex = null;
182
183                         if (input.length == 1
184                                         || (input.length == 2 && input[1].equals(" "))) {
185                                 // Setting an existing simulation context identified by value (input = rid," ")
186                                 int valid = Integer.valueOf(input[0]);
187                                 contex = getSimulationContextService().selectSimulationContext(
188                                                 valid);
189                                 if (owner instanceof Study)
190                                         getStudyService()
191                                                         .addProjectContext(((Study) owner), contex);
192                                 else
193                                         getStepService().addSimulationContext(step, contex);
194                         } else {
195                                 // Setting a new simulation context value (input = 0,"new context value")
196                                 int typid = Integer.valueOf(selectype);
197                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
198                                 cprop.setType(getSimulationContextService().selectType(typid))
199                                                 .setValue(input[1].trim());
200                                 if (owner instanceof Study)
201                                         contex = getStudyService().addProjectContext(
202                                                         ((Study) owner), cprop); // Re-indexes knowledges and the study
203                                 else
204                                         contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
205                         }
206                         mystudy.add(contex);
207                         contype = getInvolvedContexts();
208
209 //                      transax.commit();
210                         return SUCCESS;
211                 } catch (RuntimeException saverror) {
212                         logger.error("Reason:", saverror);
213                         return ERROR;
214                 } catch (Exception error) {
215                         value = input[0];
216                         return INPUT;
217                 }
218         }
219
220         // ==============================================================================================================================
221         // Getters and setters
222         // ==============================================================================================================================
223
224         public SimulationContextType getContextType() {
225                 // ----------------------------------------------
226                 return type;
227         }
228
229         public String getContextName() {
230                 // -------------------------------
231                 return newtype;
232         }
233
234         // public List<SimulationContextFacade> getSimulationContexts () {
235         // -------------------------------------------------------
236         // return mystudy.getSelectedStep().getAllSimulationContexts();
237         // }
238         public List<SimulationContextType> getSimulationContextTypes() {
239                 // ---------------------------------------------------------------
240                 return contype;
241         }
242
243         public List<SimulationContext> getSimulationContextValues() {
244                 // ------------------------------------------------------------
245                 return contelm;
246         }
247
248         public void setContextType(String type) {
249                 // ----------------------------------------
250                 this.selectype = type;
251         }
252
253         public void setContextValue(String value) {
254                 // -----------------------------------------
255                 this.value = value;
256         }
257
258         public void setNewType(String name) {
259                 // ------------------------------------
260                 this.newtype = name;
261         }
262
263         // ==============================================================================================================================
264         // Private service
265         // ==============================================================================================================================
266
267         private List<SimulationContextType> getInvolvedContexts() {
268                 // ----------------------------------------------------------
269                 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
270                                 .setStep(mystudy.getSelectedStep().getStep());
271                 List<SimulationContextType> contype = getSimulationContextService()
272                                 .selectTypesWhere(sprop);
273
274                 if (!contype.isEmpty()) {
275                         // Ordering by alphabetical order of localized context types
276                         SimulationContextType[] types = contype
277                                         .toArray(new SimulationContextType[contype.size()]);
278                         ContextTypeComparator compare = new ContextTypeComparator();
279                         boolean state = types[0].isApproved();
280                         int from = 0;
281                         int to = 0;
282                         while (to < types.length - 1) {
283                                 to += 1;
284                                 if (types[to].isApproved() == state)
285                                         continue;
286
287                                 if (to > from + 1)
288                                         Arrays.sort(types, from, to, compare);
289                                 state = !state;
290                                 from = to;
291                         }
292                         if (to > from)
293                                 Arrays.sort(types, from, to + 1, compare);
294                         contype = Arrays.asList(types);
295                 }
296                 return contype;
297         }
298
299         /**
300          * Get the studyService.
301          * 
302          * @return the studyService
303          */
304         public StudyService getStudyService() {
305                 return _studyService;
306         }
307
308         /**
309          * Set the studyService.
310          * 
311          * @param studyService
312          *            the studyService to set
313          */
314         public void setStudyService(StudyService studyService) {
315                 _studyService = studyService;
316         }
317
318         /**
319          * Get the simulationContextService.
320          * 
321          * @return the simulationContextService
322          */
323         public SimulationContextService getSimulationContextService() {
324                 return _simulationContextService;
325         }
326
327         /**
328          * Set the simulationContextService.
329          * 
330          * @param simulationContextService
331          *            the simulationContextService to set
332          */
333         public void setSimulationContextService(
334                         SimulationContextService simulationContextService) {
335                 _simulationContextService = simulationContextService;
336         }
337
338         /**
339          * Get the simulationContextTypeService.
340          * @return the simulationContextTypeService
341          */
342         public SimulationContextTypeService getSimulationContextTypeService() {
343                 return _simulationContextTypeService;
344         }
345
346         /**
347          * Set the simulationContextTypeService.
348          * @param simulationContextTypeService the simulationContextTypeService to set
349          */
350         public void setSimulationContextTypeService(
351                         SimulationContextTypeService simulationContextTypeService) {
352                 _simulationContextTypeService = simulationContextTypeService;
353         }
354         
355         /**
356          * Get the menuProperty.
357          * @return the menuProperty
358          */
359         public String getMenuProperty() {
360                 return _menuProperty;
361         }
362
363         /**
364          * Set the menuProperty.
365          * @param menuProperty the menuProperty to set
366          */
367         public void setMenuProperty(String menuProperty) {
368                 this._menuProperty = menuProperty;
369         }
370 }