]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/admin/SimulationContextAction.java
Salome HOME
4366eb204f056b706828120484cc6fb6bfa7a602
[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          * Context name comparator.
81          */
82         private class ContextNameComparator implements
83                         Comparator<SimulationContextType> {
84                 /**
85                  * {@inheritDoc}
86                  * 
87                  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
88                  */
89                 public int compare(SimulationContextType t1, SimulationContextType t2) {
90                         String name1 = t1.getName();
91                         String name2 = t2.getName();
92
93                         return name1.compareToIgnoreCase(name2);
94                 }
95         }
96
97         public class LocalizedContextTypes {
98                 // ----------------------------------
99                 private Locale locale;
100                 private List<String> sortype; // Existing approved simulation context types ordered by localized names
101
102                 public LocalizedContextTypes(List<SimulationContextType> types,
103                                 Locale lang) {
104                         ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
105                         SimulationContextType[] tosort = types
106                                         .toArray(new SimulationContextType[types.size()]);
107                         String[] name = new String[types.size()];
108
109                         for (int i = 0; i < name.length; i++)
110                                 name[i] = bundle.getString("type.context."
111                                                 + tosort[i].getName());
112                         Arrays.sort(name);
113                         sortype = Arrays.asList(name);
114                         locale = lang;
115                 }
116
117                 public String getLocale() {
118                         return locale.toString();
119                 }
120
121                 public List<String> getTypeNames() {
122                         return sortype;
123                 }
124
125                 public boolean isCurrent() {
126                         return (locale.equals(ApplicationSettings.getCurrentLocale()));
127                 }
128         }
129
130         // ==============================================================================================================================
131         // Action methods
132         // ==============================================================================================================================
133
134         /**
135          * Initialize the simulation context list.
136          * @return SUCCESS if succeeded, otherwise - ERROR
137          */
138         public String doInitialize() {
139                 try {
140                         tocheck = getSimulationContextService()
141                                         .getSimulationContextsInState(ProgressState.inCHECK);
142                         selection = 0;
143                         edition = null;
144                         owner = null;
145                         
146                         setMenuProperty("study");
147                         setToolProperty("none");
148                 initializationScreenContext(_menuProperty, _toolProperty);
149
150                         return SUCCESS;
151                 } catch (Exception error) {
152                         logger.error("Reason:", error);
153                         return ERROR; // No need to roll-back the transaction as it is read-only
154                 }
155         }
156
157         public String doSelect() {
158                 // -------------------------
159                 
160                 setMenuProperty("study");
161                 setToolProperty("none");
162         initializationScreenContext(_menuProperty, _toolProperty);
163         
164                 Session connex = Database.getCurSession();
165                 Transaction transax = connex.beginTransaction();
166                 try {
167                         SimulationContext.Properties cprop = new SimulationContext.Properties();
168                         List<SimulationContext> context = getSimulationContextService()
169                                         .selectSimulationContextsWhere(
170                                                         cprop.setState(ProgressState.inCHECK));
171                         List<SimulationContext> selected = new ArrayList<SimulationContext>(
172                                         1);
173
174                         tocheck = new Vector<SimulationContextFacade>(context.size());
175                         for (Iterator<SimulationContext> i = context.iterator(); i
176                                         .hasNext();) {
177                                 SimulationContext next = i.next();
178                                 if (next.getIndex() == selection) {
179                                         edition = next;
180                                         selected.add(edition);
181                                 }
182                                 tocheck.add(new SimulationContextFacade(next,
183                                                 getProjectSettings().getAllSteps()));
184                         }
185                         KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
186                         List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
187                                         kprop.setSimulationContexts(selected).setState(
188                                                         ProgressState.inWORK));
189
190                         step = getSimulationContextService().getAttachedStep(
191                                         edition.getType());
192                         owner = new HashSet<ProjectElementFacade>();
193                         for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
194                                 KnowledgeElement next = getKnowledgeElementService()
195                                                 .selectKnowledgeElement(i.next().getIndex());
196                                 ProjectElementFacade facade;
197                                 if (step.appliesTo(Study.class))
198                                         facade = new ProjectElementFacade(next.getOwnerScenario()
199                                                         .getOwnerStudy(), step);
200                                 else
201                                         facade = new ProjectElementFacade(next.getOwnerScenario(),
202                                                         step);
203                                 owner.add(facade);
204                         }
205                         SimulationContextType.Properties sprop = new SimulationContextType.Properties();
206                         List<SimulationContextType> types = getSimulationContextService()
207                                         .selectTypesWhere(
208                                                         sprop.setProgressState(ProgressState.APPROVED));
209                         Locale[] support = ApplicationSettings.getSupportedLocales();
210
211                         // Sort localized type names
212                         existype = new ArrayList<LocalizedContextTypes>(support.length);
213                         for (int i = 0; i < support.length; i++) {
214                                 existype.add(new LocalizedContextTypes(types, support[i]));
215                         }
216                         SimulationContextType[] names = types
217                                         .toArray(new SimulationContextType[types.size()]);
218
219                         Arrays.sort(names, new ContextNameComparator());
220                         exisname = Arrays.asList(names);
221
222                         existing = getSimulationContextService()
223                                         .selectSimulationContextsWhere(
224                                                         cprop.setType(edition.getType()).setState(
225                                                                         ProgressState.APPROVED));
226
227                         transax.commit();
228                         return SUCCESS;
229                 } catch (Exception error) {
230                         logger.error("Reason:", error);
231                         return ERROR; // No need to roll-back the transaction as it is read-only
232                 }
233         }
234
235         // ==============================================================================================================================
236         // Getters and setters
237         // ==============================================================================================================================
238
239         public List<ProjectSettingsService.Step> getAllStudySteps() {
240                 // -----------------------------------------------------
241                 return getProjectSettings().getAllSteps();
242         }
243
244         /**
245          * Get project settings.
246          * 
247          * @return Project settings service
248          */
249         private ProjectSettingsService getProjectSettings() {
250                 return _projectSettingsService;
251         }
252
253         /**
254          * Set project settings service.
255          * 
256          * @param projectSettingsService
257          *            project settings service
258          */
259         public void setProjectSettings(ProjectSettingsService projectSettingsService) {
260                 _projectSettingsService = projectSettingsService;
261         }
262
263         public ProjectSettingsService.Step getAttachedStep() {
264                 // ----------------------------------------------
265                 return step;
266         }
267
268         public List<SimulationContextFacade> getContextsToBeApproved() {
269                 // ---------------------------------------------------------------
270                 return tocheck;
271         }
272
273         public SimulationContext getEdited() {
274                 // -------------------------------------
275                 return edition;
276         }
277
278         public List<SimulationContext> getExistingContexts() {
279                 // -----------------------------------------------------
280                 return existing;
281         }
282
283         public List<SimulationContextType> getExistingNames() {
284                 // ------------------------------------------------------
285                 return exisname;
286         }
287
288         public List<LocalizedContextTypes> getSupportedLocales() {
289                 // ---------------------------------------------------------
290                 return existype;
291         }
292
293         public Set<ProjectElementFacade> getIndexedElements() {
294                 // ------------------------------------------------------
295                 return owner;
296         }
297
298         public int getSelection() {
299                 // --------------------------
300                 return selection;
301         }
302
303         public void setSelection(String index) {
304                 // ---------------------------------------
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                         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                         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(SearchService searchService) {
364                 _searchService = searchService;
365         }
366         
367         /**
368          * Get the menuProperty. 
369          * @return the menuProperty
370          */
371         public String getMenuProperty() {
372                 return _menuProperty;
373         }
374
375         /**
376          * Set the menuProperty.
377          * @param menuProperty
378          *            the menuProperty to set
379          */
380         public void setMenuProperty(final String menuProperty) {
381                 this._menuProperty = menuProperty;
382         }
383         
384         /**
385          * Get the toolProperty.
386          * @return the toolProperty
387          */
388         public String getToolProperty() {
389                 return _toolProperty;
390         }
391
392         /**
393          * Set the toolProperty.
394          * @param toolProperty the toolProperty to set
395          */
396         public void setToolProperty(final String toolProperty) {
397                 _toolProperty = toolProperty;
398         }
399 }