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.StepService;
11 import org.splat.service.StudyService;
12 import org.splat.som.Step;
13 import org.splat.dal.bo.som.Study;
15 public class EditSimulationContextAction extends DisplayStudyStepAction {
17 private List<SimulationContextType> contype = null;
18 private List<SimulationContext> contelm = null;
19 private String selectype = null; // Context type, if selected
20 private String newtype = null; // Context type, if newed
21 private SimulationContextType type = null; // Corresponding context type object
22 private String value = null; // Context value
24 * Injected study service.
26 private StudyService _studyService;
28 * Injected step service.
30 private StepService _stepService;
32 * Injected simulation context service.
34 private SimulationContextService _simulationContextService;
37 * Get the stepService.
39 * @return the stepService
41 public StepService getStepService() {
46 * Set the stepService.
49 * the stepService to set
51 public void setStepService(StepService stepService) {
52 _stepService = stepService;
55 private static final long serialVersionUID = -641719644024601042L;
57 // ==============================================================================================================================
59 // ==============================================================================================================================
61 public String doInitialize() {
63 mystudy = getOpenStudy();
64 contype = getInvolvedContexts();
66 if (contype.isEmpty())
72 public String doSelectContext() {
73 mystudy = getOpenStudy();
74 int typid = Integer.valueOf(selectype);
78 SimulationContext.Properties cprop = new SimulationContext.Properties();
79 type = getSimulationContextService().selectType(typid);
80 newtype = type.getName();
81 contype = getInvolvedContexts();
82 contelm = getSimulationContextService()
83 .selectSimulationContextsWhere(cprop.setType(type));
88 public String doCreateContext() {
89 // --------------------------------
91 mystudy = getOpenStudy();
92 if (newtype.length() == 0 || value.length() == 0)
95 Step step = mystudy.getSelectedStep();
96 ProjectElement owner = step.getOwner();
98 SimulationContext.Properties cprop = new SimulationContext.Properties();
99 SimulationContext contex = null;
100 type = getSimulationContextService().createType(newtype,
102 cprop.setType(type).setValue(value);
103 if (owner instanceof Study)
104 contex = getStudyService().addProjectContext(((Study) owner),
105 cprop); // Re-indexes knowledges and the study
107 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
111 } catch (RuntimeException saverror) {
112 logger.error("Reason:", saverror);
114 } catch (Exception error) {
119 public String doDeleteContext() {
121 mystudy = getOpenStudy();
123 Step step = mystudy.getSelectedStep();
124 ProjectElement owner = step.getOwner();
125 SimulationContext context = step.getSimulationContext(Integer
127 if (owner instanceof Study)
129 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
131 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
133 mystudy.remove(context);
135 } catch (RuntimeException saverror) {
136 logger.error("Reason:", saverror);
141 public String doSetContext() {
142 // -----------------------------
143 String[] input = value.split(",");
144 // Session connex = Database.getCurSession();
145 // Transaction transax = connex.beginTransaction();
147 mystudy = getOpenStudy();
149 Step step = mystudy.getSelectedStep();
150 ProjectElement owner = step.getOwner();
151 SimulationContext contex = null;
153 if (input.length == 1
154 || (input.length == 2 && input[1].equals(" "))) {
155 // Setting an existing simulation context identified by value (input = rid," ")
156 int valid = Integer.valueOf(input[0]);
157 contex = getSimulationContextService().selectSimulationContext(
159 if (owner instanceof Study)
161 .addProjectContext(((Study) owner), contex);
163 getStepService().addSimulationContext(step, contex);
165 // Setting a new simulation context value (input = 0,"new context value")
166 int typid = Integer.valueOf(selectype);
167 SimulationContext.Properties cprop = new SimulationContext.Properties();
168 cprop.setType(getSimulationContextService().selectType(typid))
169 .setValue(input[1].trim());
170 if (owner instanceof Study)
171 contex = getStudyService().addProjectContext(
172 ((Study) owner), cprop); // Re-indexes knowledges and the study
174 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
177 contype = getInvolvedContexts();
181 } catch (RuntimeException saverror) {
182 logger.error("Reason:", saverror);
184 } catch (Exception error) {
190 // ==============================================================================================================================
191 // Getters and setters
192 // ==============================================================================================================================
194 public SimulationContextType getContextType() {
195 // ----------------------------------------------
199 public String getContextName() {
200 // -------------------------------
204 // public List<SimulationContextFacade> getSimulationContexts () {
205 // -------------------------------------------------------
206 // return mystudy.getSelectedStep().getAllSimulationContexts();
208 public List<SimulationContextType> getSimulationContextTypes() {
209 // ---------------------------------------------------------------
213 public List<SimulationContext> getSimulationContextValues() {
214 // ------------------------------------------------------------
218 public void setContextType(String type) {
219 // ----------------------------------------
220 this.selectype = type;
223 public void setContextValue(String value) {
224 // -----------------------------------------
228 public void setNewType(String name) {
229 // ------------------------------------
233 // ==============================================================================================================================
235 // ==============================================================================================================================
237 private List<SimulationContextType> getInvolvedContexts() {
238 // ----------------------------------------------------------
239 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
240 .setStep(mystudy.getSelectedStep().getStep());
241 List<SimulationContextType> contype = getSimulationContextService()
242 .selectTypesWhere(sprop);
244 if (!contype.isEmpty()) {
245 // Ordering by alphabetical order of localized context types
246 SimulationContextType[] types = contype
247 .toArray(new SimulationContextType[contype.size()]);
248 ContextTypeComparator compare = new ContextTypeComparator();
249 boolean state = types[0].isApproved();
252 while (to < types.length - 1) {
254 if (types[to].isApproved() == state)
258 Arrays.sort(types, from, to, compare);
263 Arrays.sort(types, from, to + 1, compare);
264 contype = Arrays.asList(types);
270 * Get the studyService.
272 * @return the studyService
274 public StudyService getStudyService() {
275 return _studyService;
279 * Set the studyService.
281 * @param studyService
282 * the studyService to set
284 public void setStudyService(StudyService studyService) {
285 _studyService = studyService;
289 * Get the simulationContextService.
291 * @return the simulationContextService
293 public SimulationContextService getSimulationContextService() {
294 return _simulationContextService;
298 * Set the simulationContextService.
300 * @param simulationContextService
301 * the simulationContextService to set
303 public void setSimulationContextService(
304 SimulationContextService simulationContextService) {
305 _simulationContextService = simulationContextService;