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.SearchService;
23 import org.splat.service.technical.ProjectSettingsService;
24 import org.splat.service.dto.Proxy;
25 import org.splat.dal.bo.som.SimulationContext;
26 import org.splat.dal.bo.som.SimulationContextType;
27 import org.splat.dal.bo.som.Study;
30 public class SimulationContextAction extends Action {
32 private List<SimulationContextFacade> tocheck; // Simulation contexts to be approved
33 private int selection; // Index of the selected simulation context presented in the approval form
34 private SimulationContext edition; // Corresponding simulation context object
35 private ProjectSettingsService.Step step; // Study step to which the selected simulation context is attached
36 private Set<ProjectElementFacade> owner; // Study scenarios indexed by this simulation context
37 private List<LocalizedContextTypes> existype; // Existing approved simulation context types ordered by localized names
38 private List<SimulationContextType> exisname; // Existing approved simulation context types ordered by internal codes
39 private List<SimulationContext> existing; // Existing simulation contexts of selected type
40 private SearchService _searchService;
41 private ProjectSettingsService _projectSettingsService;
43 private static final long serialVersionUID = 7083323229359094699L;
45 private class ContextNameComparator implements Comparator<SimulationContextType> {
46 // ---------------------------------------------------------------------------------
47 public int compare(SimulationContextType t1, SimulationContextType t2)
49 String name1 = t1.getName();
50 String name2 = t2.getName();
52 return name1.compareToIgnoreCase(name2);
56 public class LocalizedContextTypes {
57 // ----------------------------------
58 private Locale locale;
59 private List<String> sortype; // Existing approved simulation context types ordered by localized names
61 public LocalizedContextTypes (List<SimulationContextType> types, Locale lang)
63 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
64 SimulationContextType[] tosort = types.toArray( new SimulationContextType[types.size()] );
65 String[] name = new String[types.size()];
67 for (int i=0; i<name.length; i++) name[i] = bundle.getString("type.context." + tosort[i].getName());
69 sortype = Arrays.asList(name);
72 public String getLocale ()
74 return locale.toString();
76 public List<String> getTypeNames ()
80 public boolean isCurrent ()
82 return (locale.equals(ApplicationSettings.getCurrentLocale()));
86 // ==============================================================================================================================
88 // ==============================================================================================================================
90 public String doInitialize () {
91 // -----------------------------
92 Session connex = Database.getSession();
93 Transaction transax = connex.beginTransaction();
95 SimulationContext.Properties cprop = new SimulationContext.Properties();
96 List<SimulationContext> context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
98 tocheck = new Vector<SimulationContextFacade>(context.size());
99 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
100 tocheck.add( new SimulationContextFacade(i.next()) );
109 catch (Exception error) {
110 logger.error("Reason:", error);
111 return ERROR; // No need to roll-back the transaction as it is read-only
115 public String doSelect () {
116 // -------------------------
117 Session connex = Database.getSession();
118 Transaction transax = connex.beginTransaction();
120 SimulationContext.Properties cprop = new SimulationContext.Properties();
121 List<SimulationContext> context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
122 List<SimulationContext> selected = new ArrayList<SimulationContext>(1);
124 tocheck = new Vector<SimulationContextFacade>(context.size());
125 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
126 SimulationContext next = i.next();
127 if (next.getIndex() == selection) {
129 selected.add(edition);
131 tocheck.add( new SimulationContextFacade(next) );
133 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
134 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(kprop.setSimulationContexts(selected).setState(ProgressState.inWORK));
136 step = edition.getType().getAttachedStep();
137 owner = new HashSet<ProjectElementFacade>();
138 for (Iterator<Proxy> i=kelm.iterator(); i.hasNext(); ) {
139 KnowledgeElement next = Database.selectKnowledgeElement(i.next().getIndex());
140 ProjectElementFacade facade;
141 if (step.appliesTo(Study.class)) facade = new ProjectElementFacade(next.getOwnerScenario().getOwnerStudy(), step);
142 else facade = new ProjectElementFacade(next.getOwnerScenario(), step);
145 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
146 List<SimulationContextType> types = SimulationContext.selectTypesWhere(sprop.setState(ProgressState.APPROVED));
147 Locale[] support = ApplicationSettings.getSupportedLocales();
149 // Sort localized type names
150 existype = new ArrayList<LocalizedContextTypes>(support.length);
151 for (int i=0; i<support.length; i++) {
152 existype.add( new LocalizedContextTypes(types, support[i]) );
154 SimulationContextType[] names = types.toArray( new SimulationContextType[types.size()] );
156 Arrays.sort(names, new ContextNameComparator() );
157 exisname = Arrays.asList(names);
159 existing = Database.selectSimulationContextsWhere(cprop.setType(edition.getType()).setState(ProgressState.APPROVED));
164 catch (Exception error) {
165 logger.error("Reason:", error);
166 return ERROR; // No need to roll-back the transaction as it is read-only
170 // ==============================================================================================================================
171 // Getters and setters
172 // ==============================================================================================================================
177 public SearchService getSearchService() {
178 // TODO Auto-generated method stub
179 return _searchService;
182 public void setSearchService(SearchService searchService) {
183 _searchService = searchService;
186 public List<ProjectSettingsService.Step> getAllStudySteps () {
187 // -----------------------------------------------------
188 return getProjectSettings().getAllSteps();
191 * Get project settings.
192 * @return Project settings service
194 private ProjectSettingsService getProjectSettings() {
195 return _projectSettingsService;
199 * Set project settings service.
200 * @param projectSettingsService project settings service
202 public void setProjectSettings(
203 ProjectSettingsService projectSettingsService) {
204 _projectSettingsService = projectSettingsService;
207 public ProjectSettingsService.Step getAttachedStep () {
208 // ----------------------------------------------
211 public List<SimulationContextFacade> getContextsToBeApproved () {
212 // ---------------------------------------------------------------
215 public SimulationContext getEdited () {
216 // -------------------------------------
219 public List<SimulationContext> getExistingContexts () {
220 // -----------------------------------------------------
223 public List<SimulationContextType> getExistingNames () {
224 // ------------------------------------------------------
227 public List<LocalizedContextTypes> getSupportedLocales () {
228 // ---------------------------------------------------------
231 public Set<ProjectElementFacade> getIndexedElements () {
232 // ------------------------------------------------------
235 public int getSelection () {
236 // --------------------------
240 public void setSelection (String index) {
241 // ---------------------------------------
242 selection = Integer.valueOf(index);