1 package org.splat.simer;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
8 import org.splat.dal.bo.som.ProjectElement;
9 import org.splat.dal.bo.som.SimulationContext;
10 import org.splat.dal.bo.som.SimulationContextType;
11 import org.splat.dal.bo.som.Study;
12 import org.splat.service.SimulationContextService;
13 import org.splat.service.SimulationContextTypeService;
14 import org.splat.service.StepService;
15 import org.splat.som.Step;
18 * Action for simulation context selection or input.
20 public class EditSimulationContextAction extends DisplayStudyStepAction {
23 * Serialization version id.
25 private static final long serialVersionUID = -641719644024601042L;
28 * List of involved context types.
30 private transient List<SimulationContextType> _contype = null;
32 * List of simulation context values of the selected type.
34 private transient List<SimulationContext> _contelm = null;
36 * Context type, if selected.
38 private transient String _selectype = null;
40 * Context type, if newed.
42 private transient String _newtype = null;
44 * Corresponding context type object.
46 private transient SimulationContextType _type = null;
50 private transient String _value = null;
52 * Injected step service.
54 private StepService _stepService;
56 * Injected simulation context service.
58 private SimulationContextService _simulationContextService;
60 * Injected simulation context type service.
62 private SimulationContextTypeService _simulationContextTypeService;
64 // ==============================================================================================================================
66 // ==============================================================================================================================
69 * Initialize context input/selection action.
71 * @return "create" if there is no involved context types or "select" otherwise
73 public String doInitialize() {
76 _openStudy = getOpenStudy();
77 _contype = getInvolvedContexts();
81 if (_contype.isEmpty()) {
83 setAction("newContext");
86 setAction("selectContext");
92 * Select or create a simulation context. If a type has been selected then initialize the list of context values.
94 * @return "set" if context type is selected or "create" otherwise
96 public String doSelectContext() {
98 _openStudy = getOpenStudy();
102 int typid = Integer.valueOf(_selectype);
105 setAction("newContext");
108 SimulationContext.Properties cprop = new SimulationContext.Properties();
109 _type = getSimulationContextService().selectType(typid);
110 _newtype = _type.getName();
111 _contype = getInvolvedContexts();
112 _contelm = getSimulationContextService()
113 .selectSimulationContextsWhere(cprop.setType(_type));
115 setAction("setContext");
116 setIndex(String.valueOf(getContextType().getIndex()));
123 * Create new context. Add the created context to the step and reindex the study. <BR>
124 * If type or value are undefined then return INPUT.
126 * @return SUCCESS if added or ERROR or INPUT if failed
128 public String doCreateContext() {
129 String res = SUCCESS;
131 _openStudy = getOpenStudy();
135 if (_newtype.length() == 0 || _value.length() == 0) {
137 setAction("newContext");
140 Step step = _openStudy.getSelectedStep();
141 ProjectElement owner = step.getOwner();
143 SimulationContext.Properties cprop = new SimulationContext.Properties();
144 SimulationContext contex = null;
145 _type = getSimulationContextTypeService().createType(_newtype,
147 cprop.setType(_type).setValue(_value);
148 if (owner instanceof Study) {
149 contex = getStudyService().addProjectContext(
150 ((Study) owner), cprop); // Re-indexes knowledges and the study
152 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
155 _openStudy.add(contex);
157 } catch (RuntimeException saverror) {
158 LOG.error("Reason:", saverror);
160 } catch (Exception error) {
161 setAction("newContext");
168 * Remove the selected simulation context from the study step.
170 * @return SUCCESS if removed or ERROR if failed
172 public String doDeleteContext() {
174 String res = SUCCESS;
178 _openStudy = getOpenStudy();
180 Step step = _openStudy.getSelectedStep();
181 ProjectElement owner = step.getOwner();
182 SimulationContext context = step.getSimulationContext(Integer
184 if (owner instanceof Study) {
186 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
188 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
191 _openStudy.remove(context);
192 } catch (RuntimeException saverror) {
193 LOG.error("Reason:", saverror);
200 * Add the selected context to the study step and reindex the study. <BR>
201 * If type or value are undefined then return INPUT.
203 * @return SUCCESS if added or ERROR or INPUT if failed
205 public String doSetContext() {
206 String res = SUCCESS;
207 String[] input = _value.split(",");
212 _openStudy = getOpenStudy();
214 Step step = _openStudy.getSelectedStep();
215 ProjectElement owner = step.getOwner();
216 SimulationContext contex = null;
218 if (input.length == 1
219 || (input.length == 2 && input[1].equals(" "))) {
220 // Setting an existing simulation context identified by value (input = rid," ")
221 int valid = Integer.valueOf(input[0]);
222 contex = getSimulationContextService().selectSimulationContext(
224 if (owner instanceof Study) {
226 .addProjectContext(((Study) owner), contex);
228 getStepService().addSimulationContext(step, contex);
231 // Setting a new simulation context value (input = 0,"new context value")
232 int typid = Integer.valueOf(_selectype);
233 SimulationContext.Properties cprop = new SimulationContext.Properties();
234 cprop.setType(getSimulationContextService().selectType(typid))
235 .setValue(input[1].trim());
236 if (owner instanceof Study) {
237 contex = getStudyService().addProjectContext(
238 ((Study) owner), cprop); // Re-indexes knowledges and the study
240 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
243 _openStudy.add(contex);
244 _contype = getInvolvedContexts();
246 setAction("setContext");
247 setIndex(String.valueOf(getContextType().getIndex()));
249 } catch (RuntimeException saverror) {
250 LOG.error("Reason:", saverror);
252 } catch (Exception error) {
254 setAction("setContext");
260 // ==============================================================================================================================
261 // Getters and setters
262 // ==============================================================================================================================
267 * @return Corresponding context type object
269 public SimulationContextType getContextType() {
274 * Get the new context type to be created.
276 * @return Context type, if newed
278 public String getContextName() {
283 * Get the list of involved context types.
285 * @return List of involved context types
287 public List<SimulationContextType> getSimulationContextTypes() {
292 * Get the list of simulation context values of the selected type.
294 * @return List of simulation context values of the selected type
296 public List<SimulationContext> getSimulationContextValues() {
301 * Get the selected context type.
304 * Context type, if selected
306 public void setContextType(final String type) {
307 this._selectype = type;
311 * Set the context value.
316 public void setContextValue(final String value) {
321 * Set the new context type.
324 * the new context type
326 public void setNewType(final String name) {
327 this._newtype = name;
330 // ==============================================================================================================================
332 // ==============================================================================================================================
335 * Get the list of context types involved in the current study step.
337 * @return the list of context types
339 private List<SimulationContextType> getInvolvedContexts() {
340 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
341 .setStep(_openStudy.getSelectedStep().getStep());
342 List<SimulationContextType> contype = getSimulationContextService()
343 .selectTypesWhere(sprop);
345 if (!contype.isEmpty()) {
346 // Ordering by alphabetical order of localized context types
347 SimulationContextType[] types = contype
348 .toArray(new SimulationContextType[contype.size()]);
349 ContextTypeComparator compare = new ContextTypeComparator();
350 boolean state = types[0].isApproved();
353 while (to < types.length - 1) {
355 if (types[to].isApproved() == state) {
360 Arrays.sort(types, from, to, compare);
366 Arrays.sort(types, from, to + 1, compare);
368 contype = Arrays.asList(types);
374 * Get the simulationContextService.
376 * @return the simulationContextService
378 public SimulationContextService getSimulationContextService() {
379 return _simulationContextService;
383 * Set the simulationContextService.
385 * @param simulationContextService
386 * the simulationContextService to set
388 public void setSimulationContextService(
389 final SimulationContextService simulationContextService) {
390 _simulationContextService = simulationContextService;
394 * Get the simulationContextTypeService.
396 * @return the simulationContextTypeService
398 public SimulationContextTypeService getSimulationContextTypeService() {
399 return _simulationContextTypeService;
403 * Set the simulationContextTypeService.
405 * @param simulationContextTypeService
406 * the simulationContextTypeService to set
408 public void setSimulationContextTypeService(
409 final SimulationContextTypeService simulationContextTypeService) {
410 _simulationContextTypeService = simulationContextTypeService;
414 * Get the stepService.
416 * @return the stepService
418 public StepService getStepService() {
423 * Set the stepService.
426 * the stepService to set
428 public void setStepService(final StepService stepService) {
429 _stepService = stepService;