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