]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/AbstractSearchBaseAction.java
Salome HOME
Modifications to respect PMD rules.
[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().selectSimulationContextsWhere(
128                                 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                                 } else {
231                                         _candidates.add(next);
232                                 }
233                         }
234                 }
235         }
236
237         protected void setContextTypeOptions(final List<SimulationContextType> critext) {
238                 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
239                         critext.remove(i.next().getType()); // Already used context type
240                 }
241                 // Ordering by alphabetical order of localized context types
242                 SimulationContextType[] types = critext
243                                 .toArray(new SimulationContextType[critext.size()]);
244                 ContextTypeComparator compare = new ContextTypeComparator();
245                 ProjectSettingsService.Step step = getSimulationContextService()
246                                 .getAttachedStep(types[0]);
247                 int from = 0;
248                 int to = 0;
249                 while (to < types.length - 1) {
250                         to += 1;
251                         if (types[to].isAttachedTo(step)) {
252                                 continue;
253                         }
254
255                         if (to > from + 1) {
256                                 Arrays.sort(types, from, to, compare);
257                         }
258                         from = to;
259                         step = getSimulationContextService().getAttachedStep(types[to]);
260                 }
261                 if (to > from) {
262                         Arrays.sort(types, from, to + 1, compare);
263                 }
264                 this._critext = Arrays.asList(types);
265         }
266
267         // ==============================================================================================================================
268         // Abstract services
269         // ==============================================================================================================================
270
271         protected abstract String doSearch() throws InvalidPropertyException;
272
273         protected abstract List<SimulationContextType> getInvolvedContexts();
274
275         protected abstract void loadFilter();
276
277         protected abstract void saveFilter();
278
279         /**
280          * Get the simulationContextService.
281          * 
282          * @return the simulationContextService
283          */
284         public SimulationContextService getSimulationContextService() {
285                 return _simulationContextService;
286         }
287
288         /**
289          * Set the simulationContextService.
290          * 
291          * @param simulationContextService
292          *            the simulationContextService to set
293          */
294         public void setSimulationContextService(
295                         final SimulationContextService simulationContextService) {
296                 _simulationContextService = simulationContextService;
297         }
298
299         /**
300          * Get the userService.
301          * @return the userService
302          */
303         public UserService getUserService() {
304                 return _userService;
305         }
306
307         /**
308          * Set the userService.
309          * @param userService the userService to set
310          */
311         public void setUserService(final UserService userService) {
312                 _userService = userService;
313         }
314 }