1 package org.splat.simer;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
8 import org.splat.dal.bo.kernel.User;
9 import org.splat.dal.bo.som.SimulationContext;
10 import org.splat.dal.bo.som.SimulationContextType;
11 import org.splat.kernel.InvalidPropertyException;
12 import org.splat.kernel.Name;
13 import org.splat.service.SimulationContextService;
14 import org.splat.service.UserService;
15 import org.splat.service.dto.Proxy;
16 import org.splat.service.technical.ProjectSettingsService;
17 import org.splat.som.ApplicationRights;
19 public abstract class AbstractSearchBaseAction extends Action {
24 private static final long serialVersionUID = 7863055790228544510L;
27 * Search result key in the session.
29 public static final String RESULT_KEY = "search.result";
32 * Context type index, when selected.
34 protected transient String _ctype = null;
36 * Context value index, when selected.
38 protected transient String _cvalue = null;
40 * Context index, when removed.
42 protected transient String _cindex = "";
43 protected String _author = null;
44 protected List<Name> _candidates = null;
46 * Context type to be valued.
48 protected transient SimulationContextType _newtype;
50 * Context value to be selected.
52 protected transient List<SimulationContext> _newvalue;
54 * Addable context types.
56 protected transient List<SimulationContextType> _critext;
58 * Current contexts search criteria.
60 protected transient List<SimulationContext> _context;
61 protected transient List<Proxy> _result;
63 * Injected simulation context service.
65 private SimulationContextService _simulationContextService;
68 * Injected user service.
70 private UserService _userService;
73 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
76 // ==============================================================================================================================
78 // ==============================================================================================================================
80 public String doSubmitForm() {
81 // Identification of the user action
82 UserAction action = UserAction.refreshResult;
83 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
84 action = UserAction.selectContextType;
85 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
86 action = UserAction.selectContextValue;
87 } else if (_cindex.length() > 0) {
88 int index = Integer.valueOf(_cindex);
90 action = UserAction.removeContext;
91 } else if (index < 0) {
92 action = UserAction.cancelSelect;
95 // Execution of the user action
98 saveFilter(); // Also reinitializes the form, if needed
100 if (action == UserAction.selectContextType) {
101 done = doSelectContextType();
102 } else if (action == UserAction.selectContextValue) {
103 done = doAddContext();
104 } else if (action == UserAction.removeContext) {
105 done = doRemoveContext();
106 } else if (action == UserAction.cancelSelect) {
108 } else { // UserAction.refreshResult
110 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
113 } catch (Exception error) {
114 // No need to roll back the transaction as it is read only
115 LOG.error("Reason: ", error);
121 @SuppressWarnings("unchecked")
122 protected String doSelectContextType() {
123 SimulationContext.Properties sprop = new SimulationContext.Properties();
125 _newtype = getSimulationContextService().selectType(
126 Integer.valueOf(_ctype));
127 _newvalue = getSimulationContextService().selectSimulationContextsWhere(
128 sprop.setType(_newtype));
129 if (_cindex.length() > 0 && Integer.valueOf(_cindex) == 0) {
130 getSession().remove(RESULT_KEY);
132 _result = (List<Proxy>) getSession().get(RESULT_KEY); // We keep the previous result search, if valid
137 protected String doAddContext() {
138 SimulationContext selected = getSimulationContextService()
139 .selectSimulationContext(Integer.valueOf(_cvalue));
141 _context.add(selected);
142 setContextTypeOptions(getInvolvedContexts()); // Sets critext
143 getSession().remove(RESULT_KEY); // The current result is obsolete
147 protected String doRemoveContext() {
148 int index = Integer.valueOf(_cindex);
149 for (Iterator<SimulationContext> selected = _context.iterator(); selected
151 if (selected.next().getIndex() == index) {
156 setContextTypeOptions(getInvolvedContexts()); // Sets critext
157 getSession().remove(RESULT_KEY); // The current result is obsolete
161 @SuppressWarnings("unchecked")
162 protected String doCancel() {
163 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
164 setContextTypeOptions(getInvolvedContexts()); // Sets critext
168 // ==============================================================================================================================
170 // ==============================================================================================================================
172 public String getAuthor() {
176 public List<Name> getCandidates() {
180 public List<SimulationContextType> getContextTypeOptions() {
184 public List<SimulationContext> getContextValueOptions() {
188 public SimulationContextType getSelectedContextType() {
192 public List<SimulationContext> getSimulationContexts() {
196 public List<Proxy> getResult() {
200 // ==============================================================================================================================
202 // ==============================================================================================================================
204 public void setAuthor(final String index) {
205 this._author = index;
208 public void setContextType(final String type) {
212 public void setContextValue(final String value) {
213 this._cvalue = value;
216 public void setContextIndex(final String value) {
217 this._cindex = value;
220 protected void setCandidates() {
221 _candidates = new ArrayList<Name>();
222 List<User> users = getUserService().selectAllUsers();
223 User me = getConnectedUser(); // May be null
224 for (Iterator<User> i = users.iterator(); i.hasNext();) {
225 User next = i.next();
226 ApplicationRights he = new ApplicationRights(next);
227 if (he.canCreateStudy()) {
228 if (next.equals(me)) {
229 _candidates.add(0, new ValidationFacade.ByManager(me));
231 _candidates.add(next);
237 protected void setContextTypeOptions(final List<SimulationContextType> critext) {
238 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
239 critext.remove(i.next().getType()); // Already used context type
241 // Ordering by alphabetical order of localized context types
242 SimulationContextType[] types = critext
243 .toArray(new SimulationContextType[critext.size()]);
244 ContextTypeComparator compare = new ContextTypeComparator();
245 ProjectSettingsService.Step step = getSimulationContextService()
246 .getAttachedStep(types[0]);
249 while (to < types.length - 1) {
251 if (types[to].isAttachedTo(step)) {
256 Arrays.sort(types, from, to, compare);
259 step = getSimulationContextService().getAttachedStep(types[to]);
262 Arrays.sort(types, from, to + 1, compare);
264 this._critext = Arrays.asList(types);
267 // ==============================================================================================================================
269 // ==============================================================================================================================
271 protected abstract String doSearch() throws InvalidPropertyException;
273 protected abstract List<SimulationContextType> getInvolvedContexts();
275 protected abstract void loadFilter();
277 protected abstract void saveFilter();
280 * Get the simulationContextService.
282 * @return the simulationContextService
284 public SimulationContextService getSimulationContextService() {
285 return _simulationContextService;
289 * Set the simulationContextService.
291 * @param simulationContextService
292 * the simulationContextService to set
294 public void setSimulationContextService(
295 final SimulationContextService simulationContextService) {
296 _simulationContextService = simulationContextService;
300 * Get the userService.
301 * @return the userService
303 public UserService getUserService() {
308 * Set the userService.
309 * @param userService the userService to set
311 public void setUserService(final UserService userService) {
312 _userService = userService;