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;
9 import org.splat.dal.bo.kernel.User;
10 import org.splat.dal.bo.som.SimulationContext;
11 import org.splat.dal.bo.som.SimulationContextType;
12 import org.splat.kernel.InvalidPropertyException;
13 import org.splat.kernel.Name;
14 import org.splat.service.SimulationContextService;
15 import org.splat.service.UserService;
16 import org.splat.service.dto.Proxy;
17 import org.splat.service.technical.ProjectSettingsService;
18 import org.splat.som.ApplicationRights;
21 * Base search action class used for searching studies and knowledge.
23 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
25 public abstract class AbstractSearchBaseAction extends Action {
30 private static final long serialVersionUID = 7863055790228544510L;
33 * Search result key in the session.
35 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 = "";
49 protected String _author = null;
50 protected List<Name> _candidates = null;
52 * Context type to be valued.
54 protected transient SimulationContextType _newtype;
56 * Context value to be selected.
58 protected transient List<SimulationContext> _newvalue;
60 * Addable context types.
62 protected transient List<SimulationContextType> _critext;
64 * Current contexts search criteria.
66 protected transient List<SimulationContext> _context;
68 * List of found objects.
70 protected transient List<Proxy> _result;
72 * Injected simulation context service.
74 private SimulationContextService _simulationContextService;
77 * Injected user service.
79 private UserService _userService;
82 * Search action modes enumeration.
85 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
88 // ==============================================================================================================================
90 // ==============================================================================================================================
93 * Perform actions according to the current mode.
95 * @return action result or ERROR if failed
97 public String doSubmitForm() {
98 // Identification of the user action
99 UserAction action = UserAction.refreshResult;
100 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
101 action = UserAction.selectContextType;
102 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
103 action = UserAction.selectContextValue;
104 } else if (_cindex.length() > 0) {
105 int index = Integer.valueOf(_cindex);
107 action = UserAction.removeContext;
108 } else if (index < 0) {
109 action = UserAction.cancelSelect;
112 // Execution of the user action
115 saveFilter(); // Also reinitializes the form, if needed
117 if (action == UserAction.selectContextType) {
118 done = doSelectContextType();
119 } else if (action == UserAction.selectContextValue) {
120 done = doAddContext();
121 } else if (action == UserAction.removeContext) {
122 done = doRemoveContext();
123 } else if (action == UserAction.cancelSelect) {
125 } else { // UserAction.refreshResult
127 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
130 } catch (Exception error) {
131 // No need to roll back the transaction as it is read only
132 LOG.error("Reason: ", error);
139 * Add a selected context type to the search filter. Obsolete the current result if necessary.
141 * @return "selectype"
143 @SuppressWarnings("unchecked")
144 protected String doSelectContextType() {
145 SimulationContext.Properties sprop = new SimulationContext.Properties();
147 _newtype = getSimulationContextService().selectType(
148 Integer.valueOf(_ctype));
149 _newvalue = getSimulationContextService()
150 .selectSimulationContextsWhere(sprop.setType(_newtype));
151 if (_cindex.length() > 0 && Integer.valueOf(_cindex) == 0) {
152 getSession().remove(RESULT_KEY);
154 // We keep the previous result search, if valid
155 _result = (List<Proxy>) getSession().get(RESULT_KEY);
161 * Add a selected context to the search filter. Obsolete the current result.
165 protected String doAddContext() {
166 SimulationContext selected = getSimulationContextService()
167 .selectSimulationContext(Integer.valueOf(_cvalue));
169 _context.add(selected);
170 setContextTypeOptions(getInvolvedContexts()); // Sets critext
171 getSession().remove(RESULT_KEY); // The current result is obsolete
176 * Remove context from the search filter.
180 protected String doRemoveContext() {
181 int index = Integer.valueOf(_cindex);
182 for (Iterator<SimulationContext> selected = _context.iterator(); selected
184 if (selected.next().getIndex() == index) {
189 setContextTypeOptions(getInvolvedContexts()); // Sets critext
190 getSession().remove(RESULT_KEY); // The current result is obsolete
198 @SuppressWarnings("unchecked")
199 protected String doCancel() {
200 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
201 setContextTypeOptions(getInvolvedContexts()); // Sets critext
205 // ==============================================================================================================================
207 // ==============================================================================================================================
210 * Get date format string.
212 * @return date format string
214 public String getFormat() {
215 return getText("date.format");
219 * Get formatted today date as a string.
221 * @return current date as a string
223 public String getToday() {
224 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
225 getApplicationSettings().getCurrentLocale());
226 return tostring.format(java.util.Calendar.getInstance().getTime());
230 * Get search result state.
232 * @return "obsolete" if there is no results in the session, otherwise "uptodate"
234 public String getResultState() {
236 if (getSession().get(RESULT_KEY) == null) {
244 public String getAuthor() {
248 public List<Name> getCandidates() {
252 public List<SimulationContextType> getContextTypeOptions() {
256 public List<SimulationContext> getContextValueOptions() {
260 public SimulationContextType getSelectedContextType() {
264 public List<SimulationContext> getSimulationContexts() {
269 * Get list of found objects.
271 * @return list of found objects
273 public List<Proxy> getResult() {
277 // ==============================================================================================================================
279 // ==============================================================================================================================
281 public void setAuthor(final String index) {
282 this._author = index;
285 public void setContextType(final String type) {
289 public void setContextValue(final String value) {
290 this._cvalue = value;
293 public void setContextIndex(final String value) {
294 this._cindex = value;
297 protected void setCandidates() {
298 _candidates = new ArrayList<Name>();
299 List<User> users = getUserService().selectAllUsers();
300 User me = getConnectedUser(); // May be null
301 for (Iterator<User> i = users.iterator(); i.hasNext();) {
302 User next = i.next();
303 ApplicationRights he = new ApplicationRights(next);
304 if (he.canCreateStudy()) {
305 if (next.equals(me)) {
306 _candidates.add(0, new ValidationFacade.ByManager(me,
307 getApplicationSettings().getCurrentLocale()));
309 _candidates.add(next);
316 * Build available context types list with localized names.
319 * context types already used in the search filter
321 protected void setContextTypeOptions(
322 final List<SimulationContextType> critext) {
323 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
324 critext.remove(i.next().getType()); // Already used context type
326 // Ordering by alphabetical order of localized context types
327 SimulationContextType[] types = critext
328 .toArray(new SimulationContextType[critext.size()]);
329 ContextTypeComparator compare = new ContextTypeComparator();
330 ProjectSettingsService.Step step = getSimulationContextService()
331 .getAttachedStep(types[0]);
334 while (to < types.length - 1) {
336 if (types[to].isAttachedTo(step)) {
341 Arrays.sort(types, from, to, compare);
344 step = getSimulationContextService().getAttachedStep(types[to]);
347 Arrays.sort(types, from, to + 1, compare);
349 this._critext = Arrays.asList(types);
352 // ==============================================================================================================================
354 // ==============================================================================================================================
356 protected abstract String doSearch() throws InvalidPropertyException;
358 protected abstract List<SimulationContextType> getInvolvedContexts();
360 protected abstract void loadFilter();
362 protected abstract void saveFilter();
365 * Get the simulationContextService.
367 * @return the simulationContextService
369 public SimulationContextService getSimulationContextService() {
370 return _simulationContextService;
374 * Set the simulationContextService.
376 * @param simulationContextService
377 * the simulationContextService to set
379 public void setSimulationContextService(
380 final SimulationContextService simulationContextService) {
381 _simulationContextService = simulationContextService;
385 * Get the userService.
387 * @return the userService
389 public UserService getUserService() {
394 * Set the userService.
397 * the userService to set
399 public void setUserService(final UserService userService) {
400 _userService = userService;