1 package org.splat.simer.admin;
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;
9 import java.util.Locale;
10 import java.util.ResourceBundle;
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.Proxy;
25 import org.splat.service.dto.SimulationContextFacade;
26 import org.splat.service.technical.ProjectSettingsService;
27 import org.splat.simer.Action;
28 import org.splat.simer.ApplicationSettings;
31 * Action for operations on simulation contexts.
33 public class SimulationContextAction extends Action {
38 private static final long serialVersionUID = 7083323229359094699L;
41 * Simulation contexts to be approved.
43 private transient List<SimulationContextFacade> _tocheck;
45 * Index of the selected simulation context presented in the approval form.
47 private int _selection;
49 * Corresponding simulation context object.
51 private transient SimulationContext _edition;
53 * Study step to which the selected simulation context is attached.
55 private transient ProjectSettingsService.Step _step;
57 * Study scenarios indexed by this simulation context.
59 private transient Set<ProjectElementFacade> _owner;
61 * Existing approved simulation context types ordered by localized names.
63 private transient List<LocalizedContextTypes> _existype;
65 * Existing approved simulation context types ordered by internal codes.
67 private transient List<SimulationContextType> _exisname;
69 * Existing simulation contexts of selected type.
71 private transient List<SimulationContext> _existing;
73 * Injected search service.
75 private SearchService _searchService;
77 * Injected project settings service.
79 private ProjectSettingsService _projectSettings;
81 * Injected knowledge element service.
83 private KnowledgeElementService _knowledgeElementService;
85 * Injected simulation context service.
87 private SimulationContextService _simulationContextService;
90 * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
92 private String _menuProperty;
95 * Value of the tool bar property. It can be: none, standard, study, back.
97 private String _toolProperty;
100 * Value of the left menu property. It can be: open, study, knowledge, scenario.
102 private String _leftMenuProperty;
105 * Context name comparator.
107 private class ContextNameComparator implements
108 Comparator<SimulationContextType> {
112 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
114 public int compare(final SimulationContextType t1,
115 final SimulationContextType t2) {
116 String name1 = t1.getName();
117 String name2 = t2.getName();
119 return name1.compareToIgnoreCase(name2);
123 public class LocalizedContextTypes {
124 private transient final Locale _locale;
125 private transient final List<String> _sortype; // Existing approved simulation context types ordered by localized names
127 public LocalizedContextTypes(final List<SimulationContextType> types,
129 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
130 SimulationContextType[] tosort = types
131 .toArray(new SimulationContextType[types.size()]);
132 String[] name = new String[types.size()];
134 for (int i = 0; i < name.length; i++) {
135 name[i] = bundle.getString("type.context."
136 + tosort[i].getName());
139 _sortype = Arrays.asList(name);
143 public String getLocale() {
144 return _locale.toString();
147 public List<String> getTypeNames() {
151 public boolean isCurrent() {
152 return _locale.equals(getApplicationSettings().getCurrentLocale());
156 // ==============================================================================================================================
158 // ==============================================================================================================================
161 * Initialize the simulation context list.
163 * @return SUCCESS if succeeded, otherwise - ERROR
165 public String doInitialize() {
166 String res = SUCCESS;
168 _tocheck = getSimulationContextService()
169 .getSimulationContextsInState(ProgressState.inCHECK);
174 setMenuProperty("study");
175 setToolProperty("none");
176 setLeftMenuProperty("open");
177 initializationFullScreenContext(_menuProperty, _toolProperty,
179 } catch (Exception error) {
180 LOG.error("Reason:", error);
181 res = ERROR; // No need to roll-back the transaction as it is read-only
186 public String doSelect() {
188 String res = SUCCESS;
189 setMenuProperty("study");
190 setToolProperty("none");
191 setLeftMenuProperty("open");
192 initializationFullScreenContext(_menuProperty, _toolProperty,
195 Session connex = Database.getCurSession();
196 Transaction transax = connex.beginTransaction();
198 SimulationContext.Properties cprop = new SimulationContext.Properties();
199 List<SimulationContext> context = getSimulationContextService()
200 .selectSimulationContextsWhere(
201 cprop.setState(ProgressState.inCHECK));
202 List<SimulationContext> selected = new ArrayList<SimulationContext>(
205 _tocheck = new ArrayList<SimulationContextFacade>(context.size());
206 for (Iterator<SimulationContext> i = context.iterator(); i
208 SimulationContext next = i.next();
209 if (next.getIndex() == _selection) {
211 selected.add(_edition);
213 _tocheck.add(new SimulationContextFacade(next,
214 getProjectSettings().getAllSteps()));
216 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
217 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
218 kprop.setSimulationContexts(selected).setState(
219 ProgressState.inWORK));
221 _step = getSimulationContextService().getAttachedStep(
223 _owner = new HashSet<ProjectElementFacade>();
224 for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
225 KnowledgeElement next = getKnowledgeElementService()
226 .selectKnowledgeElement(i.next().getIndex());
227 ProjectElementFacade facade;
228 if (_step.appliesTo(Study.class)) {
229 facade = new ProjectElementFacade(next.getOwnerScenario()
230 .getOwnerStudy(), _step);
232 facade = new ProjectElementFacade(next.getOwnerScenario(),
237 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
238 List<SimulationContextType> types = getSimulationContextService()
240 sprop.setProgressState(ProgressState.APPROVED));
241 Locale[] support = ApplicationSettings.getSupportedLocales();
243 // Sort localized type names
244 _existype = new ArrayList<LocalizedContextTypes>(support.length);
245 for (int i = 0; i < support.length; i++) {
246 _existype.add(new LocalizedContextTypes(types, support[i]));
248 SimulationContextType[] names = types
249 .toArray(new SimulationContextType[types.size()]);
251 Arrays.sort(names, new ContextNameComparator());
252 _exisname = Arrays.asList(names);
254 _existing = getSimulationContextService()
255 .selectSimulationContextsWhere(
256 cprop.setType(_edition.getType()).setState(
257 ProgressState.APPROVED));
260 } catch (Exception error) {
261 LOG.error("Reason:", error);
262 res = ERROR; // No need to roll-back the transaction as it is read-only
267 // ==============================================================================================================================
268 // Getters and setters
269 // ==============================================================================================================================
271 public List<ProjectSettingsService.Step> getAllStudySteps() {
272 return getProjectSettings().getAllSteps();
276 * Get project settings.
278 * @return Project settings service
280 private ProjectSettingsService getProjectSettings() {
281 return _projectSettings;
285 * Set project settings service.
287 * @param projectSettingsService
288 * project settings service
290 public void setProjectSettings(
291 final ProjectSettingsService projectSettingsService) {
292 _projectSettings = projectSettingsService;
295 public ProjectSettingsService.Step getAttachedStep() {
299 public List<SimulationContextFacade> getContextsToBeApproved() {
303 public SimulationContext getEdited() {
307 public List<SimulationContext> getExistingContexts() {
311 public List<SimulationContextType> getExistingNames() {
315 public List<LocalizedContextTypes> getSupportedLocales() {
319 public Set<ProjectElementFacade> getIndexedElements() {
323 public int getSelection() {
327 public void setSelection(final String index) {
328 _selection = Integer.valueOf(index);
332 * Get the knowledgeElementService.
334 * @return the knowledgeElementService
336 public KnowledgeElementService getKnowledgeElementService() {
337 return _knowledgeElementService;
341 * Set the knowledgeElementService.
343 * @param knowledgeElementService
344 * the knowledgeElementService to set
346 public void setKnowledgeElementService(
347 final KnowledgeElementService knowledgeElementService) {
348 _knowledgeElementService = knowledgeElementService;
352 * Get the simulationContextService.
354 * @return the simulationContextService
356 public SimulationContextService getSimulationContextService() {
357 return _simulationContextService;
361 * Set the simulationContextService.
363 * @param simulationContextService
364 * the simulationContextService to set
366 public void setSimulationContextService(
367 final SimulationContextService simulationContextService) {
368 _simulationContextService = simulationContextService;
372 * Get the searchService.
374 * @return the searchService
376 public SearchService getSearchService() {
377 return _searchService;
381 * Set the searchService.
383 * @param searchService
384 * the searchService to set
386 public void setSearchService(final SearchService searchService) {
387 _searchService = searchService;
391 * Get the menuProperty.
393 * @return the menuProperty
395 public String getMenuProperty() {
396 return _menuProperty;
400 * Set the menuProperty.
402 * @param menuProperty
403 * the menuProperty to set
405 public void setMenuProperty(final String menuProperty) {
406 this._menuProperty = menuProperty;
410 * Get the toolProperty.
412 * @return the toolProperty
414 public String getToolProperty() {
415 return _toolProperty;
419 * Set the toolProperty.
421 * @param toolProperty
422 * the toolProperty to set
424 public void setToolProperty(final String toolProperty) {
425 _toolProperty = toolProperty;
429 * Get the leftMenuProperty.
431 * @return the leftMenuProperty
433 public String getLeftMenuProperty() {
434 return _leftMenuProperty;
438 * Set the leftMenuProperty.
440 * @param leftMenuProperty
441 * the leftMenuProperty to set
443 public void setLeftMenuProperty(final String leftMenuProperty) {
444 _leftMenuProperty = leftMenuProperty;