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 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
29 public abstract class AbstractSearchBaseAction<FilterClass extends SearchFilterDTO>
35 private static final long serialVersionUID = 7863055790228544510L;
37 * Search result key in the session.
39 public static final String RESULT_KEY = "search.result";
41 * Context type index, when selected.
43 protected transient String _ctype = null;
45 * Context value index, when selected.
47 protected transient String _cvalue = null;
49 * Context index, when removed.
51 protected transient String _cindex = "";
55 private FilterClass _filter;
57 * List of users who can create studies.
59 protected List<Name> _candidates = null;
61 * Context type to be valued.
63 protected transient SimulationContextType _newtype;
65 * Context value to be selected.
67 protected transient List<SimulationContext> _newvalue;
69 * Addable context types.
71 protected transient List<SimulationContextType> _critext;
73 * List of found objects.
75 protected transient List<Proxy> _result;
77 * Injected simulation context service.
79 private SimulationContextService _simulationContextService;
82 * Injected user service.
84 private UserService _userService;
87 * Search action modes enumeration.
90 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
93 // ==============================================================================================================================
95 // ==============================================================================================================================
98 * Perform actions according to the current mode.
100 * @return action result or ERROR if failed
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);
112 action = UserAction.removeContext;
113 } else if (index < 0) {
114 action = UserAction.cancelSelect;
117 // Execution of the user action
120 saveFilter(); // Also reinitializes the form, if needed
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) {
130 } else { // UserAction.refreshResult
132 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
135 } catch (Exception error) {
136 // No need to roll back the transaction as it is read only
137 LOG.error("Reason: ", error);
144 * Add a selected context type to the search filter. Obsolete the current result if necessary.
146 * @return "selectype"
148 @SuppressWarnings(Constants.UNCHECKED)
149 protected String doSelectContextType() {
150 SimulationContext.Properties sprop = new SimulationContext.Properties();
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);
159 // We keep the previous result search, if valid
160 _result = (List<Proxy>) getSession().get(RESULT_KEY);
162 setActionType("setContext");
167 * Add a selected context to the search filter. Obsolete the current result.
171 protected String doAddContext() {
172 SimulationContext selected = getSimulationContextService()
173 .selectSimulationContext(Integer.valueOf(_cvalue));
175 getFilter().getSimContexts().add(selected);
176 setContextTypeOptions(getInvolvedContexts()); // Sets critext
177 getSession().remove(RESULT_KEY); // The current result is obsolete
182 * Remove context from the search filter.
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) {
195 setContextTypeOptions(getInvolvedContexts()); // Sets critext
196 getSession().remove(RESULT_KEY); // The current result is obsolete
201 * Cancel simulation context selection.
205 @SuppressWarnings(Constants.UNCHECKED)
206 protected String doCancel() {
207 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
208 setContextTypeOptions(getInvolvedContexts()); // Sets critext
212 // ==============================================================================================================================
214 // ==============================================================================================================================
217 * Get date format string.
219 * @return date format string
221 public String getFormat() {
222 return getText("date.format");
226 * Get formatted today date as a string.
228 * @return current date as a string
230 public String getToday() {
231 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
232 getApplicationSettings().getCurrentLocale());
233 return tostring.format(java.util.Calendar.getInstance().getTime());
237 * Get search result state.
239 * @return "obsolete" if there is no results in the session, otherwise "uptodate"
241 public String getResultState() {
243 if (getSession().get(RESULT_KEY) == null) {
251 public List<Name> getCandidates() {
255 public List<SimulationContextType> getContextTypeOptions() {
259 public List<SimulationContext> getContextValueOptions() {
263 public SimulationContextType getSelectedContextType() {
268 * Get list of found objects.
270 * @return list of found objects
272 public List<Proxy> getResult() {
276 // ==============================================================================================================================
278 // ==============================================================================================================================
281 * Set applied simulation context type id to search.
284 * persistent simulation context type id
286 public void setContextType(final String type) {
291 * Set value of simulation context to search.
294 * the simulation context value
296 public void setContextValue(final String value) {
297 this._cvalue = value;
301 * Set simulation context value id.
304 * the persistent id as string
306 public void setContextIndex(final String value) {
307 this._cindex = value;
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.
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()));
326 _candidates.add(next);
333 * Build available context types list with localized names.
336 * context types already used in the search filter
338 protected void setContextTypeOptions(
339 final List<SimulationContextType> critext) {
340 for (SimulationContext ctx : getFilter().getSimContexts()) {
341 critext.remove(ctx.getType()); // Already used context type
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]);
351 while (to < types.length - 1) {
353 if (!types[to].isAttachedTo(step)) {
355 Arrays.sort(types, from, to, compare);
358 step = getSimulationContextService().getAttachedStep(types[to]);
362 Arrays.sort(types, from, to + 1, compare);
364 this._critext = Arrays.asList(types);
368 * Load the search filter. The filter is taken from the session by its name.
370 * @see #getFilterName()
371 * @return the loaded filter
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
378 setFilter((FilterClass) filter.get("criteria"));
383 * Save search criteria in the session as a map with the defined name.
385 * @see #getFilterName()
386 * @return the saved filter
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
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());
400 filter.put("criteria", getFilter());
404 // ==============================================================================================================================
406 // ==============================================================================================================================
409 * Search objects according to the given criteria.
411 * @return string result key
412 * @throws InvalidPropertyException
413 * if some criteria values are incorrect
415 protected abstract String doSearch() throws InvalidPropertyException;
418 * List of context types available for filtering.
420 * @return list of simulation context types
422 protected abstract List<SimulationContextType> getInvolvedContexts();
425 * Get the name of the search criteria filter saved as a map of objects in the session.
427 * @return the search filter name in the session
429 protected abstract String getFilterName();
432 * Get search filter implementation.
434 * @return the search filter
436 protected abstract FilterClass getNewFilter();
438 // ==============================================================================================================================
439 // Getters and Setters
440 // ==============================================================================================================================
445 * @return array of options with key and value properties
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"));
455 * Get the simulationContextService.
457 * @return the simulationContextService
459 public SimulationContextService getSimulationContextService() {
460 return _simulationContextService;
464 * Set the simulationContextService.
466 * @param simulationContextService
467 * the simulationContextService to set
469 public void setSimulationContextService(
470 final SimulationContextService simulationContextService) {
471 _simulationContextService = simulationContextService;
475 * Get the userService.
477 * @return the userService
479 public UserService getUserService() {
484 * Set the userService.
487 * the userService to set
489 public void setUserService(final UserService userService) {
490 _userService = userService;
498 public FilterClass getFilter() {
499 if (_filter == null) {
500 _filter = getNewFilter();
511 public void setFilter(final FilterClass filter) {
512 if (filter == null) {
513 _filter = getNewFilter();