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 * Context name comparator.
82 private class ContextNameComparator implements
83 Comparator<SimulationContextType> {
87 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
89 public int compare(SimulationContextType t1, SimulationContextType t2) {
90 String name1 = t1.getName();
91 String name2 = t2.getName();
93 return name1.compareToIgnoreCase(name2);
97 public class LocalizedContextTypes {
98 // ----------------------------------
99 private Locale locale;
100 private List<String> sortype; // Existing approved simulation context types ordered by localized names
102 public LocalizedContextTypes(List<SimulationContextType> types,
104 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
105 SimulationContextType[] tosort = types
106 .toArray(new SimulationContextType[types.size()]);
107 String[] name = new String[types.size()];
109 for (int i = 0; i < name.length; i++)
110 name[i] = bundle.getString("type.context."
111 + tosort[i].getName());
113 sortype = Arrays.asList(name);
117 public String getLocale() {
118 return locale.toString();
121 public List<String> getTypeNames() {
125 public boolean isCurrent() {
126 return (locale.equals(ApplicationSettings.getCurrentLocale()));
130 // ==============================================================================================================================
132 // ==============================================================================================================================
135 * Initialize the simulation context list.
136 * @return SUCCESS if succeeded, otherwise - ERROR
138 public String doInitialize() {
140 tocheck = getSimulationContextService()
141 .getSimulationContextsInState(ProgressState.inCHECK);
146 setMenuProperty("study");
147 setToolProperty("none");
148 initializationScreenContext(_menuProperty, _toolProperty);
151 } catch (Exception error) {
152 logger.error("Reason:", error);
153 return ERROR; // No need to roll-back the transaction as it is read-only
157 public String doSelect() {
158 // -------------------------
160 setMenuProperty("study");
161 setToolProperty("none");
162 initializationScreenContext(_menuProperty, _toolProperty);
164 Session connex = Database.getCurSession();
165 Transaction transax = connex.beginTransaction();
167 SimulationContext.Properties cprop = new SimulationContext.Properties();
168 List<SimulationContext> context = getSimulationContextService()
169 .selectSimulationContextsWhere(
170 cprop.setState(ProgressState.inCHECK));
171 List<SimulationContext> selected = new ArrayList<SimulationContext>(
174 tocheck = new Vector<SimulationContextFacade>(context.size());
175 for (Iterator<SimulationContext> i = context.iterator(); i
177 SimulationContext next = i.next();
178 if (next.getIndex() == selection) {
180 selected.add(edition);
182 tocheck.add(new SimulationContextFacade(next,
183 getProjectSettings().getAllSteps()));
185 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
186 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
187 kprop.setSimulationContexts(selected).setState(
188 ProgressState.inWORK));
190 step = getSimulationContextService().getAttachedStep(
192 owner = new HashSet<ProjectElementFacade>();
193 for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
194 KnowledgeElement next = getKnowledgeElementService()
195 .selectKnowledgeElement(i.next().getIndex());
196 ProjectElementFacade facade;
197 if (step.appliesTo(Study.class))
198 facade = new ProjectElementFacade(next.getOwnerScenario()
199 .getOwnerStudy(), step);
201 facade = new ProjectElementFacade(next.getOwnerScenario(),
205 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
206 List<SimulationContextType> types = getSimulationContextService()
208 sprop.setProgressState(ProgressState.APPROVED));
209 Locale[] support = ApplicationSettings.getSupportedLocales();
211 // Sort localized type names
212 existype = new ArrayList<LocalizedContextTypes>(support.length);
213 for (int i = 0; i < support.length; i++) {
214 existype.add(new LocalizedContextTypes(types, support[i]));
216 SimulationContextType[] names = types
217 .toArray(new SimulationContextType[types.size()]);
219 Arrays.sort(names, new ContextNameComparator());
220 exisname = Arrays.asList(names);
222 existing = getSimulationContextService()
223 .selectSimulationContextsWhere(
224 cprop.setType(edition.getType()).setState(
225 ProgressState.APPROVED));
229 } catch (Exception error) {
230 logger.error("Reason:", error);
231 return ERROR; // No need to roll-back the transaction as it is read-only
235 // ==============================================================================================================================
236 // Getters and setters
237 // ==============================================================================================================================
239 public List<ProjectSettingsService.Step> getAllStudySteps() {
240 // -----------------------------------------------------
241 return getProjectSettings().getAllSteps();
245 * Get project settings.
247 * @return Project settings service
249 private ProjectSettingsService getProjectSettings() {
250 return _projectSettingsService;
254 * Set project settings service.
256 * @param projectSettingsService
257 * project settings service
259 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
260 _projectSettingsService = projectSettingsService;
263 public ProjectSettingsService.Step getAttachedStep() {
264 // ----------------------------------------------
268 public List<SimulationContextFacade> getContextsToBeApproved() {
269 // ---------------------------------------------------------------
273 public SimulationContext getEdited() {
274 // -------------------------------------
278 public List<SimulationContext> getExistingContexts() {
279 // -----------------------------------------------------
283 public List<SimulationContextType> getExistingNames() {
284 // ------------------------------------------------------
288 public List<LocalizedContextTypes> getSupportedLocales() {
289 // ---------------------------------------------------------
293 public Set<ProjectElementFacade> getIndexedElements() {
294 // ------------------------------------------------------
298 public int getSelection() {
299 // --------------------------
303 public void setSelection(String index) {
304 // ---------------------------------------
305 selection = Integer.valueOf(index);
309 * Get the knowledgeElementService.
311 * @return the knowledgeElementService
313 public KnowledgeElementService getKnowledgeElementService() {
314 return _knowledgeElementService;
318 * Set the knowledgeElementService.
320 * @param knowledgeElementService
321 * the knowledgeElementService to set
323 public void setKnowledgeElementService(
324 KnowledgeElementService knowledgeElementService) {
325 _knowledgeElementService = knowledgeElementService;
329 * Get the simulationContextService.
331 * @return the simulationContextService
333 public SimulationContextService getSimulationContextService() {
334 return _simulationContextService;
338 * Set the simulationContextService.
340 * @param simulationContextService
341 * the simulationContextService to set
343 public void setSimulationContextService(
344 SimulationContextService simulationContextService) {
345 _simulationContextService = simulationContextService;
349 * Get the searchService.
351 * @return the searchService
353 public SearchService getSearchService() {
354 return _searchService;
358 * Set the searchService.
360 * @param searchService
361 * the searchService to set
363 public void setSearchService(SearchService searchService) {
364 _searchService = searchService;
368 * Get the menuProperty.
369 * @return the menuProperty
371 public String getMenuProperty() {
372 return _menuProperty;
376 * Set the menuProperty.
377 * @param menuProperty
378 * the menuProperty to set
380 public void setMenuProperty(final String menuProperty) {
381 this._menuProperty = menuProperty;
385 * Get the toolProperty.
386 * @return the toolProperty
388 public String getToolProperty() {
389 return _toolProperty;
393 * Set the toolProperty.
394 * @param toolProperty the toolProperty to set
396 public void setToolProperty(final String toolProperty) {
397 _toolProperty = toolProperty;