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.kernel.UserDirectory;
14 import org.splat.som.ApplicationRights;
15 import org.splat.dal.dao.som.Database;
16 import org.splat.service.SimulationContextService;
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 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
48 // ==============================================================================================================================
50 // ==============================================================================================================================
52 public String doSubmitForm() {
53 // -----------------------------
54 // Identification of the user action
55 UserAction action = UserAction.refreshResult;
56 if (ctype != null && Integer.valueOf(ctype) > 0)
57 action = UserAction.selectContextType;
58 else if (cvalue != null && Integer.valueOf(cvalue) > 0)
59 action = UserAction.selectContextValue;
60 else if (cindex.length() > 0) {
61 int index = Integer.valueOf(cindex);
63 action = UserAction.removeContext;
65 action = UserAction.cancelSelect;
67 // Execution of the user action
68 Session connex = Database.getSession();
69 Transaction transax = connex.beginTransaction();
72 saveFilter(); // Also reinitializes the form, if needed
74 if (action == UserAction.selectContextType) {
75 done = doSelectContextType();
76 } else if (action == UserAction.selectContextValue) {
77 done = doAddContext();
78 } else if (action == UserAction.removeContext) {
79 done = doRemoveContext();
80 } else if (action == UserAction.cancelSelect) {
82 } else { // UserAction.refreshResult
84 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
89 } catch (Exception error) {
90 // No need to roll back the transaction as it is read only
91 logger.error("Reason: ", error);
96 @SuppressWarnings("unchecked")
97 protected String doSelectContextType() {
98 // ---------------------------------------
99 SimulationContext.Properties sprop = new SimulationContext.Properties();
101 newtype = getSimulationContextService().selectType(Integer.valueOf(ctype));
102 newvalue = getSimulationContextService().selectSimulationContextsWhere(
103 sprop.setType(newtype));
104 if (cindex.length() > 0 && Integer.valueOf(cindex) == 0)
105 getSession().remove("search.result");
107 result = (List<Proxy>) getSession().get("search.result"); // We keep the previous result search, if valid
111 protected String doAddContext() {
112 // --------------------------------
113 SimulationContext selected = getSimulationContextService()
114 .selectSimulationContext(Integer.valueOf(cvalue));
116 context.add(selected);
117 setContextTypeOptions(getInvolvedContexts()); // Sets critext
118 getSession().remove("search.result"); // The current result is obsolete
122 protected String doRemoveContext() {
123 // -----------------------------------
124 int index = Integer.valueOf(cindex);
125 for (Iterator<SimulationContext> selected = context.iterator(); selected
127 if (selected.next().getIndex() == index) {
132 setContextTypeOptions(getInvolvedContexts()); // Sets critext
133 getSession().remove("search.result"); // The current result is obsolete
137 @SuppressWarnings("unchecked")
138 protected String doCancel() {
139 // ----------------------------
140 result = (List<Proxy>) getSession().get("search.result"); // Current result search
141 setContextTypeOptions(getInvolvedContexts()); // Sets critext
145 // ==============================================================================================================================
147 // ==============================================================================================================================
149 public String getAuthor() {
150 // --------------------------
154 public List<Name> getCandidates() {
155 // ----------------------------------
159 public List<SimulationContextType> getContextTypeOptions() {
160 // -----------------------------------------------------------
164 public List<SimulationContext> getContextValueOptions() {
165 // --------------------------------------------------------
169 public SimulationContextType getSelectedContextType() {
170 // ------------------------------------------------------
174 public List<SimulationContext> getSimulationContexts() {
175 // -------------------------------------------------------
179 public List<Proxy> getResult() {
180 // -------------------------------
184 // ==============================================================================================================================
186 // ==============================================================================================================================
188 public void setAuthor(String index) {
189 // ------------------------------------
193 public void setContextType(String type) {
194 // ----------------------------------------
198 public void setContextValue(String value) {
199 // ------------------------------------------
203 public void setContextIndex(String value) {
204 // ------------------------------------------
208 protected void setCandidates() {
209 // -------------------------------
210 manager = new Vector<Name>();
211 List<User> users = UserDirectory.selectAllUsers();
212 User me = getConnectedUser(); // May be null
213 for (Iterator<User> i = users.iterator(); i.hasNext();) {
214 User next = i.next();
215 ApplicationRights he = new ApplicationRights(next);
216 if (he.canCreateStudy()) {
218 manager.add(0, new ValidationFacade.ByManager(me));
225 protected void setContextTypeOptions(List<SimulationContextType> critext) {
226 // --------------------------------------------------------------------------
227 for (Iterator<SimulationContext> i = context.iterator(); i.hasNext();) {
228 critext.remove(i.next().getType()); // Already used context type
230 // Ordering by alphabetical order of localized context types
231 SimulationContextType[] types = critext
232 .toArray(new SimulationContextType[critext.size()]);
233 ContextTypeComparator compare = new ContextTypeComparator();
234 ProjectSettingsService.Step step = types[0].getAttachedStep();
237 while (to < types.length - 1) {
239 if (types[to].isAttachedTo(step))
243 Arrays.sort(types, from, to, compare);
245 step = types[to].getAttachedStep();
248 Arrays.sort(types, from, to + 1, compare);
249 this.critext = Arrays.asList(types);
252 // ==============================================================================================================================
254 // ==============================================================================================================================
256 protected abstract String doSearch() throws InvalidPropertyException;
258 protected abstract List<SimulationContextType> getInvolvedContexts();
260 protected abstract void loadFilter();
262 protected abstract void saveFilter();
265 * Get the simulationContextService.
267 * @return the simulationContextService
269 public SimulationContextService getSimulationContextService() {
270 return _simulationContextService;
274 * Set the simulationContextService.
276 * @param simulationContextService
277 * the simulationContextService to set
279 public void setSimulationContextService(
280 SimulationContextService simulationContextService) {
281 _simulationContextService = simulationContextService;