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;
12 import java.util.Vector;
14 import org.hibernate.Session;
15 import org.hibernate.Transaction;
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;
33 * Action for operations on simulation contexts.
35 public class SimulationContextAction extends Action {
40 private static final long serialVersionUID = 7083323229359094699L;
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
51 * Injected search service.
53 private SearchService _searchService;
55 * Injected project settings service.
57 private ProjectSettingsService _projectSettingsService;
59 * Injected knowledge element service.
61 private KnowledgeElementService _knowledgeElementService;
63 * Injected simulation context service.
65 private SimulationContextService _simulationContextService;
68 * Value of the menu property.
69 * It can be: none, create, open, study, knowledge, sysadmin, help.
71 private String _menuProperty;
74 * Value of the tool bar property.
75 * It can be: none, standard, study, back.
77 private String _toolProperty;
80 * Value of the left menu property.
81 * It can be: open, study, knowledge, scenario.
83 private String _leftMenuProperty;
86 * Context name comparator.
88 private class ContextNameComparator implements
89 Comparator<SimulationContextType> {
93 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
95 public int compare(SimulationContextType t1, SimulationContextType t2) {
96 String name1 = t1.getName();
97 String name2 = t2.getName();
99 return name1.compareToIgnoreCase(name2);
103 public class LocalizedContextTypes {
104 // ----------------------------------
105 private Locale locale;
106 private List<String> sortype; // Existing approved simulation context types ordered by localized names
108 public LocalizedContextTypes(List<SimulationContextType> types,
110 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
111 SimulationContextType[] tosort = types
112 .toArray(new SimulationContextType[types.size()]);
113 String[] name = new String[types.size()];
115 for (int i = 0; i < name.length; i++)
116 name[i] = bundle.getString("type.context."
117 + tosort[i].getName());
119 sortype = Arrays.asList(name);
123 public String getLocale() {
124 return locale.toString();
127 public List<String> getTypeNames() {
131 public boolean isCurrent() {
132 return (locale.equals(ApplicationSettings.getCurrentLocale()));
136 // ==============================================================================================================================
138 // ==============================================================================================================================
141 * Initialize the simulation context list.
142 * @return SUCCESS if succeeded, otherwise - ERROR
144 public String doInitialize() {
146 tocheck = getSimulationContextService()
147 .getSimulationContextsInState(ProgressState.inCHECK);
152 setMenuProperty("study");
153 setToolProperty("none");
154 setLeftMenuProperty("open");
155 initializationFullScreenContext(_menuProperty, _toolProperty, _leftMenuProperty);
158 } catch (Exception error) {
159 logger.error("Reason:", error);
160 return ERROR; // No need to roll-back the transaction as it is read-only
164 public String doSelect() {
165 // -------------------------
167 setMenuProperty("study");
168 setToolProperty("none");
169 setLeftMenuProperty("open");
170 initializationFullScreenContext(_menuProperty, _toolProperty, _leftMenuProperty);
172 Session connex = Database.getCurSession();
173 Transaction transax = connex.beginTransaction();
175 SimulationContext.Properties cprop = new SimulationContext.Properties();
176 List<SimulationContext> context = getSimulationContextService()
177 .selectSimulationContextsWhere(
178 cprop.setState(ProgressState.inCHECK));
179 List<SimulationContext> selected = new ArrayList<SimulationContext>(
182 tocheck = new Vector<SimulationContextFacade>(context.size());
183 for (Iterator<SimulationContext> i = context.iterator(); i
185 SimulationContext next = i.next();
186 if (next.getIndex() == selection) {
188 selected.add(edition);
190 tocheck.add(new SimulationContextFacade(next,
191 getProjectSettings().getAllSteps()));
193 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
194 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
195 kprop.setSimulationContexts(selected).setState(
196 ProgressState.inWORK));
198 step = getSimulationContextService().getAttachedStep(
200 owner = new HashSet<ProjectElementFacade>();
201 for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
202 KnowledgeElement next = getKnowledgeElementService()
203 .selectKnowledgeElement(i.next().getIndex());
204 ProjectElementFacade facade;
205 if (step.appliesTo(Study.class))
206 facade = new ProjectElementFacade(next.getOwnerScenario()
207 .getOwnerStudy(), step);
209 facade = new ProjectElementFacade(next.getOwnerScenario(),
213 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
214 List<SimulationContextType> types = getSimulationContextService()
216 sprop.setProgressState(ProgressState.APPROVED));
217 Locale[] support = ApplicationSettings.getSupportedLocales();
219 // Sort localized type names
220 existype = new ArrayList<LocalizedContextTypes>(support.length);
221 for (int i = 0; i < support.length; i++) {
222 existype.add(new LocalizedContextTypes(types, support[i]));
224 SimulationContextType[] names = types
225 .toArray(new SimulationContextType[types.size()]);
227 Arrays.sort(names, new ContextNameComparator());
228 exisname = Arrays.asList(names);
230 existing = getSimulationContextService()
231 .selectSimulationContextsWhere(
232 cprop.setType(edition.getType()).setState(
233 ProgressState.APPROVED));
237 } catch (Exception error) {
238 logger.error("Reason:", error);
239 return ERROR; // No need to roll-back the transaction as it is read-only
243 // ==============================================================================================================================
244 // Getters and setters
245 // ==============================================================================================================================
247 public List<ProjectSettingsService.Step> getAllStudySteps() {
248 // -----------------------------------------------------
249 return getProjectSettings().getAllSteps();
253 * Get project settings.
255 * @return Project settings service
257 private ProjectSettingsService getProjectSettings() {
258 return _projectSettingsService;
262 * Set project settings service.
264 * @param projectSettingsService
265 * project settings service
267 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
268 _projectSettingsService = projectSettingsService;
271 public ProjectSettingsService.Step getAttachedStep() {
272 // ----------------------------------------------
276 public List<SimulationContextFacade> getContextsToBeApproved() {
277 // ---------------------------------------------------------------
281 public SimulationContext getEdited() {
282 // -------------------------------------
286 public List<SimulationContext> getExistingContexts() {
287 // -----------------------------------------------------
291 public List<SimulationContextType> getExistingNames() {
292 // ------------------------------------------------------
296 public List<LocalizedContextTypes> getSupportedLocales() {
297 // ---------------------------------------------------------
301 public Set<ProjectElementFacade> getIndexedElements() {
302 // ------------------------------------------------------
306 public int getSelection() {
307 // --------------------------
311 public void setSelection(String index) {
312 // ---------------------------------------
313 selection = Integer.valueOf(index);
317 * Get the knowledgeElementService.
319 * @return the knowledgeElementService
321 public KnowledgeElementService getKnowledgeElementService() {
322 return _knowledgeElementService;
326 * Set the knowledgeElementService.
328 * @param knowledgeElementService
329 * the knowledgeElementService to set
331 public void setKnowledgeElementService(
332 KnowledgeElementService knowledgeElementService) {
333 _knowledgeElementService = knowledgeElementService;
337 * Get the simulationContextService.
339 * @return the simulationContextService
341 public SimulationContextService getSimulationContextService() {
342 return _simulationContextService;
346 * Set the simulationContextService.
348 * @param simulationContextService
349 * the simulationContextService to set
351 public void setSimulationContextService(
352 SimulationContextService simulationContextService) {
353 _simulationContextService = simulationContextService;
357 * Get the searchService.
359 * @return the searchService
361 public SearchService getSearchService() {
362 return _searchService;
366 * Set the searchService.
368 * @param searchService
369 * the searchService to set
371 public void setSearchService(SearchService searchService) {
372 _searchService = searchService;
376 * Get the menuProperty.
377 * @return the menuProperty
379 public String getMenuProperty() {
380 return _menuProperty;
384 * Set the menuProperty.
385 * @param menuProperty
386 * the menuProperty to set
388 public void setMenuProperty(final String menuProperty) {
389 this._menuProperty = menuProperty;
393 * Get the toolProperty.
394 * @return the toolProperty
396 public String getToolProperty() {
397 return _toolProperty;
401 * Set the toolProperty.
402 * @param toolProperty the toolProperty to set
404 public void setToolProperty(final String toolProperty) {
405 _toolProperty = toolProperty;
409 * Get the leftMenuProperty.
410 * @return the leftMenuProperty
412 public String getLeftMenuProperty() {
413 return _leftMenuProperty;
417 * Set the leftMenuProperty.
418 * @param leftMenuProperty the leftMenuProperty to set
420 public void setLeftMenuProperty(final String leftMenuProperty) {
421 _leftMenuProperty = leftMenuProperty;