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