]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/AbstractSearchBaseAction.java
Salome HOME
477a447674f4f49b87c30fe37ef50956528bf46f
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / AbstractSearchBaseAction.java
1 package org.splat.simer;
2
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Iterator;
7 import java.util.List;
8
9 import org.splat.dal.bo.kernel.User;
10 import org.splat.dal.bo.som.SimulationContext;
11 import org.splat.dal.bo.som.SimulationContextType;
12 import org.splat.kernel.InvalidPropertyException;
13 import org.splat.kernel.Name;
14 import org.splat.service.SimulationContextService;
15 import org.splat.service.UserService;
16 import org.splat.service.dto.Proxy;
17 import org.splat.service.technical.ProjectSettingsService;
18 import org.splat.som.ApplicationRights;
19
20 /**
21  * Base search action class used for searching studies and knowledge.
22  * 
23  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
24  */
25 public abstract class AbstractSearchBaseAction extends Action {
26
27         /**
28          * Serial version ID.
29          */
30         private static final long serialVersionUID = 7863055790228544510L;
31
32         /**
33          * Search result key in the session.
34          */
35         public static final String RESULT_KEY = "search.result";
36
37         /**
38          * Context type index, when selected.
39          */
40         protected transient String _ctype = null;
41         /**
42          * Context value index, when selected.
43          */
44         protected transient String _cvalue = null;
45         /**
46          * Context index, when removed.
47          */
48         protected transient String _cindex = "";
49         protected String _author = null;
50         protected List<Name> _candidates = null;
51         /**
52          * Context type to be valued.
53          */
54         protected transient SimulationContextType _newtype;
55         /**
56          * Context value to be selected.
57          */
58         protected transient List<SimulationContext> _newvalue;
59         /**
60          * Addable context types.
61          */
62         protected transient List<SimulationContextType> _critext;
63         /**
64          * Current contexts search criteria.
65          */
66         protected transient List<SimulationContext> _context;
67         /**
68          * List of found objects.
69          */
70         protected transient List<Proxy> _result;
71         /**
72          * Injected simulation context service.
73          */
74         private SimulationContextService _simulationContextService;
75
76         /**
77          * Injected user service.
78          */
79         private UserService _userService;
80
81         /**
82          * Search action modes enumeration.
83          */
84         enum UserAction {
85                 refreshResult, selectContextType, selectContextValue, cancelSelect, removeContext
86         }
87
88         // ==============================================================================================================================
89         // Action methods
90         // ==============================================================================================================================
91
92         /**
93          * Perform actions according to the current mode.
94          * 
95          * @return action result or ERROR if failed
96          */
97         public String doSubmitForm() {
98                 // Identification of the user action
99                 UserAction action = UserAction.refreshResult;
100                 if (_ctype != null && Integer.valueOf(_ctype) > 0) {
101                         action = UserAction.selectContextType;
102                 } else if (_cvalue != null && Integer.valueOf(_cvalue) > 0) {
103                         action = UserAction.selectContextValue;
104                 } else if (_cindex.length() > 0) {
105                         int index = Integer.valueOf(_cindex);
106                         if (index > 0) {
107                                 action = UserAction.removeContext;
108                         } else if (index < 0) {
109                                 action = UserAction.cancelSelect;
110                         }
111                 }
112                 // Execution of the user action
113                 String done;
114                 try {
115                         saveFilter(); // Also reinitializes the form, if needed
116
117                         if (action == UserAction.selectContextType) {
118                                 done = doSelectContextType();
119                         } else if (action == UserAction.selectContextValue) {
120                                 done = doAddContext();
121                         } else if (action == UserAction.removeContext) {
122                                 done = doRemoveContext();
123                         } else if (action == UserAction.cancelSelect) {
124                                 done = doCancel();
125                         } else { // UserAction.refreshResult
126                                 done = doSearch();
127                                 setContextTypeOptions(getInvolvedContexts()); // Done in other do functions, when required
128                         }
129                         setCandidates();
130                 } catch (Exception error) {
131                         // No need to roll back the transaction as it is read only
132                         LOG.error("Reason: ", error);
133                         done = ERROR;
134                 }
135                 return done;
136         }
137
138         /**
139          * Add a selected context type to the search filter. Obsolete the current result if necessary.
140          * 
141          * @return "selectype"
142          */
143         @SuppressWarnings("unchecked")
144         protected String doSelectContextType() {
145                 SimulationContext.Properties sprop = new SimulationContext.Properties();
146
147                 _newtype = getSimulationContextService().selectType(
148                                 Integer.valueOf(_ctype));
149                 _newvalue = getSimulationContextService()
150                                 .selectSimulationContextsWhere(sprop.setType(_newtype));
151                 if (_cindex.length() > 0 && Integer.valueOf(_cindex) == 0) {
152                         getSession().remove(RESULT_KEY);
153                 } else {
154                         // We keep the previous result search, if valid
155                         _result = (List<Proxy>) getSession().get(RESULT_KEY);
156                 }
157                 setActionType("setContext");
158                 return "selectype";
159         }
160
161         /**
162          * Add a selected context to the search filter. Obsolete the current result.
163          * 
164          * @return "refresh"
165          */
166         protected String doAddContext() {
167                 SimulationContext selected = getSimulationContextService()
168                                 .selectSimulationContext(Integer.valueOf(_cvalue));
169
170                 _context.add(selected);
171                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
172                 getSession().remove(RESULT_KEY); // The current result is obsolete
173                 return "refresh";
174         }
175
176         /**
177          * Remove context from the search filter.
178          * 
179          * @return "refresh"
180          */
181         protected String doRemoveContext() {
182                 int index = Integer.valueOf(_cindex);
183                 for (Iterator<SimulationContext> selected = _context.iterator(); selected
184                                 .hasNext();) {
185                         if (selected.next().getIndex() == index) {
186                                 selected.remove();
187                                 break;
188                         }
189                 }
190                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
191                 getSession().remove(RESULT_KEY); // The current result is obsolete
192                 return "refresh";
193         }
194
195         /**
196          * 
197          * @return
198          */
199         @SuppressWarnings("unchecked")
200         protected String doCancel() {
201                 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
202                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
203                 return "refresh";
204         }
205
206         // ==============================================================================================================================
207         // Getters
208         // ==============================================================================================================================
209
210         /**
211          * Get date format string.
212          * 
213          * @return date format string
214          */
215         public String getFormat() {
216                 return getText("date.format");
217         }
218
219         /**
220          * Get formatted today date as a string.
221          * 
222          * @return current date as a string
223          */
224         public String getToday() {
225                 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
226                                 getApplicationSettings().getCurrentLocale());
227                 return tostring.format(java.util.Calendar.getInstance().getTime());
228         }
229
230         /**
231          * Get search result state.
232          * 
233          * @return "obsolete" if there is no results in the session, otherwise "uptodate"
234          */
235         public String getResultState() {
236                 String result;
237                 if (getSession().get(RESULT_KEY) == null) {
238                         result = "obsolete";
239                 } else {
240                         result = "uptodate";
241                 }
242                 return result;
243         }
244
245         public String getAuthor() {
246                 return _author;
247         }
248
249         public List<Name> getCandidates() {
250                 return _candidates;
251         }
252
253         public List<SimulationContextType> getContextTypeOptions() {
254                 return _critext;
255         }
256
257         public List<SimulationContext> getContextValueOptions() {
258                 return _newvalue;
259         }
260
261         public SimulationContextType getSelectedContextType() {
262                 return _newtype;
263         }
264
265         public List<SimulationContext> getSimulationContexts() {
266                 return _context;
267         }
268
269         /**
270          * Get list of found objects.
271          * 
272          * @return list of found objects
273          */
274         public List<Proxy> getResult() {
275                 return _result;
276         }
277
278         // ==============================================================================================================================
279         // Setters
280         // ==============================================================================================================================
281
282         public void setAuthor(final String index) {
283                 this._author = index;
284         }
285
286         public void setContextType(final String type) {
287                 this._ctype = type;
288         }
289
290         public void setContextValue(final String value) {
291                 this._cvalue = value;
292         }
293
294         public void setContextIndex(final String value) {
295                 this._cindex = value;
296         }
297
298         protected void setCandidates() {
299                 _candidates = new ArrayList<Name>();
300                 List<User> users = getUserService().selectAllUsers();
301                 User me = getConnectedUser(); // May be null
302                 for (Iterator<User> i = users.iterator(); i.hasNext();) {
303                         User next = i.next();
304                         ApplicationRights he = new ApplicationRights(next);
305                         if (he.canCreateStudy()) {
306                                 if (next.equals(me)) {
307                                         _candidates.add(0, new ValidationFacade.ByManager(me,
308                                                         getApplicationSettings().getCurrentLocale()));
309                                 } else {
310                                         _candidates.add(next);
311                                 }
312                         }
313                 }
314         }
315
316         /**
317          * Build available context types list with localized names.
318          * 
319          * @param critext
320          *            context types already used in the search filter
321          */
322         protected void setContextTypeOptions(
323                         final List<SimulationContextType> critext) {
324                 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
325                         critext.remove(i.next().getType()); // Already used context type
326                 }
327                 // Ordering by alphabetical order of localized context types
328                 SimulationContextType[] types = critext
329                                 .toArray(new SimulationContextType[critext.size()]);
330                 ContextTypeComparator compare = new ContextTypeComparator();
331                 ProjectSettingsService.Step step = getSimulationContextService()
332                                 .getAttachedStep(types[0]);
333                 int from = 0;
334                 int to = 0;
335                 while (to < types.length - 1) {
336                         to += 1;
337                         if (types[to].isAttachedTo(step)) {
338                                 continue;
339                         }
340
341                         if (to > from + 1) {
342                                 Arrays.sort(types, from, to, compare);
343                         }
344                         from = to;
345                         step = getSimulationContextService().getAttachedStep(types[to]);
346                 }
347                 if (to > from) {
348                         Arrays.sort(types, from, to + 1, compare);
349                 }
350                 this._critext = Arrays.asList(types);
351         }
352
353         // ==============================================================================================================================
354         // Abstract services
355         // ==============================================================================================================================
356
357         protected abstract String doSearch() throws InvalidPropertyException;
358
359         protected abstract List<SimulationContextType> getInvolvedContexts();
360
361         protected abstract void loadFilter();
362
363         protected abstract void saveFilter();
364
365         /**
366          * Get the simulationContextService.
367          * 
368          * @return the simulationContextService
369          */
370         public SimulationContextService getSimulationContextService() {
371                 return _simulationContextService;
372         }
373
374         /**
375          * Set the simulationContextService.
376          * 
377          * @param simulationContextService
378          *            the simulationContextService to set
379          */
380         public void setSimulationContextService(
381                         final SimulationContextService simulationContextService) {
382                 _simulationContextService = simulationContextService;
383         }
384
385         /**
386          * Get the userService.
387          * 
388          * @return the userService
389          */
390         public UserService getUserService() {
391                 return _userService;
392         }
393
394         /**
395          * Set the userService.
396          * 
397          * @param userService
398          *            the userService to set
399          */
400         public void setUserService(final UserService userService) {
401                 _userService = userService;
402         }
403 }