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.technical.ProjectSettingsService;
20 import org.splat.som.ApplicationRights;
23 * Base search action class used for searching studies and knowledge.
25 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
27 public abstract class AbstractSearchBaseAction extends Action {
32 private static final long serialVersionUID = 7863055790228544510L;
34 * Search result key in the session.
36 public static final String RESULT_KEY = "search.result";
38 * Context type index, when selected.
40 protected transient String _ctype = null;
42 * Context value index, when selected.
44 protected transient String _cvalue = null;
46 * Context index, when removed.
48 protected transient String _cindex = "";
50 * Study author to search.
52 protected String _author = null;
54 * List of users who can create studies.
56 protected List<Name> _candidates = null;
58 * Context type to be valued.
60 protected transient SimulationContextType _newtype;
62 * Context value to be selected.
64 protected transient List<SimulationContext> _newvalue;
66 * Addable context types.
68 protected transient List<SimulationContextType> _critext;
70 * Current contexts search criteria.
72 protected transient List<SimulationContext> _context;
74 * List of found objects.
76 protected transient List<Proxy> _result;
78 * Injected simulation context service.
80 private SimulationContextService _simulationContextService;
83 * Injected user service.
85 private UserService _userService;
87 * Simulation context match: "all" or "any".
89 private String _contextMatch = null;
91 * Criteria match: "all" or "any".
93 private String _criteriaMatch = null;
95 * Full text search words.
97 private String _words = null;
100 * Search action modes enumeration.
103 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
106 // ==============================================================================================================================
108 // ==============================================================================================================================
111 * Perform actions according to the current mode.
113 * @return action result or ERROR if failed
115 public String doSubmitForm() {
116 // Identification of the user action
117 UserAction action = UserAction.refreshResult;
118 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
119 action = UserAction.selectContextType;
120 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
121 action = UserAction.selectContextValue;
122 } else if (_cindex.length() > 0) {
123 long index = Long.valueOf(_cindex);
125 action = UserAction.removeContext;
126 } else if (index < 0) {
127 action = UserAction.cancelSelect;
130 // Execution of the user action
133 saveFilter(); // Also reinitializes the form, if needed
135 if (action == UserAction.selectContextType) {
136 done = doSelectContextType();
137 } else if (action == UserAction.selectContextValue) {
138 done = doAddContext();
139 } else if (action == UserAction.removeContext) {
140 done = doRemoveContext();
141 } else if (action == UserAction.cancelSelect) {
143 } else { // UserAction.refreshResult
145 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
148 } catch (Exception error) {
149 // No need to roll back the transaction as it is read only
150 LOG.error("Reason: ", error);
157 * Add a selected context type to the search filter. Obsolete the current result if necessary.
159 * @return "selectype"
161 @SuppressWarnings("unchecked")
162 protected String doSelectContextType() {
163 SimulationContext.Properties sprop = new SimulationContext.Properties();
165 _newtype = getSimulationContextService().selectType(
166 Integer.valueOf(_ctype));
167 _newvalue = getSimulationContextService()
168 .selectSimulationContextsWhere(sprop.setType(_newtype));
169 if (_cindex.length() > 0 && Long.valueOf(_cindex) == 0) {
170 getSession().remove(RESULT_KEY);
172 // We keep the previous result search, if valid
173 _result = (List<Proxy>) getSession().get(RESULT_KEY);
175 setActionType("setContext");
180 * Add a selected context to the search filter. Obsolete the current result.
184 protected String doAddContext() {
185 SimulationContext selected = getSimulationContextService()
186 .selectSimulationContext(Integer.valueOf(_cvalue));
188 _context.add(selected);
189 setContextTypeOptions(getInvolvedContexts()); // Sets critext
190 getSession().remove(RESULT_KEY); // The current result is obsolete
195 * Remove context from the search filter.
199 protected String doRemoveContext() {
200 long index = Long.valueOf(_cindex);
201 for (Iterator<SimulationContext> selected = _context.iterator(); selected
203 if (selected.next().getIndex() == index) {
208 setContextTypeOptions(getInvolvedContexts()); // Sets critext
209 getSession().remove(RESULT_KEY); // The current result is obsolete
217 @SuppressWarnings("unchecked")
218 protected String doCancel() {
219 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
220 setContextTypeOptions(getInvolvedContexts()); // Sets critext
224 // ==============================================================================================================================
226 // ==============================================================================================================================
229 * Get date format string.
231 * @return date format string
233 public String getFormat() {
234 return getText("date.format");
238 * Get formatted today date as a string.
240 * @return current date as a string
242 public String getToday() {
243 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
244 getApplicationSettings().getCurrentLocale());
245 return tostring.format(java.util.Calendar.getInstance().getTime());
249 * Get search result state.
251 * @return "obsolete" if there is no results in the session, otherwise "uptodate"
253 public String getResultState() {
255 if (getSession().get(RESULT_KEY) == null) {
263 public String getAuthor() {
267 public List<Name> getCandidates() {
271 public List<SimulationContextType> getContextTypeOptions() {
275 public List<SimulationContext> getContextValueOptions() {
279 public SimulationContextType getSelectedContextType() {
283 public List<SimulationContext> getSimulationContexts() {
288 * Get list of found objects.
290 * @return list of found objects
292 public List<Proxy> getResult() {
296 // ==============================================================================================================================
298 // ==============================================================================================================================
301 * Set author id to search.
306 public void setAuthor(final String index) {
307 this._author = index;
311 * Set applied simulation context type id to search.
314 * persistent simulation context type id
316 public void setContextType(final String type) {
321 * Set value of simulation context to search.
324 * the simulation context value
326 public void setContextValue(final String value) {
327 this._cvalue = value;
331 * Set simulation context value id.
334 * the persistent id as string
336 public void setContextIndex(final String value) {
337 this._cindex = value;
341 * Build the list of study authors. If the current user also can create <BR>
342 * a study then it is placed on the top of the list.
344 protected void setCandidates() {
345 _candidates = new ArrayList<Name>();
346 List<User> users = getUserService().selectAllUsers();
347 User me = getConnectedUser(); // May be null
348 for (Iterator<User> i = users.iterator(); i.hasNext();) {
349 User next = i.next();
350 ApplicationRights he = new ApplicationRights(next);
351 if (he.canCreateStudy()) {
352 if (next.equals(me)) {
353 _candidates.add(0, new ValidationFacade.ByManager(me,
354 getApplicationSettings().getCurrentLocale()));
356 _candidates.add(next);
363 * Build available context types list with localized names.
366 * context types already used in the search filter
368 protected void setContextTypeOptions(
369 final List<SimulationContextType> critext) {
370 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
371 critext.remove(i.next().getType()); // Already used context type
373 // Ordering by alphabetical order of localized context types
374 SimulationContextType[] types = critext
375 .toArray(new SimulationContextType[critext.size()]);
376 ContextTypeComparator compare = new ContextTypeComparator();
377 ProjectSettingsService.Step step = getSimulationContextService()
378 .getAttachedStep(types[0]);
381 while (to < types.length - 1) {
383 if (!types[to].isAttachedTo(step)) {
385 Arrays.sort(types, from, to, compare);
388 step = getSimulationContextService().getAttachedStep(types[to]);
392 Arrays.sort(types, from, to + 1, compare);
394 this._critext = Arrays.asList(types);
397 // ==============================================================================================================================
399 // ==============================================================================================================================
401 protected abstract String doSearch() throws InvalidPropertyException;
403 protected abstract List<SimulationContextType> getInvolvedContexts();
405 protected abstract void loadFilter();
407 protected abstract void saveFilter();
409 // ==============================================================================================================================
410 // Getters and Setters
411 // ==============================================================================================================================
416 * @return array of options with key and value properties
418 public Map<String, String> getMatchOptions() {
419 Map<String, String> options = new LinkedHashMap<String, String>();
420 options.put("all", getText("field.matchall"));
421 options.put("any", getText("field.matchany"));
426 * Get the simulationContextService.
428 * @return the simulationContextService
430 public SimulationContextService getSimulationContextService() {
431 return _simulationContextService;
435 * Set the simulationContextService.
437 * @param simulationContextService
438 * the simulationContextService to set
440 public void setSimulationContextService(
441 final SimulationContextService simulationContextService) {
442 _simulationContextService = simulationContextService;
446 * Get the userService.
448 * @return the userService
450 public UserService getUserService() {
455 * Set the userService.
458 * the userService to set
460 public void setUserService(final UserService userService) {
461 _userService = userService;
464 public String getContextMatch() {
465 return _contextMatch;
468 public String getCriteriaMatch() {
469 return _criteriaMatch;
472 public void setContextMatch(final String value) {
473 this._contextMatch = value;
476 public void setCriteriaMatch(final String value) {
477 this._criteriaMatch = value;
480 public String getWords() {
484 public void setWords(final String value) {