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