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