Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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.som.ApplicationRights;
14 import org.splat.dal.dao.som.Database;
15 import org.splat.service.SimulationContextService;
16 import org.splat.service.UserService;
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         /**
45          * Injected user service.
46          */
47         private UserService _userService;
48
49         enum UserAction {
50                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
51         }
52
53         // ==============================================================================================================================
54         // Action methods
55         // ==============================================================================================================================
56
57         public String doSubmitForm() {
58                 // -----------------------------
59                 // Identification of the user action
60                 UserAction action = UserAction.refreshResult;
61                 if (ctype != null && Integer.valueOf(ctype) > 0)
62                         action = UserAction.selectContextType;
63                 else if (cvalue != null && Integer.valueOf(cvalue) > 0)
64                         action = UserAction.selectContextValue;
65                 else if (cindex.length() > 0) {
66                         int index = Integer.valueOf(cindex);
67                         if (index > 0)
68                                 action = UserAction.removeContext;
69                         else if (index < 0)
70                                 action = UserAction.cancelSelect;
71                 }
72                 // Execution of the user action
73                 Session connex = Database.getCurSession();
74                 Transaction transax = connex.beginTransaction();
75                 String done;
76                 try {
77                         saveFilter(); // Also reinitializes the form, if needed
78
79                         if (action == UserAction.selectContextType) {
80                                 done = doSelectContextType();
81                         } else if (action == UserAction.selectContextValue) {
82                                 done = doAddContext();
83                         } else if (action == UserAction.removeContext) {
84                                 done = doRemoveContext();
85                         } else if (action == UserAction.cancelSelect) {
86                                 done = doCancel();
87                         } else { // UserAction.refreshResult
88                                 done = doSearch();
89                                 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
90                         }
91                         setCandidates();
92                         transax.commit();
93                         return done;
94                 } catch (Exception error) {
95                         // No need to roll back the transaction as it is read only
96                         logger.error("Reason: ", error);
97                         return ERROR;
98                 }
99         }
100
101         @SuppressWarnings("unchecked")
102         protected String doSelectContextType() {
103                 // ---------------------------------------
104                 SimulationContext.Properties sprop = new SimulationContext.Properties();
105
106                 newtype = getSimulationContextService().selectType(
107                                 Integer.valueOf(ctype));
108                 newvalue = getSimulationContextService().selectSimulationContextsWhere(
109                                 sprop.setType(newtype));
110                 if (cindex.length() > 0 && Integer.valueOf(cindex) == 0)
111                         getSession().remove("search.result");
112                 else
113                         result = (List<Proxy>) getSession().get("search.result"); // We keep the previous result search, if valid
114                 return "selectype";
115         }
116
117         protected String doAddContext() {
118                 // --------------------------------
119                 SimulationContext selected = getSimulationContextService()
120                                 .selectSimulationContext(Integer.valueOf(cvalue));
121
122                 context.add(selected);
123                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
124                 getSession().remove("search.result"); // The current result is obsolete
125                 return "refresh";
126         }
127
128         protected String doRemoveContext() {
129                 // -----------------------------------
130                 int index = Integer.valueOf(cindex);
131                 for (Iterator<SimulationContext> selected = context.iterator(); selected
132                                 .hasNext();) {
133                         if (selected.next().getIndex() == index) {
134                                 selected.remove();
135                                 break;
136                         }
137                 }
138                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
139                 getSession().remove("search.result"); // The current result is obsolete
140                 return "refresh";
141         }
142
143         @SuppressWarnings("unchecked")
144         protected String doCancel() {
145                 // ----------------------------
146                 result = (List<Proxy>) getSession().get("search.result"); // Current result search
147                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
148                 return "refresh";
149         }
150
151         // ==============================================================================================================================
152         // Getters
153         // ==============================================================================================================================
154
155         public String getAuthor() {
156                 // --------------------------
157                 return author;
158         }
159
160         public List<Name> getCandidates() {
161                 // ----------------------------------
162                 return manager;
163         }
164
165         public List<SimulationContextType> getContextTypeOptions() {
166                 // -----------------------------------------------------------
167                 return critext;
168         }
169
170         public List<SimulationContext> getContextValueOptions() {
171                 // --------------------------------------------------------
172                 return newvalue;
173         }
174
175         public SimulationContextType getSelectedContextType() {
176                 // ------------------------------------------------------
177                 return newtype;
178         }
179
180         public List<SimulationContext> getSimulationContexts() {
181                 // -------------------------------------------------------
182                 return context;
183         }
184
185         public List<Proxy> getResult() {
186                 // -------------------------------
187                 return result;
188         }
189
190         // ==============================================================================================================================
191         // Setters
192         // ==============================================================================================================================
193
194         public void setAuthor(String index) {
195                 // ------------------------------------
196                 this.author = index;
197         }
198
199         public void setContextType(String type) {
200                 // ----------------------------------------
201                 this.ctype = type;
202         }
203
204         public void setContextValue(String value) {
205                 // ------------------------------------------
206                 this.cvalue = value;
207         }
208
209         public void setContextIndex(String value) {
210                 // ------------------------------------------
211                 this.cindex = value;
212         }
213
214         protected void setCandidates() {
215                 // -------------------------------
216                 manager = new Vector<Name>();
217                 List<User> users = getUserService().selectAllUsers();
218                 User me = getConnectedUser(); // May be null
219                 for (Iterator<User> i = users.iterator(); i.hasNext();) {
220                         User next = i.next();
221                         ApplicationRights he = new ApplicationRights(next);
222                         if (he.canCreateStudy()) {
223                                 if (next.equals(me))
224                                         manager.add(0, new ValidationFacade.ByManager(me));
225                                 else
226                                         manager.add(next);
227                         }
228                 }
229         }
230
231         protected void setContextTypeOptions(List<SimulationContextType> critext) {
232                 // --------------------------------------------------------------------------
233                 for (Iterator<SimulationContext> i = context.iterator(); i.hasNext();) {
234                         critext.remove(i.next().getType()); // Already used context type
235                 }
236                 // Ordering by alphabetical order of localized context types
237                 SimulationContextType[] types = critext
238                                 .toArray(new SimulationContextType[critext.size()]);
239                 ContextTypeComparator compare = new ContextTypeComparator();
240                 ProjectSettingsService.Step step = getSimulationContextService()
241                                 .getAttachedStep(types[0]);
242                 int from = 0;
243                 int to = 0;
244                 while (to < types.length - 1) {
245                         to += 1;
246                         if (types[to].isAttachedTo(step))
247                                 continue;
248
249                         if (to > from + 1)
250                                 Arrays.sort(types, from, to, compare);
251                         from = to;
252                         step = getSimulationContextService().getAttachedStep(types[to]);
253                 }
254                 if (to > from)
255                         Arrays.sort(types, from, to + 1, compare);
256                 this.critext = Arrays.asList(types);
257         }
258
259         // ==============================================================================================================================
260         // Abstract services
261         // ==============================================================================================================================
262
263         protected abstract String doSearch() throws InvalidPropertyException;
264
265         protected abstract List<SimulationContextType> getInvolvedContexts();
266
267         protected abstract void loadFilter();
268
269         protected abstract void saveFilter();
270
271         /**
272          * Get the simulationContextService.
273          * 
274          * @return the simulationContextService
275          */
276         public SimulationContextService getSimulationContextService() {
277                 return _simulationContextService;
278         }
279
280         /**
281          * Set the simulationContextService.
282          * 
283          * @param simulationContextService
284          *            the simulationContextService to set
285          */
286         public void setSimulationContextService(
287                         SimulationContextService simulationContextService) {
288                 _simulationContextService = simulationContextService;
289         }
290
291         /**
292          * Get the userService.
293          * @return the userService
294          */
295         public UserService getUserService() {
296                 return _userService;
297         }
298
299         /**
300          * Set the userService.
301          * @param userService the userService to set
302          */
303         public void setUserService(UserService userService) {
304                 _userService = userService;
305         }
306 }