1 package org.splat.simer;
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;
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;
25 * Base search action class used for searching studies and knowledge.
27 * @param <FilterClass>
29 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
31 public abstract class AbstractSearchBaseAction<FilterClass extends SearchFilterDTO>
37 private static final long serialVersionUID = 7863055790228544510L;
39 * Search result key in the session.
41 public static final String RESULT_KEY = "search.result";
43 * Context type index, when selected.
45 protected transient String _ctype = null;
47 * Context value index, when selected.
49 protected transient String _cvalue = null;
51 * Context index, when removed.
53 protected transient String _cindex = "";
57 private FilterClass _filter;
59 * List of users who can create studies.
61 protected List<Name> _candidates = null;
63 * Context type to be valued.
65 protected transient SimulationContextType _newtype;
67 * Context value to be selected.
69 protected transient List<SimulationContext> _newvalue;
71 * Addable context types.
73 protected transient List<SimulationContextType> _critext;
75 * List of found objects.
77 protected transient List<Proxy> _result;
79 * Injected simulation context service.
81 private SimulationContextService _simulationContextService;
84 * Injected user service.
86 private UserService _userService;
89 * Search action modes enumeration.
92 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
95 // ==============================================================================================================================
97 // ==============================================================================================================================
100 * Perform actions according to the current mode.
102 * @return action result or ERROR if failed
104 public String doSubmitForm() {
105 // Identification of the user action
106 UserAction action = UserAction.refreshResult;
107 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
108 action = UserAction.selectContextType;
109 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
110 action = UserAction.selectContextValue;
111 } else if (_cindex.length() > 0) {
112 long index = Long.valueOf(_cindex);
114 action = UserAction.removeContext;
115 } else if (index < 0) {
116 action = UserAction.cancelSelect;
119 // Execution of the user action
122 saveFilter(); // Also reinitializes the form, if needed
124 if (action == UserAction.selectContextType) {
125 done = doSelectContextType();
126 } else if (action == UserAction.selectContextValue) {
127 done = doAddContext();
128 } else if (action == UserAction.removeContext) {
129 done = doRemoveContext();
130 } else if (action == UserAction.cancelSelect) {
132 } else { // UserAction.refreshResult
134 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
137 } catch (Exception error) {
138 // No need to roll back the transaction as it is read only
139 LOG.error("Reason: ", error);
146 * Add a selected context type to the search filter. Obsolete the current result if necessary.
148 * @return "selectype"
150 @SuppressWarnings(Constants.UNCHECKED)
151 protected String doSelectContextType() {
152 SimulationContext.Properties sprop = new SimulationContext.Properties();
154 _newtype = getSimulationContextService().selectType(
155 Integer.valueOf(_ctype));
156 _newvalue = getSimulationContextService()
157 .selectSimulationContextsWhere(sprop.setType(_newtype));
158 if (_cindex.length() > 0 && Long.valueOf(_cindex) == 0) {
159 getSession().remove(RESULT_KEY);
161 // We keep the previous result search, if valid
162 _result = (List<Proxy>) getSession().get(RESULT_KEY);
164 setActionType("setContext");
169 * Add a selected context to the search filter. Obsolete the current result.
173 protected String doAddContext() {
174 SimulationContext selected = getSimulationContextService()
175 .selectSimulationContext(Integer.valueOf(_cvalue));
177 getFilter().getSimContexts().add(selected);
178 setContextTypeOptions(getInvolvedContexts()); // Sets critext
179 getSession().remove(RESULT_KEY); // The current result is obsolete
184 * Remove context from the search filter.
188 protected String doRemoveContext() {
189 long index = Long.valueOf(_cindex);
190 for (Iterator<SimulationContext> selected = getFilter()
191 .getSimContexts().iterator(); selected.hasNext();) {
192 if (selected.next().getIndex() == index) {
197 setContextTypeOptions(getInvolvedContexts()); // Sets critext
198 getSession().remove(RESULT_KEY); // The current result is obsolete
203 * Cancel simulation context selection.
207 @SuppressWarnings(Constants.UNCHECKED)
208 protected String doCancel() {
209 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
210 setContextTypeOptions(getInvolvedContexts()); // Sets critext
214 // ==============================================================================================================================
216 // ==============================================================================================================================
219 * Get date format string.
221 * @return date format string
223 public String getFormat() {
224 return getText("date.format");
228 * Get formatted today date as a string.
230 * @return current date as a string
232 public String getToday() {
233 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
234 getApplicationSettings().getCurrentLocale());
235 return tostring.format(java.util.Calendar.getInstance().getTime());
239 * Get search result state.
241 * @return "obsolete" if there is no results in the session, otherwise "uptodate"
243 public String getResultState() {
245 if (getSession().get(RESULT_KEY) == null) {
253 public List<Name> getCandidates() {
257 public List<SimulationContextType> getContextTypeOptions() {
261 public List<SimulationContext> getContextValueOptions() {
265 public SimulationContextType getSelectedContextType() {
270 * Get list of found objects.
272 * @return list of found objects
274 public List<Proxy> getResult() {
278 // ==============================================================================================================================
280 // ==============================================================================================================================
283 * Set applied simulation context type id to search.
286 * persistent simulation context type id
288 public void setContextType(final String type) {
293 * Set value of simulation context to search.
296 * the simulation context value
298 public void setContextValue(final String value) {
299 this._cvalue = value;
303 * Set simulation context value id.
306 * the persistent id as string
308 public void setContextIndex(final String value) {
309 this._cindex = value;
313 * Build the list of study authors. If the current user also can create <BR>
314 * a study then it is placed on the top of the list.
316 protected void setCandidates() {
317 _candidates = new ArrayList<Name>();
318 List<User> users = getUserService().selectAllUsers();
319 User me = getConnectedUser(); // May be null
320 for (Iterator<User> i = users.iterator(); i.hasNext();) {
321 User next = i.next();
322 ApplicationRights he = new ApplicationRights(next);
323 if (he.canCreateStudy()) {
324 if (next.equals(me)) {
325 _candidates.add(0, new ValidationFacade.ByManager(me,
326 getApplicationSettings().getCurrentLocale()));
328 _candidates.add(next);
335 * Build available context types list with localized names.
338 * context types already used in the search filter
340 protected void setContextTypeOptions(
341 final List<SimulationContextType> critext) {
342 for (SimulationContext ctx : getFilter().getSimContexts()) {
343 critext.remove(ctx.getType()); // Already used context type
345 // Ordering by alphabetical order of localized context types
346 SimulationContextType[] types = critext
347 .toArray(new SimulationContextType[critext.size()]);
348 ContextTypeComparator compare = new ContextTypeComparator();
349 ProjectSettingsService.Step step = getSimulationContextService()
350 .getAttachedStep(types[0]);
353 while (to < types.length - 1) {
355 if (!types[to].isAttachedTo(step)) {
357 Arrays.sort(types, from, to, compare);
360 step = getSimulationContextService().getAttachedStep(types[to]);
364 Arrays.sort(types, from, to + 1, compare);
366 this._critext = Arrays.asList(types);
370 * Load the search filter. The filter is taken from the session by its name.
372 * @see #getFilterName()
373 * @return the loaded filter
375 @SuppressWarnings(Constants.UNCHECKED)
376 protected Map<String, Object> loadFilter() {
377 Map<String, Object> filter = (Map<String, Object>) getSession().get(
378 getFilterName()); // A default filter is supposed being set at start
380 setFilter((FilterClass) filter.get("criteria"));
385 * Save search criteria in the session as a map with the defined name.
387 * @see #getFilterName()
388 * @return the saved filter
390 @SuppressWarnings(Constants.UNCHECKED)
391 protected Map<String, Object> saveFilter() {
392 Map<String, Object> filter = (Map<String, Object>) getSession().get(
393 getFilterName()); // A default filter is supposed being set at start
395 FilterClass savedCriteria = (FilterClass) filter.get("criteria");
396 FilterClass newCriteria = getFilter();
397 if (savedCriteria != null) {
398 // Only contexts are not part of the form so keep them between requests
399 newCriteria.setSimContexts(savedCriteria.getSimContexts());
402 filter.put("criteria", getFilter());
406 // ==============================================================================================================================
408 // ==============================================================================================================================
411 * Search objects according to the given criteria.
413 * @return string result key
414 * @throws InvalidPropertyException
415 * if some criteria values are incorrect
417 protected abstract String doSearch() throws InvalidPropertyException;
420 * List of context types available for filtering.
422 * @return list of simulation context types
424 protected abstract List<SimulationContextType> getInvolvedContexts();
427 * Get the name of the search criteria filter saved as a map of objects in the session.
429 * @return the search filter name in the session
431 protected abstract String getFilterName();
434 * Get search filter implementation.
436 * @return the search filter
438 protected abstract FilterClass getNewFilter();
440 // ==============================================================================================================================
441 // Getters and Setters
442 // ==============================================================================================================================
447 * @return array of options with key and value properties
449 public Map<String, String> getMatchOptions() {
450 Map<String, String> options = new LinkedHashMap<String, String>();
451 options.put("all", getText("field.matchall"));
452 options.put("any", getText("field.matchany"));
457 * Get the simulationContextService.
459 * @return the simulationContextService
461 public SimulationContextService getSimulationContextService() {
462 return _simulationContextService;
466 * Set the simulationContextService.
468 * @param simulationContextService
469 * the simulationContextService to set
471 public void setSimulationContextService(
472 final SimulationContextService simulationContextService) {
473 _simulationContextService = simulationContextService;
477 * Get the userService.
479 * @return the userService
481 public UserService getUserService() {
486 * Set the userService.
489 * the userService to set
491 public void setUserService(final UserService userService) {
492 _userService = userService;
500 public FilterClass getFilter() {
501 if (_filter == null) {
502 _filter = getNewFilter();
513 public void setFilter(final FilterClass filter) {
514 if (filter == null) {
515 _filter = getNewFilter();