Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[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.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;
17
18 public class EditSimulationContextAction extends DisplayStudyStepAction {
19
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;
28
29         /**
30          * Get the stepService.
31          * 
32          * @return the stepService
33          */
34         public StepService getStepService() {
35                 return _stepService;
36         }
37
38         /**
39          * Set the stepService.
40          * 
41          * @param stepService
42          *            the stepService to set
43          */
44         public void setStepService(StepService stepService) {
45                 _stepService = stepService;
46         }
47
48         private static final long serialVersionUID = -641719644024601042L;
49
50         // ==============================================================================================================================
51         // Action methods
52         // ==============================================================================================================================
53
54         public String doInitialize() {
55                 // -----------------------------
56                 Session connex = Database.getSession();
57                 Transaction transax = connex.beginTransaction();
58
59                 mystudy = getOpenStudy();
60                 contype = getInvolvedContexts();
61
62                 transax.commit();
63                 if (contype.isEmpty())
64                         return "create";
65                 else
66                         return "select";
67         }
68
69         public String doSelectContext() {
70                 // --------------------------------
71                 Session connex = Database.getSession();
72                 Transaction transax = connex.beginTransaction();
73                 try {
74                         mystudy = getOpenStudy();
75                         int typid = Integer.valueOf(selectype);
76                         if (typid == 0)
77                                 return "create";
78
79                         SimulationContext.Properties cprop = new SimulationContext.Properties();
80                         type = SimulationContext.selectType(typid);
81                         newtype = type.getName();
82                         contype = getInvolvedContexts();
83                         contelm = Database.selectSimulationContextsWhere(cprop
84                                         .setType(type));
85
86                         return "set";
87                 } finally {
88                         transax.commit();
89                 }
90         }
91
92         public String doCreateContext() {
93                 // --------------------------------
94                 Session connex = Database.getSession();
95                 Transaction transax = connex.beginTransaction();
96                 try {
97                         mystudy = getOpenStudy();
98                         if (newtype.length() == 0 || value.length() == 0)
99                                 return INPUT;
100
101                         Step step = mystudy.getSelectedStep();
102                         ProjectElement owner = step.getOwner();
103
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
111                         else
112                                 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
113
114                         mystudy.add(contex);
115                         transax.commit();
116                         return SUCCESS;
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
121                                 try {
122                                         transax.rollback();
123                                 } catch (HibernateException backerror) {
124                                         logger.debug("Error rolling back transaction", backerror);
125                                 }
126                         }
127                         return ERROR;
128                 } catch (Exception error) {
129                         transax.commit();
130                         return INPUT;
131                 }
132         }
133
134         public String doDeleteContext() {
135                 // --------------------------------
136                 Session connex = Database.getSession();
137                 Transaction transax = connex.beginTransaction();
138                 try {
139                         mystudy = getOpenStudy();
140
141                         Step step = mystudy.getSelectedStep();
142                         ProjectElement owner = step.getOwner();
143                         SimulationContext context = step.getSimulationContext(Integer
144                                         .valueOf(myindex));
145                         if (owner instanceof Study)
146                                 getStudyService()
147                                                 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
148                         else
149                                 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
150
151                         mystudy.remove(context);
152                         transax.commit();
153                         return SUCCESS;
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
158                                 try {
159                                         transax.rollback();
160                                 } catch (HibernateException backerror) {
161                                         logger.debug("Error rolling back transaction", backerror);
162                                 }
163                         }
164                         return ERROR;
165                 }
166         }
167
168         public String doSetContext() {
169                 // -----------------------------
170                 String[] input = value.split(",");
171                 Session connex = Database.getSession();
172                 Transaction transax = connex.beginTransaction();
173                 try {
174                         mystudy = getOpenStudy();
175
176                         Step step = mystudy.getSelectedStep();
177                         ProjectElement owner = step.getOwner();
178                         SimulationContext contex = null;
179
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)
186                                         getStudyService()
187                                                         .addProjectContext(((Study) owner), contex);
188                                 else
189                                         getStepService().addSimulationContext(step, contex);
190                         } else {
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(
195                                                 input[1].trim());
196                                 if (owner instanceof Study)
197                                         contex = getStudyService().addProjectContext(
198                                                         ((Study) owner), cprop); // Re-indexes knowledges and the study
199                                 else
200                                         contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
201                         }
202                         mystudy.add(contex);
203                         contype = getInvolvedContexts();
204
205                         transax.commit();
206                         return SUCCESS;
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
211                                 try {
212                                         transax.rollback();
213                                 } catch (HibernateException backerror) {
214                                         logger.debug("Error rolling back transaction", backerror);
215                                 }
216                         }
217                         return ERROR;
218                 } catch (Exception error) {
219                         value = input[0];
220                         transax.commit();
221                         return INPUT;
222                 }
223         }
224
225         // ==============================================================================================================================
226         // Getters and setters
227         // ==============================================================================================================================
228
229         public SimulationContextType getContextType() {
230                 // ----------------------------------------------
231                 return type;
232         }
233
234         public String getContextName() {
235                 // -------------------------------
236                 return newtype;
237         }
238
239         // public List<SimulationContextFacade> getSimulationContexts () {
240         // -------------------------------------------------------
241         // return mystudy.getSelectedStep().getAllSimulationContexts();
242         // }
243         public List<SimulationContextType> getSimulationContextTypes() {
244                 // ---------------------------------------------------------------
245                 return contype;
246         }
247
248         public List<SimulationContext> getSimulationContextValues() {
249                 // ------------------------------------------------------------
250                 return contelm;
251         }
252
253         public void setContextType(String type) {
254                 // ----------------------------------------
255                 this.selectype = type;
256         }
257
258         public void setContextValue(String value) {
259                 // -----------------------------------------
260                 this.value = value;
261         }
262
263         public void setNewType(String name) {
264                 // ------------------------------------
265                 this.newtype = name;
266         }
267
268         // ==============================================================================================================================
269         // Private service
270         // ==============================================================================================================================
271
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);
278
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();
285                         int from = 0;
286                         int to = 0;
287                         while (to < types.length - 1) {
288                                 to += 1;
289                                 if (types[to].isApproved() == state)
290                                         continue;
291
292                                 if (to > from + 1)
293                                         Arrays.sort(types, from, to, compare);
294                                 state = !state;
295                                 from = to;
296                         }
297                         if (to > from)
298                                 Arrays.sort(types, from, to + 1, compare);
299                         contype = Arrays.asList(types);
300                 }
301                 return contype;
302         }
303
304         /**
305          * Get the studyService.
306          * 
307          * @return the studyService
308          */
309         public StudyService getStudyService() {
310                 return _studyService;
311         }
312
313         /**
314          * Set the studyService.
315          * 
316          * @param studyService
317          *            the studyService to set
318          */
319         public void setStudyService(StudyService studyService) {
320                 _studyService = studyService;
321         }
322 }