Salome HOME
bfdb2d4384c74e347454f810d9801b2cf1cd3c9d
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditKnowledgeElementAction.java
1 package org.splat.simer;
2
3 import org.splat.dal.bo.som.KnowledgeElement;
4 import org.splat.dal.bo.som.KnowledgeElementType;
5 import org.splat.dal.bo.som.Scenario;
6 import org.splat.service.KnowledgeElementService;
7 import org.splat.service.KnowledgeElementTypeService;
8 import org.splat.service.ScenarioService;
9 import org.splat.service.dto.KnowledgeElementDTO;
10 import org.splat.som.Step;
11 import org.splat.wapp.Constants;
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 _knowledgeType = null; // Edited knowledge type
27         /**
28          * Edited knowledge element title.
29          */
30         private String _knowledgeTitle = null;
31         /**
32          * Edited knowledge element value.
33          */
34         private String _knowledgeValue = 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         // Action methods
50         // ==============================================================================================================================
51
52         /**
53          * Initialize the action. Get selected study from the session.
54          * 
55          * @return SUCCESS if no exceptions
56          */
57         public String doInitialize() {
58                 _openStudy = getOpenStudy();
59                 setMenu();
60                 return SUCCESS;
61         }
62
63         /**
64          * Add a new or update an existing knowledge element of the selected scenario in the open study.
65          * 
66          * @return SUCCESS if operation succeeded, ERROR if Runtime exception, otherwise INPUT
67          */
68         public String doSetKnowledge() {
69
70                 String res = SUCCESS;
71                 setMenu();
72
73                 try {
74                         _openStudy = getOpenStudy();
75
76                         if ((_knowledgeTitle == null) || (_knowledgeValue == null)) {
77                                 KnowledgeElementDTO kelm = new KnowledgeElementDTO(Integer
78                                                 .valueOf(_knowledgeType), _knowledgeTitle,
79                                                 _knowledgeValue);
80                                 if (_knowledgeValue == null) { // Renaming of an existing Knowledge Element
81                                         getKnowledgeElementService().rename(kelm, _knowledgeTitle);
82                                 } else { // Edition of a knowledge
83                                         getKnowledgeElementService().update(kelm, _knowledgeValue);
84                                 }
85                                 _openStudy.update(kelm); // For updating the truncated value
86                         } else { // Addition of a new Knowledge Element
87                                 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
88                                 KnowledgeElementType ktype = getKnowledgeElementTypeService()
89                                                 .selectType(Integer.valueOf(_knowledgeType));
90                                 kprop.setType(ktype).setTitle(_knowledgeTitle).setValue(
91                                                 _knowledgeValue).setAuthor(getConnectedUser());
92                                 _openStudy.add(getScenarioService().addKnowledgeElement(
93                                                 (Scenario) _openStudy.getSelectedStep().getOwner(),
94                                                 kprop));
95                                 getMenu(Constants.STUDY_MENU)
96                                                 .selects(_openStudy.getSelection()); // Updates the menu icon, in case of first added document
97                         }
98                 } catch (RuntimeException saverror) {
99                         LOG.error("Reason:", saverror);
100                         res = ERROR;
101                 } catch (Exception error) {
102                         LOG.error("Exception while saving a knowledge: ", error);
103                         res = INPUT;
104                 }
105                 return res;
106         }
107
108         /**
109          * Delete a knowledge element from the current scenario.
110          * 
111          * @return SUCCESS if no exceptions
112          */
113         public String doDeleteKnowledge() {
114                 _openStudy = getOpenStudy();
115                 Step step = _openStudy.getSelectedStep();
116                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
117
118                 KnowledgeElement kelm = scene.getKnowledgeElement(Integer
119                                 .valueOf(_myindex));
120                 getScenarioService().removeKnowledgeElement(scene, kelm); // The knowledge element necessarily exists
121                 
122                 if (_selection == null) { // Opening a study just newed
123                         _selection = _openStudy.getSelection(); // Default selection
124                 }
125                 _openStudy = open(getStudyService().selectStudy(
126                                 _openStudy.getIndex())); // Closes the previously open study
127                 _openStudy.setSelection(_selection);
128                 
129                 return SUCCESS;
130         }
131
132         /**
133          * Promote a knowledge element from the current scenario.
134          * 
135          * @return SUCCESS if no exceptions
136          */
137         public String doPromoteKnowledge() {
138                 getKnowledgeElementService().promote(getKnowledgeElement()); // The knowledge element necessarily exists
139                 updateMenu();
140                 return SUCCESS;
141         }
142
143         /**
144          * Demote a knowledge element from the current scenario.
145          * 
146          * @return SUCCESS if no exceptions
147          */
148         public String doDemoteKnowledge() {
149                 getKnowledgeElementService().demote(getKnowledgeElement()); // The knowledge element necessarily exists
150                 updateMenu();
151                 return SUCCESS;
152         }
153
154         /**
155          * Get selected knowledge element.
156          * 
157          * @return the selected knowledge element
158          */
159         private KnowledgeElement getKnowledgeElement() {
160                 _openStudy = getOpenStudy();
161                 Step step = _openStudy.getSelectedStep();
162                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
163                 return scene.getKnowledgeElement(Integer.valueOf(_myindex));
164         }
165
166         /**
167          * Update current menu.
168          */
169         private void updateMenu() {
170                 getMenu(Constants.STUDY_MENU).selects(_openStudy.getSelection()); // Updates the menu icon, in case of last removed document
171                 setMenu();
172         }
173
174         // ==============================================================================================================================
175         // Getters and setters
176         // ==============================================================================================================================
177
178         /**
179          * Get the type of the modified knowledge element.
180          * 
181          * @return the knowledge type name
182          */
183         public String getKnowledgeType() {
184                 return _knowledgeType;
185         }
186
187         /**
188          * Get the id of the modified knowledge element.
189          * 
190          * @return the knowledge element id
191          */
192         public String getSelectedKnowledge() {
193                 return _myindex;
194         }
195
196         /**
197          * Set the type of the modified knowledge element.
198          * 
199          * @param type
200          *            the knowledge type name
201          */
202         public void setKnowledgeType(final String type) {
203                 this._knowledgeType = type;
204         }
205
206         /**
207          * Set the title of the modified knowledge element.
208          * 
209          * @param title
210          *            the new knowledge title
211          */
212         public void setKnowledgeTitle(final String title) {
213                 this._knowledgeTitle = title;
214         }
215
216         /**
217          * Set the value of the modified knowledge element.
218          * 
219          * @param value
220          *            the knowledge value
221          */
222         public void setKnowledgeValue(final String value) {
223                 this._knowledgeValue = value;
224         }
225
226         /**
227          * Get the scenarioService.
228          * 
229          * @return the scenarioService
230          */
231         public ScenarioService getScenarioService() {
232                 return _scenarioService;
233         }
234
235         /**
236          * Set the scenarioService.
237          * 
238          * @param scenarioService
239          *            the scenarioService to set
240          */
241         public void setScenarioService(final ScenarioService scenarioService) {
242                 _scenarioService = scenarioService;
243         }
244
245         /**
246          * Get the knowledgeElementService.
247          * 
248          * @return the knowledgeElementService
249          */
250         public KnowledgeElementService getKnowledgeElementService() {
251                 return _knowledgeElementService;
252         }
253
254         /**
255          * Set the knowledgeElementService.
256          * 
257          * @param knowledgeElementService
258          *            the knowledgeElementService to set
259          */
260         public void setKnowledgeElementService(
261                         final KnowledgeElementService knowledgeElementService) {
262                 _knowledgeElementService = knowledgeElementService;
263         }
264
265         /**
266          * Get the knowledgeElementTypeService.
267          * 
268          * @return the knowledgeElementTypeService
269          */
270         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
271                 return _knowledgeElementTypeService;
272         }
273
274         /**
275          * Set the knowledgeElementTypeService.
276          * 
277          * @param knowledgeElementTypeService
278          *            the knowledgeElementTypeService to set
279          */
280         public void setKnowledgeElementTypeService(
281                         final KnowledgeElementTypeService knowledgeElementTypeService) {
282                 _knowledgeElementTypeService = knowledgeElementTypeService;
283         }
284
285         /**
286          * Get the knowledgeValue.
287          * 
288          * @return the knowledgeValue
289          */
290         public String getKnowledgeValue() {
291                 return _knowledgeValue;
292         }
293
294         /**
295          * Get the knowledgeTitle.
296          * 
297          * @return the knowledgeTitle
298          */
299         public String getKnowledgeTitle() {
300                 return _knowledgeTitle;
301         }
302 }