Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditSimulationContextAction.java
1 package org.splat.simer;
2
3 import java.util.Arrays;
4 import java.util.List;
5
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;
15
16
17 public class EditSimulationContextAction extends DisplayStudyStepAction {
18
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
25
26         private static final long    serialVersionUID = -641719644024601042L;
27
28 //  ==============================================================================================================================
29 //  Action methods
30 //  ==============================================================================================================================
31
32     public String doInitialize () {
33 //  -----------------------------
34       Session     connex  = Database.getSession();
35           Transaction transax = connex.beginTransaction();
36
37           mystudy = getOpenStudy();
38       contype = getInvolvedContexts();
39       
40       transax.commit();
41       if (contype.isEmpty()) return "create";
42       else                   return "select";
43     }
44     
45     public String doSelectContext () {
46 //  --------------------------------
47       Session     connex  = Database.getSession();
48           Transaction transax = connex.beginTransaction();
49       try {       
50         mystudy   = getOpenStudy();
51         int typid = Integer.valueOf(selectype);      
52         if (typid == 0) return "create";
53       
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));
59
60         return "set";
61       }
62       finally {
63         transax.commit();
64       }
65     }
66
67     public String doCreateContext () {
68 //  --------------------------------
69       Session     connex  = Database.getSession();
70       Transaction transax = connex.beginTransaction();
71       try {
72         mystudy = getOpenStudy();
73         if (newtype.length() == 0 || value.length() == 0) return INPUT;
74
75         Step            step  = mystudy.getSelectedStep();
76         ProjectElement  owner = step.getOwner();
77
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
84
85             mystudy.add(contex);
86         transax.commit();
87         return SUCCESS;
88           }
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
93           try {
94                 transax.rollback();
95           } catch (HibernateException backerror) {
96             logger.debug("Error rolling back transaction", backerror);
97           }
98         }
99         return ERROR;
100           }
101       catch (Exception error) {
102         transax.commit();
103         return INPUT;
104       }
105     }
106
107     public String doDeleteContext () {
108 //  --------------------------------
109       Session     connex  = Database.getSession();
110           Transaction transax = connex.beginTransaction();
111           try {
112                 mystudy = getOpenStudy();
113          
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
119
120         mystudy.remove(context);
121         transax.commit();
122         return SUCCESS;
123           }
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
128           try {
129                 transax.rollback();
130           } catch (HibernateException backerror) {
131             logger.debug("Error rolling back transaction", backerror);
132           }
133         }
134         return ERROR;
135           }
136     }
137     
138     public String doSetContext () {
139 //  -----------------------------      
140           String[]    input   = value.split(",");
141       Session     connex  = Database.getSession();
142           Transaction transax = connex.beginTransaction();
143           try {
144                 mystudy = getOpenStudy();
145
146         Step              step   = mystudy.getSelectedStep();
147         ProjectElement    owner  = step.getOwner();
148         SimulationContext contex = null;
149
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);
156             }
157             else {
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
165             }
166             mystudy.add(contex);
167         contype = getInvolvedContexts();
168
169         transax.commit();
170             return SUCCESS;
171           }
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
176           try {
177                 transax.rollback();
178           } catch (HibernateException backerror) {
179             logger.debug("Error rolling back transaction", backerror);
180           }
181         }
182         return ERROR;
183           }
184       catch (Exception error) {
185         value = input[0];
186         transax.commit();
187         return INPUT;
188       }
189     }
190
191 //  ==============================================================================================================================
192 //  Getters and setters
193 //  ==============================================================================================================================
194
195     public SimulationContextType getContextType () {
196 //  ----------------------------------------------
197       return type;
198     }
199     public String getContextName () {
200 //  -------------------------------
201       return newtype;
202     }
203 //    public List<SimulationContextFacade> getSimulationContexts () {
204 //  -------------------------------------------------------
205 //      return mystudy.getSelectedStep().getAllSimulationContexts();
206 //    }
207     public List<SimulationContextType> getSimulationContextTypes () {
208 //  ---------------------------------------------------------------
209       return contype;
210     }
211     public List<SimulationContext> getSimulationContextValues () {
212 //  ------------------------------------------------------------
213       return contelm;
214     }
215     
216     public void setContextType (String type) {
217 //  ----------------------------------------
218       this.selectype = type;
219     }
220     public void setContextValue (String value) {
221 //  -----------------------------------------
222       this.value = value;
223     }
224     public void setNewType (String name) {
225 //  ------------------------------------
226       this.newtype = name;
227     }
228
229 //  ==============================================================================================================================
230 //  Private service
231 //  ==============================================================================================================================
232
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);
238
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();
244         int     from  = 0;
245         int     to    = 0;
246         while (to < types.length-1) {
247           to += 1;
248               if (types[to].isApproved() == state) continue;
249
250           if (to > from+1) Arrays.sort(types, from, to, compare);
251           state = !state;
252           from  = to;
253         }
254         if (to > from) Arrays.sort(types, from, to+1, compare);
255         contype = Arrays.asList(types);
256       }
257       return contype;
258         }
259 }