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.som.Database;
20 import org.splat.som.KnowledgeElement;
21 import org.splat.som.ProgressState;
22 import org.splat.som.ProjectSettings;
23 import org.splat.som.Proxy;
24 import org.splat.som.SimulationContext;
25 import org.splat.som.SimulationContextType;
26 import org.splat.som.Study;
29 public class SimulationContextAction extends Action {
31 private List<SimulationContextFacade> tocheck; // Simulation contexts to be approved
32 private int selection; // Index of the selected simulation context presented in the approval form
33 private SimulationContext edition; // Corresponding simulation context object
34 private ProjectSettings.Step step; // Study step to which the selected simulation context is attached
35 private Set<ProjectElementFacade> owner; // Study scenarios indexed by this simulation context
36 private List<LocalizedContextTypes> existype; // Existing approved simulation context types ordered by localized names
37 private List<SimulationContextType> exisname; // Existing approved simulation context types ordered by internal codes
38 private List<SimulationContext> existing; // Existing simulation contexts of selected type
40 private static final long serialVersionUID = 7083323229359094699L;
42 private class ContextNameComparator implements Comparator<SimulationContextType> {
43 // ---------------------------------------------------------------------------------
44 public int compare(SimulationContextType t1, SimulationContextType t2)
46 String name1 = t1.getName();
47 String name2 = t2.getName();
49 return name1.compareToIgnoreCase(name2);
53 public class LocalizedContextTypes {
54 // ----------------------------------
55 private Locale locale;
56 private List<String> sortype; // Existing approved simulation context types ordered by localized names
58 public LocalizedContextTypes (List<SimulationContextType> types, Locale lang)
60 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
61 SimulationContextType[] tosort = types.toArray( new SimulationContextType[types.size()] );
62 String[] name = new String[types.size()];
64 for (int i=0; i<name.length; i++) name[i] = bundle.getString("type.context." + tosort[i].getName());
66 sortype = Arrays.asList(name);
69 public String getLocale ()
71 return locale.toString();
73 public List<String> getTypeNames ()
77 public boolean isCurrent ()
79 return (locale.equals(ApplicationSettings.getCurrentLocale()));
83 // ==============================================================================================================================
85 // ==============================================================================================================================
87 public String doInitialize () {
88 // -----------------------------
89 Session connex = Database.getSession();
90 Transaction transax = connex.beginTransaction();
92 SimulationContext.Properties cprop = new SimulationContext.Properties();
93 List<SimulationContext> context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
95 tocheck = new Vector<SimulationContextFacade>(context.size());
96 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
97 tocheck.add( new SimulationContextFacade(i.next()) );
106 catch (Exception error) {
107 logger.error("Reason:", error);
108 return ERROR; // No need to roll-back the transaction as it is read-only
112 public String doSelect () {
113 // -------------------------
114 Session connex = Database.getSession();
115 Transaction transax = connex.beginTransaction();
117 SimulationContext.Properties cprop = new SimulationContext.Properties();
118 List<SimulationContext> context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
119 List<SimulationContext> selected = new ArrayList<SimulationContext>(1);
121 tocheck = new Vector<SimulationContextFacade>(context.size());
122 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
123 SimulationContext next = i.next();
124 if (next.getIndex() == selection) {
126 selected.add(edition);
128 tocheck.add( new SimulationContextFacade(next) );
130 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
131 List<Proxy> kelm = Database.selectKnowledgeElementsWhere(kprop.setSimulationContexts(selected).setState(ProgressState.inWORK));
133 step = edition.getType().getAttachedStep();
134 owner = new HashSet<ProjectElementFacade>();
135 for (Iterator<Proxy> i=kelm.iterator(); i.hasNext(); ) {
136 KnowledgeElement next = Database.selectKnowledgeElement(i.next().getIndex());
137 ProjectElementFacade facade;
138 if (step.appliesTo(Study.class)) facade = new ProjectElementFacade(next.getOwnerScenario().getOwnerStudy(), step);
139 else facade = new ProjectElementFacade(next.getOwnerScenario(), step);
142 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
143 List<SimulationContextType> types = SimulationContext.selectTypesWhere(sprop.setState(ProgressState.APPROVED));
144 Locale[] support = ApplicationSettings.getSupportedLocales();
146 // Sort localized type names
147 existype = new ArrayList<LocalizedContextTypes>(support.length);
148 for (int i=0; i<support.length; i++) {
149 existype.add( new LocalizedContextTypes(types, support[i]) );
151 SimulationContextType[] names = types.toArray( new SimulationContextType[types.size()] );
153 Arrays.sort(names, new ContextNameComparator() );
154 exisname = Arrays.asList(names);
156 existing = Database.selectSimulationContextsWhere(cprop.setType(edition.getType()).setState(ProgressState.APPROVED));
161 catch (Exception error) {
162 logger.error("Reason:", error);
163 return ERROR; // No need to roll-back the transaction as it is read-only
167 // ==============================================================================================================================
168 // Getters and setters
169 // ==============================================================================================================================
171 public List<ProjectSettings.Step> getAllStudySteps () {
172 // -----------------------------------------------------
173 return ProjectSettings.getAllSteps();
175 public ProjectSettings.Step getAttachedStep () {
176 // ----------------------------------------------
179 public List<SimulationContextFacade> getContextsToBeApproved () {
180 // ---------------------------------------------------------------
183 public SimulationContext getEdited () {
184 // -------------------------------------
187 public List<SimulationContext> getExistingContexts () {
188 // -----------------------------------------------------
191 public List<SimulationContextType> getExistingNames () {
192 // ------------------------------------------------------
195 public List<LocalizedContextTypes> getSupportedLocales () {
196 // ---------------------------------------------------------
199 public Set<ProjectElementFacade> getIndexedElements () {
200 // ------------------------------------------------------
203 public int getSelection () {
204 // --------------------------
208 public void setSelection (String index) {
209 // ---------------------------------------
210 selection = Integer.valueOf(index);