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