Salome HOME
XML comment is fixed
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / SearchBaseAction.java
1 package org.splat.simer;
2
3 import java.util.Arrays;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Vector;
7
8 import org.hibernate.Session;
9 import org.hibernate.Transaction;
10 import org.splat.kernel.InvalidPropertyException;
11 import org.splat.kernel.User;
12 import org.splat.kernel.UserDirectory;
13 import org.splat.som.ApplicationRights;
14 import org.splat.som.Database;
15 import org.splat.som.ProjectSettings;
16 import org.splat.som.Proxy;
17 import org.splat.som.SimulationContext;
18 import org.splat.som.SimulationContextType;
19
20
21 public abstract class SearchBaseAction extends Action {
22
23         protected String                      ctype    = null;   // Context type  index, when selected
24         protected String                      cvalue   = null;   // Context value index, when selected
25         protected String                      cindex   = "";     // Context index, when removed
26     protected String                      newarea  = null;   // Selected repository area
27     protected String                      author  = null;
28     protected List<User>                  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;
34
35         private static final long   serialVersionUID = 7863055790228544510L;
36
37     enum UserAction { refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext }
38
39 //  ==============================================================================================================================
40 //  Action methods
41 //  ==============================================================================================================================
42
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;
53       }
54 //  Execution of the user action
55       Session      connex  = Database.getSession();
56       Transaction  transax = connex.beginTransaction();
57       String       done;
58       try {
59         saveFilter();                                      // Also reinitializes the form, if needed
60
61         if (action == UserAction.selectContextType) {
62           done = doSelectContextType();
63         } else
64         if (action == UserAction.selectContextValue) {
65           done = doAddContext();
66         } else
67         if (action == UserAction.removeContext) {
68           done = doRemoveContext();
69         } else
70         if (action == UserAction.cancelSelect) {
71           done = doCancel();
72         }
73         else {     // UserAction.refreshResult
74           done = doSearch();
75           setContextTypeOptions(getInvolvedContexts());    // Done in other do functions, when required
76         }
77         setCandidates();
78         transax.commit();
79         return done;
80       }
81       catch (Exception error) {
82 //      No need to roll back the transaction as it is read only
83         logger.error("Reason: ", error);
84         return ERROR;
85       }
86         }
87
88     @SuppressWarnings("unchecked")
89         protected String doSelectContextType () {
90 //  ---------------------------------------
91       SimulationContext.Properties  sprop = new SimulationContext.Properties();
92
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");
97       else
98         result = (List<Proxy>)getSession().get("search.result");   // We keep the previous result search, if valid
99       return "selectype";
100     }
101
102     protected String doAddContext () {
103 //  --------------------------------
104       SimulationContext  selected = Database.selectSimulationContext(Integer.valueOf(cvalue));
105
106       context.add(selected);
107       setContextTypeOptions(getInvolvedContexts());                // Sets critext
108       getSession().remove("search.result");                        // The current result is obsolete
109       return "refresh";
110     }
111
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) {
117           selected.remove();
118           break;
119         }
120       }
121       setContextTypeOptions(getInvolvedContexts());                // Sets critext
122       getSession().remove("search.result");                        // The current result is obsolete
123       return "refresh";
124     }
125
126     @SuppressWarnings("unchecked")
127         protected String doCancel () {
128 //  ----------------------------
129       result = (List<Proxy>)getSession().get("search.result");     // Current result search
130       setContextTypeOptions(getInvolvedContexts());                // Sets critext
131       return "refresh";
132     }
133
134 //  ==============================================================================================================================
135 //  Getters
136 //  ==============================================================================================================================
137
138     public String getAuthor () {
139 //  --------------------------
140       return author;
141     }
142     public List<User> getCandidates () {
143 //  ----------------------------------
144       return manager;
145     }
146     public List<SimulationContextType> getContextTypeOptions () {
147 //  -----------------------------------------------------------
148       return critext;
149     }
150     public List<SimulationContext> getContextValueOptions () {
151 //  --------------------------------------------------------
152       return newvalue;
153     }
154     public SimulationContextType getSelectedContextType () {
155 //  ------------------------------------------------------
156       return newtype;
157     }
158     public List<SimulationContext> getSimulationContexts () {
159 //  -------------------------------------------------------
160       return context;
161     }
162     public List<Proxy> getResult () {
163 //  -------------------------------
164       return result;
165     }
166
167 //  ==============================================================================================================================
168 //  Setters
169 //  ==============================================================================================================================
170     
171     public void setArea (String name) {
172 //  ---------------------------------
173       this.newarea = name;
174     }
175     public void setAuthor (String index) {
176 //  ------------------------------------
177       this.author = index;
178     }
179     public void setContextType (String type) {
180 //  ----------------------------------------
181       this.ctype = type;
182     }
183     public void setContextValue (String value) {
184 //  ------------------------------------------
185       this.cvalue = value;
186     }
187     public void setContextIndex (String value) {
188 //  ------------------------------------------
189       this.cindex = value;
190     }
191
192     protected void setCandidates () {
193 //  -------------------------------
194       manager = new Vector<User>();
195       List<User> users = UserDirectory.selectAllUsers();
196       User       me    = getConnectedUser();        // May be null
197       for (Iterator<User> i=users.iterator(); i.hasNext(); ) {
198         User next = i.next();
199         if ( next.equals(me) ) continue;
200         ApplicationRights  he = new ApplicationRights(next);
201         if (he.canCreateStudy()) manager.add(next);
202       }
203     }
204
205     protected void setContextTypeOptions (List<SimulationContextType> critext) {
206 //  --------------------------------------------------------------------------
207       for (Iterator<SimulationContext> i=context.iterator(); i.hasNext();) {
208         critext.remove(i.next().getType());                        // Already used context type
209       }
210 //    Ordering by alphabetical order of localized context types
211       SimulationContextType[] types   = critext.toArray( new SimulationContextType[critext.size()] );
212       ContextTypeComparator   compare = new ContextTypeComparator();
213       ProjectSettings.Step    step    = types[0].getAttachedStep();
214       int  from = 0;
215       int  to   = 0;
216       while (to < types.length-1) {
217         to += 1;
218         if (types[to].isAttachedTo(step)) continue;
219
220         if (to > from+1) Arrays.sort(types, from, to, compare);
221         from = to;
222         step = types[to].getAttachedStep();
223       }
224       if (to > from) Arrays.sort(types, from, to+1, compare);
225       this.critext  =  Arrays.asList(types);
226     }
227
228 //  ==============================================================================================================================
229 //  Abstract services
230 //  ==============================================================================================================================
231
232         protected abstract String                      doSearch () throws InvalidPropertyException;
233         protected abstract List<SimulationContextType> getInvolvedContexts ();
234         protected abstract void                        loadFilter ();
235         protected abstract void                        saveFilter ();
236 }