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.technical.ProjectSettingsService;
25 import org.splat.service.dto.Proxy;
26 import org.splat.dal.bo.som.SimulationContext;
27 import org.splat.dal.bo.som.SimulationContextType;
28 import org.splat.dal.bo.som.Study;
31 public class SimulationContextAction extends Action {
33 private List<SimulationContextFacade> tocheck; // Simulation contexts to be approved
34 private int selection; // Index of the selected simulation context presented in the approval form
35 private SimulationContext edition; // Corresponding simulation context object
36 private ProjectSettingsService.Step step; // Study step to which the selected simulation context is attached
37 private Set<ProjectElementFacade> owner; // Study scenarios indexed by this simulation context
38 private List<LocalizedContextTypes> existype; // Existing approved simulation context types ordered by localized names
39 private List<SimulationContextType> exisname; // Existing approved simulation context types ordered by internal codes
40 private List<SimulationContext> existing; // Existing simulation contexts of selected type
41 private SearchService _searchService;
42 private ProjectSettingsService _projectSettingsService;
43 private KnowledgeElementService _knowledgeElementService;
45 private static final long serialVersionUID = 7083323229359094699L;
47 private class ContextNameComparator implements Comparator<SimulationContextType> {
48 // ---------------------------------------------------------------------------------
49 public int compare(SimulationContextType t1, SimulationContextType t2)
51 String name1 = t1.getName();
52 String name2 = t2.getName();
54 return name1.compareToIgnoreCase(name2);
58 public class LocalizedContextTypes {
59 // ----------------------------------
60 private Locale locale;
61 private List<String> sortype; // Existing approved simulation context types ordered by localized names
63 public LocalizedContextTypes (List<SimulationContextType> types, Locale lang)
65 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
66 SimulationContextType[] tosort = types.toArray( new SimulationContextType[types.size()] );
67 String[] name = new String[types.size()];
69 for (int i=0; i<name.length; i++) name[i] = bundle.getString("type.context." + tosort[i].getName());
71 sortype = Arrays.asList(name);
74 public String getLocale ()
76 return locale.toString();
78 public List<String> getTypeNames ()
82 public boolean isCurrent ()
84 return (locale.equals(ApplicationSettings.getCurrentLocale()));
88 // ==============================================================================================================================
90 // ==============================================================================================================================
92 public String doInitialize () {
93 // -----------------------------
94 Session connex = Database.getSession();
95 Transaction transax = connex.beginTransaction();
97 SimulationContext.Properties cprop = new SimulationContext.Properties();
98 List<SimulationContext> context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
100 tocheck = new Vector<SimulationContextFacade>(context.size());
101 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
102 tocheck.add( new SimulationContextFacade(i.next(), getProjectSettings().getAllSteps()) );
111 catch (Exception error) {
112 logger.error("Reason:", error);
113 return ERROR; // No need to roll-back the transaction as it is read-only
117 public String doSelect () {
118 // -------------------------
119 Session connex = Database.getSession();
120 Transaction transax = connex.beginTransaction();
122 SimulationContext.Properties cprop = new SimulationContext.Properties();
123 List<SimulationContext> context = Database.selectSimulationContextsWhere(cprop.setState(ProgressState.inCHECK));
124 List<SimulationContext> selected = new ArrayList<SimulationContext>(1);
126 tocheck = new Vector<SimulationContextFacade>(context.size());
127 for (Iterator<SimulationContext> i=context.iterator(); i.hasNext(); ) {
128 SimulationContext next = i.next();
129 if (next.getIndex() == selection) {
131 selected.add(edition);
133 tocheck.add( new SimulationContextFacade(next, getProjectSettings().getAllSteps()) );
135 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
136 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(kprop.setSimulationContexts(selected).setState(ProgressState.inWORK));
138 step = edition.getType().getAttachedStep();
139 owner = new HashSet<ProjectElementFacade>();
140 for (Iterator<Proxy> i=kelm.iterator(); i.hasNext(); ) {
141 KnowledgeElement next = getKnowledgeElementService().selectKnowledgeElement(i.next().getIndex());
142 ProjectElementFacade facade;
143 if (step.appliesTo(Study.class)) facade = new ProjectElementFacade(next.getOwnerScenario().getOwnerStudy(), step);
144 else facade = new ProjectElementFacade(next.getOwnerScenario(), step);
147 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
148 List<SimulationContextType> types = SimulationContext.selectTypesWhere(sprop.setState(ProgressState.APPROVED));
149 Locale[] support = ApplicationSettings.getSupportedLocales();
151 // Sort localized type names
152 existype = new ArrayList<LocalizedContextTypes>(support.length);
153 for (int i=0; i<support.length; i++) {
154 existype.add( new LocalizedContextTypes(types, support[i]) );
156 SimulationContextType[] names = types.toArray( new SimulationContextType[types.size()] );
158 Arrays.sort(names, new ContextNameComparator() );
159 exisname = Arrays.asList(names);
161 existing = Database.selectSimulationContextsWhere(cprop.setType(edition.getType()).setState(ProgressState.APPROVED));
166 catch (Exception error) {
167 logger.error("Reason:", error);
168 return ERROR; // No need to roll-back the transaction as it is read-only
172 // ==============================================================================================================================
173 // Getters and setters
174 // ==============================================================================================================================
179 public SearchService getSearchService() {
180 // TODO Auto-generated method stub
181 return _searchService;
184 public void setSearchService(SearchService searchService) {
185 _searchService = searchService;
188 public List<ProjectSettingsService.Step> getAllStudySteps () {
189 // -----------------------------------------------------
190 return getProjectSettings().getAllSteps();
193 * Get project settings.
194 * @return Project settings service
196 private ProjectSettingsService getProjectSettings() {
197 return _projectSettingsService;
201 * Set project settings service.
202 * @param projectSettingsService project settings service
204 public void setProjectSettings(
205 ProjectSettingsService projectSettingsService) {
206 _projectSettingsService = projectSettingsService;
209 public ProjectSettingsService.Step getAttachedStep () {
210 // ----------------------------------------------
213 public List<SimulationContextFacade> getContextsToBeApproved () {
214 // ---------------------------------------------------------------
217 public SimulationContext getEdited () {
218 // -------------------------------------
221 public List<SimulationContext> getExistingContexts () {
222 // -----------------------------------------------------
225 public List<SimulationContextType> getExistingNames () {
226 // ------------------------------------------------------
229 public List<LocalizedContextTypes> getSupportedLocales () {
230 // ---------------------------------------------------------
233 public Set<ProjectElementFacade> getIndexedElements () {
234 // ------------------------------------------------------
237 public int getSelection () {
238 // --------------------------
242 public void setSelection (String index) {
243 // ---------------------------------------
244 selection = Integer.valueOf(index);
248 * Get the knowledgeElementService.
249 * @return the knowledgeElementService
251 public KnowledgeElementService getKnowledgeElementService() {
252 return _knowledgeElementService;
256 * Set the knowledgeElementService.
257 * @param knowledgeElementService the knowledgeElementService to set
259 public void setKnowledgeElementService(
260 KnowledgeElementService knowledgeElementService) {
261 _knowledgeElementService = knowledgeElementService;