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