1 package org.splat.simer;
3 import java.util.Arrays;
4 import java.util.Iterator;
6 import java.util.Vector;
8 import org.splat.kernel.InvalidPropertyException;
9 import org.splat.kernel.Name;
10 import org.splat.dal.bo.kernel.User;
11 import org.splat.som.ApplicationRights;
12 import org.splat.service.SimulationContextService;
13 import org.splat.service.UserService;
14 import org.splat.service.technical.ProjectSettingsService;
15 import org.splat.service.dto.Proxy;
16 import org.splat.dal.bo.som.SimulationContext;
17 import org.splat.dal.bo.som.SimulationContextType;
19 public abstract class SearchBaseAction extends Action {
24 private static final long serialVersionUID = 7863055790228544510L;
26 protected String ctype = null; // Context type index, when selected
27 protected String cvalue = null; // Context value index, when selected
28 protected String cindex = ""; // Context index, when removed
29 protected String author = null;
30 protected List<Name> manager = null;
31 protected SimulationContextType newtype; // Context type to be valued
32 protected List<SimulationContext> newvalue; // Context value to be selected
33 protected List<SimulationContextType> critext; // Addable context types
34 protected List<SimulationContext> context; // Current contexts search criteria
35 protected List<Proxy> result;
37 * Injected simulation context service.
39 private SimulationContextService _simulationContextService;
42 * Injected user service.
44 private UserService _userService;
47 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
50 // ==============================================================================================================================
52 // ==============================================================================================================================
54 public String doSubmitForm() {
55 // Identification of the user action
56 UserAction action = UserAction.refreshResult;
57 if (ctype != null && Integer.valueOf(ctype) > 0)
58 action = UserAction.selectContextType;
59 else if (cvalue != null && Integer.valueOf(cvalue) > 0)
60 action = UserAction.selectContextValue;
61 else if (cindex.length() > 0) {
62 int index = Integer.valueOf(cindex);
64 action = UserAction.removeContext;
66 action = UserAction.cancelSelect;
68 // Execution of the user action
71 saveFilter(); // Also reinitializes the form, if needed
73 if (action == UserAction.selectContextType) {
74 done = doSelectContextType();
75 } else if (action == UserAction.selectContextValue) {
76 done = doAddContext();
77 } else if (action == UserAction.removeContext) {
78 done = doRemoveContext();
79 } else if (action == UserAction.cancelSelect) {
81 } else { // UserAction.refreshResult
83 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
87 } catch (Exception error) {
88 // No need to roll back the transaction as it is read only
89 logger.error("Reason: ", error);
94 @SuppressWarnings("unchecked")
95 protected String doSelectContextType() {
96 SimulationContext.Properties sprop = new SimulationContext.Properties();
98 newtype = getSimulationContextService().selectType(
99 Integer.valueOf(ctype));
100 newvalue = getSimulationContextService().selectSimulationContextsWhere(
101 sprop.setType(newtype));
102 if (cindex.length() > 0 && Integer.valueOf(cindex) == 0)
103 getSession().remove("search.result");
105 result = (List<Proxy>) getSession().get("search.result"); // We keep the previous result search, if valid
109 protected String doAddContext() {
110 SimulationContext selected = getSimulationContextService()
111 .selectSimulationContext(Integer.valueOf(cvalue));
113 context.add(selected);
114 setContextTypeOptions(getInvolvedContexts()); // Sets critext
115 getSession().remove("search.result"); // The current result is obsolete
119 protected String doRemoveContext() {
120 int index = Integer.valueOf(cindex);
121 for (Iterator<SimulationContext> selected = context.iterator(); selected
123 if (selected.next().getIndex() == index) {
128 setContextTypeOptions(getInvolvedContexts()); // Sets critext
129 getSession().remove("search.result"); // The current result is obsolete
133 @SuppressWarnings("unchecked")
134 protected String doCancel() {
135 result = (List<Proxy>) getSession().get("search.result"); // Current result search
136 setContextTypeOptions(getInvolvedContexts()); // Sets critext
140 // ==============================================================================================================================
142 // ==============================================================================================================================
144 public String getAuthor() {
148 public List<Name> getCandidates() {
152 public List<SimulationContextType> getContextTypeOptions() {
156 public List<SimulationContext> getContextValueOptions() {
160 public SimulationContextType getSelectedContextType() {
164 public List<SimulationContext> getSimulationContexts() {
168 public List<Proxy> getResult() {
172 // ==============================================================================================================================
174 // ==============================================================================================================================
176 public void setAuthor(String index) {
180 public void setContextType(String type) {
184 public void setContextValue(String value) {
188 public void setContextIndex(String value) {
192 protected void setCandidates() {
193 manager = new Vector<Name>();
194 List<User> users = getUserService().selectAllUsers();
195 User me = getConnectedUser(); // May be null
196 for (Iterator<User> i = users.iterator(); i.hasNext();) {
197 User next = i.next();
198 ApplicationRights he = new ApplicationRights(next);
199 if (he.canCreateStudy()) {
201 manager.add(0, new ValidationFacade.ByManager(me));
208 protected void setContextTypeOptions(List<SimulationContextType> critext) {
209 for (Iterator<SimulationContext> i = context.iterator(); i.hasNext();) {
210 critext.remove(i.next().getType()); // Already used context type
212 // Ordering by alphabetical order of localized context types
213 SimulationContextType[] types = critext
214 .toArray(new SimulationContextType[critext.size()]);
215 ContextTypeComparator compare = new ContextTypeComparator();
216 ProjectSettingsService.Step step = getSimulationContextService()
217 .getAttachedStep(types[0]);
220 while (to < types.length - 1) {
222 if (types[to].isAttachedTo(step))
226 Arrays.sort(types, from, to, compare);
228 step = getSimulationContextService().getAttachedStep(types[to]);
231 Arrays.sort(types, from, to + 1, compare);
232 this.critext = Arrays.asList(types);
235 // ==============================================================================================================================
237 // ==============================================================================================================================
239 protected abstract String doSearch() throws InvalidPropertyException;
241 protected abstract List<SimulationContextType> getInvolvedContexts();
243 protected abstract void loadFilter();
245 protected abstract void saveFilter();
248 * Get the simulationContextService.
250 * @return the simulationContextService
252 public SimulationContextService getSimulationContextService() {
253 return _simulationContextService;
257 * Set the simulationContextService.
259 * @param simulationContextService
260 * the simulationContextService to set
262 public void setSimulationContextService(
263 SimulationContextService simulationContextService) {
264 _simulationContextService = simulationContextService;
268 * Get the userService.
269 * @return the userService
271 public UserService getUserService() {
276 * Set the userService.
277 * @param userService the userService to set
279 public void setUserService(UserService userService) {
280 _userService = userService;