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.StepService;
14 import org.splat.service.StudyService;
15 import org.splat.som.Step;
16 import org.splat.dal.bo.som.Study;
18 public class EditSimulationContextAction extends DisplayStudyStepAction {
20 private List<SimulationContextType> contype = null;
21 private List<SimulationContext> contelm = null;
22 private String selectype = null; // Context type, if selected
23 private String newtype = null; // Context type, if newed
24 private SimulationContextType type = null; // Corresponding context type object
25 private String value = null; // Context value
26 private StudyService _studyService;
27 private StepService _stepService;
30 * Get the stepService.
32 * @return the stepService
34 public StepService getStepService() {
39 * Set the stepService.
42 * the stepService to set
44 public void setStepService(StepService stepService) {
45 _stepService = stepService;
48 private static final long serialVersionUID = -641719644024601042L;
50 // ==============================================================================================================================
52 // ==============================================================================================================================
54 public String doInitialize() {
55 // -----------------------------
56 Session connex = Database.getSession();
57 Transaction transax = connex.beginTransaction();
59 mystudy = getOpenStudy();
60 contype = getInvolvedContexts();
63 if (contype.isEmpty())
69 public String doSelectContext() {
70 // --------------------------------
71 Session connex = Database.getSession();
72 Transaction transax = connex.beginTransaction();
74 mystudy = getOpenStudy();
75 int typid = Integer.valueOf(selectype);
79 SimulationContext.Properties cprop = new SimulationContext.Properties();
80 type = SimulationContext.selectType(typid);
81 newtype = type.getName();
82 contype = getInvolvedContexts();
83 contelm = Database.selectSimulationContextsWhere(cprop
92 public String doCreateContext() {
93 // --------------------------------
94 Session connex = Database.getSession();
95 Transaction transax = connex.beginTransaction();
97 mystudy = getOpenStudy();
98 if (newtype.length() == 0 || value.length() == 0)
101 Step step = mystudy.getSelectedStep();
102 ProjectElement owner = step.getOwner();
104 SimulationContext.Properties cprop = new SimulationContext.Properties();
105 SimulationContext contex = null;
106 type = SimulationContext.createType(newtype, step.getStep());
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
117 } catch (RuntimeException saverror) {
118 logger.error("Reason:", saverror);
119 if (transax != null && transax.isActive()) {
120 // Second try-catch as the rollback could fail as well
123 } catch (HibernateException backerror) {
124 logger.debug("Error rolling back transaction", backerror);
128 } catch (Exception error) {
134 public String doDeleteContext() {
135 // --------------------------------
136 Session connex = Database.getSession();
137 Transaction transax = connex.beginTransaction();
139 mystudy = getOpenStudy();
141 Step step = mystudy.getSelectedStep();
142 ProjectElement owner = step.getOwner();
143 SimulationContext context = step.getSimulationContext(Integer
145 if (owner instanceof Study)
147 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
149 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
151 mystudy.remove(context);
154 } catch (RuntimeException saverror) {
155 logger.error("Reason:", saverror);
156 if (transax != null && transax.isActive()) {
157 // Second try-catch as the rollback could fail as well
160 } catch (HibernateException backerror) {
161 logger.debug("Error rolling back transaction", backerror);
168 public String doSetContext() {
169 // -----------------------------
170 String[] input = value.split(",");
171 Session connex = Database.getSession();
172 Transaction transax = connex.beginTransaction();
174 mystudy = getOpenStudy();
176 Step step = mystudy.getSelectedStep();
177 ProjectElement owner = step.getOwner();
178 SimulationContext contex = null;
180 if (input.length == 1
181 || (input.length == 2 && input[1].equals(" "))) {
182 // Setting an existing simulation context identified by value (input = rid," ")
183 int valid = Integer.valueOf(input[0]);
184 contex = Database.selectSimulationContext(valid);
185 if (owner instanceof Study)
187 .addProjectContext(((Study) owner), contex);
189 getStepService().addSimulationContext(step, contex);
191 // Setting a new simulation context value (input = 0,"new context value")
192 int typid = Integer.valueOf(selectype);
193 SimulationContext.Properties cprop = new SimulationContext.Properties();
194 cprop.setType(SimulationContext.selectType(typid)).setValue(
196 if (owner instanceof Study)
197 contex = getStudyService().addProjectContext(
198 ((Study) owner), cprop); // Re-indexes knowledges and the study
200 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
203 contype = getInvolvedContexts();
207 } catch (RuntimeException saverror) {
208 logger.error("Reason:", saverror);
209 if (transax != null && transax.isActive()) {
210 // Second try-catch as the rollback could fail as well
213 } catch (HibernateException backerror) {
214 logger.debug("Error rolling back transaction", backerror);
218 } catch (Exception error) {
225 // ==============================================================================================================================
226 // Getters and setters
227 // ==============================================================================================================================
229 public SimulationContextType getContextType() {
230 // ----------------------------------------------
234 public String getContextName() {
235 // -------------------------------
239 // public List<SimulationContextFacade> getSimulationContexts () {
240 // -------------------------------------------------------
241 // return mystudy.getSelectedStep().getAllSimulationContexts();
243 public List<SimulationContextType> getSimulationContextTypes() {
244 // ---------------------------------------------------------------
248 public List<SimulationContext> getSimulationContextValues() {
249 // ------------------------------------------------------------
253 public void setContextType(String type) {
254 // ----------------------------------------
255 this.selectype = type;
258 public void setContextValue(String value) {
259 // -----------------------------------------
263 public void setNewType(String name) {
264 // ------------------------------------
268 // ==============================================================================================================================
270 // ==============================================================================================================================
272 private List<SimulationContextType> getInvolvedContexts() {
273 // ----------------------------------------------------------
274 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
275 .setStep(mystudy.getSelectedStep().getStep());
276 List<SimulationContextType> contype = SimulationContext
277 .selectTypesWhere(sprop);
279 if (!contype.isEmpty()) {
280 // Ordering by alphabetical order of localized context types
281 SimulationContextType[] types = contype
282 .toArray(new SimulationContextType[contype.size()]);
283 ContextTypeComparator compare = new ContextTypeComparator();
284 boolean state = types[0].isApproved();
287 while (to < types.length - 1) {
289 if (types[to].isApproved() == state)
293 Arrays.sort(types, from, to, compare);
298 Arrays.sort(types, from, to + 1, compare);
299 contype = Arrays.asList(types);
305 * Get the studyService.
307 * @return the studyService
309 public StudyService getStudyService() {
310 return _studyService;
314 * Set the studyService.
316 * @param studyService
317 * the studyService to set
319 public void setStudyService(StudyService studyService) {
320 _studyService = studyService;