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