Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[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.hibernate.Session;
7 import org.hibernate.Transaction;
8 import org.splat.kernel.InvalidPropertyException;
9 import org.splat.dal.bo.kernel.User;
10 import org.splat.kernel.UserDirectory;
11 import org.splat.service.SearchService;
12 import org.splat.dal.dao.som.Database;
13 import org.splat.dal.bo.som.KnowledgeElement;
14 import org.splat.dal.bo.som.KnowledgeElementType;
15 import org.splat.dal.bo.som.ProgressState;
16 import org.splat.dal.bo.som.SimulationContext;
17 import org.splat.dal.bo.som.SimulationContextType;
18 import org.splat.dal.bo.som.Visibility;
19
20
21 public class SearchKnowledgeAction extends SearchBaseAction {
22
23     private String                       visibility = null;   // "Private", "Public", "All"
24     private String                       typid      = null;   // Knowledge type index when among all
25     private String                       matchamong = null;   // "all" or "any"
26     private String                       matcontext = null;   // "all" or "any"
27     private String                       refid      = null;   // Knowledge reference  when among ref
28     private String                       words      = null;   // Full text search words
29     private List<KnowledgeElementType>   types;            // Available knowledge types filter (initialized below)
30     private SearchService                _searchService;
31
32         private static final long serialVersionUID = -3104321907432838476L;
33
34 //  ==============================================================================================================================
35 //  Action methods
36 //  ==============================================================================================================================
37
38         public String doInitialize () {
39 //  -----------------------------
40       Session      connex  = Database.getSession();
41       Transaction  transax = connex.beginTransaction();      
42       try {
43         loadFilter();          
44         doSearch();
45
46 //      Final initialization of the form
47         types = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
48         setCandidates();
49         setContextTypeOptions(getInvolvedContexts());
50
51         transax.commit();
52         return SUCCESS;
53       }
54       catch (Exception error) {
55 //          No need to roll back the transaction as it is read only
56         logger.error("Reason: ", error);
57         return ERROR;
58       }
59         }
60
61         protected String doSearch () throws InvalidPropertyException {
62 //  ----------------------------
63       Map<String, Object> session = getSession();
64       User                user    = getConnectedUser();
65
66       KnowledgeElement.Properties  sprop = new KnowledgeElement.Properties();
67
68 //    Search matching all criteria
69         sprop.setType( KnowledgeElement.selectType(Integer.valueOf(typid)) );
70         if (words.length() > 0)          sprop.setTitle(words);
71         if (refid.length() > 0)          sprop.setReference(refid);
72         if (context.size() > 0)          sprop.setSimulationContexts(context);
73         int index = Integer.valueOf(author);
74         if (index > 0) {
75           User him = UserDirectory.selectUser(index);
76           sprop.setAuthor(him);
77         }
78 //      Set of the visibility
79         if (visibility.equals("all")) {
80           KnowledgeElement.Properties  other = sprop.copy();
81
82           other.setVisibility(Visibility.PUBLIC);
83           sprop.setVisibility(Visibility.PRIVATE);
84           sprop.setActor(user);
85
86           result = getSearchService().selectKnowledgeElementsWhere(sprop, other);
87         }
88         else {
89           Visibility reparea = null;
90           if (visibility.equals("onlypublic")) reparea = Visibility.PUBLIC;
91           else                                 reparea = Visibility.valueOf(visibility);
92           sprop.setVisibility(reparea);
93           if (reparea == Visibility.PRIVATE) sprop.setActor(user);
94
95           result = getSearchService().selectKnowledgeElementsWhere(sprop);
96       }
97       session.put("search.result", result);     // For redisplaying the page without re-executing the search
98       return "refresh";
99         }
100
101 //  ==============================================================================================================================
102 //  Getters
103 //  ==============================================================================================================================
104
105     public String getContextMatch () {
106 //  --------------------------------
107       return matcontext;
108     }
109     public String getCriteriaMatch () {
110 //  ---------------------------------
111       return matchamong;
112     }
113     public List<KnowledgeElementType> getKnowledgeTypes () {
114 //  ------------------------------------------------------
115       return types;
116     }
117     public String getReference () {
118 //  -----------------------------
119       return refid;
120     }
121     public String getState () {
122 //  ------------------------
123       return typid;
124     }
125     public String getVisibility () {
126 //  ------------------------------
127       return visibility;
128     }
129     public String getWords () {
130 //  -------------------------
131       return words;
132     }
133
134 //  ==============================================================================================================================
135 //  Setters
136 //  ==============================================================================================================================
137
138     public void setContextMatch (String value) {
139 //  ------------------------------------------
140       this.matcontext = value;
141     }
142     public void setCriteriaMatch (String value) {
143 //  -------------------------------------------
144       this.matchamong = value;
145     }
146     public void setReference (String value) {
147 //  ---------------------------------------
148       this.refid = value;
149     }
150     public void setState (String value) {
151 //  ----------------------------------
152       this.typid = value;
153     }
154     public void setVisibility (String value) {
155 //  ----------------------------------------
156       this.visibility = value;
157     }
158     public void setWords (String value) {
159 //  -----------------------------------
160       this.words = value;
161     }
162
163 //  ==============================================================================================================================
164 //  Implementation of abstract services
165 //  ==============================================================================================================================
166
167         protected List<SimulationContextType> getInvolvedContexts () {
168 //  ------------------------------------------------------------
169       return SimulationContext.selectAllTypes();
170         }
171
172     @SuppressWarnings("unchecked")
173         protected void loadFilter () {
174 //  ----------------------------
175       Map<String,Object> session = getSession();
176       User               user    = getConnectedUser();
177       Map<String,Object> filter  = (Map<String, Object>)session.get("knowledge.filter");   // A default filter is supposed being set at start
178
179       visibility = (String)filter.get("visibility");
180       matchamong = (String)filter.get("matchamong");
181       matcontext = (String)filter.get("matcontext");
182       typid      = (String)filter.get("type");
183       author     = (String)filter.get("author");
184       refid      = (String)filter.get("reference");
185       words      = (String)filter.get("title");
186       context    = (List<SimulationContext>)filter.get("context");
187
188       if (user == null) {
189         visibility = "onlypublic";
190       }
191     }
192
193         @SuppressWarnings("unchecked")
194         protected void saveFilter () {
195 //  ----------------------------
196       Map<String,Object> session = getSession();
197       Map<String,Object> filter  = (Map<String, Object>)session.get("knowledge.filter");   // A default filter is supposed being set at start
198
199       filter.put("visibility", this.visibility);
200       filter.put("matchamong", this.matchamong);
201       filter.put("matcontext", this.matcontext);
202       filter.put("type",   this.typid);
203       filter.put("author", this.author);
204       filter.put("reference", "");
205       filter.put("title", this.words);
206
207       context = (List<SimulationContext>)filter.get("context");  // Only criteria not part of the form
208
209 //    Initialization required by all do functions
210       types = KnowledgeElement.selectTypesWhere(ProgressState.APPROVED);
211         }
212     /**
213          * Get the searchService.
214          * @return the searchService
215          */
216         public SearchService getSearchService() {
217                 return _searchService;
218         }
219
220         /**
221          * Set the searchService.
222          * @param searchService the searchService to set
223          */
224         public void setSearchService(SearchService searchService) {
225                 _searchService = searchService;
226         }
227 }