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