Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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.som.Step;
11
12 /**
13  * Action for addition and modification of knowledge elements in the selected scenario.
14  */
15 public class EditKnowledgeElementAction extends DisplayStudyStepAction {
16
17         /**
18          * Serial version ID.
19          */
20         private static final long serialVersionUID = 4636919137087687068L;
21
22         /**
23          * Edited knowledge element type.
24          */
25         private String type = null; // Edited knowledge type
26         /**
27          * Edited knowledge element title.
28          */
29         private String title = null;
30         /**
31          * Edited knowledge element value.
32          */
33         private String value = null;
34         /**
35          * Injected scenario service.
36          */
37         private ScenarioService _scenarioService;
38         /**
39          * Injected knowledge element service.
40          */
41         private KnowledgeElementService _knowledgeElementService;
42         /**
43          * Injected knowledge element type service.
44          */
45         private KnowledgeElementTypeService _knowledgeElementTypeService;
46
47         // ==============================================================================================================================
48         // Action methods
49         // ==============================================================================================================================
50
51         /**
52          * Initialize the action. Get selected study from the session.
53          * 
54          * @return SUCCESS if no exceptions
55          */
56         public String doInitialize() {
57                 mystudy = getOpenStudy();
58                 return SUCCESS;
59         }
60
61         /**
62          * Add a new or update an existing knowledge element of the selected scenario in the open study.
63          * 
64          * @return SUCCESS if operation succeeded, ERROR if Runtime exception, otherwise INPUT
65          */
66         public String doSetKnowledge() {
67                 try {
68                         User user = getConnectedUser();
69                         mystudy = getOpenStudy();
70
71                         Step step = mystudy.getSelectedStep();
72                         Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
73
74                         if (title != null && value != null) { // Addition of a new Knowledge Element
75                                 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
76                                 KnowledgeElementType ktype = getKnowledgeElementTypeService()
77                                                 .selectType(Integer.valueOf(type));
78                                 kprop.setType(ktype).setTitle(title).setValue(value).setAuthor(
79                                                 user);
80                                 mystudy.add(getScenarioService().addKnowledgeElement(scene,
81                                                 kprop));
82                                 getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of first added document
83                         } else if (title != null) { // Renaming of an existing Knowledge Element
84                                 KnowledgeElement kelm = scene.getKnowledgeElement(Integer
85                                                 .valueOf(type));
86                                 getKnowledgeElementService().rename(kelm, title);
87                                 // Useless to update the open study
88                         } else if (value != null) { // Edition of a knowledge
89                                 KnowledgeElement kelm = scene.getKnowledgeElement(Integer
90                                                 .valueOf(type));
91                                 getKnowledgeElementService().update(kelm, value);
92                                 mystudy.update(kelm); // For updating the truncated value
93                         }
94                         return SUCCESS;
95                 } catch (RuntimeException saverror) {
96                         logger.error("Reason:", saverror);
97                         return ERROR;
98                 } catch (Exception error) {
99                         return INPUT;
100                 }
101         }
102
103         /**
104          * Delete a knowledge element from the current scenario.
105          * 
106          * @return SUCCESS if no exceptions
107          */
108         public String doDeleteKnowledge() {
109                 mystudy = getOpenStudy();
110                 Step step = mystudy.getSelectedStep();
111                 Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
112
113                 KnowledgeElement kelm = scene.getKnowledgeElement(Integer
114                                 .valueOf(myindex));
115                 getScenarioService().removeKnowledgeElement(scene, kelm); // The knowledge element necessarily exists
116
117                 mystudy.remove(kelm);
118                 getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of last removed document
119
120                 return SUCCESS;
121         }
122
123         // ==============================================================================================================================
124         // Getters and setters
125         // ==============================================================================================================================
126
127         /**
128          * Get the type of the modified knowledge element.
129          * 
130          * @return the knowledge type name
131          */
132         public String getKnowledgeType() {
133                 // ---------------------------------
134                 return type;
135         }
136
137         /**
138          * Get the id of the modified knowledge element.
139          * 
140          * @return the knowledge element id
141          */
142         public String getSelectedKnowledge() {
143                 // -------------------------------------
144                 return myindex;
145         }
146
147         /**
148          * Set the type of the modified knowledge element.
149          * 
150          * @param type
151          *            the knowledge type name
152          */
153         public void setKnowledgeType(String type) {
154                 // ------------------------------------------
155                 this.type = type;
156         }
157
158         /**
159          * Set the title of the modified knowledge element.
160          * 
161          * @param title
162          *            the new knowledge title
163          */
164         public void setKnowledgeTitle(String title) {
165                 // --------------------------------------------
166                 this.title = title;
167         }
168
169         /**
170          * Set the value of the modified knowledge element.
171          * 
172          * @param value
173          *            the knowledge value
174          */
175         public void setKnowledgeValue(String value) {
176                 // --------------------------------------------
177                 this.value = value;
178         }
179
180         /**
181          * Get the scenarioService.
182          * 
183          * @return the scenarioService
184          */
185         public ScenarioService getScenarioService() {
186                 return _scenarioService;
187         }
188
189         /**
190          * Set the scenarioService.
191          * 
192          * @param scenarioService
193          *            the scenarioService to set
194          */
195         public void setScenarioService(ScenarioService scenarioService) {
196                 _scenarioService = scenarioService;
197         }
198
199         /**
200          * Get the knowledgeElementService.
201          * 
202          * @return the knowledgeElementService
203          */
204         public KnowledgeElementService getKnowledgeElementService() {
205                 return _knowledgeElementService;
206         }
207
208         /**
209          * Set the knowledgeElementService.
210          * 
211          * @param knowledgeElementService
212          *            the knowledgeElementService to set
213          */
214         public void setKnowledgeElementService(
215                         KnowledgeElementService knowledgeElementService) {
216                 _knowledgeElementService = knowledgeElementService;
217         }
218
219         /**
220          * Get the knowledgeElementTypeService.
221          * @return the knowledgeElementTypeService
222          */
223         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
224                 return _knowledgeElementTypeService;
225         }
226
227         /**
228          * Set the knowledgeElementTypeService.
229          * @param knowledgeElementTypeService the knowledgeElementTypeService to set
230          */
231         public void setKnowledgeElementTypeService(
232                         KnowledgeElementTypeService knowledgeElementTypeService) {
233                 _knowledgeElementTypeService = knowledgeElementTypeService;
234         }
235 }