]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/SearchKnowledgeAction.java
Salome HOME
Actions menu properties are refactored, unnecessary code is removed.
[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.wapp.Constants;
17
18 /**
19  * Action for searching a knowledge in the database.
20  */
21 public class SearchKnowledgeAction extends AbstractSearchBaseAction {
22
23         /**
24          * Serial version ID.
25          */
26         private static final long serialVersionUID = -3104321907432838476L;
27
28         /**
29          * "Private", "Public", "All".
30          */
31         private String _visibility = null;
32         /**
33          * Knowledge type index when among all.
34          */
35         private String _state = null;
36         /**
37          * Criteria match: "all" or "any".
38          */
39         private String _criteriaMatch = null;
40         /**
41          * Simulation context match: "all" or "any".
42          */
43         private String _contextMatch = null;
44         /**
45          * Knowledge reference when among ref.
46          */
47         private String _reference = null;
48         /**
49          * Full text search words.
50          */
51         private String _words = null;
52         /**
53          * Available knowledge types filter (initialized below).
54          */
55         private transient List<KnowledgeElementType> _knowledgeTypes;
56         /**
57          * Injected search service.
58          */
59         private SearchService _searchService;
60         /**
61          * Injected knowledge element service.
62          */
63         private KnowledgeElementTypeService _knowledgeElementTypeService;
64
65         // ==============================================================================================================================
66         // Action methods
67         // ==============================================================================================================================
68
69         /**
70          * The action initialization.
71          * 
72          * @return SUCCESS if succeeded, ERROR if doSearch() is failed
73          */
74         public String doInitialize() {
75
76                 initializationFullScreenContext(Constants.OPEN, Constants.NONE,
77                                 Constants.OPEN);
78
79                 String res = SUCCESS;
80                 try {
81                         loadFilter();
82                         doSearch();
83
84                         // Final initialization of the form
85                         _knowledgeTypes = getKnowledgeElementTypeService()
86                                         .selectTypesWhere(ProgressState.APPROVED);
87                         setCandidates();
88                         setContextTypeOptions(getInvolvedContexts());
89                 } catch (Exception error) {
90                         // No need to roll back the transaction as it is read only
91                         LOG.error("Reason: ", error);
92                         res = ERROR;
93                 }
94                 return res;
95         }
96
97         @Override
98         protected String doSearch() throws InvalidPropertyException {
99
100                 initializationScreenContext(Constants.OPEN);
101                 Map<String, Object> session = getSession();
102                 KnowledgeElement.Properties sprop = new KnowledgeElement.Properties();
103
104                 // Search matching all criteria
105                 sprop.setType(getKnowledgeElementTypeService().selectType(
106                                 Integer.valueOf(_state)));
107                 if (_words.length() > 0) {
108                         sprop.setTitle(_words);
109                 }
110                 if (_reference.length() > 0) {
111                         sprop.setReference(_reference);
112                 }
113                 if (_context.size() > 0) {
114                         sprop.setSimulationContexts(_context);
115                 }
116                 int index = Integer.valueOf(_author);
117                 if (index > 0) {
118                         User him = getUserService().selectUser(index);
119                         sprop.setAuthor(him);
120                 }
121                 // Set of the visibility
122                 if ("all".equals(_visibility)) {
123                         KnowledgeElement.Properties other = sprop.copy();
124
125                         other.setVisibility(Visibility.PUBLIC);
126                         sprop.setVisibility(Visibility.PRIVATE);
127                         sprop.setActor(getConnectedUser());
128
129                         _result = getSearchService().selectKnowledgeElementsWhere(sprop,
130                                         other);
131                 } else {
132                         Visibility reparea;
133                         if ("onlypublic".equals(_visibility)) {
134                                 reparea = Visibility.PUBLIC;
135                         } else {
136                                 reparea = Visibility.valueOf(_visibility);
137                         }
138                         sprop.setVisibility(reparea);
139                         if (reparea == Visibility.PRIVATE) {
140                                 sprop.setActor(getConnectedUser());
141                         }
142
143                         _result = getSearchService().selectKnowledgeElementsWhere(sprop);
144                 }
145                 session.put(RESULT_KEY, _result); // For redisplaying the page without re-executing the search
146                 return "refresh";
147         }
148
149         // ==============================================================================================================================
150         // Getters
151         // ==============================================================================================================================
152
153         public String getContextMatch() {
154                 return _contextMatch;
155         }
156
157         public String getCriteriaMatch() {
158                 return _criteriaMatch;
159         }
160
161         public List<KnowledgeElementType> getKnowledgeTypes() {
162                 return _knowledgeTypes;
163         }
164
165         public String getReference() {
166                 return _reference;
167         }
168
169         public String getState() {
170                 return _state;
171         }
172
173         public String getVisibility() {
174                 return _visibility;
175         }
176
177         public String getWords() {
178                 return _words;
179         }
180
181         // ==============================================================================================================================
182         // Setters
183         // ==============================================================================================================================
184
185         public void setContextMatch(final String value) {
186                 this._contextMatch = value;
187         }
188
189         public void setCriteriaMatch(final String value) {
190                 this._criteriaMatch = value;
191         }
192
193         public void setReference(final String value) {
194                 this._reference = value;
195         }
196
197         public void setState(final String value) {
198                 this._state = value;
199         }
200
201         public void setVisibility(final String value) {
202                 this._visibility = value;
203         }
204
205         public void setWords(final String value) {
206                 this._words = value;
207         }
208
209         // ==============================================================================================================================
210         // Implementation of abstract services
211         // ==============================================================================================================================
212
213         @Override
214         protected List<SimulationContextType> getInvolvedContexts() {
215                 return getSimulationContextService().selectAllTypes();
216         }
217
218         @Override
219         @SuppressWarnings("unchecked")
220         protected void loadFilter() {
221                 Map<String, Object> session = getSession();
222                 User user = getConnectedUser();
223                 Map<String, Object> filter = (Map<String, Object>) session
224                                 .get("knowledge.filter"); // A default filter is supposed being set at start
225
226                 _visibility = (String) filter.get("visibility");
227                 _criteriaMatch = (String) filter.get("matchamong");
228                 _contextMatch = (String) filter.get("matcontext");
229                 _state = (String) filter.get("type");
230                 _author = (String) filter.get("author");
231                 _reference = (String) filter.get("reference");
232                 _words = (String) filter.get("title");
233                 _context = (List<SimulationContext>) filter.get("context");
234
235                 if (user == null) {
236                         _visibility = "onlypublic";
237                 }
238         }
239
240         @Override
241         @SuppressWarnings("unchecked")
242         protected void saveFilter() {
243                 Map<String, Object> session = getSession();
244                 Map<String, Object> filter = (Map<String, Object>) session
245                                 .get("knowledge.filter"); // A default filter is supposed being set at start
246
247                 filter.put("visibility", this._visibility);
248                 filter.put("matchamong", this._criteriaMatch);
249                 filter.put("matcontext", this._contextMatch);
250                 filter.put("type", this._state);
251                 filter.put("author", this._author);
252                 filter.put("reference", "");
253                 filter.put("title", this._words);
254
255                 _context = (List<SimulationContext>) filter.get("context"); // Only criteria not part of the form
256
257                 // Initialization required by all do functions
258                 _knowledgeTypes = getKnowledgeElementTypeService().selectTypesWhere(
259                                 ProgressState.APPROVED);
260         }
261
262         /**
263          * Get the searchService.
264          * 
265          * @return the searchService
266          */
267         public SearchService getSearchService() {
268                 return _searchService;
269         }
270
271         /**
272          * Set the searchService.
273          * 
274          * @param searchService
275          *            the searchService to set
276          */
277         public void setSearchService(final SearchService searchService) {
278                 _searchService = searchService;
279         }
280
281         /**
282          * Get the knowledgeElementTypeService.
283          * 
284          * @return the knowledgeElementTypeService
285          */
286         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
287                 return _knowledgeElementTypeService;
288         }
289
290         /**
291          * Set the knowledgeElementTypeService.
292          * 
293          * @param knowledgeElementTypeService
294          *            the knowledgeElementTypeService to set
295          */
296         public void setKnowledgeElementTypeService(
297                         final KnowledgeElementTypeService knowledgeElementTypeService) {
298                 _knowledgeElementTypeService = knowledgeElementTypeService;
299         }
300 }