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.som.Database;
10 import org.splat.som.ProjectElement;
11 import org.splat.som.SimulationContext;
12 import org.splat.som.SimulationContextType;
13 import org.splat.som.Step;
14 import org.splat.som.Study;
17 public class EditSimulationContextAction extends DisplayStudyStepAction {
19 private List<SimulationContextType> contype = null;
20 private List<SimulationContext> contelm = null;
21 private String selectype = null; // Context type, if selected
22 private String newtype = null; // Context type, if newed
23 private SimulationContextType type = null; // Corresponding context type object
24 private String value = null; // Context value
26 private static final long serialVersionUID = -641719644024601042L;
28 // ==============================================================================================================================
30 // ==============================================================================================================================
32 public String doInitialize () {
33 // -----------------------------
34 Session connex = Database.getSession();
35 Transaction transax = connex.beginTransaction();
37 mystudy = getOpenStudy();
38 contype = getInvolvedContexts();
41 if (contype.isEmpty()) return "create";
45 public String doSelectContext () {
46 // --------------------------------
47 Session connex = Database.getSession();
48 Transaction transax = connex.beginTransaction();
50 mystudy = getOpenStudy();
51 int typid = Integer.valueOf(selectype);
52 if (typid == 0) return "create";
54 SimulationContext.Properties cprop = new SimulationContext.Properties();
55 type = SimulationContext.selectType(typid);
56 newtype = type.getName();
57 contype = getInvolvedContexts();
58 contelm = Database.selectSimulationContextsWhere(cprop.setType(type));
67 public String doCreateContext () {
68 // --------------------------------
69 Session connex = Database.getSession();
70 Transaction transax = connex.beginTransaction();
72 mystudy = getOpenStudy();
73 if (newtype.length() == 0 || value.length() == 0) return INPUT;
75 Step step = mystudy.getSelectedStep();
76 ProjectElement owner = step.getOwner();
78 SimulationContext.Properties cprop = new SimulationContext.Properties();
79 SimulationContext contex = null;
80 type = SimulationContext.createType(newtype, step.getStep());
81 cprop.setType(type).setValue(value);
82 if (owner instanceof Study) contex = ((Study)owner).addProjectContext(cprop); // Re-indexes knowledges and the study
83 else contex = step.addSimulationContext(cprop); // Re-indexes knowledges only
89 catch (RuntimeException saverror) {
90 logger.error("Reason:", saverror);
91 if (transax != null && transax.isActive()) {
92 // Second try-catch as the rollback could fail as well
95 } catch (HibernateException backerror) {
96 logger.debug("Error rolling back transaction", backerror);
101 catch (Exception error) {
107 public String doDeleteContext () {
108 // --------------------------------
109 Session connex = Database.getSession();
110 Transaction transax = connex.beginTransaction();
112 mystudy = getOpenStudy();
114 Step step = mystudy.getSelectedStep();
115 ProjectElement owner = step.getOwner();
116 SimulationContext context = step.getSimulationContext(Integer.valueOf(myindex));
117 if (owner instanceof Study) ((Study)owner).removeProjectContext(context); // Re-indexes knowledges and the study
118 else step.removeSimulationContext(context); // Re-indexes knowledges only
120 mystudy.remove(context);
124 catch (RuntimeException saverror) {
125 logger.error("Reason:", saverror);
126 if (transax != null && transax.isActive()) {
127 // Second try-catch as the rollback could fail as well
130 } catch (HibernateException backerror) {
131 logger.debug("Error rolling back transaction", backerror);
138 public String doSetContext () {
139 // -----------------------------
140 String[] input = value.split(",");
141 Session connex = Database.getSession();
142 Transaction transax = connex.beginTransaction();
144 mystudy = getOpenStudy();
146 Step step = mystudy.getSelectedStep();
147 ProjectElement owner = step.getOwner();
148 SimulationContext contex = null;
150 if (input.length == 1 || (input.length == 2 && input[1].equals(" "))) {
151 // Setting an existing simulation context identified by value (input = rid," ")
152 int valid = Integer.valueOf(input[0]);
153 contex = Database.selectSimulationContext(valid);
154 if (owner instanceof Study) ((Study)owner).addProjectContext(contex);
155 else step.addSimulationContext(contex);
158 // Setting a new simulation context value (input = 0,"new context value")
159 int typid = Integer.valueOf(selectype);
160 SimulationContext.Properties cprop = new SimulationContext.Properties();
161 cprop.setType(SimulationContext.selectType(typid))
162 .setValue(input[1].trim());
163 if (owner instanceof Study) contex = ((Study)owner).addProjectContext(cprop); // Re-indexes knowledges and the study
164 else contex = step.addSimulationContext(cprop); // Re-indexes knowledges only
167 contype = getInvolvedContexts();
172 catch (RuntimeException saverror) {
173 logger.error("Reason:", saverror);
174 if (transax != null && transax.isActive()) {
175 // Second try-catch as the rollback could fail as well
178 } catch (HibernateException backerror) {
179 logger.debug("Error rolling back transaction", backerror);
184 catch (Exception error) {
191 // ==============================================================================================================================
192 // Getters and setters
193 // ==============================================================================================================================
195 public SimulationContextType getContextType () {
196 // ----------------------------------------------
199 public String getContextName () {
200 // -------------------------------
203 // public List<SimulationContextFacade> getSimulationContexts () {
204 // -------------------------------------------------------
205 // return mystudy.getSelectedStep().getAllSimulationContexts();
207 public List<SimulationContextType> getSimulationContextTypes () {
208 // ---------------------------------------------------------------
211 public List<SimulationContext> getSimulationContextValues () {
212 // ------------------------------------------------------------
216 public void setContextType (String type) {
217 // ----------------------------------------
218 this.selectype = type;
220 public void setContextValue (String value) {
221 // -----------------------------------------
224 public void setNewType (String name) {
225 // ------------------------------------
229 // ==============================================================================================================================
231 // ==============================================================================================================================
233 private List<SimulationContextType> getInvolvedContexts () {
234 // ----------------------------------------------------------
235 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
236 .setStep(mystudy.getSelectedStep().getStep());
237 List<SimulationContextType> contype = SimulationContext.selectTypesWhere(sprop);
239 if (!contype.isEmpty()) {
240 // Ordering by alphabetical order of localized context types
241 SimulationContextType[] types = contype.toArray( new SimulationContextType[contype.size()] );
242 ContextTypeComparator compare = new ContextTypeComparator();
243 boolean state = types[0].isApproved();
246 while (to < types.length-1) {
248 if (types[to].isApproved() == state) continue;
250 if (to > from+1) Arrays.sort(types, from, to, compare);
254 if (to > from) Arrays.sort(types, from, to+1, compare);
255 contype = Arrays.asList(types);