Salome HOME
Rename the scenario functionality is implemented
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / AbstractSearchBaseAction.java
1 package org.splat.simer;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.splat.dal.bo.kernel.User;
9 import org.splat.dal.bo.som.SimulationContext;
10 import org.splat.dal.bo.som.SimulationContextType;
11 import org.splat.kernel.InvalidPropertyException;
12 import org.splat.kernel.Name;
13 import org.splat.service.SimulationContextService;
14 import org.splat.service.UserService;
15 import org.splat.service.dto.Proxy;
16 import org.splat.service.technical.ProjectSettingsService;
17 import org.splat.som.ApplicationRights;
18
19 public abstract class AbstractSearchBaseAction extends Action {
20
21         /**
22          * Serial version ID.
23          */
24         private static final long serialVersionUID = 7863055790228544510L;
25
26         /**
27          * Search result key in the session.
28          */
29         public static final String RESULT_KEY = "search.result";
30
31         /**
32          * Context type index, when selected.
33          */
34         protected transient String _ctype = null;
35         /**
36          * Context value index, when selected.
37          */
38         protected transient String _cvalue = null;
39         /**
40          * Context index, when removed.
41          */
42         protected transient String _cindex = "";
43         protected String _author = null;
44         protected List<Name> _candidates = null;
45         /**
46          * Context type to be valued.
47          */
48         protected transient SimulationContextType _newtype;
49         /**
50          * Context value to be selected.
51          */
52         protected transient List<SimulationContext> _newvalue;
53         /**
54          * Addable context types.
55          */
56         protected transient List<SimulationContextType> _critext;
57         /**
58          * Current contexts search criteria.
59          */
60         protected transient List<SimulationContext> _context;
61         protected transient List<Proxy> _result;
62         /**
63          * Injected simulation context service.
64          */
65         private SimulationContextService _simulationContextService;
66
67         /**
68          * Injected user service.
69          */
70         private UserService _userService;
71
72         enum UserAction {
73                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
74         }
75
76         // ==============================================================================================================================
77         // Action methods
78         // ==============================================================================================================================
79
80         public String doSubmitForm() {
81                 // Identification of the user action
82                 UserAction action = UserAction.refreshResult;
83                 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
84                         action = UserAction.selectContextType;
85                 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
86                         action = UserAction.selectContextValue;
87                 } else if (_cindex.length() > 0) {
88                         int index = Integer.valueOf(_cindex);
89                         if (index > 0) {
90                                 action = UserAction.removeContext;
91                         } else if (index < 0) {
92                                 action = UserAction.cancelSelect;
93                         }
94                 }
95                 // Execution of the user action
96                 String done;
97                 try {
98                         saveFilter(); // Also reinitializes the form, if needed
99
100                         if (action == UserAction.selectContextType) {
101                                 done = doSelectContextType();
102                         } else if (action == UserAction.selectContextValue) {
103                                 done = doAddContext();
104                         } else if (action == UserAction.removeContext) {
105                                 done = doRemoveContext();
106                         } else if (action == UserAction.cancelSelect) {
107                                 done = doCancel();
108                         } else { // UserAction.refreshResult
109                                 done = doSearch();
110                                 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
111                         }
112                         setCandidates();
113                 } catch (Exception error) {
114                         // No need to roll back the transaction as it is read only
115                         LOG.error("Reason: ", error);
116                         done = ERROR;
117                 }
118                 return done;
119         }
120
121         @SuppressWarnings("unchecked")
122         protected String doSelectContextType() {
123                 SimulationContext.Properties sprop = new SimulationContext.Properties();
124
125                 _newtype = getSimulationContextService().selectType(
126                                 Integer.valueOf(_ctype));
127                 _newvalue = getSimulationContextService()
128                                 .selectSimulationContextsWhere(sprop.setType(_newtype));
129                 if (_cindex.length() > 0 && Integer.valueOf(_cindex) == 0) {
130                         getSession().remove(RESULT_KEY);
131                 } else {
132                         _result = (List<Proxy>) getSession().get(RESULT_KEY); // We keep the previous result search, if valid
133                 }
134                 return "selectype";
135         }
136
137         protected String doAddContext() {
138                 SimulationContext selected = getSimulationContextService()
139                                 .selectSimulationContext(Integer.valueOf(_cvalue));
140
141                 _context.add(selected);
142                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
143                 getSession().remove(RESULT_KEY); // The current result is obsolete
144                 return "refresh";
145         }
146
147         protected String doRemoveContext() {
148                 int index = Integer.valueOf(_cindex);
149                 for (Iterator<SimulationContext> selected = _context.iterator(); selected
150                                 .hasNext();) {
151                         if (selected.next().getIndex() == index) {
152                                 selected.remove();
153                                 break;
154                         }
155                 }
156                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
157                 getSession().remove(RESULT_KEY); // The current result is obsolete
158                 return "refresh";
159         }
160
161         @SuppressWarnings("unchecked")
162         protected String doCancel() {
163                 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
164                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
165                 return "refresh";
166         }
167
168         // ==============================================================================================================================
169         // Getters
170         // ==============================================================================================================================
171
172         public String getAuthor() {
173                 return _author;
174         }
175
176         public List<Name> getCandidates() {
177                 return _candidates;
178         }
179
180         public List<SimulationContextType> getContextTypeOptions() {
181                 return _critext;
182         }
183
184         public List<SimulationContext> getContextValueOptions() {
185                 return _newvalue;
186         }
187
188         public SimulationContextType getSelectedContextType() {
189                 return _newtype;
190         }
191
192         public List<SimulationContext> getSimulationContexts() {
193                 return _context;
194         }
195
196         public List<Proxy> getResult() {
197                 return _result;
198         }
199
200         // ==============================================================================================================================
201         // Setters
202         // ==============================================================================================================================
203
204         public void setAuthor(final String index) {
205                 this._author = index;
206         }
207
208         public void setContextType(final String type) {
209                 this._ctype = type;
210         }
211
212         public void setContextValue(final String value) {
213                 this._cvalue = value;
214         }
215
216         public void setContextIndex(final String value) {
217                 this._cindex = value;
218         }
219
220         protected void setCandidates() {
221                 _candidates = new ArrayList<Name>();
222                 List<User> users = getUserService().selectAllUsers();
223                 User me = getConnectedUser(); // May be null
224                 for (Iterator<User> i = users.iterator(); i.hasNext();) {
225                         User next = i.next();
226                         ApplicationRights he = new ApplicationRights(next);
227                         if (he.canCreateStudy()) {
228                                 if (next.equals(me)) {
229                                         _candidates.add(0, new ValidationFacade.ByManager(me,
230                                                         getApplicationSettings().getCurrentLocale()));
231                                 } else {
232                                         _candidates.add(next);
233                                 }
234                         }
235                 }
236         }
237
238         protected void setContextTypeOptions(
239                         final List<SimulationContextType> critext) {
240                 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
241                         critext.remove(i.next().getType()); // Already used context type
242                 }
243                 // Ordering by alphabetical order of localized context types
244                 SimulationContextType[] types = critext
245                                 .toArray(new SimulationContextType[critext.size()]);
246                 ContextTypeComparator compare = new ContextTypeComparator();
247                 ProjectSettingsService.Step step = getSimulationContextService()
248                                 .getAttachedStep(types[0]);
249                 int from = 0;
250                 int to = 0;
251                 while (to < types.length - 1) {
252                         to += 1;
253                         if (types[to].isAttachedTo(step)) {
254                                 continue;
255                         }
256
257                         if (to > from + 1) {
258                                 Arrays.sort(types, from, to, compare);
259                         }
260                         from = to;
261                         step = getSimulationContextService().getAttachedStep(types[to]);
262                 }
263                 if (to > from) {
264                         Arrays.sort(types, from, to + 1, compare);
265                 }
266                 this._critext = Arrays.asList(types);
267         }
268
269         // ==============================================================================================================================
270         // Abstract services
271         // ==============================================================================================================================
272
273         protected abstract String doSearch() throws InvalidPropertyException;
274
275         protected abstract List<SimulationContextType> getInvolvedContexts();
276
277         protected abstract void loadFilter();
278
279         protected abstract void saveFilter();
280
281         /**
282          * Get the simulationContextService.
283          * 
284          * @return the simulationContextService
285          */
286         public SimulationContextService getSimulationContextService() {
287                 return _simulationContextService;
288         }
289
290         /**
291          * Set the simulationContextService.
292          * 
293          * @param simulationContextService
294          *            the simulationContextService to set
295          */
296         public void setSimulationContextService(
297                         final SimulationContextService simulationContextService) {
298                 _simulationContextService = simulationContextService;
299         }
300
301         /**
302          * Get the userService.
303          * 
304          * @return the userService
305          */
306         public UserService getUserService() {
307                 return _userService;
308         }
309
310         /**
311          * Set the userService.
312          * 
313          * @param userService
314          *            the userService to set
315          */
316         public void setUserService(final UserService userService) {
317                 _userService = userService;
318         }
319 }