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