Salome HOME
Tool bar is improved.
[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         // Action methods
65         // ==============================================================================================================================
66
67         /**
68          * The action initialization.
69          * 
70          * @return SUCCESS if succeeded, ERROR if doSearch() is failed
71          */
72         public String doInitialize() {
73                 
74                 setMenuProperty("open");
75                 setToolProperty("none");
76                 initializationScreenContext(_menuProperty, _toolProperty);
77                 
78                 try {
79                         loadFilter();
80                         doSearch();
81
82                         // Final initialization of the form
83                         types = getKnowledgeElementTypeService().selectTypesWhere(
84                                         ProgressState.APPROVED);
85                         setCandidates();
86                         setContextTypeOptions(getInvolvedContexts());
87
88                         return SUCCESS;
89                 } catch (Exception error) {
90                         // No need to roll back the transaction as it is read only
91                         logger.error("Reason: ", error);
92                         return ERROR;
93                 }
94         }
95
96         protected String doSearch() throws InvalidPropertyException {
97                 // ----------------------------
98                 setMenuProperty("open");
99                 initializationScreenContext(_menuProperty);
100                 
101                 Map<String, Object> session = getSession();
102                 User user = getConnectedUser();
103
104                 KnowledgeElement.Properties sprop = new KnowledgeElement.Properties();
105
106                 // Search matching all criteria
107                 sprop.setType(getKnowledgeElementTypeService().selectType(
108                                 Integer.valueOf(typid)));
109                 if (words.length() > 0)
110                         sprop.setTitle(words);
111                 if (refid.length() > 0)
112                         sprop.setReference(refid);
113                 if (context.size() > 0)
114                         sprop.setSimulationContexts(context);
115                 int index = Integer.valueOf(author);
116                 if (index > 0) {
117                         User him = getUserService().selectUser(index);
118                         sprop.setAuthor(him);
119                 }
120                 // Set of the visibility
121                 if (visibility.equals("all")) {
122                         KnowledgeElement.Properties other = sprop.copy();
123
124                         other.setVisibility(Visibility.PUBLIC);
125                         sprop.setVisibility(Visibility.PRIVATE);
126                         sprop.setActor(user);
127
128                         result = getSearchService().selectKnowledgeElementsWhere(sprop,
129                                         other);
130                 } else {
131                         Visibility reparea = null;
132                         if (visibility.equals("onlypublic"))
133                                 reparea = Visibility.PUBLIC;
134                         else
135                                 reparea = Visibility.valueOf(visibility);
136                         sprop.setVisibility(reparea);
137                         if (reparea == Visibility.PRIVATE)
138                                 sprop.setActor(user);
139
140                         result = getSearchService().selectKnowledgeElementsWhere(sprop);
141                 }
142                 session.put("search.result", result); // For redisplaying the page without re-executing the search
143                 return "refresh";
144         }
145
146         // ==============================================================================================================================
147         // Getters
148         // ==============================================================================================================================
149
150         public String getContextMatch() {
151                 // --------------------------------
152                 return matcontext;
153         }
154
155         public String getCriteriaMatch() {
156                 // ---------------------------------
157                 return matchamong;
158         }
159
160         public List<KnowledgeElementType> getKnowledgeTypes() {
161                 // ------------------------------------------------------
162                 return types;
163         }
164
165         public String getReference() {
166                 // -----------------------------
167                 return refid;
168         }
169
170         public String getState() {
171                 // ------------------------
172                 return typid;
173         }
174
175         public String getVisibility() {
176                 // ------------------------------
177                 return visibility;
178         }
179
180         public String getWords() {
181                 // -------------------------
182                 return words;
183         }
184
185         // ==============================================================================================================================
186         // Setters
187         // ==============================================================================================================================
188
189         public void setContextMatch(String value) {
190                 // ------------------------------------------
191                 this.matcontext = value;
192         }
193
194         public void setCriteriaMatch(String value) {
195                 // -------------------------------------------
196                 this.matchamong = value;
197         }
198
199         public void setReference(String value) {
200                 // ---------------------------------------
201                 this.refid = value;
202         }
203
204         public void setState(String value) {
205                 // ----------------------------------
206                 this.typid = value;
207         }
208
209         public void setVisibility(String value) {
210                 // ----------------------------------------
211                 this.visibility = value;
212         }
213
214         public void setWords(String value) {
215                 // -----------------------------------
216                 this.words = value;
217         }
218
219         // ==============================================================================================================================
220         // Implementation of abstract services
221         // ==============================================================================================================================
222
223         protected List<SimulationContextType> getInvolvedContexts() {
224                 // ------------------------------------------------------------
225                 return getSimulationContextService().selectAllTypes();
226         }
227
228         @SuppressWarnings("unchecked")
229         protected void loadFilter() {
230                 // ----------------------------
231                 Map<String, Object> session = getSession();
232                 User user = getConnectedUser();
233                 Map<String, Object> filter = (Map<String, Object>) session
234                                 .get("knowledge.filter"); // A default filter is supposed being set at start
235
236                 visibility = (String) filter.get("visibility");
237                 matchamong = (String) filter.get("matchamong");
238                 matcontext = (String) filter.get("matcontext");
239                 typid = (String) filter.get("type");
240                 author = (String) filter.get("author");
241                 refid = (String) filter.get("reference");
242                 words = (String) filter.get("title");
243                 context = (List<SimulationContext>) filter.get("context");
244
245                 if (user == null) {
246                         visibility = "onlypublic";
247                 }
248         }
249
250         @SuppressWarnings("unchecked")
251         protected void saveFilter() {
252                 // ----------------------------
253                 Map<String, Object> session = getSession();
254                 Map<String, Object> filter = (Map<String, Object>) session
255                                 .get("knowledge.filter"); // A default filter is supposed being set at start
256
257                 filter.put("visibility", this.visibility);
258                 filter.put("matchamong", this.matchamong);
259                 filter.put("matcontext", this.matcontext);
260                 filter.put("type", this.typid);
261                 filter.put("author", this.author);
262                 filter.put("reference", "");
263                 filter.put("title", this.words);
264
265                 context = (List<SimulationContext>) filter.get("context"); // Only criteria not part of the form
266
267                 // Initialization required by all do functions
268                 types = getKnowledgeElementTypeService().selectTypesWhere(ProgressState.APPROVED);
269         }
270
271         /**
272          * Get the searchService.
273          * 
274          * @return the searchService
275          */
276         public SearchService getSearchService() {
277                 return _searchService;
278         }
279
280         /**
281          * Set the searchService.
282          * 
283          * @param searchService
284          *            the searchService to set
285          */
286         public void setSearchService(SearchService searchService) {
287                 _searchService = searchService;
288         }
289
290         /**
291          * Get the simulationContextService.
292          * 
293          * @return the simulationContextService
294          */
295         public SimulationContextService getSimulationContextService() {
296                 return _simulationContextService;
297         }
298
299         /**
300          * Set the simulationContextService.
301          * 
302          * @param simulationContextService
303          *            the simulationContextService to set
304          */
305         public void setSimulationContextService(
306                         SimulationContextService simulationContextService) {
307                 _simulationContextService = simulationContextService;
308         }
309
310         /**
311          * Get the knowledgeElementTypeService.
312          * 
313          * @return the knowledgeElementTypeService
314          */
315         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
316                 return _knowledgeElementTypeService;
317         }
318
319         /**
320          * Set the knowledgeElementTypeService.
321          * 
322          * @param knowledgeElementTypeService
323          *            the knowledgeElementTypeService to set
324          */
325         public void setKnowledgeElementTypeService(
326                         KnowledgeElementTypeService knowledgeElementTypeService) {
327                 _knowledgeElementTypeService = knowledgeElementTypeService;
328         }
329
330         /**
331          * Get the userService.
332          * @return the userService
333          */
334         public UserService getUserService() {
335                 return _userService;
336         }
337
338         /**
339          * Set the userService.
340          * @param userService the userService to set
341          */
342         public void setUserService(UserService userService) {
343                 _userService = userService;
344         }
345         
346         /**
347          * Get the menuProperty.
348          * @return the menuProperty
349          */
350         public String getMenuProperty() {
351                 return _menuProperty;
352         }
353
354         /**
355          * Set the menuProperty.
356          * @param menuProperty the menuProperty to set
357          */
358         public void setMenuProperty(String menuProperty) {
359                 this._menuProperty = menuProperty;
360         }
361         
362         /**
363          * Get the toolProperty.
364          * @return the toolProperty
365          */
366         public String getToolProperty() {
367                 return _toolProperty;
368         }
369
370         /**
371          * Set the toolProperty.
372          * @param toolProperty the toolProperty to set
373          */
374         public void setToolProperty(final String toolProperty) {
375                 _toolProperty = toolProperty;
376         }
377 }