]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/AbstractSearchBaseAction.java
Salome HOME
Creation of a new study from an existing one is implemented.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / AbstractSearchBaseAction.java
1 package org.splat.simer;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Iterator;
7 import java.util.LinkedHashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.splat.dal.bo.kernel.User;
12 import org.splat.dal.bo.som.SimulationContext;
13 import org.splat.dal.bo.som.SimulationContextType;
14 import org.splat.kernel.InvalidPropertyException;
15 import org.splat.kernel.Name;
16 import org.splat.service.SimulationContextService;
17 import org.splat.service.UserService;
18 import org.splat.service.dto.Proxy;
19 import org.splat.service.dto.SearchFilterDTO;
20 import org.splat.service.technical.ProjectSettingsService;
21 import org.splat.som.ApplicationRights;
22 import org.splat.wapp.Constants;
23
24 /**
25  * Base search action class used for searching studies and knowledge.
26  * 
27  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
28  */
29 public abstract class AbstractSearchBaseAction<FilterClass extends SearchFilterDTO>
30                 extends Action {
31
32         /**
33          * Serial version ID.
34          */
35         private static final long serialVersionUID = 7863055790228544510L;
36         /**
37          * Search result key in the session.
38          */
39         public static final String RESULT_KEY = "search.result";
40         /**
41          * Context type index, when selected.
42          */
43         protected transient String _ctype = null;
44         /**
45          * Context value index, when selected.
46          */
47         protected transient String _cvalue = null;
48         /**
49          * Context index, when removed.
50          */
51         protected transient String _cindex = "";
52         /**
53          * Search criteria.
54          */
55         private FilterClass _filter;
56         /**
57          * List of users who can create studies.
58          */
59         protected List<Name> _candidates = null;
60         /**
61          * Context type to be valued.
62          */
63         protected transient SimulationContextType _newtype;
64         /**
65          * Context value to be selected.
66          */
67         protected transient List<SimulationContext> _newvalue;
68         /**
69          * Addable context types.
70          */
71         protected transient List<SimulationContextType> _critext;
72         /**
73          * List of found objects.
74          */
75         protected transient List<Proxy> _result;
76         /**
77          * Injected simulation context service.
78          */
79         private SimulationContextService _simulationContextService;
80
81         /**
82          * Injected user service.
83          */
84         private UserService _userService;
85
86         /**
87          * Search action modes enumeration.
88          */
89         enum UserAction {
90                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
91         }
92
93         // ==============================================================================================================================
94         // Action methods
95         // ==============================================================================================================================
96
97         /**
98          * Perform actions according to the current mode.
99          * 
100          * @return action result or ERROR if failed
101          */
102         public String doSubmitForm() {
103                 // Identification of the user action
104                 UserAction action = UserAction.refreshResult;
105                 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
106                         action = UserAction.selectContextType;
107                 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
108                         action = UserAction.selectContextValue;
109                 } else if (_cindex.length() > 0) {
110                         long index = Long.valueOf(_cindex);
111                         if (index > 0) {
112                                 action = UserAction.removeContext;
113                         } else if (index < 0) {
114                                 action = UserAction.cancelSelect;
115                         }
116                 }
117                 // Execution of the user action
118                 String done;
119                 try {
120                         saveFilter(); // Also reinitializes the form, if needed
121
122                         if (action == UserAction.selectContextType) {
123                                 done = doSelectContextType();
124                         } else if (action == UserAction.selectContextValue) {
125                                 done = doAddContext();
126                         } else if (action == UserAction.removeContext) {
127                                 done = doRemoveContext();
128                         } else if (action == UserAction.cancelSelect) {
129                                 done = doCancel();
130                         } else { // UserAction.refreshResult
131                                 done = doSearch();
132                                 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
133                         }
134                         setCandidates();
135                 } catch (Exception error) {
136                         // No need to roll back the transaction as it is read only
137                         LOG.error("Reason: ", error);
138                         done = ERROR;
139                 }
140                 return done;
141         }
142
143         /**
144          * Add a selected context type to the search filter. Obsolete the current result if necessary.
145          * 
146          * @return "selectype"
147          */
148         @SuppressWarnings(Constants.UNCHECKED)
149         protected String doSelectContextType() {
150                 SimulationContext.Properties sprop = new SimulationContext.Properties();
151
152                 _newtype = getSimulationContextService().selectType(
153                                 Integer.valueOf(_ctype));
154                 _newvalue = getSimulationContextService()
155                                 .selectSimulationContextsWhere(sprop.setType(_newtype));
156                 if (_cindex.length() > 0 && Long.valueOf(_cindex) == 0) {
157                         getSession().remove(RESULT_KEY);
158                 } else {
159                         // We keep the previous result search, if valid
160                         _result = (List<Proxy>) getSession().get(RESULT_KEY);
161                 }
162                 setActionType("setContext");
163                 return "selectype";
164         }
165
166         /**
167          * Add a selected context to the search filter. Obsolete the current result.
168          * 
169          * @return "refresh"
170          */
171         protected String doAddContext() {
172                 SimulationContext selected = getSimulationContextService()
173                                 .selectSimulationContext(Integer.valueOf(_cvalue));
174
175                 getFilter().getSimContexts().add(selected);
176                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
177                 getSession().remove(RESULT_KEY); // The current result is obsolete
178                 return "refresh";
179         }
180
181         /**
182          * Remove context from the search filter.
183          * 
184          * @return "refresh"
185          */
186         protected String doRemoveContext() {
187                 long index = Long.valueOf(_cindex);
188                 for (Iterator<SimulationContext> selected = getFilter()
189                                 .getSimContexts().iterator(); selected.hasNext();) {
190                         if (selected.next().getIndex() == index) {
191                                 selected.remove();
192                                 break;
193                         }
194                 }
195                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
196                 getSession().remove(RESULT_KEY); // The current result is obsolete
197                 return "refresh";
198         }
199
200         /**
201          * Cancel simulation context selection.
202          * 
203          * @return "refresh"
204          */
205         @SuppressWarnings(Constants.UNCHECKED)
206         protected String doCancel() {
207                 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
208                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
209                 return "refresh";
210         }
211
212         // ==============================================================================================================================
213         // Getters
214         // ==============================================================================================================================
215
216         /**
217          * Get date format string.
218          * 
219          * @return date format string
220          */
221         public String getFormat() {
222                 return getText("date.format");
223         }
224
225         /**
226          * Get formatted today date as a string.
227          * 
228          * @return current date as a string
229          */
230         public String getToday() {
231                 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
232                                 getApplicationSettings().getCurrentLocale());
233                 return tostring.format(java.util.Calendar.getInstance().getTime());
234         }
235
236         /**
237          * Get search result state.
238          * 
239          * @return "obsolete" if there is no results in the session, otherwise "uptodate"
240          */
241         public String getResultState() {
242                 String result;
243                 if (getSession().get(RESULT_KEY) == null) {
244                         result = "obsolete";
245                 } else {
246                         result = "uptodate";
247                 }
248                 return result;
249         }
250
251         public List<Name> getCandidates() {
252                 return _candidates;
253         }
254
255         public List<SimulationContextType> getContextTypeOptions() {
256                 return _critext;
257         }
258
259         public List<SimulationContext> getContextValueOptions() {
260                 return _newvalue;
261         }
262
263         public SimulationContextType getSelectedContextType() {
264                 return _newtype;
265         }
266
267         /**
268          * Get list of found objects.
269          * 
270          * @return list of found objects
271          */
272         public List<Proxy> getResult() {
273                 return _result;
274         }
275
276         // ==============================================================================================================================
277         // Setters
278         // ==============================================================================================================================
279
280         /**
281          * Set applied simulation context type id to search.
282          * 
283          * @param type
284          *            persistent simulation context type id
285          */
286         public void setContextType(final String type) {
287                 this._ctype = type;
288         }
289
290         /**
291          * Set value of simulation context to search.
292          * 
293          * @param value
294          *            the simulation context value
295          */
296         public void setContextValue(final String value) {
297                 this._cvalue = value;
298         }
299
300         /**
301          * Set simulation context value id.
302          * 
303          * @param value
304          *            the persistent id as string
305          */
306         public void setContextIndex(final String value) {
307                 this._cindex = value;
308         }
309
310         /**
311          * Build the list of study authors. If the current user also can create <BR>
312          * a study then it is placed on the top of the list.
313          */
314         protected void setCandidates() {
315                 _candidates = new ArrayList<Name>();
316                 List<User> users = getUserService().selectAllUsers();
317                 User me = getConnectedUser(); // May be null
318                 for (Iterator<User> i = users.iterator(); i.hasNext();) {
319                         User next = i.next();
320                         ApplicationRights he = new ApplicationRights(next);
321                         if (he.canCreateStudy()) {
322                                 if (next.equals(me)) {
323                                         _candidates.add(0, new ValidationFacade.ByManager(me,
324                                                         getApplicationSettings().getCurrentLocale()));
325                                 } else {
326                                         _candidates.add(next);
327                                 }
328                         }
329                 }
330         }
331
332         /**
333          * Build available context types list with localized names.
334          * 
335          * @param critext
336          *            context types already used in the search filter
337          */
338         protected void setContextTypeOptions(
339                         final List<SimulationContextType> critext) {
340                 for (SimulationContext ctx : getFilter().getSimContexts()) {
341                         critext.remove(ctx.getType()); // Already used context type
342                 }
343                 // Ordering by alphabetical order of localized context types
344                 SimulationContextType[] types = critext
345                                 .toArray(new SimulationContextType[critext.size()]);
346                 ContextTypeComparator compare = new ContextTypeComparator();
347                 ProjectSettingsService.Step step = getSimulationContextService()
348                                 .getAttachedStep(types[0]);
349                 int from = 0;
350                 int to = 0;
351                 while (to < types.length - 1) {
352                         to += 1;
353                         if (!types[to].isAttachedTo(step)) {
354                                 if (to > from + 1) {
355                                         Arrays.sort(types, from, to, compare);
356                                 }
357                                 from = to;
358                                 step = getSimulationContextService().getAttachedStep(types[to]);
359                         }
360                 }
361                 if (to > from) {
362                         Arrays.sort(types, from, to + 1, compare);
363                 }
364                 this._critext = Arrays.asList(types);
365         }
366
367         /**
368          * Load the search filter. The filter is taken from the session by its name.
369          * 
370          * @see #getFilterName()
371          * @return the loaded filter
372          */
373         @SuppressWarnings(Constants.UNCHECKED)
374         protected Map<String, Object> loadFilter() {
375                 Map<String, Object> filter = (Map<String, Object>) getSession().get(
376                                 getFilterName()); // A default filter is supposed being set at start
377
378                 setFilter((FilterClass) filter.get("criteria"));
379                 return filter;
380         }
381
382         /**
383          * Save search criteria in the session as a map with the defined name.
384          * 
385          * @see #getFilterName()
386          * @return the saved filter
387          */
388         @SuppressWarnings(Constants.UNCHECKED)
389         protected Map<String, Object> saveFilter() {
390                 Map<String, Object> filter = (Map<String, Object>) getSession().get(
391                                 getFilterName()); // A default filter is supposed being set at start
392
393                 FilterClass savedCriteria = (FilterClass) filter.get("criteria");
394                 FilterClass newCriteria = getFilter();
395                 if (savedCriteria != null) {
396                         // Only contexts are not part of the form so keep them between requests
397                         newCriteria.setSimContexts(savedCriteria.getSimContexts());
398                 }
399
400                 filter.put("criteria", getFilter());
401                 return filter;
402         }
403
404         // ==============================================================================================================================
405         // Abstract services
406         // ==============================================================================================================================
407
408         /**
409          * Search objects according to the given criteria.
410          * 
411          * @return string result key
412          * @throws InvalidPropertyException
413          *             if some criteria values are incorrect
414          */
415         protected abstract String doSearch() throws InvalidPropertyException;
416
417         /**
418          * List of context types available for filtering.
419          * 
420          * @return list of simulation context types
421          */
422         protected abstract List<SimulationContextType> getInvolvedContexts();
423
424         /**
425          * Get the name of the search criteria filter saved as a map of objects in the session.
426          * 
427          * @return the search filter name in the session
428          */
429         protected abstract String getFilterName();
430
431         /**
432          * Get search filter implementation.
433          * 
434          * @return the search filter
435          */
436         protected abstract FilterClass getNewFilter();
437
438         // ==============================================================================================================================
439         // Getters and Setters
440         // ==============================================================================================================================
441
442         /**
443          * Get match options.
444          * 
445          * @return array of options with key and value properties
446          */
447         public Map<String, String> getMatchOptions() {
448                 Map<String, String> options = new LinkedHashMap<String, String>();
449                 options.put("all", getText("field.matchall"));
450                 options.put("any", getText("field.matchany"));
451                 return options;
452         }
453
454         /**
455          * Get the simulationContextService.
456          * 
457          * @return the simulationContextService
458          */
459         public SimulationContextService getSimulationContextService() {
460                 return _simulationContextService;
461         }
462
463         /**
464          * Set the simulationContextService.
465          * 
466          * @param simulationContextService
467          *            the simulationContextService to set
468          */
469         public void setSimulationContextService(
470                         final SimulationContextService simulationContextService) {
471                 _simulationContextService = simulationContextService;
472         }
473
474         /**
475          * Get the userService.
476          * 
477          * @return the userService
478          */
479         public UserService getUserService() {
480                 return _userService;
481         }
482
483         /**
484          * Set the userService.
485          * 
486          * @param userService
487          *            the userService to set
488          */
489         public void setUserService(final UserService userService) {
490                 _userService = userService;
491         }
492
493         /**
494          * Get the filter.
495          * 
496          * @return the filter
497          */
498         public FilterClass getFilter() {
499                 if (_filter == null) {
500                         _filter = getNewFilter();
501                 }
502                 return _filter;
503         }
504
505         /**
506          * Set the filter.
507          * 
508          * @param filter
509          *            the filter to set
510          */
511         public void setFilter(final FilterClass filter) {
512                 if (filter == null) {
513                         _filter = getNewFilter();
514                 } else {
515                         _filter = filter;
516                 }
517         }
518 }