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.KnowledgeSearchFilterDTO;
25 import org.splat.service.dto.Proxy;
26 import org.splat.service.dto.SimulationContextFacade;
27 import org.splat.service.technical.ProjectSettingsService;
28 import org.splat.simer.Action;
29 import org.splat.simer.ApplicationSettings;
32 * Action for operations on simulation contexts.
34 public class SimulationContextAction extends Action {
39 private static final long serialVersionUID = 7083323229359094699L;
42 * Simulation contexts to be approved.
44 private transient List<SimulationContextFacade> _tocheck;
46 * Index of the selected simulation context presented in the approval form.
48 private int _selection;
50 * Corresponding simulation context object.
52 private transient SimulationContext _edition;
54 * Study step to which the selected simulation context is attached.
56 private transient ProjectSettingsService.Step _step;
58 * Study scenarios indexed by this simulation context.
60 private transient Set<ProjectElementFacade> _owner;
62 * Existing approved simulation context types ordered by localized names.
64 private transient List<LocalizedContextTypes> _existype;
66 * Existing approved simulation context types ordered by internal codes.
68 private transient List<SimulationContextType> _exisname;
70 * Existing simulation contexts of selected type.
72 private transient List<SimulationContext> _existing;
74 * Injected search service.
76 private SearchService _searchService;
78 * Injected project settings service.
80 private ProjectSettingsService _projectSettings;
82 * Injected knowledge element service.
84 private KnowledgeElementService _knowledgeElementService;
86 * Injected simulation context service.
88 private SimulationContextService _simulationContextService;
91 * Context name comparator.
93 private class ContextNameComparator implements
94 Comparator<SimulationContextType> {
98 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
100 public int compare(final SimulationContextType t1,
101 final SimulationContextType t2) {
102 String name1 = t1.getName();
103 String name2 = t2.getName();
105 return name1.compareToIgnoreCase(name2);
109 public class LocalizedContextTypes {
110 private transient final Locale _locale;
111 private transient final List<String> _sortype; // Existing approved simulation context types ordered by localized names
113 public LocalizedContextTypes(final List<SimulationContextType> types,
115 ResourceBundle bundle = ResourceBundle.getBundle("som", lang);
116 SimulationContextType[] tosort = types
117 .toArray(new SimulationContextType[types.size()]);
118 String[] name = new String[types.size()];
120 for (int i = 0; i < name.length; i++) {
121 name[i] = bundle.getString("type.context."
122 + tosort[i].getName());
125 _sortype = Arrays.asList(name);
129 public String getLocale() {
130 return _locale.toString();
133 public List<String> getTypeNames() {
137 public boolean isCurrent() {
138 return _locale.equals(getApplicationSettings().getCurrentLocale());
142 // ==============================================================================================================================
144 // ==============================================================================================================================
147 * Initialize the simulation context list.
149 * @return SUCCESS if succeeded, otherwise - ERROR
151 public String doInitialize() {
152 String res = SUCCESS;
154 _tocheck = getSimulationContextService()
155 .getSimulationContextsInState(ProgressState.inCHECK);
160 initializationFullScreenContext("study", "none", "open");
161 } catch (Exception error) {
162 LOG.error("Reason:", error);
163 res = ERROR; // No need to roll-back the transaction as it is read-only
168 public String doSelect() {
170 String res = SUCCESS;
171 initializationFullScreenContext("study", "none", "open");
173 Session connex = Database.getCurSession();
174 Transaction transax = connex.beginTransaction();
176 SimulationContext.Properties cprop = new SimulationContext.Properties();
177 List<SimulationContext> context = getSimulationContextService()
178 .selectSimulationContextsWhere(
179 cprop.setState(ProgressState.inCHECK));
180 List<SimulationContext> selected = new ArrayList<SimulationContext>(
183 _tocheck = new ArrayList<SimulationContextFacade>(context.size());
184 for (Iterator<SimulationContext> i = context.iterator(); i
186 SimulationContext next = i.next();
187 if (next.getIndex() == _selection) {
189 selected.add(_edition);
191 _tocheck.add(new SimulationContextFacade(next,
192 getProjectSettings().getAllSteps()));
194 // KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
195 // List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(
196 // kprop.setSimulationContexts(selected).setState(
197 // ProgressState.inWORK));
198 KnowledgeSearchFilterDTO knowFilter = new KnowledgeSearchFilterDTO();
199 knowFilter.setSimContexts(selected);
200 knowFilter.setState(ProgressState.inWORK);
201 List<Proxy> kelm = getSearchService().selectKnowledgeElementsWhere(knowFilter);
204 _step = getSimulationContextService().getAttachedStep(
206 _owner = new HashSet<ProjectElementFacade>();
207 for (Iterator<Proxy> i = kelm.iterator(); i.hasNext();) {
208 KnowledgeElement next = getKnowledgeElementService()
209 .selectKnowledgeElement(i.next().getIndex());
210 ProjectElementFacade facade;
211 if (_step.appliesTo(Study.class)) {
212 facade = new ProjectElementFacade(next.getOwnerScenario()
213 .getOwnerStudy(), _step);
215 facade = new ProjectElementFacade(next.getOwnerScenario(),
220 SimulationContextType.Properties sprop = new SimulationContextType.Properties();
221 List<SimulationContextType> types = getSimulationContextService()
223 sprop.setProgressState(ProgressState.APPROVED));
224 Locale[] support = ApplicationSettings.getSupportedLocales();
226 // Sort localized type names
227 _existype = new ArrayList<LocalizedContextTypes>(support.length);
228 for (int i = 0; i < support.length; i++) {
229 _existype.add(new LocalizedContextTypes(types, support[i]));
231 SimulationContextType[] names = types
232 .toArray(new SimulationContextType[types.size()]);
234 Arrays.sort(names, new ContextNameComparator());
235 _exisname = Arrays.asList(names);
237 _existing = getSimulationContextService()
238 .selectSimulationContextsWhere(
239 cprop.setType(_edition.getType()).setState(
240 ProgressState.APPROVED));
243 } catch (Exception error) {
244 LOG.error("Reason:", error);
245 res = ERROR; // No need to roll-back the transaction as it is read-only
250 // ==============================================================================================================================
251 // Getters and setters
252 // ==============================================================================================================================
254 public List<ProjectSettingsService.Step> getAllStudySteps() {
255 return getProjectSettings().getAllSteps();
259 * Get project settings.
261 * @return Project settings service
263 private ProjectSettingsService getProjectSettings() {
264 return _projectSettings;
268 * Set project settings service.
270 * @param projectSettingsService
271 * project settings service
273 public void setProjectSettings(
274 final ProjectSettingsService projectSettingsService) {
275 _projectSettings = projectSettingsService;
278 public ProjectSettingsService.Step getAttachedStep() {
282 public List<SimulationContextFacade> getContextsToBeApproved() {
286 public SimulationContext getEdited() {
290 public List<SimulationContext> getExistingContexts() {
294 public List<SimulationContextType> getExistingNames() {
298 public List<LocalizedContextTypes> getSupportedLocales() {
302 public Set<ProjectElementFacade> getIndexedElements() {
306 public int getSelection() {
310 public void setSelection(final String index) {
311 _selection = Integer.valueOf(index);
315 * Get the knowledgeElementService.
317 * @return the knowledgeElementService
319 public KnowledgeElementService getKnowledgeElementService() {
320 return _knowledgeElementService;
324 * Set the knowledgeElementService.
326 * @param knowledgeElementService
327 * the knowledgeElementService to set
329 public void setKnowledgeElementService(
330 final KnowledgeElementService knowledgeElementService) {
331 _knowledgeElementService = knowledgeElementService;
335 * Get the simulationContextService.
337 * @return the simulationContextService
339 public SimulationContextService getSimulationContextService() {
340 return _simulationContextService;
344 * Set the simulationContextService.
346 * @param simulationContextService
347 * the simulationContextService to set
349 public void setSimulationContextService(
350 final SimulationContextService simulationContextService) {
351 _simulationContextService = simulationContextService;
355 * Get the searchService.
357 * @return the searchService
359 public SearchService getSearchService() {
360 return _searchService;
364 * Set the searchService.
366 * @param searchService
367 * the searchService to set
369 public void setSearchService(final SearchService searchService) {
370 _searchService = searchService;