]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/SearchBaseAction.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[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.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;
21
22 public abstract class SearchBaseAction extends Action {
23
24         /**
25          * Serial version ID.
26          */
27         private static final long serialVersionUID = 7863055790228544510L;
28
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;
39         /**
40          * Injected simulation context service.
41          */
42         private SimulationContextService _simulationContextService;
43
44         enum UserAction {
45                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
46         }
47
48         // ==============================================================================================================================
49         // Action methods
50         // ==============================================================================================================================
51
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);
62                         if (index > 0)
63                                 action = UserAction.removeContext;
64                         else if (index < 0)
65                                 action = UserAction.cancelSelect;
66                 }
67                 // Execution of the user action
68                 Session connex = Database.getSession();
69                 Transaction transax = connex.beginTransaction();
70                 String done;
71                 try {
72                         saveFilter(); // Also reinitializes the form, if needed
73
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) {
81                                 done = doCancel();
82                         } else { // UserAction.refreshResult
83                                 done = doSearch();
84                                 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
85                         }
86                         setCandidates();
87                         transax.commit();
88                         return done;
89                 } catch (Exception error) {
90                         // No need to roll back the transaction as it is read only
91                         logger.error("Reason: ", error);
92                         return ERROR;
93                 }
94         }
95
96         @SuppressWarnings("unchecked")
97         protected String doSelectContextType() {
98                 // ---------------------------------------
99                 SimulationContext.Properties sprop = new SimulationContext.Properties();
100
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");
106                 else
107                         result = (List<Proxy>) getSession().get("search.result"); // We keep the previous result search, if valid
108                 return "selectype";
109         }
110
111         protected String doAddContext() {
112                 // --------------------------------
113                 SimulationContext selected = getSimulationContextService()
114                                 .selectSimulationContext(Integer.valueOf(cvalue));
115
116                 context.add(selected);
117                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
118                 getSession().remove("search.result"); // The current result is obsolete
119                 return "refresh";
120         }
121
122         protected String doRemoveContext() {
123                 // -----------------------------------
124                 int index = Integer.valueOf(cindex);
125                 for (Iterator<SimulationContext> selected = context.iterator(); selected
126                                 .hasNext();) {
127                         if (selected.next().getIndex() == index) {
128                                 selected.remove();
129                                 break;
130                         }
131                 }
132                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
133                 getSession().remove("search.result"); // The current result is obsolete
134                 return "refresh";
135         }
136
137         @SuppressWarnings("unchecked")
138         protected String doCancel() {
139                 // ----------------------------
140                 result = (List<Proxy>) getSession().get("search.result"); // Current result search
141                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
142                 return "refresh";
143         }
144
145         // ==============================================================================================================================
146         // Getters
147         // ==============================================================================================================================
148
149         public String getAuthor() {
150                 // --------------------------
151                 return author;
152         }
153
154         public List<Name> getCandidates() {
155                 // ----------------------------------
156                 return manager;
157         }
158
159         public List<SimulationContextType> getContextTypeOptions() {
160                 // -----------------------------------------------------------
161                 return critext;
162         }
163
164         public List<SimulationContext> getContextValueOptions() {
165                 // --------------------------------------------------------
166                 return newvalue;
167         }
168
169         public SimulationContextType getSelectedContextType() {
170                 // ------------------------------------------------------
171                 return newtype;
172         }
173
174         public List<SimulationContext> getSimulationContexts() {
175                 // -------------------------------------------------------
176                 return context;
177         }
178
179         public List<Proxy> getResult() {
180                 // -------------------------------
181                 return result;
182         }
183
184         // ==============================================================================================================================
185         // Setters
186         // ==============================================================================================================================
187
188         public void setAuthor(String index) {
189                 // ------------------------------------
190                 this.author = index;
191         }
192
193         public void setContextType(String type) {
194                 // ----------------------------------------
195                 this.ctype = type;
196         }
197
198         public void setContextValue(String value) {
199                 // ------------------------------------------
200                 this.cvalue = value;
201         }
202
203         public void setContextIndex(String value) {
204                 // ------------------------------------------
205                 this.cindex = value;
206         }
207
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()) {
217                                 if (next.equals(me))
218                                         manager.add(0, new ValidationFacade.ByManager(me));
219                                 else
220                                         manager.add(next);
221                         }
222                 }
223         }
224
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
229                 }
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();
235                 int from = 0;
236                 int to = 0;
237                 while (to < types.length - 1) {
238                         to += 1;
239                         if (types[to].isAttachedTo(step))
240                                 continue;
241
242                         if (to > from + 1)
243                                 Arrays.sort(types, from, to, compare);
244                         from = to;
245                         step = types[to].getAttachedStep();
246                 }
247                 if (to > from)
248                         Arrays.sort(types, from, to + 1, compare);
249                 this.critext = Arrays.asList(types);
250         }
251
252         // ==============================================================================================================================
253         // Abstract services
254         // ==============================================================================================================================
255
256         protected abstract String doSearch() throws InvalidPropertyException;
257
258         protected abstract List<SimulationContextType> getInvolvedContexts();
259
260         protected abstract void loadFilter();
261
262         protected abstract void saveFilter();
263
264         /**
265          * Get the simulationContextService.
266          * 
267          * @return the simulationContextService
268          */
269         public SimulationContextService getSimulationContextService() {
270                 return _simulationContextService;
271         }
272
273         /**
274          * Set the simulationContextService.
275          * 
276          * @param simulationContextService
277          *            the simulationContextService to set
278          */
279         public void setSimulationContextService(
280                         SimulationContextService simulationContextService) {
281                 _simulationContextService = simulationContextService;
282         }
283 }