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