]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/AbstractSearchBaseAction.java
Salome HOME
276ca13387f43b0d8268f37304f6298cc869dd0f
[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                 return "selectype";
158         }
159
160         /**
161          * Add a selected context to the search filter. Obsolete the current result.
162          * 
163          * @return "refresh"
164          */
165         protected String doAddContext() {
166                 SimulationContext selected = getSimulationContextService()
167                                 .selectSimulationContext(Integer.valueOf(_cvalue));
168
169                 _context.add(selected);
170                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
171                 getSession().remove(RESULT_KEY); // The current result is obsolete
172                 return "refresh";
173         }
174
175         /**
176          * Remove context from the search filter.
177          * 
178          * @return "refresh"
179          */
180         protected String doRemoveContext() {
181                 int index = Integer.valueOf(_cindex);
182                 for (Iterator<SimulationContext> selected = _context.iterator(); selected
183                                 .hasNext();) {
184                         if (selected.next().getIndex() == index) {
185                                 selected.remove();
186                                 break;
187                         }
188                 }
189                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
190                 getSession().remove(RESULT_KEY); // The current result is obsolete
191                 return "refresh";
192         }
193
194         /**
195          * 
196          * @return
197          */
198         @SuppressWarnings("unchecked")
199         protected String doCancel() {
200                 _result = (List<Proxy>) getSession().get(RESULT_KEY); // Current result search
201                 setContextTypeOptions(getInvolvedContexts()); // Sets critext
202                 return "refresh";
203         }
204
205         // ==============================================================================================================================
206         // Getters
207         // ==============================================================================================================================
208
209         /**
210          * Get date format string.
211          * 
212          * @return date format string
213          */
214         public String getFormat() {
215                 return getText("date.format");
216         }
217
218         /**
219          * Get formatted today date as a string.
220          * 
221          * @return current date as a string
222          */
223         public String getToday() {
224                 SimpleDateFormat tostring = new SimpleDateFormat(getFormat(),
225                                 getApplicationSettings().getCurrentLocale());
226                 return tostring.format(java.util.Calendar.getInstance().getTime());
227         }
228
229         /**
230          * Get search result state.
231          * 
232          * @return "obsolete" if there is no results in the session, otherwise "uptodate"
233          */
234         public String getResultState() {
235                 String result;
236                 if (getSession().get(RESULT_KEY) == null) {
237                         result = "obsolete";
238                 } else {
239                         result = "uptodate";
240                 }
241                 return result;
242         }
243
244         public String getAuthor() {
245                 return _author;
246         }
247
248         public List<Name> getCandidates() {
249                 return _candidates;
250         }
251
252         public List<SimulationContextType> getContextTypeOptions() {
253                 return _critext;
254         }
255
256         public List<SimulationContext> getContextValueOptions() {
257                 return _newvalue;
258         }
259
260         public SimulationContextType getSelectedContextType() {
261                 return _newtype;
262         }
263
264         public List<SimulationContext> getSimulationContexts() {
265                 return _context;
266         }
267
268         /**
269          * Get list of found objects.
270          * 
271          * @return list of found objects
272          */
273         public List<Proxy> getResult() {
274                 return _result;
275         }
276
277         // ==============================================================================================================================
278         // Setters
279         // ==============================================================================================================================
280
281         public void setAuthor(final String index) {
282                 this._author = index;
283         }
284
285         public void setContextType(final String type) {
286                 this._ctype = type;
287         }
288
289         public void setContextValue(final String value) {
290                 this._cvalue = value;
291         }
292
293         public void setContextIndex(final String value) {
294                 this._cindex = value;
295         }
296
297         protected void setCandidates() {
298                 _candidates = new ArrayList<Name>();
299                 List<User> users = getUserService().selectAllUsers();
300                 User me = getConnectedUser(); // May be null
301                 for (Iterator<User> i = users.iterator(); i.hasNext();) {
302                         User next = i.next();
303                         ApplicationRights he = new ApplicationRights(next);
304                         if (he.canCreateStudy()) {
305                                 if (next.equals(me)) {
306                                         _candidates.add(0, new ValidationFacade.ByManager(me,
307                                                         getApplicationSettings().getCurrentLocale()));
308                                 } else {
309                                         _candidates.add(next);
310                                 }
311                         }
312                 }
313         }
314
315         /**
316          * Build available context types list with localized names.
317          * 
318          * @param critext
319          *            context types already used in the search filter
320          */
321         protected void setContextTypeOptions(
322                         final List<SimulationContextType> critext) {
323                 for (Iterator<SimulationContext> i = _context.iterator(); i.hasNext();) {
324                         critext.remove(i.next().getType()); // Already used context type
325                 }
326                 // Ordering by alphabetical order of localized context types
327                 SimulationContextType[] types = critext
328                                 .toArray(new SimulationContextType[critext.size()]);
329                 ContextTypeComparator compare = new ContextTypeComparator();
330                 ProjectSettingsService.Step step = getSimulationContextService()
331                                 .getAttachedStep(types[0]);
332                 int from = 0;
333                 int to = 0;
334                 while (to < types.length - 1) {
335                         to += 1;
336                         if (types[to].isAttachedTo(step)) {
337                                 continue;
338                         }
339
340                         if (to > from + 1) {
341                                 Arrays.sort(types, from, to, compare);
342                         }
343                         from = to;
344                         step = getSimulationContextService().getAttachedStep(types[to]);
345                 }
346                 if (to > from) {
347                         Arrays.sort(types, from, to + 1, compare);
348                 }
349                 this._critext = Arrays.asList(types);
350         }
351
352         // ==============================================================================================================================
353         // Abstract services
354         // ==============================================================================================================================
355
356         protected abstract String doSearch() throws InvalidPropertyException;
357
358         protected abstract List<SimulationContextType> getInvolvedContexts();
359
360         protected abstract void loadFilter();
361
362         protected abstract void saveFilter();
363
364         /**
365          * Get the simulationContextService.
366          * 
367          * @return the simulationContextService
368          */
369         public SimulationContextService getSimulationContextService() {
370                 return _simulationContextService;
371         }
372
373         /**
374          * Set the simulationContextService.
375          * 
376          * @param simulationContextService
377          *            the simulationContextService to set
378          */
379         public void setSimulationContextService(
380                         final SimulationContextService simulationContextService) {
381                 _simulationContextService = simulationContextService;
382         }
383
384         /**
385          * Get the userService.
386          * 
387          * @return the userService
388          */
389         public UserService getUserService() {
390                 return _userService;
391         }
392
393         /**
394          * Set the userService.
395          * 
396          * @param userService
397          *            the userService to set
398          */
399         public void setUserService(final UserService userService) {
400                 _userService = userService;
401         }
402 }