Salome HOME
d952aef679e99fa41a8b77379fa9b9081f3c0c23
[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          * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
91          */
92         private String _menuProperty;
93
94         /**
95          * Value of the tool bar property. It can be: none, standard, study, back.
96          */
97         private String _toolProperty;
98
99         /**
100          * Value of the left menu property. It can be: open, study, knowledge, scenario.
101          */
102         private String _leftMenuProperty;
103
104         /**
105          * Context name comparator.
106          */
107         private class ContextNameComparator implements
108                         Comparator<SimulationContextType> {
109                 /**
110                  * {@inheritDoc}
111                  * 
112                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
113                  */
114                 public int compare(final SimulationContextType t1,
115                                 final SimulationContextType t2) {
116                         String name1 = t1.getName();
117                         String name2 = t2.getName();
118
119                         return name1.compareToIgnoreCase(name2);
120                 }
121         }
122
123         public class LocalizedContextTypes {
124                 private transient final Locale _locale;
125                 private transient final List<String> _sortype; // Existing approved simulation context types ordered by localized names
126
127                 public LocalizedContextTypes(final List<SimulationContextType> types,
128                                 final Locale lang) {
129                         ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
130                         SimulationContextType[] tosort = types
131                                         .toArray(new SimulationContextType[types.size()]);
132                         String[] name = new String[types.size()];
133
134                         for (int i = 0; i < name.length; i++) {
135                                 name[i] = bundle.getString("type.context."
136                                                 + tosort[i].getName());
137                         }
138                         Arrays.sort(name);
139                         _sortype = Arrays.asList(name);
140                         _locale = lang;
141                 }
142
143                 public String getLocale() {
144                         return _locale.toString();
145                 }
146
147                 public List<String> getTypeNames() {
148                         return _sortype;
149                 }
150
151                 public boolean isCurrent() {
152                         return _locale.equals(getApplicationSettings().getCurrentLocale());
153                 }
154         }
155
156         // ==============================================================================================================================
157         // Action methods
158         // ==============================================================================================================================
159
160         /**
161          * Initialize the simulation context list.
162          * 
163          * @return SUCCESS if succeeded, otherwise - ERROR
164          */
165         public String doInitialize() {
166                 String res = SUCCESS;
167                 try {
168                         _tocheck = getSimulationContextService()
169                                         .getSimulationContextsInState(ProgressState.inCHECK);
170                         _selection = 0;
171                         _edition = null;
172                         _owner = null;
173
174                         setMenuProperty("study");
175                         setToolProperty("none");
176                         setLeftMenuProperty("open");
177                         initializationFullScreenContext(_menuProperty, _toolProperty,
178                                         _leftMenuProperty);
179                 } catch (Exception error) {
180                         LOG.error("Reason:", error);
181                         res = ERROR; // No need to roll-back the transaction as it is read-only
182                 }
183                 return res;
184         }
185
186         public String doSelect() {
187
188                 String res = SUCCESS;
189                 setMenuProperty("study");
190                 setToolProperty("none");
191                 setLeftMenuProperty("open");
192                 initializationFullScreenContext(_menuProperty, _toolProperty,
193                                 _leftMenuProperty);
194
195                 Session connex = Database.getCurSession();
196                 Transaction transax = connex.beginTransaction();
197                 try {
198                         SimulationContext.Properties cprop = new SimulationContext.Properties();
199                         List<SimulationContext> context = getSimulationContextService()
200                                         .selectSimulationContextsWhere(
201                                                         cprop.setState(ProgressState.inCHECK));
202                         List<SimulationContext> selected = new ArrayList<SimulationContext>(
203                                         1);
204
205                         _tocheck = new ArrayList<SimulationContextFacade>(context.size());
206                         for (Iterator<SimulationContext> i = context.iterator(); i
207                                         .hasNext();) {
208                                 SimulationContext next = i.next();
209                                 if (next.getIndex() == _selection) {
210                                         _edition = next;
211                                         selected.add(_edition);
212                                 }
213                                 _tocheck.add(new SimulationContextFacade(next,
214                                                 getProjectSettings().getAllSteps()));
215                         }
216                         KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
217                         List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
218                                         kprop.setSimulationContexts(selected).setState(
219                                                         ProgressState.inWORK));
220
221                         _step = getSimulationContextService().getAttachedStep(
222                                         _edition.getType());
223                         _owner = new HashSet<ProjectElementFacade>();
224                         for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
225                                 KnowledgeElement next = getKnowledgeElementService()
226                                                 .selectKnowledgeElement(i.next().getIndex());
227                                 ProjectElementFacade facade;
228                                 if (_step.appliesTo(Study.class)) {
229                                         facade = new ProjectElementFacade(next.getOwnerScenario()
230                                                         .getOwnerStudy(), _step);
231                                 } else {
232                                         facade = new ProjectElementFacade(next.getOwnerScenario(),
233                                                         _step);
234                                 }
235                                 _owner.add(facade);
236                         }
237                         SimulationContextType.Properties sprop = new SimulationContextType.Properties();
238                         List<SimulationContextType> types = getSimulationContextService()
239                                         .selectTypesWhere(
240                                                         sprop.setProgressState(ProgressState.APPROVED));
241                         Locale[] support = ApplicationSettings.getSupportedLocales();
242
243                         // Sort localized type names
244                         _existype = new ArrayList<LocalizedContextTypes>(support.length);
245                         for (int i = 0; i < support.length; i++) {
246                                 _existype.add(new LocalizedContextTypes(types, support[i]));
247                         }
248                         SimulationContextType[] names = types
249                                         .toArray(new SimulationContextType[types.size()]);
250
251                         Arrays.sort(names, new ContextNameComparator());
252                         _exisname = Arrays.asList(names);
253
254                         _existing = getSimulationContextService()
255                                         .selectSimulationContextsWhere(
256                                                         cprop.setType(_edition.getType()).setState(
257                                                                         ProgressState.APPROVED));
258
259                         transax.commit();
260                 } catch (Exception error) {
261                         LOG.error("Reason:", error);
262                         res = ERROR; // No need to roll-back the transaction as it is read-only
263                 }
264                 return res;
265         }
266
267         // ==============================================================================================================================
268         // Getters and setters
269         // ==============================================================================================================================
270
271         public List<ProjectSettingsService.Step> getAllStudySteps() {
272                 return getProjectSettings().getAllSteps();
273         }
274
275         /**
276          * Get project settings.
277          * 
278          * @return Project settings service
279          */
280         private ProjectSettingsService getProjectSettings() {
281                 return _projectSettings;
282         }
283
284         /**
285          * Set project settings service.
286          * 
287          * @param projectSettingsService
288          *            project settings service
289          */
290         public void setProjectSettings(
291                         final ProjectSettingsService projectSettingsService) {
292                 _projectSettings = projectSettingsService;
293         }
294
295         public ProjectSettingsService.Step getAttachedStep() {
296                 return _step;
297         }
298
299         public List<SimulationContextFacade> getContextsToBeApproved() {
300                 return _tocheck;
301         }
302
303         public SimulationContext getEdited() {
304                 return _edition;
305         }
306
307         public List<SimulationContext> getExistingContexts() {
308                 return _existing;
309         }
310
311         public List<SimulationContextType> getExistingNames() {
312                 return _exisname;
313         }
314
315         public List<LocalizedContextTypes> getSupportedLocales() {
316                 return _existype;
317         }
318
319         public Set<ProjectElementFacade> getIndexedElements() {
320                 return _owner;
321         }
322
323         public int getSelection() {
324                 return _selection;
325         }
326
327         public void setSelection(final String index) {
328                 _selection = Integer.valueOf(index);
329         }
330
331         /**
332          * Get the knowledgeElementService.
333          * 
334          * @return the knowledgeElementService
335          */
336         public KnowledgeElementService getKnowledgeElementService() {
337                 return _knowledgeElementService;
338         }
339
340         /**
341          * Set the knowledgeElementService.
342          * 
343          * @param knowledgeElementService
344          *            the knowledgeElementService to set
345          */
346         public void setKnowledgeElementService(
347                         final KnowledgeElementService knowledgeElementService) {
348                 _knowledgeElementService = knowledgeElementService;
349         }
350
351         /**
352          * Get the simulationContextService.
353          * 
354          * @return the simulationContextService
355          */
356         public SimulationContextService getSimulationContextService() {
357                 return _simulationContextService;
358         }
359
360         /**
361          * Set the simulationContextService.
362          * 
363          * @param simulationContextService
364          *            the simulationContextService to set
365          */
366         public void setSimulationContextService(
367                         final SimulationContextService simulationContextService) {
368                 _simulationContextService = simulationContextService;
369         }
370
371         /**
372          * Get the searchService.
373          * 
374          * @return the searchService
375          */
376         public SearchService getSearchService() {
377                 return _searchService;
378         }
379
380         /**
381          * Set the searchService.
382          * 
383          * @param searchService
384          *            the searchService to set
385          */
386         public void setSearchService(final SearchService searchService) {
387                 _searchService = searchService;
388         }
389
390         /**
391          * Get the menuProperty.
392          * 
393          * @return the menuProperty
394          */
395         public String getMenuProperty() {
396                 return _menuProperty;
397         }
398
399         /**
400          * Set the menuProperty.
401          * 
402          * @param menuProperty
403          *            the menuProperty to set
404          */
405         public void setMenuProperty(final String menuProperty) {
406                 this._menuProperty = menuProperty;
407         }
408
409         /**
410          * Get the toolProperty.
411          * 
412          * @return the toolProperty
413          */
414         public String getToolProperty() {
415                 return _toolProperty;
416         }
417
418         /**
419          * Set the toolProperty.
420          * 
421          * @param toolProperty
422          *            the toolProperty to set
423          */
424         public void setToolProperty(final String toolProperty) {
425                 _toolProperty = toolProperty;
426         }
427
428         /**
429          * Get the leftMenuProperty.
430          * 
431          * @return the leftMenuProperty
432          */
433         public String getLeftMenuProperty() {
434                 return _leftMenuProperty;
435         }
436
437         /**
438          * Set the leftMenuProperty.
439          * 
440          * @param leftMenuProperty
441          *            the leftMenuProperty to set
442          */
443         public void setLeftMenuProperty(final String leftMenuProperty) {
444                 _leftMenuProperty = leftMenuProperty;
445         }
446 }