1 package org.splat.simer;
3 import java.util.Arrays;
6 import org.splat.dal.bo.som.ProjectElement;
7 import org.splat.dal.bo.som.SimulationContext;
8 import org.splat.dal.bo.som.SimulationContextType;
9 import org.splat.dal.bo.som.Study;
10 import org.splat.service.SimulationContextService;
11 import org.splat.service.SimulationContextTypeService;
12 import org.splat.service.StepService;
13 import org.splat.som.Step;
16 * Action for simulation context selection or input.
18 public class EditSimulationContextAction extends DisplayStudyStepAction {
21 * Serialization version id.
23 private static final long serialVersionUID = -641719644024601042L;
26 * List of involved context types.
28 private transient List<SimulationContextType> _contype = null;
30 * List of simulation context values of the selected type.
32 private transient List<SimulationContext> _contelm = null;
34 * Context type, if selected.
36 private transient String _selectype = null;
38 * Context type, if newed.
40 private transient String _newtype = null;
42 * Corresponding context type object.
44 private transient SimulationContextType _type = null;
48 private transient String _value = null;
50 * Injected step service.
52 private StepService _stepService;
54 * Injected simulation context service.
56 private SimulationContextService _simulationContextService;
58 * Injected simulation context type service.
60 private SimulationContextTypeService _simulationContextTypeService;
62 // ==============================================================================================================================
64 // ==============================================================================================================================
67 * Initialize context input/selection action.
69 * @return "create" if there is no involved context types or "select" otherwise
71 public String doInitialize() {
74 _openStudy = getOpenStudy();
75 _contype = getInvolvedContexts();
79 if (_contype.isEmpty()) {
81 setAction("newContext");
84 setAction("selectContext");
90 * Select or create a simulation context. If a type has been selected then initialize the list of context values.
92 * @return "set" if context type is selected or "create" otherwise
94 public String doSelectContext() {
96 _openStudy = getOpenStudy();
100 int typid = Integer.valueOf(_selectype);
103 setAction("newContext");
106 SimulationContext.Properties cprop = new SimulationContext.Properties();
107 _type = getSimulationContextService().selectType(typid);
108 _newtype = _type.getName();
109 _contype = getInvolvedContexts();
110 _contelm = getSimulationContextService()
111 .selectSimulationContextsWhere(cprop.setType(_type));
113 setAction("setContext");
114 setIndex(String.valueOf(getContextType().getIndex()));
121 * Create new context. Add the created context to the step and reindex the study. <BR>
122 * If type or value are undefined then return INPUT.
124 * @return SUCCESS if added or ERROR or INPUT if failed
126 public String doCreateContext() {
127 String res = SUCCESS;
129 _openStudy = getOpenStudy();
133 if (_newtype.length() == 0 || _value.length() == 0) {
137 Step step = _openStudy.getSelectedStep();
138 ProjectElement owner = step.getOwner();
140 SimulationContext.Properties cprop = new SimulationContext.Properties();
141 SimulationContext contex = null;
142 _type = getSimulationContextTypeService().createType(_newtype,
144 cprop.setType(_type).setValue(_value);
145 if (owner instanceof Study) {
146 contex = getStudyService().addProjectContext(
147 ((Study) owner), cprop); // Re-indexes knowledges and the study
149 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
152 _openStudy.add(contex);
154 } catch (RuntimeException saverror) {
155 LOG.error("Reason:", saverror);
157 } catch (Exception error) {
158 setAction("newContext");
165 * Remove the selected simulation context from the study step.
167 * @return SUCCESS if removed or ERROR if failed
169 public String doDeleteContext() {
171 String res = SUCCESS;
175 _openStudy = getOpenStudy();
177 Step step = _openStudy.getSelectedStep();
178 ProjectElement owner = step.getOwner();
179 SimulationContext context = step.getSimulationContext(Integer
181 if (owner instanceof Study) {
183 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
185 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
188 _openStudy.remove(context);
189 } catch (RuntimeException saverror) {
190 LOG.error("Reason:", saverror);
197 * Add the selected context to the study step and reindex the study. <BR>
198 * If type or value are undefined then return INPUT.
200 * @return SUCCESS if added or ERROR or INPUT if failed
202 public String doSetContext() {
203 String res = SUCCESS;
204 String[] input = _value.split(",");
209 _openStudy = getOpenStudy();
211 Step step = _openStudy.getSelectedStep();
212 ProjectElement owner = step.getOwner();
213 SimulationContext contex = null;
215 if (input.length == 1
216 || (input.length == 2 && input[1].equals(" "))) {
217 // Setting an existing simulation context identified by value (input = rid," ")
218 int valid = Integer.valueOf(input[0]);
219 contex = getSimulationContextService().selectSimulationContext(
221 if (owner instanceof Study) {
223 .addProjectContext(((Study) owner), contex);
225 getStepService().addSimulationContext(step, contex);
228 // Setting a new simulation context value (input = 0,"new context value")
229 int typid = Integer.valueOf(_selectype);
230 SimulationContext.Properties cprop = new SimulationContext.Properties();
231 cprop.setType(getSimulationContextService().selectType(typid))
232 .setValue(input[1].trim());
233 if (owner instanceof Study) {
234 contex = getStudyService().addProjectContext(
235 ((Study) owner), cprop); // Re-indexes knowledges and the study
237 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
240 _openStudy.add(contex);
241 _contype = getInvolvedContexts();
243 setAction("setContext");
244 setIndex(String.valueOf(getContextType().getIndex()));
246 } catch (RuntimeException saverror) {
247 LOG.error("Reason:", saverror);
249 } catch (Exception error) {
251 setAction("setContext");
257 // ==============================================================================================================================
258 // Getters and setters
259 // ==============================================================================================================================
264 * @return Corresponding context type object
266 public SimulationContextType getContextType() {
271 * Get the new context type to be created.
273 * @return Context type, if newed
275 public String getContextName() {
280 * Get the list of involved context types.
282 * @return List of involved context types
284 public List<SimulationContextType> getSimulationContextTypes() {
289 * Get the list of simulation context values of the selected type.
291 * @return List of simulation context values of the selected type
293 public List<SimulationContext> getSimulationContextValues() {
298 * Get the selected context type.
301 * Context type, if selected
303 public void setContextType(final String type) {
304 this._selectype = type;
308 * Set the context value.
313 public void setContextValue(final String value) {
318 * Set the new context type.
321 * the new context type
323 public void setNewType(final String name) {
324 this._newtype = name;
327 // ==============================================================================================================================
329 // ==============================================================================================================================
332 * Get the list of context types involved in the current study step.
334 * @return the list of context types
336 private List<SimulationContextType> getInvolvedContexts() {
337 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
338 .setStep(_openStudy.getSelectedStep().getStep());
339 List<SimulationContextType> contype = getSimulationContextService()
340 .selectTypesWhere(sprop);
342 if (!contype.isEmpty()) {
343 // Ordering by alphabetical order of localized context types
344 SimulationContextType[] types = contype
345 .toArray(new SimulationContextType[contype.size()]);
346 ContextTypeComparator compare = new ContextTypeComparator();
347 boolean state = types[0].isApproved();
350 while (to < types.length - 1) {
352 if (types[to].isApproved() == state) {
357 Arrays.sort(types, from, to, compare);
363 Arrays.sort(types, from, to + 1, compare);
365 contype = Arrays.asList(types);
371 * Get the simulationContextService.
373 * @return the simulationContextService
375 public SimulationContextService getSimulationContextService() {
376 return _simulationContextService;
380 * Set the simulationContextService.
382 * @param simulationContextService
383 * the simulationContextService to set
385 public void setSimulationContextService(
386 final SimulationContextService simulationContextService) {
387 _simulationContextService = simulationContextService;
391 * Get the simulationContextTypeService.
393 * @return the simulationContextTypeService
395 public SimulationContextTypeService getSimulationContextTypeService() {
396 return _simulationContextTypeService;
400 * Set the simulationContextTypeService.
402 * @param simulationContextTypeService
403 * the simulationContextTypeService to set
405 public void setSimulationContextTypeService(
406 final SimulationContextTypeService simulationContextTypeService) {
407 _simulationContextTypeService = simulationContextTypeService;
411 * Get the stepService.
413 * @return the stepService
415 public StepService getStepService() {
420 * Set the stepService.
423 * the stepService to set
425 public void setStepService(final StepService stepService) {
426 _stepService = stepService;