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.technical.ProjectSettingsService;
17 import org.splat.service.dto.Proxy;
18 import org.splat.dal.bo.som.SimulationContext;
19 import org.splat.dal.bo.som.SimulationContextType;
22 public abstract class SearchBaseAction extends Action {
24 protected String ctype = null; // Context type index, when selected
25 protected String cvalue = null; // Context value index, when selected
26 protected String cindex = ""; // Context index, when removed
27 protected String author = null;
28 protected List<Name> manager = null;
29 protected SimulationContextType newtype; // Context type to be valued
30 protected List<SimulationContext> newvalue; // Context value to be selected
31 protected List<SimulationContextType> critext; // Addable context types
32 protected List<SimulationContext> context; // Current contexts search criteria
33 protected List<Proxy> result;
35 private static final long serialVersionUID = 7863055790228544510L;
37 enum UserAction { refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext }
39 // ==============================================================================================================================
41 // ==============================================================================================================================
43 public String doSubmitForm () {
44 // -----------------------------
45 // Identification of the user action
46 UserAction action = UserAction.refreshResult;
47 if (ctype != null && Integer.valueOf(ctype) > 0) action = UserAction.selectContextType;
48 else if (cvalue != null && Integer.valueOf(cvalue) > 0) action = UserAction.selectContextValue;
49 else if (cindex.length() > 0) {
50 int index = Integer.valueOf(cindex);
51 if (index > 0) action = UserAction.removeContext;
52 else if (index < 0) action = UserAction.cancelSelect;
54 // Execution of the user action
55 Session connex = Database.getSession();
56 Transaction transax = connex.beginTransaction();
59 saveFilter(); // Also reinitializes the form, if needed
61 if (action == UserAction.selectContextType) {
62 done = doSelectContextType();
64 if (action == UserAction.selectContextValue) {
65 done = doAddContext();
67 if (action == UserAction.removeContext) {
68 done = doRemoveContext();
70 if (action == UserAction.cancelSelect) {
73 else { // UserAction.refreshResult
75 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
81 catch (Exception error) {
82 // No need to roll back the transaction as it is read only
83 logger.error("Reason: ", error);
88 @SuppressWarnings("unchecked")
89 protected String doSelectContextType () {
90 // ---------------------------------------
91 SimulationContext.Properties sprop = new SimulationContext.Properties();
93 newtype = SimulationContext.selectType(Integer.valueOf(ctype));
94 newvalue = Database.selectSimulationContextsWhere(sprop.setType(newtype));
95 if (cindex.length() > 0 && Integer.valueOf(cindex) == 0)
96 getSession().remove("search.result");
98 result = (List<Proxy>)getSession().get("search.result"); // We keep the previous result search, if valid
102 protected String doAddContext () {
103 // --------------------------------
104 SimulationContext selected = Database.selectSimulationContext(Integer.valueOf(cvalue));
106 context.add(selected);
107 setContextTypeOptions(getInvolvedContexts()); // Sets critext
108 getSession().remove("search.result"); // The current result is obsolete
112 protected String doRemoveContext () {
113 // -----------------------------------
114 int index = Integer.valueOf(cindex);
115 for (Iterator<SimulationContext> selected=context.iterator(); selected.hasNext();) {
116 if (selected.next().getIndex() == index) {
121 setContextTypeOptions(getInvolvedContexts()); // Sets critext
122 getSession().remove("search.result"); // The current result is obsolete
126 @SuppressWarnings("unchecked")
127 protected String doCancel () {
128 // ----------------------------
129 result = (List<Proxy>)getSession().get("search.result"); // Current result search
130 setContextTypeOptions(getInvolvedContexts()); // Sets critext
134 // ==============================================================================================================================
136 // ==============================================================================================================================
138 public String getAuthor () {
139 // --------------------------
142 public List<Name> getCandidates () {
143 // ----------------------------------
146 public List<SimulationContextType> getContextTypeOptions () {
147 // -----------------------------------------------------------
150 public List<SimulationContext> getContextValueOptions () {
151 // --------------------------------------------------------
154 public SimulationContextType getSelectedContextType () {
155 // ------------------------------------------------------
158 public List<SimulationContext> getSimulationContexts () {
159 // -------------------------------------------------------
162 public List<Proxy> getResult () {
163 // -------------------------------
167 // ==============================================================================================================================
169 // ==============================================================================================================================
171 public void setAuthor (String index) {
172 // ------------------------------------
175 public void setContextType (String type) {
176 // ----------------------------------------
179 public void setContextValue (String value) {
180 // ------------------------------------------
183 public void setContextIndex (String value) {
184 // ------------------------------------------
188 protected void setCandidates () {
189 // -------------------------------
190 manager = new Vector<Name>();
191 List<User> users = UserDirectory.selectAllUsers();
192 User me = getConnectedUser(); // May be null
193 for (Iterator<User> i=users.iterator(); i.hasNext(); ) {
194 User next = i.next();
195 ApplicationRights he = new ApplicationRights(next);
196 if (he.canCreateStudy()) {
197 if (next.equals(me)) manager.add(0, new ValidationFacade.ByManager(me));
198 else manager.add(next);
203 protected void setContextTypeOptions (List<SimulationContextType> critext) {
204 // --------------------------------------------------------------------------
205 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext();) {
206 critext.remove(i.next().getType()); // Already used context type
208 // Ordering by alphabetical order of localized context types
209 SimulationContextType[] types = critext.toArray( new SimulationContextType[critext.size()] );
210 ContextTypeComparator compare = new ContextTypeComparator();
211 ProjectSettingsService.Step step = types[0].getAttachedStep();
214 while (to < types.length-1) {
216 if (types[to].isAttachedTo(step)) continue;
218 if (to > from+1) Arrays.sort(types, from, to, compare);
220 step = types[to].getAttachedStep();
222 if (to > from) Arrays.sort(types, from, to+1, compare);
223 this.critext = Arrays.asList(types);
226 // ==============================================================================================================================
228 // ==============================================================================================================================
230 protected abstract String doSearch () throws InvalidPropertyException;
231 protected abstract List<SimulationContextType> getInvolvedContexts ();
232 protected abstract void loadFilter ();
233 protected abstract void saveFilter ();