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);
157 setActionType("setContext");
162 * Add a selected context to the search filter. Obsolete the current result.
166 protected String doAddContext() {
167 SimulationContext selected = getSimulationContextService()
168 .selectSimulationContext(Integer.valueOf(_cvalue));
170 _context.add(selected);
171 setContextTypeOptions(getInvolvedContexts()); // Sets critext
172 getSession().remove(RESULT_KEY); // The current result is obsolete
177 * Remove context from the search filter.
181 protected String doRemoveContext() {
182 int index = Integer.valueOf(_cindex);
183 for (Iterator<SimulationContext> selected = _context.iterator(); selected
185 if (selected.next().getIndex() == index) {
190 setContextTypeOptions(getInvolvedContexts()); // Sets critext
191 getSession().remove(RESULT_KEY); // The current result is obsolete
199 @SuppressWarnings("unchecked")
200 protected String doCancel() {
201 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
202 setContextTypeOptions(getInvolvedContexts()); // Sets critext
206 // ==============================================================================================================================
208 // ==============================================================================================================================
211 * Get date format string.
213 * @return date format string
215 public String getFormat() {
216 return getText("date.format");
220 * Get formatted today date as a string.
222 * @return current date as a string
224 public String getToday() {
225 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
226 getApplicationSettings().getCurrentLocale());
227 return tostring.format(java.util.Calendar.getInstance().getTime());
231 * Get search result state.
233 * @return "obsolete" if there is no results in the session, otherwise "uptodate"
235 public String getResultState() {
237 if (getSession().get(RESULT_KEY) == null) {
245 public String getAuthor() {
249 public List<Name> getCandidates() {
253 public List<SimulationContextType> getContextTypeOptions() {
257 public List<SimulationContext> getContextValueOptions() {
261 public SimulationContextType getSelectedContextType() {
265 public List<SimulationContext> getSimulationContexts() {
270 * Get list of found objects.
272 * @return list of found objects
274 public List<Proxy> getResult() {
278 // ==============================================================================================================================
280 // ==============================================================================================================================
282 public void setAuthor(final String index) {
283 this._author = index;
286 public void setContextType(final String type) {
290 public void setContextValue(final String value) {
291 this._cvalue = value;
294 public void setContextIndex(final String value) {
295 this._cindex = value;
298 protected void setCandidates() {
299 _candidates = new ArrayList<Name>();
300 List<User> users = getUserService().selectAllUsers();
301 User me = getConnectedUser(); // May be null
302 for (Iterator<User> i = users.iterator(); i.hasNext();) {
303 User next = i.next();
304 ApplicationRights he = new ApplicationRights(next);
305 if (he.canCreateStudy()) {
306 if (next.equals(me)) {
307 _candidates.add(0, new ValidationFacade.ByManager(me,
308 getApplicationSettings().getCurrentLocale()));
310 _candidates.add(next);
317 * Build available context types list with localized names.
320 * context types already used in the search filter
322 protected void setContextTypeOptions(
323 final List<SimulationContextType> critext) {
324 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
325 critext.remove(i.next().getType()); // Already used context type
327 // Ordering by alphabetical order of localized context types
328 SimulationContextType[] types = critext
329 .toArray(new SimulationContextType[critext.size()]);
330 ContextTypeComparator compare = new ContextTypeComparator();
331 ProjectSettingsService.Step step = getSimulationContextService()
332 .getAttachedStep(types[0]);
335 while (to < types.length - 1) {
337 if (types[to].isAttachedTo(step)) {
342 Arrays.sort(types, from, to, compare);
345 step = getSimulationContextService().getAttachedStep(types[to]);
348 Arrays.sort(types, from, to + 1, compare);
350 this._critext = Arrays.asList(types);
353 // ==============================================================================================================================
355 // ==============================================================================================================================
357 protected abstract String doSearch() throws InvalidPropertyException;
359 protected abstract List<SimulationContextType> getInvolvedContexts();
361 protected abstract void loadFilter();
363 protected abstract void saveFilter();
366 * Get the simulationContextService.
368 * @return the simulationContextService
370 public SimulationContextService getSimulationContextService() {
371 return _simulationContextService;
375 * Set the simulationContextService.
377 * @param simulationContextService
378 * the simulationContextService to set
380 public void setSimulationContextService(
381 final SimulationContextService simulationContextService) {
382 _simulationContextService = simulationContextService;
386 * Get the userService.
388 * @return the userService
390 public UserService getUserService() {
395 * Set the userService.
398 * the userService to set
400 public void setUserService(final UserService userService) {
401 _userService = userService;