1 package org.splat.simer;
3 import java.util.Arrays;
4 import java.util.Iterator;
6 import java.util.Vector;
8 import org.hibernate.Session;
9 import org.hibernate.Transaction;
10 import org.splat.kernel.InvalidPropertyException;
11 import org.splat.kernel.Name;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.som.ApplicationRights;
14 import org.splat.dal.dao.som.Database;
15 import org.splat.service.SimulationContextService;
16 import org.splat.service.UserService;
17 import org.splat.service.technical.ProjectSettingsService;
18 import org.splat.service.dto.Proxy;
19 import org.splat.dal.bo.som.SimulationContext;
20 import org.splat.dal.bo.som.SimulationContextType;
22 public abstract class SearchBaseAction extends Action {
27 private static final long serialVersionUID = 7863055790228544510L;
29 protected String ctype = null; // Context type index, when selected
30 protected String cvalue = null; // Context value index, when selected
31 protected String cindex = ""; // Context index, when removed
32 protected String author = null;
33 protected List<Name> manager = null;
34 protected SimulationContextType newtype; // Context type to be valued
35 protected List<SimulationContext> newvalue; // Context value to be selected
36 protected List<SimulationContextType> critext; // Addable context types
37 protected List<SimulationContext> context; // Current contexts search criteria
38 protected List<Proxy> result;
40 * Injected simulation context service.
42 private SimulationContextService _simulationContextService;
45 * Injected user service.
47 private UserService _userService;
50 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
53 // ==============================================================================================================================
55 // ==============================================================================================================================
57 public String doSubmitForm() {
58 // -----------------------------
59 // Identification of the user action
60 UserAction action = UserAction.refreshResult;
61 if (ctype != null && Integer.valueOf(ctype) > 0)
62 action = UserAction.selectContextType;
63 else if (cvalue != null && Integer.valueOf(cvalue) > 0)
64 action = UserAction.selectContextValue;
65 else if (cindex.length() > 0) {
66 int index = Integer.valueOf(cindex);
68 action = UserAction.removeContext;
70 action = UserAction.cancelSelect;
72 // Execution of the user action
73 Session connex = Database.getCurSession();
74 Transaction transax = connex.beginTransaction();
77 saveFilter(); // Also reinitializes the form, if needed
79 if (action == UserAction.selectContextType) {
80 done = doSelectContextType();
81 } else if (action == UserAction.selectContextValue) {
82 done = doAddContext();
83 } else if (action == UserAction.removeContext) {
84 done = doRemoveContext();
85 } else if (action == UserAction.cancelSelect) {
87 } else { // UserAction.refreshResult
89 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
94 } catch (Exception error) {
95 // No need to roll back the transaction as it is read only
96 logger.error("Reason: ", error);
101 @SuppressWarnings("unchecked")
102 protected String doSelectContextType() {
103 // ---------------------------------------
104 SimulationContext.Properties sprop = new SimulationContext.Properties();
106 newtype = getSimulationContextService().selectType(
107 Integer.valueOf(ctype));
108 newvalue = getSimulationContextService().selectSimulationContextsWhere(
109 sprop.setType(newtype));
110 if (cindex.length() > 0 && Integer.valueOf(cindex) == 0)
111 getSession().remove("search.result");
113 result = (List<Proxy>) getSession().get("search.result"); // We keep the previous result search, if valid
117 protected String doAddContext() {
118 // --------------------------------
119 SimulationContext selected = getSimulationContextService()
120 .selectSimulationContext(Integer.valueOf(cvalue));
122 context.add(selected);
123 setContextTypeOptions(getInvolvedContexts()); // Sets critext
124 getSession().remove("search.result"); // The current result is obsolete
128 protected String doRemoveContext() {
129 // -----------------------------------
130 int index = Integer.valueOf(cindex);
131 for (Iterator<SimulationContext> selected = context.iterator(); selected
133 if (selected.next().getIndex() == index) {
138 setContextTypeOptions(getInvolvedContexts()); // Sets critext
139 getSession().remove("search.result"); // The current result is obsolete
143 @SuppressWarnings("unchecked")
144 protected String doCancel() {
145 // ----------------------------
146 result = (List<Proxy>) getSession().get("search.result"); // Current result search
147 setContextTypeOptions(getInvolvedContexts()); // Sets critext
151 // ==============================================================================================================================
153 // ==============================================================================================================================
155 public String getAuthor() {
156 // --------------------------
160 public List<Name> getCandidates() {
161 // ----------------------------------
165 public List<SimulationContextType> getContextTypeOptions() {
166 // -----------------------------------------------------------
170 public List<SimulationContext> getContextValueOptions() {
171 // --------------------------------------------------------
175 public SimulationContextType getSelectedContextType() {
176 // ------------------------------------------------------
180 public List<SimulationContext> getSimulationContexts() {
181 // -------------------------------------------------------
185 public List<Proxy> getResult() {
186 // -------------------------------
190 // ==============================================================================================================================
192 // ==============================================================================================================================
194 public void setAuthor(String index) {
195 // ------------------------------------
199 public void setContextType(String type) {
200 // ----------------------------------------
204 public void setContextValue(String value) {
205 // ------------------------------------------
209 public void setContextIndex(String value) {
210 // ------------------------------------------
214 protected void setCandidates() {
215 // -------------------------------
216 manager = new Vector<Name>();
217 List<User> users = getUserService().selectAllUsers();
218 User me = getConnectedUser(); // May be null
219 for (Iterator<User> i = users.iterator(); i.hasNext();) {
220 User next = i.next();
221 ApplicationRights he = new ApplicationRights(next);
222 if (he.canCreateStudy()) {
224 manager.add(0, new ValidationFacade.ByManager(me));
231 protected void setContextTypeOptions(List<SimulationContextType> critext) {
232 // --------------------------------------------------------------------------
233 for (Iterator<SimulationContext> i = context.iterator(); i.hasNext();) {
234 critext.remove(i.next().getType()); // Already used context type
236 // Ordering by alphabetical order of localized context types
237 SimulationContextType[] types = critext
238 .toArray(new SimulationContextType[critext.size()]);
239 ContextTypeComparator compare = new ContextTypeComparator();
240 ProjectSettingsService.Step step = getSimulationContextService()
241 .getAttachedStep(types[0]);
244 while (to < types.length - 1) {
246 if (types[to].isAttachedTo(step))
250 Arrays.sort(types, from, to, compare);
252 step = getSimulationContextService().getAttachedStep(types[to]);
255 Arrays.sort(types, from, to + 1, compare);
256 this.critext = Arrays.asList(types);
259 // ==============================================================================================================================
261 // ==============================================================================================================================
263 protected abstract String doSearch() throws InvalidPropertyException;
265 protected abstract List<SimulationContextType> getInvolvedContexts();
267 protected abstract void loadFilter();
269 protected abstract void saveFilter();
272 * Get the simulationContextService.
274 * @return the simulationContextService
276 public SimulationContextService getSimulationContextService() {
277 return _simulationContextService;
281 * Set the simulationContextService.
283 * @param simulationContextService
284 * the simulationContextService to set
286 public void setSimulationContextService(
287 SimulationContextService simulationContextService) {
288 _simulationContextService = simulationContextService;
292 * Get the userService.
293 * @return the userService
295 public UserService getUserService() {
300 * Set the userService.
301 * @param userService the userService to set
303 public void setUserService(UserService userService) {
304 _userService = userService;