Salome HOME
Tool bar is improved.
[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          * Value of the menu property. 
50          * It can be: none, create, open, study, knowledge, sysadmin, help.
51          */
52         private String _menuProperty;
53         
54         /**
55          * Value of the tool bar property. 
56          * It can be: none, standard, study, back.
57          */
58         private String _toolProperty;
59
60         /**
61          * Serial version ID.
62          */
63         private static final long serialVersionUID = -1910481357051393077L;
64
65         enum UserAction {
66                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
67         }
68
69         // ==============================================================================================================================
70         // Action methods
71         // ==============================================================================================================================
72
73         /**
74          * Initialize study search form.
75          * @return SUCCESS if no exception, otherwise return ERROR
76          */
77         public String doInitialize() {
78                 try {
79                         loadFilter();
80                         doSearch();
81
82                         // Final initialization of the form
83                         setCandidates();
84                         setContextTypeOptions(getInvolvedContexts());
85                         
86                         setMenuProperty("open");
87                         setToolProperty("none");
88                         initializationScreenContext(_menuProperty, _toolProperty);
89
90                         return SUCCESS;
91                 } catch (Exception error) {
92                         // No need to roll back the transaction as it is read only
93                         logger.error("Reason: ", error);
94                         
95                         setMenuProperty("none");
96                         initializationScreenContext(_menuProperty);
97                         
98                         return ERROR;
99                 }
100         }
101
102         protected String doSearch() throws InvalidPropertyException {
103                 // ----------------------------
104                 Map<String, Object> session = getSession();
105                 User user = getConnectedUser();
106
107                 Study.Properties sprop = new Study.Properties();
108
109                 // Search matching all criteria
110                 if (!this.state.equals("ANY"))
111                         sprop.setState(ProgressState.valueOf(this.state));
112                 if (words.length() > 0)
113                         sprop.setTitle(words);
114                 if (refid.length() > 0)
115                         sprop.setReference(refid);
116                 if (context.size() > 0)
117                         sprop.setSimulationContexts(context);
118                 int index = Integer.valueOf(author);
119                 if (index > 0) {
120                         User him = getUserService().selectUser(index);
121                         sprop.setManager(him);
122                 }
123                 // Set of the visibility
124                 if (visibility.equals("all")) {
125                         Study.Properties other = sprop.copy();
126
127                         other.setVisibility(Visibility.PUBLIC);
128                         sprop.setVisibility(Visibility.PRIVATE);
129                         sprop.setActor(user);
130
131                         result = getSearchService().selectStudiesWhere(sprop, other);
132                 } else {
133                         Visibility reparea = null;
134                         if (visibility.equals("onlypublic"))
135                                 reparea = Visibility.PUBLIC;
136                         else
137                                 reparea = Visibility.valueOf(visibility);
138                         sprop.setVisibility(reparea);
139                         if (reparea == Visibility.PRIVATE)
140                                 sprop.setActor(user);
141
142                         result = getSearchService().selectStudiesWhere(sprop);
143                 }
144                 session.put("search.result", result); // For redisplaying the page without re-executing the search
145                 return "refresh";
146         }
147
148         // ==============================================================================================================================
149         // Getters
150         // ==============================================================================================================================
151
152         public String getContextMatch() {
153                 // --------------------------------
154                 return matcontext;
155         }
156
157         public String getCriteriaMatch() {
158                 // ---------------------------------
159                 return matchamong;
160         }
161
162         public String getReference() {
163                 // -----------------------------
164                 return refid;
165         }
166
167         public String getState() {
168                 // -------------------------
169                 return state;
170         }
171
172         public String getVisibility() {
173                 // ------------------------------
174                 return visibility;
175         }
176
177         public String getWords() {
178                 // -------------------------
179                 return words;
180         }
181
182         // ==============================================================================================================================
183         // Setters
184         // ==============================================================================================================================
185
186         public void setContextMatch(String value) {
187                 // ------------------------------------------
188                 this.matcontext = value;
189         }
190
191         public void setCriteriaMatch(String value) {
192                 // -------------------------------------------
193                 this.matchamong = value;
194         }
195
196         public void setReference(String value) {
197                 // ---------------------------------------
198                 this.refid = value;
199         }
200
201         public void setState(String value) {
202                 // -----------------------------------
203                 this.state = value;
204         }
205
206         public void setVisibility(String value) {
207                 // ----------------------------------------
208                 this.visibility = value;
209         }
210
211         public void setWords(String value) {
212                 // -----------------------------------
213                 this.words = value;
214         }
215
216         // ==============================================================================================================================
217         // Implementation of abstract services
218         // ==============================================================================================================================
219
220         protected List<SimulationContextType> getInvolvedContexts() {
221                 // ------------------------------------------------------------
222                 List<ProjectSettingsService.Step> steps = getProjectSettings()
223                                 .getStepsOf(Study.class);
224                 ProjectSettingsService.Step[] number = steps
225                                 .toArray(new ProjectSettingsService.Step[steps.size()]);
226
227                 return getSimulationContextService().selectTypesOf(number);
228         }
229
230         @SuppressWarnings("unchecked")
231         protected void loadFilter() {
232                 // ----------------------------
233                 Map<String, Object> session = getSession();
234                 User user = getConnectedUser();
235                 Map<String, Object> filter = (Map<String, Object>) session
236                                 .get("study.filter"); // A default filter is supposed being set at start
237
238                 visibility = (String) filter.get("visibility");
239                 matchamong = (String) filter.get("matchamong");
240                 matcontext = (String) filter.get("matcontext");
241                 state = (String) filter.get("state");
242                 author = (String) filter.get("author");
243                 refid = (String) filter.get("reference");
244                 words = (String) filter.get("title");
245                 context = (List<SimulationContext>) filter.get("context");
246
247                 if (user == null) {
248                         visibility = "onlypublic";
249                 }
250         }
251
252         @SuppressWarnings("unchecked")
253         protected void saveFilter() {
254                 // ----------------------------
255                 Map<String, Object> session = getSession();
256                 Map<String, Object> filter = (Map<String, Object>) session
257                                 .get("study.filter"); // A default filter is supposed being set at start
258
259                 filter.put("visibility", this.visibility);
260                 filter.put("matchamong", this.matchamong);
261                 filter.put("matcontext", this.matcontext);
262                 filter.put("state", this.state);
263                 filter.put("author", this.author);
264                 filter.put("reference", this.refid);
265                 filter.put("title", this.words);
266
267                 context = (List<SimulationContext>) filter.get("context"); // Only criteria not part of the form
268
269         }
270
271         /**
272          * Get the searchService.
273          * 
274          * @return the searchService
275          */
276         public SearchService getSearchService() {
277                 return _searchService;
278         }
279
280         /**
281          * Set the searchService.
282          * 
283          * @param searchService
284          *            the searchService to set
285          */
286         public void setSearchService(SearchService searchService) {
287                 _searchService = searchService;
288         }
289
290         /**
291          * Get project settings.
292          * 
293          * @return Project settings service
294          */
295         private ProjectSettingsService getProjectSettings() {
296                 return _projectSettingsService;
297         }
298
299         /**
300          * Set project settings service.
301          * 
302          * @param projectSettingsService
303          *            project settings service
304          */
305         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
306                 _projectSettingsService = projectSettingsService;
307         }
308
309         /**
310          * Get the simulationContextService.
311          * 
312          * @return the simulationContextService
313          */
314         public SimulationContextService getSimulationContextService() {
315                 return _simulationContextService;
316         }
317
318         /**
319          * Set the simulationContextService.
320          * 
321          * @param simulationContextService
322          *            the simulationContextService to set
323          */
324         public void setSimulationContextService(
325                         SimulationContextService simulationContextService) {
326                 _simulationContextService = simulationContextService;
327         }
328
329         /**
330          * Get the userService.
331          * 
332          * @return the userService
333          */
334         public UserService getUserService() {
335                 return _userService;
336         }
337
338         /**
339          * Set the userService.
340          * 
341          * @param userService
342          *            the userService to set
343          */
344         public void setUserService(UserService userService) {
345                 _userService = userService;
346         }
347         
348         /**
349          * Get the menuProperty.
350          * @return the menuProperty
351          */
352         public String getMenuProperty() {
353                 return _menuProperty;
354         }
355
356         /**
357          * Set the menuProperty.
358          * @param menuProperty the menuProperty to set
359          */
360         public void setMenuProperty(String menuProperty) {
361                 this._menuProperty = menuProperty;
362         }
363         
364         /**
365          * Get the toolProperty.
366          * @return the toolProperty
367          */
368         public String getToolProperty() {
369                 return _toolProperty;
370         }
371
372         /**
373          * Set the toolProperty.
374          * @param toolProperty the toolProperty to set
375          */
376         public void setToolProperty(final String toolProperty) {
377                 _toolProperty = toolProperty;
378         }
379 }