]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/admin/SimulationContextAction.java
Salome HOME
47a5c37fe19c262f8f6ab56c2973a83eca582584
[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 import java.util.Vector;
13
14 import org.hibernate.Session;
15 import org.hibernate.Transaction;
16
17 import org.splat.simer.Action;
18 import org.splat.simer.ApplicationSettings;
19 import org.splat.dal.dao.som.Database;
20 import org.splat.dal.bo.som.KnowledgeElement;
21 import org.splat.dal.bo.som.ProgressState;
22 import org.splat.service.KnowledgeElementService;
23 import org.splat.service.SearchService;
24 import org.splat.service.SimulationContextService;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.splat.service.dto.Proxy;
27 import org.splat.service.dto.SimulationContextFacade;
28 import org.splat.dal.bo.som.SimulationContext;
29 import org.splat.dal.bo.som.SimulationContextType;
30 import org.splat.dal.bo.som.Study;
31
32 /**
33  * Action for operations on simulation contexts.
34  */
35 public class SimulationContextAction extends Action {
36
37         /**
38          * Serial version ID.
39          */
40         private static final long serialVersionUID = 7083323229359094699L;
41
42         private List<SimulationContextFacade> tocheck; // Simulation contexts to be approved
43         private int selection; // Index of the selected simulation context presented in the approval form
44         private SimulationContext edition; // Corresponding simulation context object
45         private ProjectSettingsService.Step step; // Study step to which the selected simulation context is attached
46         private Set<ProjectElementFacade> owner; // Study scenarios indexed by this simulation context
47         private List<LocalizedContextTypes> existype; // Existing approved simulation context types ordered by localized names
48         private List<SimulationContextType> exisname; // Existing approved simulation context types ordered by internal codes
49         private List<SimulationContext> existing; // Existing simulation contexts of selected type
50         /**
51          * Injected search service.
52          */
53         private SearchService _searchService;
54         /**
55          * Injected project settings service.
56          */
57         private ProjectSettingsService _projectSettingsService;
58         /**
59          * Injected knowledge element service.
60          */
61         private KnowledgeElementService _knowledgeElementService;
62         /**
63          * Injected simulation context service.
64          */
65         private SimulationContextService _simulationContextService;
66         
67         /**
68          * Value of the menu property. 
69          * It can be: none, create, open, study, knowledge, sysadmin, help.
70          */
71         private String _menuProperty;
72         
73         /**
74          * Value of the tool bar property. 
75          * It can be: none, standard, study, back.
76          */
77         private String _toolProperty;
78         
79         /**
80          * Value of the left menu property. 
81          * It can be: open, study, knowledge, scenario.
82          */
83         private String _leftMenuProperty;
84
85         /**
86          * Context name comparator.
87          */
88         private class ContextNameComparator implements
89                         Comparator<SimulationContextType> {
90                 /**
91                  * {@inheritDoc}
92                  * 
93                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
94                  */
95                 public int compare(SimulationContextType t1, SimulationContextType t2) {
96                         String name1 = t1.getName();
97                         String name2 = t2.getName();
98
99                         return name1.compareToIgnoreCase(name2);
100                 }
101         }
102
103         public class LocalizedContextTypes {
104                 // ----------------------------------
105                 private Locale locale;
106                 private List<String> sortype; // Existing approved simulation context types ordered by localized names
107
108                 public LocalizedContextTypes(List<SimulationContextType> types,
109                                 Locale lang) {
110                         ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
111                         SimulationContextType[] tosort = types
112                                         .toArray(new SimulationContextType[types.size()]);
113                         String[] name = new String[types.size()];
114
115                         for (int i = 0; i < name.length; i++)
116                                 name[i] = bundle.getString("type.context."
117                                                 + tosort[i].getName());
118                         Arrays.sort(name);
119                         sortype = Arrays.asList(name);
120                         locale = lang;
121                 }
122
123                 public String getLocale() {
124                         return locale.toString();
125                 }
126
127                 public List<String> getTypeNames() {
128                         return sortype;
129                 }
130
131                 public boolean isCurrent() {
132                         return (locale.equals(ApplicationSettings.getCurrentLocale()));
133                 }
134         }
135
136         // ==============================================================================================================================
137         // Action methods
138         // ==============================================================================================================================
139
140         /**
141          * Initialize the simulation context list.
142          * @return SUCCESS if succeeded, otherwise - ERROR
143          */
144         public String doInitialize() {
145                 try {
146                         tocheck = getSimulationContextService()
147                                         .getSimulationContextsInState(ProgressState.inCHECK);
148                         selection = 0;
149                         edition = null;
150                         owner = null;
151                         
152                         setMenuProperty("study");
153                         setToolProperty("none");
154                         setLeftMenuProperty("open");
155                 initializationFullScreenContext(_menuProperty, _toolProperty, _leftMenuProperty);
156
157                         return SUCCESS;
158                 } catch (Exception error) {
159                         logger.error("Reason:", error);
160                         return ERROR; // No need to roll-back the transaction as it is read-only
161                 }
162         }
163
164         public String doSelect() {
165                 // -------------------------
166                 
167                 setMenuProperty("study");
168                 setToolProperty("none");
169                 setLeftMenuProperty("open");
170         initializationFullScreenContext(_menuProperty, _toolProperty, _leftMenuProperty);
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 Vector<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                                 owner.add(facade);
212                         }
213                         SimulationContextType.Properties sprop = new SimulationContextType.Properties();
214                         List<SimulationContextType> types = getSimulationContextService()
215                                         .selectTypesWhere(
216                                                         sprop.setProgressState(ProgressState.APPROVED));
217                         Locale[] support = ApplicationSettings.getSupportedLocales();
218
219                         // Sort localized type names
220                         existype = new ArrayList<LocalizedContextTypes>(support.length);
221                         for (int i = 0; i < support.length; i++) {
222                                 existype.add(new LocalizedContextTypes(types, support[i]));
223                         }
224                         SimulationContextType[] names = types
225                                         .toArray(new SimulationContextType[types.size()]);
226
227                         Arrays.sort(names, new ContextNameComparator());
228                         exisname = Arrays.asList(names);
229
230                         existing = getSimulationContextService()
231                                         .selectSimulationContextsWhere(
232                                                         cprop.setType(edition.getType()).setState(
233                                                                         ProgressState.APPROVED));
234
235                         transax.commit();
236                         return SUCCESS;
237                 } catch (Exception error) {
238                         logger.error("Reason:", error);
239                         return ERROR; // No need to roll-back the transaction as it is read-only
240                 }
241         }
242
243         // ==============================================================================================================================
244         // Getters and setters
245         // ==============================================================================================================================
246
247         public List<ProjectSettingsService.Step> getAllStudySteps() {
248                 // -----------------------------------------------------
249                 return getProjectSettings().getAllSteps();
250         }
251
252         /**
253          * Get project settings.
254          * 
255          * @return Project settings service
256          */
257         private ProjectSettingsService getProjectSettings() {
258                 return _projectSettingsService;
259         }
260
261         /**
262          * Set project settings service.
263          * 
264          * @param projectSettingsService
265          *            project settings service
266          */
267         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
268                 _projectSettingsService = projectSettingsService;
269         }
270
271         public ProjectSettingsService.Step getAttachedStep() {
272                 // ----------------------------------------------
273                 return step;
274         }
275
276         public List<SimulationContextFacade> getContextsToBeApproved() {
277                 // ---------------------------------------------------------------
278                 return tocheck;
279         }
280
281         public SimulationContext getEdited() {
282                 // -------------------------------------
283                 return edition;
284         }
285
286         public List<SimulationContext> getExistingContexts() {
287                 // -----------------------------------------------------
288                 return existing;
289         }
290
291         public List<SimulationContextType> getExistingNames() {
292                 // ------------------------------------------------------
293                 return exisname;
294         }
295
296         public List<LocalizedContextTypes> getSupportedLocales() {
297                 // ---------------------------------------------------------
298                 return existype;
299         }
300
301         public Set<ProjectElementFacade> getIndexedElements() {
302                 // ------------------------------------------------------
303                 return owner;
304         }
305
306         public int getSelection() {
307                 // --------------------------
308                 return selection;
309         }
310
311         public void setSelection(String index) {
312                 // ---------------------------------------
313                 selection = Integer.valueOf(index);
314         }
315
316         /**
317          * Get the knowledgeElementService.
318          * 
319          * @return the knowledgeElementService
320          */
321         public KnowledgeElementService getKnowledgeElementService() {
322                 return _knowledgeElementService;
323         }
324
325         /**
326          * Set the knowledgeElementService.
327          * 
328          * @param knowledgeElementService
329          *            the knowledgeElementService to set
330          */
331         public void setKnowledgeElementService(
332                         KnowledgeElementService knowledgeElementService) {
333                 _knowledgeElementService = knowledgeElementService;
334         }
335
336         /**
337          * Get the simulationContextService.
338          * 
339          * @return the simulationContextService
340          */
341         public SimulationContextService getSimulationContextService() {
342                 return _simulationContextService;
343         }
344
345         /**
346          * Set the simulationContextService.
347          * 
348          * @param simulationContextService
349          *            the simulationContextService to set
350          */
351         public void setSimulationContextService(
352                         SimulationContextService simulationContextService) {
353                 _simulationContextService = simulationContextService;
354         }
355
356         /**
357          * Get the searchService.
358          * 
359          * @return the searchService
360          */
361         public SearchService getSearchService() {
362                 return _searchService;
363         }
364
365         /**
366          * Set the searchService.
367          * 
368          * @param searchService
369          *            the searchService to set
370          */
371         public void setSearchService(SearchService searchService) {
372                 _searchService = searchService;
373         }
374         
375         /**
376          * Get the menuProperty. 
377          * @return the menuProperty
378          */
379         public String getMenuProperty() {
380                 return _menuProperty;
381         }
382
383         /**
384          * Set the menuProperty.
385          * @param menuProperty
386          *            the menuProperty to set
387          */
388         public void setMenuProperty(final String menuProperty) {
389                 this._menuProperty = menuProperty;
390         }
391         
392         /**
393          * Get the toolProperty.
394          * @return the toolProperty
395          */
396         public String getToolProperty() {
397                 return _toolProperty;
398         }
399
400         /**
401          * Set the toolProperty.
402          * @param toolProperty the toolProperty to set
403          */
404         public void setToolProperty(final String toolProperty) {
405                 _toolProperty = toolProperty;
406         }
407         
408         /**
409          * Get the leftMenuProperty.
410          * @return the leftMenuProperty
411          */
412         public String getLeftMenuProperty() {
413                 return _leftMenuProperty;
414         }
415
416         /**
417          * Set the leftMenuProperty.
418          * @param leftMenuProperty the leftMenuProperty to set
419          */
420         public void setLeftMenuProperty(final String leftMenuProperty) {
421                 _leftMenuProperty = leftMenuProperty;
422         }
423 }