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.service.SimulationContextService;
10 import org.splat.service.SimulationContextTypeService;
11 import org.splat.service.StepService;
12 import org.splat.service.StudyService;
13 import org.splat.som.Step;
14 import org.splat.dal.bo.som.Study;
16 public class EditSimulationContextAction extends DisplayStudyStepAction {
18 private List<SimulationContextType> contype = null;
19 private List<SimulationContext> contelm = null;
20 private String selectype = null; // Context type, if selected
21 private String newtype = null; // Context type, if newed
22 private SimulationContextType type = null; // Corresponding context type object
23 private String value = null; // Context value
25 * Injected study service.
27 private StudyService _studyService;
29 * Injected step service.
31 private StepService _stepService;
33 * Injected simulation context service.
35 private SimulationContextService _simulationContextService;
37 * Injected simulation context type service.
39 private SimulationContextTypeService _simulationContextTypeService;
42 * Get the stepService.
44 * @return the stepService
46 public StepService getStepService() {
51 * Set the stepService.
54 * the stepService to set
56 public void setStepService(StepService stepService) {
57 _stepService = stepService;
60 private static final long serialVersionUID = -641719644024601042L;
62 // ==============================================================================================================================
64 // ==============================================================================================================================
66 public String doInitialize() {
68 mystudy = getOpenStudy();
69 contype = getInvolvedContexts();
71 if (contype.isEmpty())
77 public String doSelectContext() {
78 mystudy = getOpenStudy();
79 int typid = Integer.valueOf(selectype);
83 SimulationContext.Properties cprop = new SimulationContext.Properties();
84 type = getSimulationContextService().selectType(typid);
85 newtype = type.getName();
86 contype = getInvolvedContexts();
87 contelm = getSimulationContextService()
88 .selectSimulationContextsWhere(cprop.setType(type));
93 public String doCreateContext() {
94 // --------------------------------
96 mystudy = getOpenStudy();
97 if (newtype.length() == 0 || value.length() == 0)
100 Step step = mystudy.getSelectedStep();
101 ProjectElement owner = step.getOwner();
103 SimulationContext.Properties cprop = new SimulationContext.Properties();
104 SimulationContext contex = null;
105 type = getSimulationContextTypeService().createType(newtype,
107 cprop.setType(type).setValue(value);
108 if (owner instanceof Study)
109 contex = getStudyService().addProjectContext(((Study) owner),
110 cprop); // Re-indexes knowledges and the study
112 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
116 } catch (RuntimeException saverror) {
117 logger.error("Reason:", saverror);
119 } catch (Exception error) {
124 public String doDeleteContext() {
126 mystudy = getOpenStudy();
128 Step step = mystudy.getSelectedStep();
129 ProjectElement owner = step.getOwner();
130 SimulationContext context = step.getSimulationContext(Integer
132 if (owner instanceof Study)
134 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
136 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
138 mystudy.remove(context);
140 } catch (RuntimeException saverror) {
141 logger.error("Reason:", saverror);
146 public String doSetContext() {
147 // -----------------------------
148 String[] input = value.split(",");
149 // Session connex = Database.getCurSession();
150 // Transaction transax = connex.beginTransaction();
152 mystudy = getOpenStudy();
154 Step step = mystudy.getSelectedStep();
155 ProjectElement owner = step.getOwner();
156 SimulationContext contex = null;
158 if (input.length == 1
159 || (input.length == 2 && input[1].equals(" "))) {
160 // Setting an existing simulation context identified by value (input = rid," ")
161 int valid = Integer.valueOf(input[0]);
162 contex = getSimulationContextService().selectSimulationContext(
164 if (owner instanceof Study)
166 .addProjectContext(((Study) owner), contex);
168 getStepService().addSimulationContext(step, contex);
170 // Setting a new simulation context value (input = 0,"new context value")
171 int typid = Integer.valueOf(selectype);
172 SimulationContext.Properties cprop = new SimulationContext.Properties();
173 cprop.setType(getSimulationContextService().selectType(typid))
174 .setValue(input[1].trim());
175 if (owner instanceof Study)
176 contex = getStudyService().addProjectContext(
177 ((Study) owner), cprop); // Re-indexes knowledges and the study
179 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
182 contype = getInvolvedContexts();
186 } catch (RuntimeException saverror) {
187 logger.error("Reason:", saverror);
189 } catch (Exception error) {
195 // ==============================================================================================================================
196 // Getters and setters
197 // ==============================================================================================================================
199 public SimulationContextType getContextType() {
200 // ----------------------------------------------
204 public String getContextName() {
205 // -------------------------------
209 // public List<SimulationContextFacade> getSimulationContexts () {
210 // -------------------------------------------------------
211 // return mystudy.getSelectedStep().getAllSimulationContexts();
213 public List<SimulationContextType> getSimulationContextTypes() {
214 // ---------------------------------------------------------------
218 public List<SimulationContext> getSimulationContextValues() {
219 // ------------------------------------------------------------
223 public void setContextType(String type) {
224 // ----------------------------------------
225 this.selectype = type;
228 public void setContextValue(String value) {
229 // -----------------------------------------
233 public void setNewType(String name) {
234 // ------------------------------------
238 // ==============================================================================================================================
240 // ==============================================================================================================================
242 private List<SimulationContextType> getInvolvedContexts() {
243 // ----------------------------------------------------------
244 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
245 .setStep(mystudy.getSelectedStep().getStep());
246 List<SimulationContextType> contype = getSimulationContextService()
247 .selectTypesWhere(sprop);
249 if (!contype.isEmpty()) {
250 // Ordering by alphabetical order of localized context types
251 SimulationContextType[] types = contype
252 .toArray(new SimulationContextType[contype.size()]);
253 ContextTypeComparator compare = new ContextTypeComparator();
254 boolean state = types[0].isApproved();
257 while (to < types.length - 1) {
259 if (types[to].isApproved() == state)
263 Arrays.sort(types, from, to, compare);
268 Arrays.sort(types, from, to + 1, compare);
269 contype = Arrays.asList(types);
275 * Get the studyService.
277 * @return the studyService
279 public StudyService getStudyService() {
280 return _studyService;
284 * Set the studyService.
286 * @param studyService
287 * the studyService to set
289 public void setStudyService(StudyService studyService) {
290 _studyService = studyService;
294 * Get the simulationContextService.
296 * @return the simulationContextService
298 public SimulationContextService getSimulationContextService() {
299 return _simulationContextService;
303 * Set the simulationContextService.
305 * @param simulationContextService
306 * the simulationContextService to set
308 public void setSimulationContextService(
309 SimulationContextService simulationContextService) {
310 _simulationContextService = simulationContextService;
314 * Get the simulationContextTypeService.
315 * @return the simulationContextTypeService
317 public SimulationContextTypeService getSimulationContextTypeService() {
318 return _simulationContextTypeService;
322 * Set the simulationContextTypeService.
323 * @param simulationContextTypeService the simulationContextTypeService to set
325 public void setSimulationContextTypeService(
326 SimulationContextTypeService simulationContextTypeService) {
327 _simulationContextTypeService = simulationContextTypeService;