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.dal.bo.som.SimulationContext;
28 import org.splat.dal.bo.som.SimulationContextType;
29 import org.splat.dal.bo.som.Study;
32 * Action for operations on simulation contexts.
34 public class SimulationContextAction extends Action {
39 private static final long serialVersionUID = 7083323229359094699L;
41 private List<SimulationContextFacade> tocheck; // Simulation contexts to be approved
42 private int selection; // Index of the selected simulation context presented in the approval form
43 private SimulationContext edition; // Corresponding simulation context object
44 private ProjectSettingsService.Step step; // Study step to which the selected simulation context is attached
45 private Set<ProjectElementFacade> owner; // Study scenarios indexed by this simulation context
46 private List<LocalizedContextTypes> existype; // Existing approved simulation context types ordered by localized names
47 private List<SimulationContextType> exisname; // Existing approved simulation context types ordered by internal codes
48 private List<SimulationContext> existing; // Existing simulation contexts of selected type
50 * Injected search service.
52 private SearchService _searchService;
54 * Injected project settings service.
56 private ProjectSettingsService _projectSettingsService;
58 * Injected knowledge element service.
60 private KnowledgeElementService _knowledgeElementService;
62 * Injected simulation context service.
64 private SimulationContextService _simulationContextService;
67 * Context name comparator.
69 private class ContextNameComparator implements
70 Comparator<SimulationContextType> {
74 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
76 public int compare(SimulationContextType t1, SimulationContextType t2) {
77 String name1 = t1.getName();
78 String name2 = t2.getName();
80 return name1.compareToIgnoreCase(name2);
84 public class LocalizedContextTypes {
85 // ----------------------------------
86 private Locale locale;
87 private List<String> sortype; // Existing approved simulation context types ordered by localized names
89 public LocalizedContextTypes(List<SimulationContextType> types,
91 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
92 SimulationContextType[] tosort = types
93 .toArray(new SimulationContextType[types.size()]);
94 String[] name = new String[types.size()];
96 for (int i = 0; i < name.length; i++)
97 name[i] = bundle.getString("type.context."
98 + tosort[i].getName());
100 sortype = Arrays.asList(name);
104 public String getLocale() {
105 return locale.toString();
108 public List<String> getTypeNames() {
112 public boolean isCurrent() {
113 return (locale.equals(ApplicationSettings.getCurrentLocale()));
117 // ==============================================================================================================================
119 // ==============================================================================================================================
121 public String doInitialize() {
122 // -----------------------------
123 Session connex = Database.getCurSession();
124 Transaction transax = connex.beginTransaction();
126 SimulationContext.Properties cprop = new SimulationContext.Properties();
127 List<SimulationContext> context = getSimulationContextService()
128 .selectSimulationContextsWhere(
129 cprop.setState(ProgressState.inCHECK));
131 tocheck = new Vector<SimulationContextFacade>(context.size());
132 for (Iterator<SimulationContext> i = context.iterator(); i
134 tocheck.add(new SimulationContextFacade(i.next(),
135 getProjectSettings().getAllSteps()));
143 } catch (Exception error) {
144 logger.error("Reason:", error);
145 return ERROR; // No need to roll-back the transaction as it is read-only
149 public String doSelect() {
150 // -------------------------
151 Session connex = Database.getCurSession();
152 Transaction transax = connex.beginTransaction();
154 SimulationContext.Properties cprop = new SimulationContext.Properties();
155 List<SimulationContext> context = getSimulationContextService()
156 .selectSimulationContextsWhere(
157 cprop.setState(ProgressState.inCHECK));
158 List<SimulationContext> selected = new ArrayList<SimulationContext>(
161 tocheck = new Vector<SimulationContextFacade>(context.size());
162 for (Iterator<SimulationContext> i = context.iterator(); i
164 SimulationContext next = i.next();
165 if (next.getIndex() == selection) {
167 selected.add(edition);
169 tocheck.add(new SimulationContextFacade(next,
170 getProjectSettings().getAllSteps()));
172 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
173 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
174 kprop.setSimulationContexts(selected).setState(
175 ProgressState.inWORK));
177 step = getSimulationContextService().getAttachedStep(edition.getType());
178 owner = new HashSet<ProjectElementFacade>();
179 for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
180 KnowledgeElement next = getKnowledgeElementService()
181 .selectKnowledgeElement(i.next().getIndex());
182 ProjectElementFacade facade;
183 if (step.appliesTo(Study.class))
184 facade = new ProjectElementFacade(next.getOwnerScenario()
185 .getOwnerStudy(), step);
187 facade = new ProjectElementFacade(next.getOwnerScenario(),
191 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
192 List<SimulationContextType> types = getSimulationContextService()
193 .selectTypesWhere(sprop.setProgressState(ProgressState.APPROVED));
194 Locale[] support = ApplicationSettings.getSupportedLocales();
196 // Sort localized type names
197 existype = new ArrayList<LocalizedContextTypes>(support.length);
198 for (int i = 0; i < support.length; i++) {
199 existype.add(new LocalizedContextTypes(types, support[i]));
201 SimulationContextType[] names = types
202 .toArray(new SimulationContextType[types.size()]);
204 Arrays.sort(names, new ContextNameComparator());
205 exisname = Arrays.asList(names);
207 existing = getSimulationContextService()
208 .selectSimulationContextsWhere(
209 cprop.setType(edition.getType()).setState(
210 ProgressState.APPROVED));
214 } catch (Exception error) {
215 logger.error("Reason:", error);
216 return ERROR; // No need to roll-back the transaction as it is read-only
220 // ==============================================================================================================================
221 // Getters and setters
222 // ==============================================================================================================================
224 public List<ProjectSettingsService.Step> getAllStudySteps() {
225 // -----------------------------------------------------
226 return getProjectSettings().getAllSteps();
230 * Get project settings.
232 * @return Project settings service
234 private ProjectSettingsService getProjectSettings() {
235 return _projectSettingsService;
239 * Set project settings service.
241 * @param projectSettingsService
242 * project settings service
244 public void setProjectSettings(ProjectSettingsService projectSettingsService) {
245 _projectSettingsService = projectSettingsService;
248 public ProjectSettingsService.Step getAttachedStep() {
249 // ----------------------------------------------
253 public List<SimulationContextFacade> getContextsToBeApproved() {
254 // ---------------------------------------------------------------
258 public SimulationContext getEdited() {
259 // -------------------------------------
263 public List<SimulationContext> getExistingContexts() {
264 // -----------------------------------------------------
268 public List<SimulationContextType> getExistingNames() {
269 // ------------------------------------------------------
273 public List<LocalizedContextTypes> getSupportedLocales() {
274 // ---------------------------------------------------------
278 public Set<ProjectElementFacade> getIndexedElements() {
279 // ------------------------------------------------------
283 public int getSelection() {
284 // --------------------------
288 public void setSelection(String index) {
289 // ---------------------------------------
290 selection = Integer.valueOf(index);
294 * Get the knowledgeElementService.
296 * @return the knowledgeElementService
298 public KnowledgeElementService getKnowledgeElementService() {
299 return _knowledgeElementService;
303 * Set the knowledgeElementService.
305 * @param knowledgeElementService
306 * the knowledgeElementService to set
308 public void setKnowledgeElementService(
309 KnowledgeElementService knowledgeElementService) {
310 _knowledgeElementService = knowledgeElementService;
314 * Get the simulationContextService.
316 * @return the simulationContextService
318 public SimulationContextService getSimulationContextService() {
319 return _simulationContextService;
323 * Set the simulationContextService.
325 * @param simulationContextService
326 * the simulationContextService to set
328 public void setSimulationContextService(
329 SimulationContextService simulationContextService) {
330 _simulationContextService = simulationContextService;
334 * Get the searchService.
335 * @return the searchService
337 public SearchService getSearchService() {
338 return _searchService;
342 * Set the searchService.
343 * @param searchService the searchService to set
345 public void setSearchService(SearchService searchService) {
346 _searchService = searchService;