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;
13 import org.hibernate.Session;
14 import org.hibernate.Transaction;
15 import org.splat.dal.bo.som.KnowledgeElement;
16 import org.splat.dal.bo.som.ProgressState;
17 import org.splat.dal.bo.som.SimulationContext;
18 import org.splat.dal.bo.som.SimulationContextType;
19 import org.splat.dal.bo.som.Study;
20 import org.splat.dal.dao.som.Database;
21 import org.splat.service.KnowledgeElementService;
22 import org.splat.service.SearchService;
23 import org.splat.service.SimulationContextService;
24 import org.splat.service.dto.Proxy;
25 import org.splat.service.dto.SimulationContextFacade;
26 import org.splat.service.technical.ProjectSettingsService;
27 import org.splat.simer.Action;
28 import org.splat.simer.ApplicationSettings;
31 * Action for operations on simulation contexts.
33 public class SimulationContextAction extends Action {
38 private static final long serialVersionUID = 7083323229359094699L;
41 * Simulation contexts to be approved.
43 private transient List<SimulationContextFacade> _tocheck;
45 * Index of the selected simulation context presented in the approval form.
47 private int _selection;
49 * Corresponding simulation context object.
51 private transient SimulationContext _edition;
53 * Study step to which the selected simulation context is attached.
55 private transient ProjectSettingsService.Step _step;
57 * Study scenarios indexed by this simulation context.
59 private transient Set<ProjectElementFacade> _owner;
61 * Existing approved simulation context types ordered by localized names.
63 private transient List<LocalizedContextTypes> _existype;
65 * Existing approved simulation context types ordered by internal codes.
67 private transient List<SimulationContextType> _exisname;
69 * Existing simulation contexts of selected type.
71 private transient List<SimulationContext> _existing;
73 * Injected search service.
75 private SearchService _searchService;
77 * Injected project settings service.
79 private ProjectSettingsService _projectSettings;
81 * Injected knowledge element service.
83 private KnowledgeElementService _knowledgeElementService;
85 * Injected simulation context service.
87 private SimulationContextService _simulationContextService;
90 * Context name comparator.
92 private class ContextNameComparator implements
93 Comparator<SimulationContextType> {
97 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
99 public int compare(final SimulationContextType t1,
100 final SimulationContextType t2) {
101 String name1 = t1.getName();
102 String name2 = t2.getName();
104 return name1.compareToIgnoreCase(name2);
108 public class LocalizedContextTypes {
109 private transient final Locale _locale;
110 private transient final List<String> _sortype; // Existing approved simulation context types ordered by localized names
112 public LocalizedContextTypes(final List<SimulationContextType> types,
114 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
115 SimulationContextType[] tosort = types
116 .toArray(new SimulationContextType[types.size()]);
117 String[] name = new String[types.size()];
119 for (int i = 0; i < name.length; i++) {
120 name[i] = bundle.getString("type.context."
121 + tosort[i].getName());
124 _sortype = Arrays.asList(name);
128 public String getLocale() {
129 return _locale.toString();
132 public List<String> getTypeNames() {
136 public boolean isCurrent() {
137 return _locale.equals(getApplicationSettings().getCurrentLocale());
141 // ==============================================================================================================================
143 // ==============================================================================================================================
146 * Initialize the simulation context list.
148 * @return SUCCESS if succeeded, otherwise - ERROR
150 public String doInitialize() {
151 String res = SUCCESS;
153 _tocheck = getSimulationContextService()
154 .getSimulationContextsInState(ProgressState.inCHECK);
159 initializationFullScreenContext("study", "none", "open");
160 } catch (Exception error) {
161 LOG.error("Reason:", error);
162 res = ERROR; // No need to roll-back the transaction as it is read-only
167 public String doSelect() {
169 String res = SUCCESS;
170 initializationFullScreenContext("study", "none", "open");
172 Session connex = Database.getCurSession();
173 Transaction transax = connex.beginTransaction();
175 SimulationContext.Properties cprop = new SimulationContext.Properties();
176 List<SimulationContext> context = getSimulationContextService()
177 .selectSimulationContextsWhere(
178 cprop.setState(ProgressState.inCHECK));
179 List<SimulationContext> selected = new ArrayList<SimulationContext>(
182 _tocheck = new ArrayList<SimulationContextFacade>(context.size());
183 for (Iterator<SimulationContext> i = context.iterator(); i
185 SimulationContext next = i.next();
186 if (next.getIndex() == _selection) {
188 selected.add(_edition);
190 _tocheck.add(new SimulationContextFacade(next,
191 getProjectSettings().getAllSteps()));
193 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
194 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
195 kprop.setSimulationContexts(selected).setState(
196 ProgressState.inWORK));
198 _step = getSimulationContextService().getAttachedStep(
200 _owner = new HashSet<ProjectElementFacade>();
201 for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
202 KnowledgeElement next = getKnowledgeElementService()
203 .selectKnowledgeElement(i.next().getIndex());
204 ProjectElementFacade facade;
205 if (_step.appliesTo(Study.class)) {
206 facade = new ProjectElementFacade(next.getOwnerScenario()
207 .getOwnerStudy(), _step);
209 facade = new ProjectElementFacade(next.getOwnerScenario(),
214 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
215 List<SimulationContextType> types = getSimulationContextService()
217 sprop.setProgressState(ProgressState.APPROVED));
218 Locale[] support = ApplicationSettings.getSupportedLocales();
220 // Sort localized type names
221 _existype = new ArrayList<LocalizedContextTypes>(support.length);
222 for (int i = 0; i < support.length; i++) {
223 _existype.add(new LocalizedContextTypes(types, support[i]));
225 SimulationContextType[] names = types
226 .toArray(new SimulationContextType[types.size()]);
228 Arrays.sort(names, new ContextNameComparator());
229 _exisname = Arrays.asList(names);
231 _existing = getSimulationContextService()
232 .selectSimulationContextsWhere(
233 cprop.setType(_edition.getType()).setState(
234 ProgressState.APPROVED));
237 } catch (Exception error) {
238 LOG.error("Reason:", error);
239 res = ERROR; // No need to roll-back the transaction as it is read-only
244 // ==============================================================================================================================
245 // Getters and setters
246 // ==============================================================================================================================
248 public List<ProjectSettingsService.Step> getAllStudySteps() {
249 return getProjectSettings().getAllSteps();
253 * Get project settings.
255 * @return Project settings service
257 private ProjectSettingsService getProjectSettings() {
258 return _projectSettings;
262 * Set project settings service.
264 * @param projectSettingsService
265 * project settings service
267 public void setProjectSettings(
268 final ProjectSettingsService projectSettingsService) {
269 _projectSettings = projectSettingsService;
272 public ProjectSettingsService.Step getAttachedStep() {
276 public List<SimulationContextFacade> getContextsToBeApproved() {
280 public SimulationContext getEdited() {
284 public List<SimulationContext> getExistingContexts() {
288 public List<SimulationContextType> getExistingNames() {
292 public List<LocalizedContextTypes> getSupportedLocales() {
296 public Set<ProjectElementFacade> getIndexedElements() {
300 public int getSelection() {
304 public void setSelection(final String index) {
305 _selection = Integer.valueOf(index);
309 * Get the knowledgeElementService.
311 * @return the knowledgeElementService
313 public KnowledgeElementService getKnowledgeElementService() {
314 return _knowledgeElementService;
318 * Set the knowledgeElementService.
320 * @param knowledgeElementService
321 * the knowledgeElementService to set
323 public void setKnowledgeElementService(
324 final KnowledgeElementService knowledgeElementService) {
325 _knowledgeElementService = knowledgeElementService;
329 * Get the simulationContextService.
331 * @return the simulationContextService
333 public SimulationContextService getSimulationContextService() {
334 return _simulationContextService;
338 * Set the simulationContextService.
340 * @param simulationContextService
341 * the simulationContextService to set
343 public void setSimulationContextService(
344 final SimulationContextService simulationContextService) {
345 _simulationContextService = simulationContextService;
349 * Get the searchService.
351 * @return the searchService
353 public SearchService getSearchService() {
354 return _searchService;
358 * Set the searchService.
360 * @param searchService
361 * the searchService to set
363 public void setSearchService(final SearchService searchService) {
364 _searchService = searchService;