]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/EditSimulationContextAction.java
Salome HOME
<jsp:param ... > parameters are removed from the title bar
[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          * Value of the title bar property. 
49          * It can be: study, knowledge.
50          */
51         private String _titleProperty;
52         
53         /**
54          * Property that indicates whether the current open study is editable or not.
55          * On the screen it looks like pen on the status icon, pop-up menu also can be called.
56          * It is necessary for correct building the title bar.
57          */
58         private String _editDisabledProperty = "false";
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                 setMenuProperty("study");
72                 setTitleProperty("study");
73         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
74
75                 if (contype.isEmpty())
76                         return "create";
77                 else
78                         return "select";
79         }
80
81         public String doSelectContext() {
82                 mystudy = getOpenStudy();
83                 
84                 setMenuProperty("study");
85                 setTitleProperty("study");
86         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
87                 
88                 int typid = Integer.valueOf(selectype);
89                 if (typid == 0)
90                         return "create";
91
92                 SimulationContext.Properties cprop = new SimulationContext.Properties();
93                 type = getSimulationContextService().selectType(typid);
94                 newtype = type.getName();
95                 contype = getInvolvedContexts();
96                 contelm = getSimulationContextService()
97                                 .selectSimulationContextsWhere(cprop.setType(type));
98
99                 return "set";
100         }
101
102         public String doCreateContext() {
103                 // --------------------------------
104                 try {
105                         mystudy = getOpenStudy();
106                         
107                         setMenuProperty("study");
108                         setTitleProperty("study");
109                 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
110                         
111                         if (newtype.length() == 0 || value.length() == 0)
112                                 return INPUT;
113
114                         Step step = mystudy.getSelectedStep();
115                         ProjectElement owner = step.getOwner();
116
117                         SimulationContext.Properties cprop = new SimulationContext.Properties();
118                         SimulationContext contex = null;
119                         type = getSimulationContextTypeService().createType(newtype,
120                                         step.getStep());
121                         cprop.setType(type).setValue(value);
122                         if (owner instanceof Study)
123                                 contex = getStudyService().addProjectContext(((Study) owner),
124                                                 cprop); // Re-indexes knowledges and the study
125                         else
126                                 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
127
128                         mystudy.add(contex);
129                         return SUCCESS;
130                 } catch (RuntimeException saverror) {
131                         logger.error("Reason:", saverror);
132                         return ERROR;
133                 } catch (Exception error) {
134                         return INPUT;
135                 }
136         }
137
138         public String doDeleteContext() {
139                 
140                 setMenuProperty("study");
141                 setTitleProperty("study");
142         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
143                 
144                 try {
145                         mystudy = getOpenStudy();
146
147                         Step step = mystudy.getSelectedStep();
148                         ProjectElement owner = step.getOwner();
149                         SimulationContext context = step.getSimulationContext(Integer
150                                         .valueOf(myindex));
151                         if (owner instanceof Study)
152                                 getStudyService()
153                                                 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
154                         else
155                                 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
156
157                         mystudy.remove(context);
158                         return SUCCESS;
159                 } catch (RuntimeException saverror) {
160                         logger.error("Reason:", saverror);
161                         return ERROR;
162                 }
163         }
164
165         public String doSetContext() {
166                 // -----------------------------
167                 String[] input = value.split(",");
168 //              Session connex = Database.getCurSession();
169 //              Transaction transax = connex.beginTransaction();
170                 
171                 setMenuProperty("study");
172                 setTitleProperty("study");
173         initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
174                 
175                 try {
176                         mystudy = getOpenStudy();
177
178                         Step step = mystudy.getSelectedStep();
179                         ProjectElement owner = step.getOwner();
180                         SimulationContext contex = null;
181
182                         if (input.length == 1
183                                         || (input.length == 2 && input[1].equals(" "))) {
184                                 // Setting an existing simulation context identified by value (input = rid," ")
185                                 int valid = Integer.valueOf(input[0]);
186                                 contex = getSimulationContextService().selectSimulationContext(
187                                                 valid);
188                                 if (owner instanceof Study)
189                                         getStudyService()
190                                                         .addProjectContext(((Study) owner), contex);
191                                 else
192                                         getStepService().addSimulationContext(step, contex);
193                         } else {
194                                 // Setting a new simulation context value (input = 0,"new context value")
195                                 int typid = Integer.valueOf(selectype);
196                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
197                                 cprop.setType(getSimulationContextService().selectType(typid))
198                                                 .setValue(input[1].trim());
199                                 if (owner instanceof Study)
200                                         contex = getStudyService().addProjectContext(
201                                                         ((Study) owner), cprop); // Re-indexes knowledges and the study
202                                 else
203                                         contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
204                         }
205                         mystudy.add(contex);
206                         contype = getInvolvedContexts();
207
208 //                      transax.commit();
209                         return SUCCESS;
210                 } catch (RuntimeException saverror) {
211                         logger.error("Reason:", saverror);
212                         return ERROR;
213                 } catch (Exception error) {
214                         value = input[0];
215                         return INPUT;
216                 }
217         }
218
219         // ==============================================================================================================================
220         // Getters and setters
221         // ==============================================================================================================================
222
223         public SimulationContextType getContextType() {
224                 // ----------------------------------------------
225                 return type;
226         }
227
228         public String getContextName() {
229                 // -------------------------------
230                 return newtype;
231         }
232
233         // public List<SimulationContextFacade> getSimulationContexts () {
234         // -------------------------------------------------------
235         // return mystudy.getSelectedStep().getAllSimulationContexts();
236         // }
237         public List<SimulationContextType> getSimulationContextTypes() {
238                 // ---------------------------------------------------------------
239                 return contype;
240         }
241
242         public List<SimulationContext> getSimulationContextValues() {
243                 // ------------------------------------------------------------
244                 return contelm;
245         }
246
247         public void setContextType(String type) {
248                 // ----------------------------------------
249                 this.selectype = type;
250         }
251
252         public void setContextValue(String value) {
253                 // -----------------------------------------
254                 this.value = value;
255         }
256
257         public void setNewType(String name) {
258                 // ------------------------------------
259                 this.newtype = name;
260         }
261
262         // ==============================================================================================================================
263         // Private service
264         // ==============================================================================================================================
265
266         private List<SimulationContextType> getInvolvedContexts() {
267                 // ----------------------------------------------------------
268                 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
269                                 .setStep(mystudy.getSelectedStep().getStep());
270                 List<SimulationContextType> contype = getSimulationContextService()
271                                 .selectTypesWhere(sprop);
272
273                 if (!contype.isEmpty()) {
274                         // Ordering by alphabetical order of localized context types
275                         SimulationContextType[] types = contype
276                                         .toArray(new SimulationContextType[contype.size()]);
277                         ContextTypeComparator compare = new ContextTypeComparator();
278                         boolean state = types[0].isApproved();
279                         int from = 0;
280                         int to = 0;
281                         while (to < types.length - 1) {
282                                 to += 1;
283                                 if (types[to].isApproved() == state)
284                                         continue;
285
286                                 if (to > from + 1)
287                                         Arrays.sort(types, from, to, compare);
288                                 state = !state;
289                                 from = to;
290                         }
291                         if (to > from)
292                                 Arrays.sort(types, from, to + 1, compare);
293                         contype = Arrays.asList(types);
294                 }
295                 return contype;
296         }
297
298         /**
299          * Get the studyService.
300          * 
301          * @return the studyService
302          */
303         public StudyService getStudyService() {
304                 return _studyService;
305         }
306
307         /**
308          * Set the studyService.
309          * 
310          * @param studyService
311          *            the studyService to set
312          */
313         public void setStudyService(StudyService studyService) {
314                 _studyService = studyService;
315         }
316
317         /**
318          * Get the simulationContextService.
319          * 
320          * @return the simulationContextService
321          */
322         public SimulationContextService getSimulationContextService() {
323                 return _simulationContextService;
324         }
325
326         /**
327          * Set the simulationContextService.
328          * 
329          * @param simulationContextService
330          *            the simulationContextService to set
331          */
332         public void setSimulationContextService(
333                         SimulationContextService simulationContextService) {
334                 _simulationContextService = simulationContextService;
335         }
336
337         /**
338          * Get the simulationContextTypeService.
339          * @return the simulationContextTypeService
340          */
341         public SimulationContextTypeService getSimulationContextTypeService() {
342                 return _simulationContextTypeService;
343         }
344
345         /**
346          * Set the simulationContextTypeService.
347          * @param simulationContextTypeService the simulationContextTypeService to set
348          */
349         public void setSimulationContextTypeService(
350                         SimulationContextTypeService simulationContextTypeService) {
351                 _simulationContextTypeService = simulationContextTypeService;
352         }
353         
354         /**
355          * Get the stepService.
356          * 
357          * @return the stepService
358          */
359         public StepService getStepService() {
360                 return _stepService;
361         }
362
363         /**
364          * Set the stepService.
365          * 
366          * @param stepService
367          *            the stepService to set
368          */
369         public void setStepService(StepService stepService) {
370                 _stepService = stepService;
371         }
372         
373         /**
374          * Get the menuProperty.
375          * @return the menuProperty
376          */
377         public String getMenuProperty() {
378                 return _menuProperty;
379         }
380
381         /**
382          * Set the menuProperty.
383          * @param menuProperty the menuProperty to set
384          */
385         public void setMenuProperty(String menuProperty) {
386                 this._menuProperty = menuProperty;
387         }
388         
389         /**
390          * Get the _titleProperty.
391          * @return the _titleProperty
392          */
393         public String getTitleProperty() {
394                 return _titleProperty;
395         }
396
397         /**
398          * Set the _titleProperty.
399          * @param _titleProperty the titleProperty to set
400          */
401         public void setTitleProperty(String titleProperty) {
402                 _titleProperty = titleProperty;
403         }
404
405         /**
406          * Get the editDisabledProperty.
407          * @return the editDisabledProperty
408          */
409         public final String getEditDisabledProperty() {
410                 return _editDisabledProperty;
411         }
412
413         /**
414          * Set the editDisabledProperty.
415          * @param editDisabledProperty the editDisabledProperty to set
416          */
417         public final void setEditDisabledProperty(String editDisabledProperty) {
418                 _editDisabledProperty = editDisabledProperty;
419         }
420
421         
422 }