]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/SearchBaseAction.java
Salome HOME
6258bbf24fa6a1fbf12f0b92681d32a8fc5ecf8b
[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.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;
20
21
22 public abstract class SearchBaseAction extends Action {
23
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;
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<Name> 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 setAuthor (String index) {
172 //  ------------------------------------
173       this.author = index;
174     }
175     public void setContextType (String type) {
176 //  ----------------------------------------
177       this.ctype = type;
178     }
179     public void setContextValue (String value) {
180 //  ------------------------------------------
181       this.cvalue = value;
182     }
183     public void setContextIndex (String value) {
184 //  ------------------------------------------
185       this.cindex = value;
186     }
187
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);
199         }
200       }
201     }
202
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
207       }
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();
212       int  from = 0;
213       int  to   = 0;
214       while (to < types.length-1) {
215         to += 1;
216         if (types[to].isAttachedTo(step)) continue;
217
218         if (to > from+1) Arrays.sort(types, from, to, compare);
219         from = to;
220         step = types[to].getAttachedStep();
221       }
222       if (to > from) Arrays.sort(types, from, to+1, compare);
223       this.critext  =  Arrays.asList(types);
224     }
225
226 //  ==============================================================================================================================
227 //  Abstract services
228 //  ==============================================================================================================================
229
230         protected abstract String                      doSearch () throws InvalidPropertyException;
231         protected abstract List<SimulationContextType> getInvolvedContexts ();
232         protected abstract void                        loadFilter ();
233         protected abstract void                        saveFilter ();
234 }