Salome HOME
Modifications to respect PMD rules.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditKnowledgeElementAction.java
1 package org.splat.simer;
2
3 import org.splat.dal.bo.kernel.User;
4 import org.splat.dal.bo.som.KnowledgeElement;
5 import org.splat.dal.bo.som.KnowledgeElementType;
6 import org.splat.dal.bo.som.Scenario;
7 import org.splat.service.KnowledgeElementService;
8 import org.splat.service.KnowledgeElementTypeService;
9 import org.splat.service.ScenarioService;
10 import org.splat.service.dto.KnowledgeElementDTO;
11 import org.splat.som.Step;
12
13 /**
14  * Action for addition and modification of knowledge elements in the selected scenario.
15  */
16 public class EditKnowledgeElementAction extends DisplayStudyStepAction {
17
18         /**
19          * Serial version ID.
20          */
21         private static final long serialVersionUID = 4636919137087687068L;
22
23         /**
24          * Edited knowledge element type.
25          */
26         private String type = null; // Edited knowledge type
27         /**
28          * Edited knowledge element title.
29          */
30         private String title = null;
31         /**
32          * Edited knowledge element value.
33          */
34         private String value = null;
35         /**
36          * Injected scenario service.
37          */
38         private ScenarioService _scenarioService;
39         /**
40          * Injected knowledge element service.
41          */
42         private KnowledgeElementService _knowledgeElementService;
43         /**
44          * Injected knowledge element type service.
45          */
46         private KnowledgeElementTypeService _knowledgeElementTypeService;
47
48         /**
49          * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
50          */
51         private String _menuProperty;
52
53         /**
54          * Value of the title bar property. It can be: study, knowledge.
55          */
56         private String _titleProperty;
57
58         /**
59          * Property that indicates whether the current open study is editable or not. On the screen it looks like pen on the status icon, pop-up
60          * menu also can be called. It is necessary for correct building the title bar.
61          */
62         private String _editDisabledProperty = "false";
63
64         /**
65          * Value of the tool bar property. It can be: none, standard, study, back.
66          */
67         private String _toolProperty;
68
69         /**
70          * Value of the left menu property. It can be: open, study, knowledge, scenario.
71          */
72         private String _leftMenuProperty;
73
74         // ==============================================================================================================================
75         // Action methods
76         // ==============================================================================================================================
77
78         /**
79          * Initialize the action. Get selected study from the session.
80          * 
81          * @return SUCCESS if no exceptions
82          */
83         public String doInitialize() {
84                 _openStudy = getOpenStudy();
85
86                 setMenuProperty("study");
87                 setTitleProperty("study");
88                 if ("true".equals(getWriteAccess())
89                                 && getUserRights().canCreateDocument()) {
90                         setToolProperty("study");
91                 } else {
92                         setToolProperty("standard");
93                 }
94                 setLeftMenuProperty("study");
95                 initializationFullScreenContext(_menuProperty, _titleProperty,
96                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
97
98                 return SUCCESS;
99         }
100
101         /**
102          * Add a new or update an existing knowledge element of the selected scenario in the open study.
103          * 
104          * @return SUCCESS if operation succeeded, ERROR if Runtime exception, otherwise INPUT
105          */
106         public String doSetKnowledge() {
107
108                 setMenuProperty("study");
109                 setTitleProperty("study");
110                 if ("true".equals(getWriteAccess())
111                                 && getUserRights().canCreateDocument()) {
112                         setToolProperty("study");
113                 } else {
114                         setToolProperty("standard");
115                 }
116                 setLeftMenuProperty("study");
117                 initializationFullScreenContext(_menuProperty, _titleProperty,
118                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
119
120                 try {
121                         User user = getConnectedUser();
122                         _openStudy = getOpenStudy();
123
124                         Step step = _openStudy.getSelectedStep();
125                         Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
126
127                         if ((title == null) || (value == null)) { 
128                                 KnowledgeElementDTO kelm = new KnowledgeElementDTO(Integer
129                                                 .valueOf(type), title, value);
130                                 if (value == null) { // Renaming of an existing Knowledge Element
131                                         getKnowledgeElementService().rename(kelm, title);
132                                 } else { // Edition of a knowledge
133                                         getKnowledgeElementService().update(kelm, value);
134                                 }
135                                 _openStudy.update(kelm); // For updating the truncated value
136                         } else { // Addition of a new Knowledge Element
137                                 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
138                                 KnowledgeElementType ktype = getKnowledgeElementTypeService()
139                                                 .selectType(Integer.valueOf(type));
140                                 kprop.setType(ktype).setTitle(title).setValue(value).setAuthor(
141                                                 user);
142                                 _openStudy.add(getScenarioService().addKnowledgeElement(scene,
143                                                 kprop));
144                                 getMenu("study").selects(_openStudy.getSelection()); // Updates the menu icon, in case of first added document
145                         }
146                         return SUCCESS;
147                 } catch (RuntimeException saverror) {
148                         LOG.error("Reason:", saverror);
149                         return ERROR;
150                 } catch (Exception error) {
151                         LOG.error("Exception while saving a knowledge: ", error);
152                         return INPUT;
153                 }
154         }
155
156         /**
157          * Delete a knowledge element from the current scenario.
158          * 
159          * @return SUCCESS if no exceptions
160          */
161         public String doDeleteKnowledge() {
162                 _openStudy = getOpenStudy();
163                 Step step = _openStudy.getSelectedStep();
164                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
165
166                 KnowledgeElement kelm = scene.getKnowledgeElement(Integer
167                                 .valueOf(_myindex));
168                 getScenarioService().removeKnowledgeElement(scene, kelm); // The knowledge element necessarily exists
169
170                 _openStudy.remove(kelm);
171                 updateMenu();
172
173                 return SUCCESS;
174         }
175
176         /**
177          * Promote a knowledge element from the current scenario.
178          * 
179          * @return SUCCESS if no exceptions
180          */
181         public String doPromoteKnowledge() {
182                 getKnowledgeElementService().promote(getKnowledgeElement()); // The knowledge element necessarily exists
183                 updateMenu();
184                 return SUCCESS;
185         }
186
187         /**
188          * Demote a knowledge element from the current scenario.
189          * 
190          * @return SUCCESS if no exceptions
191          */
192         public String doDemoteKnowledge() {
193                 getKnowledgeElementService().demote(getKnowledgeElement()); // The knowledge element necessarily exists
194                 updateMenu();
195                 return SUCCESS;
196         }
197
198         /**
199          * Get selected knowledge element.
200          * 
201          * @return the selected knowledge element
202          */
203         private KnowledgeElement getKnowledgeElement() {
204                 _openStudy = getOpenStudy();
205                 Step step = _openStudy.getSelectedStep();
206                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
207                 return scene.getKnowledgeElement(Integer.valueOf(_myindex));
208         }
209
210         /**
211          * Update current menu.
212          */
213         private void updateMenu() {
214                 getMenu("study").selects(_openStudy.getSelection()); // Updates the menu icon, in case of last removed document
215
216                 setMenuProperty("study");
217                 setTitleProperty("study");
218                 if ("true".equals(getWriteAccess())
219                                 && getUserRights().canCreateDocument()) {
220                         setToolProperty("study");
221                 } else {
222                         setToolProperty("standard");
223                 }
224                 setLeftMenuProperty("study");
225                 initializationFullScreenContext(_menuProperty, _titleProperty,
226                                 _editDisabledProperty, _toolProperty, _leftMenuProperty);
227
228         }
229
230         // ==============================================================================================================================
231         // Getters and setters
232         // ==============================================================================================================================
233
234         /**
235          * Get the type of the modified knowledge element.
236          * 
237          * @return the knowledge type name
238          */
239         public String getKnowledgeType() {
240                 // ---------------------------------
241                 return type;
242         }
243
244         /**
245          * Get the id of the modified knowledge element.
246          * 
247          * @return the knowledge element id
248          */
249         public String getSelectedKnowledge() {
250                 // -------------------------------------
251                 return _myindex;
252         }
253
254         /**
255          * Set the type of the modified knowledge element.
256          * 
257          * @param type
258          *            the knowledge type name
259          */
260         public void setKnowledgeType(final String type) {
261                 // ------------------------------------------
262                 this.type = type;
263         }
264
265         /**
266          * Set the title of the modified knowledge element.
267          * 
268          * @param title
269          *            the new knowledge title
270          */
271         public void setKnowledgeTitle(final String title) {
272                 // --------------------------------------------
273                 this.title = title;
274         }
275
276         /**
277          * Set the value of the modified knowledge element.
278          * 
279          * @param value
280          *            the knowledge value
281          */
282         public void setKnowledgeValue(final String value) {
283                 // --------------------------------------------
284                 this.value = value;
285         }
286
287         /**
288          * Get the scenarioService.
289          * 
290          * @return the scenarioService
291          */
292         public ScenarioService getScenarioService() {
293                 return _scenarioService;
294         }
295
296         /**
297          * Set the scenarioService.
298          * 
299          * @param scenarioService
300          *            the scenarioService to set
301          */
302         public void setScenarioService(final ScenarioService scenarioService) {
303                 _scenarioService = scenarioService;
304         }
305
306         /**
307          * Get the knowledgeElementService.
308          * 
309          * @return the knowledgeElementService
310          */
311         public KnowledgeElementService getKnowledgeElementService() {
312                 return _knowledgeElementService;
313         }
314
315         /**
316          * Set the knowledgeElementService.
317          * 
318          * @param knowledgeElementService
319          *            the knowledgeElementService to set
320          */
321         public void setKnowledgeElementService(
322                         final KnowledgeElementService knowledgeElementService) {
323                 _knowledgeElementService = knowledgeElementService;
324         }
325
326         /**
327          * Get the knowledgeElementTypeService.
328          * 
329          * @return the knowledgeElementTypeService
330          */
331         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
332                 return _knowledgeElementTypeService;
333         }
334
335         /**
336          * Set the knowledgeElementTypeService.
337          * 
338          * @param knowledgeElementTypeService
339          *            the knowledgeElementTypeService to set
340          */
341         public void setKnowledgeElementTypeService(
342                         final KnowledgeElementTypeService knowledgeElementTypeService) {
343                 _knowledgeElementTypeService = knowledgeElementTypeService;
344         }
345
346         /**
347          * Get the menuProperty.
348          * 
349          * @return the menuProperty
350          */
351         @Override
352         public String getMenuProperty() {
353                 return _menuProperty;
354         }
355
356         /**
357          * Set the menuProperty.
358          * 
359          * @param menuProperty
360          *            the menuProperty to set
361          */
362         @Override
363         public void setMenuProperty(final String menuProperty) {
364                 this._menuProperty = menuProperty;
365         }
366
367         /**
368          * Get the _titleProperty.
369          * 
370          * @return the _titleProperty
371          */
372         @Override
373         public String getTitleProperty() {
374                 return _titleProperty;
375         }
376
377         /**
378          * Set the titleProperty.
379          * 
380          * @param titleProperty
381          *            the titleProperty to set
382          */
383         @Override
384         public void setTitleProperty(final String titleProperty) {
385                 _titleProperty = titleProperty;
386         }
387
388         /**
389          * Get the editDisabledProperty.
390          * 
391          * @return the editDisabledProperty
392          */
393         @Override
394         public String getEditDisabledProperty() {
395                 return _editDisabledProperty;
396         }
397
398         /**
399          * Set the editDisabledProperty.
400          * 
401          * @param editDisabledProperty
402          *            the editDisabledProperty to set
403          */
404         @Override
405         public void setEditDisabledProperty(final String editDisabledProperty) {
406                 _editDisabledProperty = editDisabledProperty;
407         }
408
409         /**
410          * Get the toolProperty.
411          * 
412          * @return the toolProperty
413          */
414         @Override
415         public String getToolProperty() {
416                 return _toolProperty;
417         }
418
419         /**
420          * Set the toolProperty.
421          * 
422          * @param toolProperty
423          *            the toolProperty to set
424          */
425         @Override
426         public void setToolProperty(final String toolProperty) {
427                 _toolProperty = toolProperty;
428         }
429
430         /**
431          * Get the leftMenuProperty.
432          * 
433          * @return the leftMenuProperty
434          */
435         @Override
436         public String getLeftMenuProperty() {
437                 return _leftMenuProperty;
438         }
439
440         /**
441          * Set the leftMenuProperty.
442          * 
443          * @param leftMenuProperty
444          *            the leftMenuProperty to set
445          */
446         @Override
447         public void setLeftMenuProperty(final String leftMenuProperty) {
448                 _leftMenuProperty = leftMenuProperty;
449         }
450
451 }