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