Salome HOME
Modifications to respect PMD rules.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / SearchKnowledgeAction.java
1 package org.splat.simer;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.splat.kernel.InvalidPropertyException;
7 import org.splat.dal.bo.kernel.User;
8 import org.splat.service.KnowledgeElementTypeService;
9 import org.splat.service.SearchService;
10 import org.splat.service.SimulationContextService;
11 import org.splat.service.UserService;
12 import org.splat.dal.bo.som.KnowledgeElement;
13 import org.splat.dal.bo.som.KnowledgeElementType;
14 import org.splat.dal.bo.som.ProgressState;
15 import org.splat.dal.bo.som.SimulationContext;
16 import org.splat.dal.bo.som.SimulationContextType;
17 import org.splat.dal.bo.som.Visibility;
18
19 /**
20  * Action for searching a knowledge in the database.
21  */
22 public class SearchKnowledgeAction extends SearchBaseAction {
23
24         /**
25          * Serial version ID.
26          */
27         private static final long serialVersionUID = -3104321907432838476L;
28
29         private String visibility = null; // "Private", "Public", "All"
30         private String typid = null; // Knowledge type index when among all
31         private String matchamong = null; // "all" or "any"
32         private String matcontext = null; // "all" or "any"
33         private String refid = null; // Knowledge reference when among ref
34         private String words = null; // Full text search words
35         private List<KnowledgeElementType> types; // Available knowledge types filter (initialized below)
36         private SearchService _searchService;
37         /**
38          * Injected simulation context service.
39          */
40         private SimulationContextService _simulationContextService;
41         /**
42          * Injected knowledge element service.
43          */
44         private KnowledgeElementTypeService _knowledgeElementTypeService;
45
46         /**
47          * Injected user service.
48          */
49         private UserService _userService;
50         
51         /**
52          * Value of the menu property. 
53          * It can be: none, create, open, study, knowledge, sysadmin, help.
54          */
55         private String _menuProperty;
56         
57         /**
58          * Value of the tool bar property. 
59          * It can be: none, standard, study, back.
60          */
61         private String _toolProperty;
62         
63         /**
64          * Value of the left menu property. 
65          * It can be: open, study, knowledge, scenario.
66          */
67         private String _leftMenuProperty;
68
69         // ==============================================================================================================================
70         // Action methods
71         // ==============================================================================================================================
72
73         /**
74          * The action initialization.
75          * 
76          * @return SUCCESS if succeeded, ERROR if doSearch() is failed
77          */
78         public String doInitialize() {
79                 
80                 setMenuProperty("open");
81                 setToolProperty("none");
82                 setLeftMenuProperty("open");
83                 initializationFullScreenContext(_menuProperty, _toolProperty, _leftMenuProperty);
84                 
85                 try {
86                         loadFilter();
87                         doSearch();
88
89                         // Final initialization of the form
90                         types = getKnowledgeElementTypeService().selectTypesWhere(
91                                         ProgressState.APPROVED);
92                         setCandidates();
93                         setContextTypeOptions(getInvolvedContexts());
94
95                         return SUCCESS;
96                 } catch (Exception error) {
97                         // No need to roll back the transaction as it is read only
98                         LOG.error("Reason: ", error);
99                         return ERROR;
100                 }
101         }
102
103         protected String doSearch() throws InvalidPropertyException {
104                 // ----------------------------
105                 setMenuProperty("open");
106                 initializationScreenContext(_menuProperty);
107                 
108                 Map<String, Object> session = getSession();
109                 User user = getConnectedUser();
110
111                 KnowledgeElement.Properties sprop = new KnowledgeElement.Properties();
112
113                 // Search matching all criteria
114                 sprop.setType(getKnowledgeElementTypeService().selectType(
115                                 Integer.valueOf(typid)));
116                 if (words.length() > 0)
117                         sprop.setTitle(words);
118                 if (refid.length() > 0)
119                         sprop.setReference(refid);
120                 if (context.size() > 0)
121                         sprop.setSimulationContexts(context);
122                 int index = Integer.valueOf(author);
123                 if (index > 0) {
124                         User him = getUserService().selectUser(index);
125                         sprop.setAuthor(him);
126                 }
127                 // Set of the visibility
128                 if (visibility.equals("all")) {
129                         KnowledgeElement.Properties other = sprop.copy();
130
131                         other.setVisibility(Visibility.PUBLIC);
132                         sprop.setVisibility(Visibility.PRIVATE);
133                         sprop.setActor(user);
134
135                         result = getSearchService().selectKnowledgeElementsWhere(sprop,
136                                         other);
137                 } else {
138                         Visibility reparea = null;
139                         if (visibility.equals("onlypublic"))
140                                 reparea = Visibility.PUBLIC;
141                         else
142                                 reparea = Visibility.valueOf(visibility);
143                         sprop.setVisibility(reparea);
144                         if (reparea == Visibility.PRIVATE)
145                                 sprop.setActor(user);
146
147                         result = getSearchService().selectKnowledgeElementsWhere(sprop);
148                 }
149                 session.put("search.result", result); // For redisplaying the page without re-executing the search
150                 return "refresh";
151         }
152
153         // ==============================================================================================================================
154         // Getters
155         // ==============================================================================================================================
156
157         public String getContextMatch() {
158                 // --------------------------------
159                 return matcontext;
160         }
161
162         public String getCriteriaMatch() {
163                 // ---------------------------------
164                 return matchamong;
165         }
166
167         public List<KnowledgeElementType> getKnowledgeTypes() {
168                 // ------------------------------------------------------
169                 return types;
170         }
171
172         public String getReference() {
173                 // -----------------------------
174                 return refid;
175         }
176
177         public String getState() {
178                 // ------------------------
179                 return typid;
180         }
181
182         public String getVisibility() {
183                 // ------------------------------
184                 return visibility;
185         }
186
187         public String getWords() {
188                 // -------------------------
189                 return words;
190         }
191
192         // ==============================================================================================================================
193         // Setters
194         // ==============================================================================================================================
195
196         public void setContextMatch(String value) {
197                 // ------------------------------------------
198                 this.matcontext = value;
199         }
200
201         public void setCriteriaMatch(String value) {
202                 // -------------------------------------------
203                 this.matchamong = value;
204         }
205
206         public void setReference(String value) {
207                 // ---------------------------------------
208                 this.refid = value;
209         }
210
211         public void setState(String value) {
212                 // ----------------------------------
213                 this.typid = value;
214         }
215
216         public void setVisibility(String value) {
217                 // ----------------------------------------
218                 this.visibility = value;
219         }
220
221         public void setWords(String value) {
222                 // -----------------------------------
223                 this.words = value;
224         }
225
226         // ==============================================================================================================================
227         // Implementation of abstract services
228         // ==============================================================================================================================
229
230         protected List<SimulationContextType> getInvolvedContexts() {
231                 // ------------------------------------------------------------
232                 return getSimulationContextService().selectAllTypes();
233         }
234
235         @SuppressWarnings("unchecked")
236         protected void loadFilter() {
237                 // ----------------------------
238                 Map<String, Object> session = getSession();
239                 User user = getConnectedUser();
240                 Map<String, Object> filter = (Map<String, Object>) session
241                                 .get("knowledge.filter"); // A default filter is supposed being set at start
242
243                 visibility = (String) filter.get("visibility");
244                 matchamong = (String) filter.get("matchamong");
245                 matcontext = (String) filter.get("matcontext");
246                 typid = (String) filter.get("type");
247                 author = (String) filter.get("author");
248                 refid = (String) filter.get("reference");
249                 words = (String) filter.get("title");
250                 context = (List<SimulationContext>) filter.get("context");
251
252                 if (user == null) {
253                         visibility = "onlypublic";
254                 }
255         }
256
257         @SuppressWarnings("unchecked")
258         protected void saveFilter() {
259                 // ----------------------------
260                 Map<String, Object> session = getSession();
261                 Map<String, Object> filter = (Map<String, Object>) session
262                                 .get("knowledge.filter"); // A default filter is supposed being set at start
263
264                 filter.put("visibility", this.visibility);
265                 filter.put("matchamong", this.matchamong);
266                 filter.put("matcontext", this.matcontext);
267                 filter.put("type", this.typid);
268                 filter.put("author", this.author);
269                 filter.put("reference", "");
270                 filter.put("title", this.words);
271
272                 context = (List<SimulationContext>) filter.get("context"); // Only criteria not part of the form
273
274                 // Initialization required by all do functions
275                 types = getKnowledgeElementTypeService().selectTypesWhere(ProgressState.APPROVED);
276         }
277
278         /**
279          * Get the searchService.
280          * 
281          * @return the searchService
282          */
283         public SearchService getSearchService() {
284                 return _searchService;
285         }
286
287         /**
288          * Set the searchService.
289          * 
290          * @param searchService
291          *            the searchService to set
292          */
293         public void setSearchService(SearchService searchService) {
294                 _searchService = searchService;
295         }
296
297         /**
298          * Get the simulationContextService.
299          * 
300          * @return the simulationContextService
301          */
302         public SimulationContextService getSimulationContextService() {
303                 return _simulationContextService;
304         }
305
306         /**
307          * Set the simulationContextService.
308          * 
309          * @param simulationContextService
310          *            the simulationContextService to set
311          */
312         public void setSimulationContextService(
313                         SimulationContextService simulationContextService) {
314                 _simulationContextService = simulationContextService;
315         }
316
317         /**
318          * Get the knowledgeElementTypeService.
319          * 
320          * @return the knowledgeElementTypeService
321          */
322         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
323                 return _knowledgeElementTypeService;
324         }
325
326         /**
327          * Set the knowledgeElementTypeService.
328          * 
329          * @param knowledgeElementTypeService
330          *            the knowledgeElementTypeService to set
331          */
332         public void setKnowledgeElementTypeService(
333                         KnowledgeElementTypeService knowledgeElementTypeService) {
334                 _knowledgeElementTypeService = knowledgeElementTypeService;
335         }
336
337         /**
338          * Get the userService.
339          * @return the userService
340          */
341         public UserService getUserService() {
342                 return _userService;
343         }
344
345         /**
346          * Set the userService.
347          * @param userService the userService to set
348          */
349         public void setUserService(UserService userService) {
350                 _userService = userService;
351         }
352         
353         /**
354          * Get the menuProperty.
355          * @return the menuProperty
356          */
357         public String getMenuProperty() {
358                 return _menuProperty;
359         }
360
361         /**
362          * Set the menuProperty.
363          * @param menuProperty the menuProperty to set
364          */
365         public void setMenuProperty(String menuProperty) {
366                 this._menuProperty = menuProperty;
367         }
368         
369         /**
370          * Get the toolProperty.
371          * @return the toolProperty
372          */
373         public String getToolProperty() {
374                 return _toolProperty;
375         }
376
377         /**
378          * Set the toolProperty.
379          * @param toolProperty the toolProperty to set
380          */
381         public void setToolProperty(final String toolProperty) {
382                 _toolProperty = toolProperty;
383         }
384         
385         /**
386          * Get the leftMenuProperty.
387          * @return the leftMenuProperty
388          */
389         public String getLeftMenuProperty() {
390                 return _leftMenuProperty;
391         }
392
393         /**
394          * Set the leftMenuProperty.
395          * @param leftMenuProperty the leftMenuProperty to set
396          */
397         public void setLeftMenuProperty(final String leftMenuProperty) {
398                 _leftMenuProperty = leftMenuProperty;
399         }
400 }