1 package org.splat.simer;
3 import java.util.Arrays;
6 import org.hibernate.HibernateException;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9 import org.splat.dal.dao.som.Database;
10 import org.splat.dal.bo.som.ProjectElement;
11 import org.splat.dal.bo.som.SimulationContext;
12 import org.splat.dal.bo.som.SimulationContextType;
13 import org.splat.service.SimulationContextService;
14 import org.splat.service.StepService;
15 import org.splat.service.StudyService;
16 import org.splat.som.Step;
17 import org.splat.dal.bo.som.Study;
19 public class EditSimulationContextAction extends DisplayStudyStepAction {
21 private List<SimulationContextType> contype = null;
22 private List<SimulationContext> contelm = null;
23 private String selectype = null; // Context type, if selected
24 private String newtype = null; // Context type, if newed
25 private SimulationContextType type = null; // Corresponding context type object
26 private String value = null; // Context value
28 * Injected study service.
30 private StudyService _studyService;
32 * Injected step service.
34 private StepService _stepService;
36 * Injected simulation context service.
38 private SimulationContextService _simulationContextService;
41 * Get the stepService.
43 * @return the stepService
45 public StepService getStepService() {
50 * Set the stepService.
53 * the stepService to set
55 public void setStepService(StepService stepService) {
56 _stepService = stepService;
59 private static final long serialVersionUID = -641719644024601042L;
61 // ==============================================================================================================================
63 // ==============================================================================================================================
65 public String doInitialize() {
66 // -----------------------------
67 Session connex = Database.getSession();
68 Transaction transax = connex.beginTransaction();
70 mystudy = getOpenStudy();
71 contype = getInvolvedContexts();
74 if (contype.isEmpty())
80 public String doSelectContext() {
81 // --------------------------------
82 Session connex = Database.getSession();
83 Transaction transax = connex.beginTransaction();
85 mystudy = getOpenStudy();
86 int typid = Integer.valueOf(selectype);
90 SimulationContext.Properties cprop = new SimulationContext.Properties();
91 type = getSimulationContextService().selectType(typid);
92 newtype = type.getName();
93 contype = getInvolvedContexts();
94 contelm = getSimulationContextService()
95 .selectSimulationContextsWhere(cprop.setType(type));
103 public String doCreateContext() {
104 // --------------------------------
105 Session connex = Database.getSession();
106 Transaction transax = connex.beginTransaction();
108 mystudy = getOpenStudy();
109 if (newtype.length() == 0 || value.length() == 0)
112 Step step = mystudy.getSelectedStep();
113 ProjectElement owner = step.getOwner();
115 SimulationContext.Properties cprop = new SimulationContext.Properties();
116 SimulationContext contex = null;
117 type = getSimulationContextService().createType(newtype,
119 cprop.setType(type).setValue(value);
120 if (owner instanceof Study)
121 contex = getStudyService().addProjectContext(((Study) owner),
122 cprop); // Re-indexes knowledges and the study
124 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
129 } catch (RuntimeException saverror) {
130 logger.error("Reason:", saverror);
131 if (transax != null && transax.isActive()) {
132 // Second try-catch as the rollback could fail as well
135 } catch (HibernateException backerror) {
136 logger.debug("Error rolling back transaction", backerror);
140 } catch (Exception error) {
146 public String doDeleteContext() {
147 // --------------------------------
148 Session connex = Database.getSession();
149 Transaction transax = connex.beginTransaction();
151 mystudy = getOpenStudy();
153 Step step = mystudy.getSelectedStep();
154 ProjectElement owner = step.getOwner();
155 SimulationContext context = step.getSimulationContext(Integer
157 if (owner instanceof Study)
159 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
161 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
163 mystudy.remove(context);
166 } catch (RuntimeException saverror) {
167 logger.error("Reason:", saverror);
168 if (transax != null && transax.isActive()) {
169 // Second try-catch as the rollback could fail as well
172 } catch (HibernateException backerror) {
173 logger.debug("Error rolling back transaction", backerror);
180 public String doSetContext() {
181 // -----------------------------
182 String[] input = value.split(",");
183 Session connex = Database.getSession();
184 Transaction transax = connex.beginTransaction();
186 mystudy = getOpenStudy();
188 Step step = mystudy.getSelectedStep();
189 ProjectElement owner = step.getOwner();
190 SimulationContext contex = null;
192 if (input.length == 1
193 || (input.length == 2 && input[1].equals(" "))) {
194 // Setting an existing simulation context identified by value (input = rid," ")
195 int valid = Integer.valueOf(input[0]);
196 contex = getSimulationContextService().selectSimulationContext(
198 if (owner instanceof Study)
200 .addProjectContext(((Study) owner), contex);
202 getStepService().addSimulationContext(step, contex);
204 // Setting a new simulation context value (input = 0,"new context value")
205 int typid = Integer.valueOf(selectype);
206 SimulationContext.Properties cprop = new SimulationContext.Properties();
207 cprop.setType(getSimulationContextService().selectType(typid))
208 .setValue(input[1].trim());
209 if (owner instanceof Study)
210 contex = getStudyService().addProjectContext(
211 ((Study) owner), cprop); // Re-indexes knowledges and the study
213 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
216 contype = getInvolvedContexts();
220 } catch (RuntimeException saverror) {
221 logger.error("Reason:", saverror);
222 if (transax != null && transax.isActive()) {
223 // Second try-catch as the rollback could fail as well
226 } catch (HibernateException backerror) {
227 logger.debug("Error rolling back transaction", backerror);
231 } catch (Exception error) {
238 // ==============================================================================================================================
239 // Getters and setters
240 // ==============================================================================================================================
242 public SimulationContextType getContextType() {
243 // ----------------------------------------------
247 public String getContextName() {
248 // -------------------------------
252 // public List<SimulationContextFacade> getSimulationContexts () {
253 // -------------------------------------------------------
254 // return mystudy.getSelectedStep().getAllSimulationContexts();
256 public List<SimulationContextType> getSimulationContextTypes() {
257 // ---------------------------------------------------------------
261 public List<SimulationContext> getSimulationContextValues() {
262 // ------------------------------------------------------------
266 public void setContextType(String type) {
267 // ----------------------------------------
268 this.selectype = type;
271 public void setContextValue(String value) {
272 // -----------------------------------------
276 public void setNewType(String name) {
277 // ------------------------------------
281 // ==============================================================================================================================
283 // ==============================================================================================================================
285 private List<SimulationContextType> getInvolvedContexts() {
286 // ----------------------------------------------------------
287 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
288 .setStep(mystudy.getSelectedStep().getStep());
289 List<SimulationContextType> contype = getSimulationContextService()
290 .selectTypesWhere(sprop);
292 if (!contype.isEmpty()) {
293 // Ordering by alphabetical order of localized context types
294 SimulationContextType[] types = contype
295 .toArray(new SimulationContextType[contype.size()]);
296 ContextTypeComparator compare = new ContextTypeComparator();
297 boolean state = types[0].isApproved();
300 while (to < types.length - 1) {
302 if (types[to].isApproved() == state)
306 Arrays.sort(types, from, to, compare);
311 Arrays.sort(types, from, to + 1, compare);
312 contype = Arrays.asList(types);
318 * Get the studyService.
320 * @return the studyService
322 public StudyService getStudyService() {
323 return _studyService;
327 * Set the studyService.
329 * @param studyService
330 * the studyService to set
332 public void setStudyService(StudyService studyService) {
333 _studyService = studyService;
337 * Get the simulationContextService.
339 * @return the simulationContextService
341 public SimulationContextService getSimulationContextService() {
342 return _simulationContextService;
346 * Set the simulationContextService.
348 * @param simulationContextService
349 * the simulationContextService to set
351 public void setSimulationContextService(
352 SimulationContextService simulationContextService) {
353 _simulationContextService = simulationContextService;