1 package org.splat.simer;
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.Iterator;
9 import java.util.LinkedHashMap;
10 import java.util.List;
13 import org.splat.dal.bo.kernel.User;
14 import org.splat.dal.bo.som.SimulationContext;
15 import org.splat.dal.bo.som.SimulationContextType;
16 import org.splat.kernel.InvalidPropertyException;
17 import org.splat.kernel.Name;
18 import org.splat.service.SimulationContextService;
19 import org.splat.service.UserService;
20 import org.splat.service.dto.Proxy;
21 import org.splat.service.dto.SearchFilterDTO;
22 import org.splat.service.dto.StudyDTO;
23 import org.splat.service.technical.ProjectSettingsService;
24 import org.splat.som.ApplicationRights;
25 import org.splat.wapp.Constants;
28 * Base search action class used for searching studies and knowledge.
30 * @param <FilterClass>
32 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
34 public abstract class AbstractSearchBaseAction<FilterClass extends SearchFilterDTO>
40 private static final long serialVersionUID = 7863055790228544510L;
42 * Search result key in the session.
44 public static final String RESULT_KEY = "search.result";
46 * Context type index, when selected.
48 protected transient String _ctype = null;
50 * Context value index, when selected.
52 protected transient String _cvalue = null;
54 * Context index, when removed.
56 protected transient String _cindex = "";
60 private FilterClass _filter;
62 * List of users who can create studies.
64 protected List<Name> _candidates = null;
66 * Context type to be valued.
68 protected transient SimulationContextType _newtype;
70 * Context value to be selected.
72 protected transient List<SimulationContext> _newvalue;
74 * Addable context types.
76 protected transient List<SimulationContextType> _critext;
78 * List of found objects.
80 protected transient List<Proxy> _result;
82 * Injected simulation context service.
84 private SimulationContextService _simulationContextService;
87 * Injected user service.
89 private UserService _userService;
92 * Search action modes enumeration.
95 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
99 * A criteria to sort studies by.
101 private SortCriterion _newSortedBy;
104 * Sort order key in the session.
106 protected static final String ORDER_KEY = "isDescendingOrder";
109 * Sort criterion key in the session.
111 protected static final String CRITERION_KEY = "sortCriterion";
113 // ==============================================================================================================================
115 // ==============================================================================================================================
118 * StudyDTO sort criteria.
138 * The person responsible.
144 * StudyDTO comparator class.
146 private class StudyComparator implements Comparator<StudyDTO> {
149 * The criteria by which studies are compared.
151 SortCriterion _criterion = SortCriterion.NAME;
154 * Constructor from comparison criteria.
158 public StudyComparator(final SortCriterion criterion) {
159 _criterion = criterion;
164 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
166 public int compare(final StudyDTO first, final StudyDTO second) {
167 switch (_criterion) {
169 return first.getReference().compareTo(second.getReference());
171 return first.getDate().compareTo(second.getDate());
173 return first.getLastModificationDate().compareTo(
174 second.getLastModificationDate());
176 return getText(first.getAuthorName())
177 .compareTo(getText(second.getAuthorName()));
179 return first.getTitle().compareTo(second.getTitle());
185 * Set search results sort order.
187 * SUCCESS if successfully found and sorted search results;
190 public String doSetOrder() {
192 _result = (List<Proxy>) getSession().get(RESULT_KEY);
193 Boolean order = (Boolean) getSession().get(ORDER_KEY);
194 SortCriterion oldSortedBy = (SortCriterion) getSession().get(CRITERION_KEY);
196 if (_result != null) {
198 if (_newSortedBy != null && !_newSortedBy.equals(oldSortedBy)) { // Sort by new criterion
199 // Direct cast into collection of another type just won't work in Java
200 Collections.sort((List<StudyDTO>)(List<?>) _result, new StudyComparator(_newSortedBy));
201 getSession().put(CRITERION_KEY, _newSortedBy);
202 getSession().put(ORDER_KEY, false);
206 } else { // need to change the order
209 getSession().put(ORDER_KEY, order);
210 Collections.reverse(_result);
219 * Perform actions according to the current mode.
221 * @return action result or ERROR if failed
223 public String doSubmitForm() {
224 // Identification of the user action
225 UserAction action = UserAction.refreshResult;
226 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
227 action = UserAction.selectContextType;
228 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
229 action = UserAction.selectContextValue;
230 } else if (_cindex.length() > 0) {
231 long index = Long.valueOf(_cindex);
233 action = UserAction.removeContext;
234 } else if (index < 0) {
235 action = UserAction.cancelSelect;
238 // Execution of the user action
241 saveFilter(); // Also reinitializes the form, if needed
243 if (action == UserAction.selectContextType) {
244 done = doSelectContextType();
245 } else if (action == UserAction.selectContextValue) {
246 done = doAddContext();
247 } else if (action == UserAction.removeContext) {
248 done = doRemoveContext();
249 } else if (action == UserAction.cancelSelect) {
251 } else { // UserAction.refreshResult
253 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
256 } catch (Exception error) {
257 // No need to roll back the transaction as it is read only
258 LOG.error("Reason: ", error);
265 * Add a selected context type to the search filter. Obsolete the current result if necessary.
267 * @return "selectype"
269 @SuppressWarnings(Constants.UNCHECKED)
270 protected String doSelectContextType() {
271 SimulationContext.Properties sprop = new SimulationContext.Properties();
273 _newtype = getSimulationContextService().selectType(
274 Integer.valueOf(_ctype));
275 _newvalue = getSimulationContextService()
276 .selectSimulationContextsWhere(sprop.setType(_newtype));
277 if (_cindex.length() > 0 && Long.valueOf(_cindex) == 0) {
278 getSession().remove(RESULT_KEY);
280 // We keep the previous result search, if valid
281 _result = (List<Proxy>) getSession().get(RESULT_KEY);
283 setActionType("setContext");
288 * Add a selected context to the search filter. Obsolete the current result.
292 protected String doAddContext() {
293 SimulationContext selected = getSimulationContextService()
294 .selectSimulationContext(Integer.valueOf(_cvalue));
296 getFilter().getSimContexts().add(selected);
297 setContextTypeOptions(getInvolvedContexts()); // Sets critext
298 getSession().remove(RESULT_KEY); // The current result is obsolete
303 * Remove context from the search filter.
307 protected String doRemoveContext() {
308 long index = Long.valueOf(_cindex);
309 for (Iterator<SimulationContext> selected = getFilter()
310 .getSimContexts().iterator(); selected.hasNext();) {
311 if (selected.next().getIndex() == index) {
316 setContextTypeOptions(getInvolvedContexts()); // Sets critext
317 getSession().remove(RESULT_KEY); // The current result is obsolete
322 * Cancel simulation context selection.
326 @SuppressWarnings(Constants.UNCHECKED)
327 protected String doCancel() {
328 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
329 setContextTypeOptions(getInvolvedContexts()); // Sets critext
333 // ==============================================================================================================================
335 // ==============================================================================================================================
338 * Get date format string.
340 * @return date format string
342 public String getFormat() {
343 return getText("date.format");
347 * Get formatted today date as a string.
349 * @return current date as a string
351 public String getToday() {
352 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
353 getApplicationSettings().getCurrentLocale());
354 return tostring.format(java.util.Calendar.getInstance().getTime());
358 * Get search result state.
360 * @return "obsolete" if there is no results in the session, otherwise "uptodate"
362 public String getResultState() {
364 if (getSession().get(RESULT_KEY) == null) {
372 public List<Name> getCandidates() {
376 public List<SimulationContextType> getContextTypeOptions() {
380 public List<SimulationContext> getContextValueOptions() {
384 public SimulationContextType getSelectedContextType() {
389 * Get list of found objects.
391 * @return list of found objects
393 public List<Proxy> getResult() {
397 // ==============================================================================================================================
399 // ==============================================================================================================================
402 * Set applied simulation context type id to search.
405 * persistent simulation context type id
407 public void setContextType(final String type) {
412 * Set value of simulation context to search.
415 * the simulation context value
417 public void setContextValue(final String value) {
418 this._cvalue = value;
422 * Set simulation context value id.
425 * the persistent id as string
427 public void setContextIndex(final String value) {
428 this._cindex = value;
432 * Build the list of study authors. If the current user also can create <BR>
433 * a study then it is placed on the top of the list.
435 protected void setCandidates() {
436 _candidates = new ArrayList<Name>();
437 List<User> users = getUserService().selectAllUsers();
438 User me = getConnectedUser(); // May be null
439 for (Iterator<User> i = users.iterator(); i.hasNext();) {
440 User next = i.next();
441 ApplicationRights he = new ApplicationRights(next);
442 if (he.canCreateStudy()) {
443 if (next.equals(me)) {
444 _candidates.add(0, new ValidationFacade.ByManager(me,
445 getApplicationSettings().getCurrentLocale()));
447 _candidates.add(next);
454 * Build available context types list with localized names.
457 * context types already used in the search filter
459 protected void setContextTypeOptions(
460 final List<SimulationContextType> critext) {
461 for (SimulationContext ctx : getFilter().getSimContexts()) {
462 critext.remove(ctx.getType()); // Already used context type
464 // Ordering by alphabetical order of localized context types
465 SimulationContextType[] types = critext
466 .toArray(new SimulationContextType[critext.size()]);
467 ContextTypeComparator compare = new ContextTypeComparator();
468 ProjectSettingsService.Step step = getSimulationContextService()
469 .getAttachedStep(types[0]);
472 while (to < types.length - 1) {
474 if (!types[to].isAttachedTo(step)) {
476 Arrays.sort(types, from, to, compare);
479 step = getSimulationContextService().getAttachedStep(types[to]);
483 Arrays.sort(types, from, to + 1, compare);
485 this._critext = Arrays.asList(types);
489 * Load the search filter. The filter is taken from the session by its name.
491 * @see #getFilterName()
492 * @return the loaded filter
494 @SuppressWarnings(Constants.UNCHECKED)
495 protected Map<String, Object> loadFilter() {
496 Map<String, Object> filter = (Map<String, Object>) getSession().get(
497 getFilterName()); // A default filter is supposed being set at start
499 setFilter((FilterClass) filter.get("criteria"));
504 * Save search criteria in the session as a map with the defined name.
506 * @see #getFilterName()
507 * @return the saved filter
509 @SuppressWarnings(Constants.UNCHECKED)
510 protected Map<String, Object> saveFilter() {
511 Map<String, Object> filter = (Map<String, Object>) getSession().get(
512 getFilterName()); // A default filter is supposed being set at start
514 FilterClass savedCriteria = (FilterClass) filter.get("criteria");
515 FilterClass newCriteria = getFilter();
516 if (savedCriteria != null) {
517 // Only contexts are not part of the form so keep them between requests
518 newCriteria.setSimContexts(savedCriteria.getSimContexts());
521 filter.put("criteria", getFilter());
525 // ==============================================================================================================================
527 // ==============================================================================================================================
530 * Search objects according to the given criteria.
532 * @return string result key
533 * @throws InvalidPropertyException
534 * if some criteria values are incorrect
536 protected abstract String doSearch() throws InvalidPropertyException;
539 * List of context types available for filtering.
541 * @return list of simulation context types
543 protected abstract List<SimulationContextType> getInvolvedContexts();
546 * Get the name of the search criteria filter saved as a map of objects in the session.
548 * @return the search filter name in the session
550 protected abstract String getFilterName();
553 * Get search filter implementation.
555 * @return the search filter
557 protected abstract FilterClass getNewFilter();
559 // ==============================================================================================================================
560 // Getters and Setters
561 // ==============================================================================================================================
566 * @return array of options with key and value properties
568 public Map<String, String> getMatchOptions() {
569 Map<String, String> options = new LinkedHashMap<String, String>();
570 options.put("all", getText("field.matchall"));
571 options.put("any", getText("field.matchany"));
576 * Get the simulationContextService.
578 * @return the simulationContextService
580 public SimulationContextService getSimulationContextService() {
581 return _simulationContextService;
585 * Set the simulationContextService.
587 * @param simulationContextService
588 * the simulationContextService to set
590 public void setSimulationContextService(
591 final SimulationContextService simulationContextService) {
592 _simulationContextService = simulationContextService;
596 * Get the userService.
598 * @return the userService
600 public UserService getUserService() {
605 * Set the userService.
608 * the userService to set
610 public void setUserService(final UserService userService) {
611 _userService = userService;
619 public FilterClass getFilter() {
620 if (_filter == null) {
621 _filter = getNewFilter();
632 public void setFilter(final FilterClass filter) {
633 if (filter == null) {
634 _filter = getNewFilter();
641 * Get the newSortedBy.
642 * @return the newSortedBy
644 public SortCriterion getNewSortedBy() {
649 * Set the newSortedBy.
650 * @param newSortedBy the newSortedBy to set
652 public void setNewSortedBy(final SortCriterion newSortedBy) {
653 _newSortedBy = newSortedBy;