]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/EditKnowledgeElementAction.java
Salome HOME
Update uses list functionality
[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                 _openStudy.updateCurrentStep();
116                 Step step = _openStudy.getSelectedStep();
117                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
118
119                 KnowledgeElement kelm = scene.getKnowledgeElement(Integer
120                                 .valueOf(_myindex));
121                 getScenarioService().removeKnowledgeElement(scene, kelm); // The knowledge element necessarily exists
122                 
123                 if (_selection == null) { // Opening a study just newed
124                         _selection = _openStudy.getSelection(); // Default selection
125                 }
126                 _openStudy = open(getStudyService().selectStudy(
127                                 _openStudy.getIndex())); // Closes the previously open study
128                 _openStudy.setSelection(_selection);
129                 
130                 return SUCCESS;
131         }
132
133         /**
134          * Promote a knowledge element from the current scenario.
135          * 
136          * @return SUCCESS if no exceptions
137          */
138         public String doPromoteKnowledge() {
139                 getKnowledgeElementService().promote(getKnowledgeElement()); // The knowledge element necessarily exists
140                 updateMenu();
141                 return SUCCESS;
142         }
143
144         /**
145          * Demote a knowledge element from the current scenario.
146          * 
147          * @return SUCCESS if no exceptions
148          */
149         public String doDemoteKnowledge() {
150                 getKnowledgeElementService().demote(getKnowledgeElement()); // The knowledge element necessarily exists
151                 updateMenu();
152                 return SUCCESS;
153         }
154
155         /**
156          * Get selected knowledge element.
157          * 
158          * @return the selected knowledge element
159          */
160         private KnowledgeElement getKnowledgeElement() {
161                 _openStudy = getOpenStudy();
162                 Step step = _openStudy.getSelectedStep();
163                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
164                 return scene.getKnowledgeElement(Integer.valueOf(_myindex));
165         }
166
167         /**
168          * Update current menu.
169          */
170         private void updateMenu() {
171                 getMenu(Constants.STUDY_MENU).selects(_openStudy.getSelection()); // Updates the menu icon, in case of last removed document
172                 setMenu();
173         }
174
175         // ==============================================================================================================================
176         // Getters and setters
177         // ==============================================================================================================================
178
179         /**
180          * Get the type of the modified knowledge element.
181          * 
182          * @return the knowledge type name
183          */
184         public String getKnowledgeType() {
185                 return _knowledgeType;
186         }
187
188         /**
189          * Get the id of the modified knowledge element.
190          * 
191          * @return the knowledge element id
192          */
193         public String getSelectedKnowledge() {
194                 return _myindex;
195         }
196
197         /**
198          * Set the type of the modified knowledge element.
199          * 
200          * @param type
201          *            the knowledge type name
202          */
203         public void setKnowledgeType(final String type) {
204                 this._knowledgeType = type;
205         }
206
207         /**
208          * Set the title of the modified knowledge element.
209          * 
210          * @param title
211          *            the new knowledge title
212          */
213         public void setKnowledgeTitle(final String title) {
214                 this._knowledgeTitle = title;
215         }
216
217         /**
218          * Set the value of the modified knowledge element.
219          * 
220          * @param value
221          *            the knowledge value
222          */
223         public void setKnowledgeValue(final String value) {
224                 this._knowledgeValue = value;
225         }
226
227         /**
228          * Get the scenarioService.
229          * 
230          * @return the scenarioService
231          */
232         public ScenarioService getScenarioService() {
233                 return _scenarioService;
234         }
235
236         /**
237          * Set the scenarioService.
238          * 
239          * @param scenarioService
240          *            the scenarioService to set
241          */
242         public void setScenarioService(final ScenarioService scenarioService) {
243                 _scenarioService = scenarioService;
244         }
245
246         /**
247          * Get the knowledgeElementService.
248          * 
249          * @return the knowledgeElementService
250          */
251         public KnowledgeElementService getKnowledgeElementService() {
252                 return _knowledgeElementService;
253         }
254
255         /**
256          * Set the knowledgeElementService.
257          * 
258          * @param knowledgeElementService
259          *            the knowledgeElementService to set
260          */
261         public void setKnowledgeElementService(
262                         final KnowledgeElementService knowledgeElementService) {
263                 _knowledgeElementService = knowledgeElementService;
264         }
265
266         /**
267          * Get the knowledgeElementTypeService.
268          * 
269          * @return the knowledgeElementTypeService
270          */
271         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
272                 return _knowledgeElementTypeService;
273         }
274
275         /**
276          * Set the knowledgeElementTypeService.
277          * 
278          * @param knowledgeElementTypeService
279          *            the knowledgeElementTypeService to set
280          */
281         public void setKnowledgeElementTypeService(
282                         final KnowledgeElementTypeService knowledgeElementTypeService) {
283                 _knowledgeElementTypeService = knowledgeElementTypeService;
284         }
285
286         /**
287          * Get the knowledgeValue.
288          * 
289          * @return the knowledgeValue
290          */
291         public String getKnowledgeValue() {
292                 return _knowledgeValue;
293         }
294
295         /**
296          * Get the knowledgeTitle.
297          * 
298          * @return the knowledgeTitle
299          */
300         public String getKnowledgeTitle() {
301                 return _knowledgeTitle;
302         }
303 }