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 * Context name comparator.
70 private class ContextNameComparator implements
71 Comparator<SimulationContextType> {
75 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
77 public int compare(SimulationContextType t1, SimulationContextType t2) {
78 String name1 = t1.getName();
79 String name2 = t2.getName();
81 return name1.compareToIgnoreCase(name2);
85 public class LocalizedContextTypes {
86 // ----------------------------------
87 private Locale locale;
88 private List<String> sortype; // Existing approved simulation context types ordered by localized names
90 public LocalizedContextTypes(List<SimulationContextType> types,
92 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
93 SimulationContextType[] tosort = types
94 .toArray(new SimulationContextType[types.size()]);
95 String[] name = new String[types.size()];
97 for (int i = 0; i < name.length; i++)
98 name[i] = bundle.getString("type.context."
99 + tosort[i].getName());
101 sortype = Arrays.asList(name);
105 public String getLocale() {
106 return locale.toString();
109 public List<String> getTypeNames() {
113 public boolean isCurrent() {
114 return (locale.equals(ApplicationSettings.getCurrentLocale()));
118 // ==============================================================================================================================
120 // ==============================================================================================================================
123 * Initialize the simulation context list.
124 * @return SUCCESS if succeeded, otherwise - ERROR
126 public String doInitialize() {
128 tocheck = getSimulationContextService()
129 .getSimulationContextsInState(ProgressState.inCHECK);
135 } catch (Exception error) {
136 logger.error("Reason:", error);
137 return ERROR; // No need to roll-back the transaction as it is read-only
141 public String doSelect() {
142 // -------------------------
143 Session connex = Database.getCurSession();
144 Transaction transax = connex.beginTransaction();
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>(
153 tocheck = new Vector<SimulationContextFacade>(context.size());
154 for (Iterator<SimulationContext> i = context.iterator(); i
156 SimulationContext next = i.next();
157 if (next.getIndex() == selection) {
159 selected.add(edition);
161 tocheck.add(new SimulationContextFacade(next,
162 getProjectSettings().getAllSteps()));
164 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
165 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
166 kprop.setSimulationContexts(selected).setState(
167 ProgressState.inWORK));
169 step = getSimulationContextService().getAttachedStep(
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);
180 facade = new ProjectElementFacade(next.getOwnerScenario(),
184 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
185 List<SimulationContextType> types = getSimulationContextService()
187 sprop.setProgressState(ProgressState.APPROVED));
188 Locale[] support = ApplicationSettings.getSupportedLocales();
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]));
195 SimulationContextType[] names = types
196 .toArray(new SimulationContextType[types.size()]);
198 Arrays.sort(names, new ContextNameComparator());
199 exisname = Arrays.asList(names);
201 existing = getSimulationContextService()
202 .selectSimulationContextsWhere(
203 cprop.setType(edition.getType()).setState(
204 ProgressState.APPROVED));
208 } catch (Exception error) {
209 logger.error("Reason:", error);
210 return ERROR; // No need to roll-back the transaction as it is read-only
214 // ==============================================================================================================================
215 // Getters and setters
216 // ==============================================================================================================================
218 public List<ProjectSettingsService.Step> getAllStudySteps() {
219 // -----------------------------------------------------
220 return getProjectSettings().getAllSteps();
224 * Get project settings.
226 * @return Project settings service
228 private ProjectSettingsService getProjectSettings() {
229 return _projectSettingsService;
233 * Set project settings service.
235 * @param projectSettingsService
236 * project settings service
238 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
239 _projectSettingsService = projectSettingsService;
242 public ProjectSettingsService.Step getAttachedStep() {
243 // ----------------------------------------------
247 public List<SimulationContextFacade> getContextsToBeApproved() {
248 // ---------------------------------------------------------------
252 public SimulationContext getEdited() {
253 // -------------------------------------
257 public List<SimulationContext> getExistingContexts() {
258 // -----------------------------------------------------
262 public List<SimulationContextType> getExistingNames() {
263 // ------------------------------------------------------
267 public List<LocalizedContextTypes> getSupportedLocales() {
268 // ---------------------------------------------------------
272 public Set<ProjectElementFacade> getIndexedElements() {
273 // ------------------------------------------------------
277 public int getSelection() {
278 // --------------------------
282 public void setSelection(String index) {
283 // ---------------------------------------
284 selection = Integer.valueOf(index);
288 * Get the knowledgeElementService.
290 * @return the knowledgeElementService
292 public KnowledgeElementService getKnowledgeElementService() {
293 return _knowledgeElementService;
297 * Set the knowledgeElementService.
299 * @param knowledgeElementService
300 * the knowledgeElementService to set
302 public void setKnowledgeElementService(
303 KnowledgeElementService knowledgeElementService) {
304 _knowledgeElementService = knowledgeElementService;
308 * Get the simulationContextService.
310 * @return the simulationContextService
312 public SimulationContextService getSimulationContextService() {
313 return _simulationContextService;
317 * Set the simulationContextService.
319 * @param simulationContextService
320 * the simulationContextService to set
322 public void setSimulationContextService(
323 SimulationContextService simulationContextService) {
324 _simulationContextService = simulationContextService;
328 * Get the searchService.
330 * @return the searchService
332 public SearchService getSearchService() {
333 return _searchService;
337 * Set the searchService.
339 * @param searchService
340 * the searchService to set
342 public void setSearchService(SearchService searchService) {
343 _searchService = searchService;