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