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