Salome HOME
Actions menu properties are refactored, unnecessary code is removed.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / admin / SimulationContextAction.java
1 package org.splat.simer.admin;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Comparator;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Locale;
10 import java.util.ResourceBundle;
11 import java.util.Set;
12
13 import org.hibernate.Session;
14 import org.hibernate.Transaction;
15 import org.splat.dal.bo.som.KnowledgeElement;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.SimulationContext;
18 import org.splat.dal.bo.som.SimulationContextType;
19 import org.splat.dal.bo.som.Study;
20 import org.splat.dal.dao.som.Database;
21 import org.splat.service.KnowledgeElementService;
22 import org.splat.service.SearchService;
23 import org.splat.service.SimulationContextService;
24 import org.splat.service.dto.Proxy;
25 import org.splat.service.dto.SimulationContextFacade;
26 import org.splat.service.technical.ProjectSettingsService;
27 import org.splat.simer.Action;
28 import org.splat.simer.ApplicationSettings;
29
30 /**
31  * Action for operations on simulation contexts.
32  */
33 public class SimulationContextAction extends Action {
34
35         /**
36          * Serial version ID.
37          */
38         private static final long serialVersionUID = 7083323229359094699L;
39
40         /**
41          * Simulation contexts to be approved.
42          */
43         private transient List<SimulationContextFacade> _tocheck;
44         /**
45          * Index of the selected simulation context presented in the approval form.
46          */
47         private int _selection;
48         /**
49          * Corresponding simulation context object.
50          */
51         private transient SimulationContext _edition;
52         /**
53          * Study step to which the selected simulation context is attached.
54          */
55         private transient ProjectSettingsService.Step _step;
56         /**
57          * Study scenarios indexed by this simulation context.
58          */
59         private transient Set<ProjectElementFacade> _owner;
60         /**
61          * Existing approved simulation context types ordered by localized names.
62          */
63         private transient List<LocalizedContextTypes> _existype;
64         /**
65          * Existing approved simulation context types ordered by internal codes.
66          */
67         private transient List<SimulationContextType> _exisname;
68         /**
69          * Existing simulation contexts of selected type.
70          */
71         private transient List<SimulationContext> _existing;
72         /**
73          * Injected search service.
74          */
75         private SearchService _searchService;
76         /**
77          * Injected project settings service.
78          */
79         private ProjectSettingsService _projectSettings;
80         /**
81          * Injected knowledge element service.
82          */
83         private KnowledgeElementService _knowledgeElementService;
84         /**
85          * Injected simulation context service.
86          */
87         private SimulationContextService _simulationContextService;
88
89         /**
90          * Context name comparator.
91          */
92         private class ContextNameComparator implements
93                         Comparator<SimulationContextType> {
94                 /**
95                  * {@inheritDoc}
96                  * 
97                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
98                  */
99                 public int compare(final SimulationContextType t1,
100                                 final SimulationContextType t2) {
101                         String name1 = t1.getName();
102                         String name2 = t2.getName();
103
104                         return name1.compareToIgnoreCase(name2);
105                 }
106         }
107
108         public class LocalizedContextTypes {
109                 private transient final Locale _locale;
110                 private transient final List<String> _sortype; // Existing approved simulation context types ordered by localized names
111
112                 public LocalizedContextTypes(final List<SimulationContextType> types,
113                                 final Locale lang) {
114                         ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
115                         SimulationContextType[] tosort = types
116                                         .toArray(new SimulationContextType[types.size()]);
117                         String[] name = new String[types.size()];
118
119                         for (int i = 0; i < name.length; i++) {
120                                 name[i] = bundle.getString("type.context."
121                                                 + tosort[i].getName());
122                         }
123                         Arrays.sort(name);
124                         _sortype = Arrays.asList(name);
125                         _locale = lang;
126                 }
127
128                 public String getLocale() {
129                         return _locale.toString();
130                 }
131
132                 public List<String> getTypeNames() {
133                         return _sortype;
134                 }
135
136                 public boolean isCurrent() {
137                         return _locale.equals(getApplicationSettings().getCurrentLocale());
138                 }
139         }
140
141         // ==============================================================================================================================
142         // Action methods
143         // ==============================================================================================================================
144
145         /**
146          * Initialize the simulation context list.
147          * 
148          * @return SUCCESS if succeeded, otherwise - ERROR
149          */
150         public String doInitialize() {
151                 String res = SUCCESS;
152                 try {
153                         _tocheck = getSimulationContextService()
154                                         .getSimulationContextsInState(ProgressState.inCHECK);
155                         _selection = 0;
156                         _edition = null;
157                         _owner = null;
158
159                         initializationFullScreenContext("study", "none", "open");
160                 } catch (Exception error) {
161                         LOG.error("Reason:", error);
162                         res = ERROR; // No need to roll-back the transaction as it is read-only
163                 }
164                 return res;
165         }
166
167         public String doSelect() {
168
169                 String res = SUCCESS;
170                 initializationFullScreenContext("study", "none", "open");
171
172                 Session connex = Database.getCurSession();
173                 Transaction transax = connex.beginTransaction();
174                 try {
175                         SimulationContext.Properties cprop = new SimulationContext.Properties();
176                         List<SimulationContext> context = getSimulationContextService()
177                                         .selectSimulationContextsWhere(
178                                                         cprop.setState(ProgressState.inCHECK));
179                         List<SimulationContext> selected = new ArrayList<SimulationContext>(
180                                         1);
181
182                         _tocheck = new ArrayList<SimulationContextFacade>(context.size());
183                         for (Iterator<SimulationContext> i = context.iterator(); i
184                                         .hasNext();) {
185                                 SimulationContext next = i.next();
186                                 if (next.getIndex() == _selection) {
187                                         _edition = next;
188                                         selected.add(_edition);
189                                 }
190                                 _tocheck.add(new SimulationContextFacade(next,
191                                                 getProjectSettings().getAllSteps()));
192                         }
193                         KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
194                         List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
195                                         kprop.setSimulationContexts(selected).setState(
196                                                         ProgressState.inWORK));
197
198                         _step = getSimulationContextService().getAttachedStep(
199                                         _edition.getType());
200                         _owner = new HashSet<ProjectElementFacade>();
201                         for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
202                                 KnowledgeElement next = getKnowledgeElementService()
203                                                 .selectKnowledgeElement(i.next().getIndex());
204                                 ProjectElementFacade facade;
205                                 if (_step.appliesTo(Study.class)) {
206                                         facade = new ProjectElementFacade(next.getOwnerScenario()
207                                                         .getOwnerStudy(), _step);
208                                 } else {
209                                         facade = new ProjectElementFacade(next.getOwnerScenario(),
210                                                         _step);
211                                 }
212                                 _owner.add(facade);
213                         }
214                         SimulationContextType.Properties sprop = new SimulationContextType.Properties();
215                         List<SimulationContextType> types = getSimulationContextService()
216                                         .selectTypesWhere(
217                                                         sprop.setProgressState(ProgressState.APPROVED));
218                         Locale[] support = ApplicationSettings.getSupportedLocales();
219
220                         // Sort localized type names
221                         _existype = new ArrayList<LocalizedContextTypes>(support.length);
222                         for (int i = 0; i < support.length; i++) {
223                                 _existype.add(new LocalizedContextTypes(types, support[i]));
224                         }
225                         SimulationContextType[] names = types
226                                         .toArray(new SimulationContextType[types.size()]);
227
228                         Arrays.sort(names, new ContextNameComparator());
229                         _exisname = Arrays.asList(names);
230
231                         _existing = getSimulationContextService()
232                                         .selectSimulationContextsWhere(
233                                                         cprop.setType(_edition.getType()).setState(
234                                                                         ProgressState.APPROVED));
235
236                         transax.commit();
237                 } catch (Exception error) {
238                         LOG.error("Reason:", error);
239                         res = ERROR; // No need to roll-back the transaction as it is read-only
240                 }
241                 return res;
242         }
243
244         // ==============================================================================================================================
245         // Getters and setters
246         // ==============================================================================================================================
247
248         public List<ProjectSettingsService.Step> getAllStudySteps() {
249                 return getProjectSettings().getAllSteps();
250         }
251
252         /**
253          * Get project settings.
254          * 
255          * @return Project settings service
256          */
257         private ProjectSettingsService getProjectSettings() {
258                 return _projectSettings;
259         }
260
261         /**
262          * Set project settings service.
263          * 
264          * @param projectSettingsService
265          *            project settings service
266          */
267         public void setProjectSettings(
268                         final ProjectSettingsService projectSettingsService) {
269                 _projectSettings = projectSettingsService;
270         }
271
272         public ProjectSettingsService.Step getAttachedStep() {
273                 return _step;
274         }
275
276         public List<SimulationContextFacade> getContextsToBeApproved() {
277                 return _tocheck;
278         }
279
280         public SimulationContext getEdited() {
281                 return _edition;
282         }
283
284         public List<SimulationContext> getExistingContexts() {
285                 return _existing;
286         }
287
288         public List<SimulationContextType> getExistingNames() {
289                 return _exisname;
290         }
291
292         public List<LocalizedContextTypes> getSupportedLocales() {
293                 return _existype;
294         }
295
296         public Set<ProjectElementFacade> getIndexedElements() {
297                 return _owner;
298         }
299
300         public int getSelection() {
301                 return _selection;
302         }
303
304         public void setSelection(final String index) {
305                 _selection = Integer.valueOf(index);
306         }
307
308         /**
309          * Get the knowledgeElementService.
310          * 
311          * @return the knowledgeElementService
312          */
313         public KnowledgeElementService getKnowledgeElementService() {
314                 return _knowledgeElementService;
315         }
316
317         /**
318          * Set the knowledgeElementService.
319          * 
320          * @param knowledgeElementService
321          *            the knowledgeElementService to set
322          */
323         public void setKnowledgeElementService(
324                         final KnowledgeElementService knowledgeElementService) {
325                 _knowledgeElementService = knowledgeElementService;
326         }
327
328         /**
329          * Get the simulationContextService.
330          * 
331          * @return the simulationContextService
332          */
333         public SimulationContextService getSimulationContextService() {
334                 return _simulationContextService;
335         }
336
337         /**
338          * Set the simulationContextService.
339          * 
340          * @param simulationContextService
341          *            the simulationContextService to set
342          */
343         public void setSimulationContextService(
344                         final SimulationContextService simulationContextService) {
345                 _simulationContextService = simulationContextService;
346         }
347
348         /**
349          * Get the searchService.
350          * 
351          * @return the searchService
352          */
353         public SearchService getSearchService() {
354                 return _searchService;
355         }
356
357         /**
358          * Set the searchService.
359          * 
360          * @param searchService
361          *            the searchService to set
362          */
363         public void setSearchService(final SearchService searchService) {
364                 _searchService = searchService;
365         }
366 }