Salome HOME
Reindex of studies is fixed.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / SearchStudyAction.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.dal.bo.som.ProgressState;
9 import org.splat.service.SearchService;
10 import org.splat.service.SimulationContextService;
11 import org.splat.service.UserService;
12 import org.splat.service.technical.ProjectSettingsService;
13 import org.splat.dal.bo.som.SimulationContext;
14 import org.splat.dal.bo.som.SimulationContextType;
15 import org.splat.dal.bo.som.Study;
16 import org.splat.dal.bo.som.Visibility;
17
18 /**
19  * Search studies form action.
20  *
21  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
22  */
23 public class SearchStudyAction extends SearchBaseAction {
24
25         private String visibility = null; // "Private", "Public", "All"
26         private String state = null; // "In-Work", "In-Draft", "In-Check"...
27         private String matchamong = null; // "all" or "any"
28         private String matcontext = null; // "all" or "any"
29         private String refid = null; // Study reference
30         private String words = null; // Full text search words
31         /**
32          * Injected project settings service.
33          */
34         private ProjectSettingsService _projectSettingsService;
35         /**
36          * Injected search service.
37          */
38         private SearchService _searchService;
39         /**
40          * Injected simulation context service.
41          */
42         private SimulationContextService _simulationContextService;
43         /**
44          * Injected user service.
45          */
46         private UserService _userService;
47
48         /**
49          * Serial version ID.
50          */
51         private static final long serialVersionUID = -1910481357051393077L;
52
53         enum UserAction {
54                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
55         }
56
57         // ==============================================================================================================================
58         // Action methods
59         // ==============================================================================================================================
60
61         /**
62          * Initialize study search form.
63          * @return SUCCESS if no exception, otherwise return ERROR
64          */
65         public String doInitialize() {
66                 try {
67                         loadFilter();
68                         doSearch();
69
70                         // Final initialization of the form
71                         setCandidates();
72                         setContextTypeOptions(getInvolvedContexts());
73
74                         return SUCCESS;
75                 } catch (Exception error) {
76                         // No need to roll back the transaction as it is read only
77                         logger.error("Reason: ", error);
78                         return ERROR;
79                 }
80         }
81
82         protected String doSearch() throws InvalidPropertyException {
83                 // ----------------------------
84                 Map<String, Object> session = getSession();
85                 User user = getConnectedUser();
86
87                 Study.Properties sprop = new Study.Properties();
88
89                 // Search matching all criteria
90                 if (!this.state.equals("ANY"))
91                         sprop.setState(ProgressState.valueOf(this.state));
92                 if (words.length() > 0)
93                         sprop.setTitle(words);
94                 if (refid.length() > 0)
95                         sprop.setReference(refid);
96                 if (context.size() > 0)
97                         sprop.setSimulationContexts(context);
98                 int index = Integer.valueOf(author);
99                 if (index > 0) {
100                         User him = getUserService().selectUser(index);
101                         sprop.setManager(him);
102                 }
103                 // Set of the visibility
104                 if (visibility.equals("all")) {
105                         Study.Properties other = sprop.copy();
106
107                         other.setVisibility(Visibility.PUBLIC);
108                         sprop.setVisibility(Visibility.PRIVATE);
109                         sprop.setActor(user);
110
111                         result = getSearchService().selectStudiesWhere(sprop, other);
112                 } else {
113                         Visibility reparea = null;
114                         if (visibility.equals("onlypublic"))
115                                 reparea = Visibility.PUBLIC;
116                         else
117                                 reparea = Visibility.valueOf(visibility);
118                         sprop.setVisibility(reparea);
119                         if (reparea == Visibility.PRIVATE)
120                                 sprop.setActor(user);
121
122                         result = getSearchService().selectStudiesWhere(sprop);
123                 }
124                 session.put("search.result", result); // For redisplaying the page without re-executing the search
125                 return "refresh";
126         }
127
128         // ==============================================================================================================================
129         // Getters
130         // ==============================================================================================================================
131
132         public String getContextMatch() {
133                 // --------------------------------
134                 return matcontext;
135         }
136
137         public String getCriteriaMatch() {
138                 // ---------------------------------
139                 return matchamong;
140         }
141
142         public String getReference() {
143                 // -----------------------------
144                 return refid;
145         }
146
147         public String getState() {
148                 // -------------------------
149                 return state;
150         }
151
152         public String getVisibility() {
153                 // ------------------------------
154                 return visibility;
155         }
156
157         public String getWords() {
158                 // -------------------------
159                 return words;
160         }
161
162         // ==============================================================================================================================
163         // Setters
164         // ==============================================================================================================================
165
166         public void setContextMatch(String value) {
167                 // ------------------------------------------
168                 this.matcontext = value;
169         }
170
171         public void setCriteriaMatch(String value) {
172                 // -------------------------------------------
173                 this.matchamong = value;
174         }
175
176         public void setReference(String value) {
177                 // ---------------------------------------
178                 this.refid = value;
179         }
180
181         public void setState(String value) {
182                 // -----------------------------------
183                 this.state = value;
184         }
185
186         public void setVisibility(String value) {
187                 // ----------------------------------------
188                 this.visibility = value;
189         }
190
191         public void setWords(String value) {
192                 // -----------------------------------
193                 this.words = value;
194         }
195
196         // ==============================================================================================================================
197         // Implementation of abstract services
198         // ==============================================================================================================================
199
200         protected List<SimulationContextType> getInvolvedContexts() {
201                 // ------------------------------------------------------------
202                 List<ProjectSettingsService.Step> steps = getProjectSettings()
203                                 .getStepsOf(Study.class);
204                 ProjectSettingsService.Step[] number = steps
205                                 .toArray(new ProjectSettingsService.Step[steps.size()]);
206
207                 return getSimulationContextService().selectTypesOf(number);
208         }
209
210         @SuppressWarnings("unchecked")
211         protected void loadFilter() {
212                 // ----------------------------
213                 Map<String, Object> session = getSession();
214                 User user = getConnectedUser();
215                 Map<String, Object> filter = (Map<String, Object>) session
216                                 .get("study.filter"); // A default filter is supposed being set at start
217
218                 visibility = (String) filter.get("visibility");
219                 matchamong = (String) filter.get("matchamong");
220                 matcontext = (String) filter.get("matcontext");
221                 state = (String) filter.get("state");
222                 author = (String) filter.get("author");
223                 refid = (String) filter.get("reference");
224                 words = (String) filter.get("title");
225                 context = (List<SimulationContext>) filter.get("context");
226
227                 if (user == null) {
228                         visibility = "onlypublic";
229                 }
230         }
231
232         @SuppressWarnings("unchecked")
233         protected void saveFilter() {
234                 // ----------------------------
235                 Map<String, Object> session = getSession();
236                 Map<String, Object> filter = (Map<String, Object>) session
237                                 .get("study.filter"); // A default filter is supposed being set at start
238
239                 filter.put("visibility", this.visibility);
240                 filter.put("matchamong", this.matchamong);
241                 filter.put("matcontext", this.matcontext);
242                 filter.put("state", this.state);
243                 filter.put("author", this.author);
244                 filter.put("reference", this.refid);
245                 filter.put("title", this.words);
246
247                 context = (List<SimulationContext>) filter.get("context"); // Only criteria not part of the form
248
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 project settings.
272          * 
273          * @return Project settings service
274          */
275         private ProjectSettingsService getProjectSettings() {
276                 return _projectSettingsService;
277         }
278
279         /**
280          * Set project settings service.
281          * 
282          * @param projectSettingsService
283          *            project settings service
284          */
285         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
286                 _projectSettingsService = projectSettingsService;
287         }
288
289         /**
290          * Get the simulationContextService.
291          * 
292          * @return the simulationContextService
293          */
294         public SimulationContextService getSimulationContextService() {
295                 return _simulationContextService;
296         }
297
298         /**
299          * Set the simulationContextService.
300          * 
301          * @param simulationContextService
302          *            the simulationContextService to set
303          */
304         public void setSimulationContextService(
305                         SimulationContextService simulationContextService) {
306                 _simulationContextService = simulationContextService;
307         }
308
309         /**
310          * Get the userService.
311          * 
312          * @return the userService
313          */
314         public UserService getUserService() {
315                 return _userService;
316         }
317
318         /**
319          * Set the userService.
320          * 
321          * @param userService
322          *            the userService to set
323          */
324         public void setUserService(UserService userService) {
325                 _userService = userService;
326         }
327 }