]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/EditSimulationContextAction.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[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.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;
18
19 public class EditSimulationContextAction extends DisplayStudyStepAction {
20
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
27         /**
28          * Injected study service.
29          */
30         private StudyService _studyService;
31         /**
32          * Injected step service.
33          */
34         private StepService _stepService;
35         /**
36          * Injected simulation context service.
37          */
38         private SimulationContextService _simulationContextService;
39
40         /**
41          * Get the stepService.
42          * 
43          * @return the stepService
44          */
45         public StepService getStepService() {
46                 return _stepService;
47         }
48
49         /**
50          * Set the stepService.
51          * 
52          * @param stepService
53          *            the stepService to set
54          */
55         public void setStepService(StepService stepService) {
56                 _stepService = stepService;
57         }
58
59         private static final long serialVersionUID = -641719644024601042L;
60
61         // ==============================================================================================================================
62         // Action methods
63         // ==============================================================================================================================
64
65         public String doInitialize() {
66                 // -----------------------------
67                 Session connex = Database.getSession();
68                 Transaction transax = connex.beginTransaction();
69
70                 mystudy = getOpenStudy();
71                 contype = getInvolvedContexts();
72
73                 transax.commit();
74                 if (contype.isEmpty())
75                         return "create";
76                 else
77                         return "select";
78         }
79
80         public String doSelectContext() {
81                 // --------------------------------
82                 Session connex = Database.getSession();
83                 Transaction transax = connex.beginTransaction();
84                 try {
85                         mystudy = getOpenStudy();
86                         int typid = Integer.valueOf(selectype);
87                         if (typid == 0)
88                                 return "create";
89
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));
96
97                         return "set";
98                 } finally {
99                         transax.commit();
100                 }
101         }
102
103         public String doCreateContext() {
104                 // --------------------------------
105                 Session connex = Database.getSession();
106                 Transaction transax = connex.beginTransaction();
107                 try {
108                         mystudy = getOpenStudy();
109                         if (newtype.length() == 0 || value.length() == 0)
110                                 return INPUT;
111
112                         Step step = mystudy.getSelectedStep();
113                         ProjectElement owner = step.getOwner();
114
115                         SimulationContext.Properties cprop = new SimulationContext.Properties();
116                         SimulationContext contex = null;
117                         type = getSimulationContextService().createType(newtype,
118                                         step.getStep());
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
123                         else
124                                 contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
125
126                         mystudy.add(contex);
127                         transax.commit();
128                         return SUCCESS;
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
133                                 try {
134                                         transax.rollback();
135                                 } catch (HibernateException backerror) {
136                                         logger.debug("Error rolling back transaction", backerror);
137                                 }
138                         }
139                         return ERROR;
140                 } catch (Exception error) {
141                         transax.commit();
142                         return INPUT;
143                 }
144         }
145
146         public String doDeleteContext() {
147                 // --------------------------------
148                 Session connex = Database.getSession();
149                 Transaction transax = connex.beginTransaction();
150                 try {
151                         mystudy = getOpenStudy();
152
153                         Step step = mystudy.getSelectedStep();
154                         ProjectElement owner = step.getOwner();
155                         SimulationContext context = step.getSimulationContext(Integer
156                                         .valueOf(myindex));
157                         if (owner instanceof Study)
158                                 getStudyService()
159                                                 .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
160                         else
161                                 getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
162
163                         mystudy.remove(context);
164                         transax.commit();
165                         return SUCCESS;
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
170                                 try {
171                                         transax.rollback();
172                                 } catch (HibernateException backerror) {
173                                         logger.debug("Error rolling back transaction", backerror);
174                                 }
175                         }
176                         return ERROR;
177                 }
178         }
179
180         public String doSetContext() {
181                 // -----------------------------
182                 String[] input = value.split(",");
183                 Session connex = Database.getSession();
184                 Transaction transax = connex.beginTransaction();
185                 try {
186                         mystudy = getOpenStudy();
187
188                         Step step = mystudy.getSelectedStep();
189                         ProjectElement owner = step.getOwner();
190                         SimulationContext contex = null;
191
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(
197                                                 valid);
198                                 if (owner instanceof Study)
199                                         getStudyService()
200                                                         .addProjectContext(((Study) owner), contex);
201                                 else
202                                         getStepService().addSimulationContext(step, contex);
203                         } else {
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
212                                 else
213                                         contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
214                         }
215                         mystudy.add(contex);
216                         contype = getInvolvedContexts();
217
218                         transax.commit();
219                         return SUCCESS;
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
224                                 try {
225                                         transax.rollback();
226                                 } catch (HibernateException backerror) {
227                                         logger.debug("Error rolling back transaction", backerror);
228                                 }
229                         }
230                         return ERROR;
231                 } catch (Exception error) {
232                         value = input[0];
233                         transax.commit();
234                         return INPUT;
235                 }
236         }
237
238         // ==============================================================================================================================
239         // Getters and setters
240         // ==============================================================================================================================
241
242         public SimulationContextType getContextType() {
243                 // ----------------------------------------------
244                 return type;
245         }
246
247         public String getContextName() {
248                 // -------------------------------
249                 return newtype;
250         }
251
252         // public List<SimulationContextFacade> getSimulationContexts () {
253         // -------------------------------------------------------
254         // return mystudy.getSelectedStep().getAllSimulationContexts();
255         // }
256         public List<SimulationContextType> getSimulationContextTypes() {
257                 // ---------------------------------------------------------------
258                 return contype;
259         }
260
261         public List<SimulationContext> getSimulationContextValues() {
262                 // ------------------------------------------------------------
263                 return contelm;
264         }
265
266         public void setContextType(String type) {
267                 // ----------------------------------------
268                 this.selectype = type;
269         }
270
271         public void setContextValue(String value) {
272                 // -----------------------------------------
273                 this.value = value;
274         }
275
276         public void setNewType(String name) {
277                 // ------------------------------------
278                 this.newtype = name;
279         }
280
281         // ==============================================================================================================================
282         // Private service
283         // ==============================================================================================================================
284
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);
291
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();
298                         int from = 0;
299                         int to = 0;
300                         while (to < types.length - 1) {
301                                 to += 1;
302                                 if (types[to].isApproved() == state)
303                                         continue;
304
305                                 if (to > from + 1)
306                                         Arrays.sort(types, from, to, compare);
307                                 state = !state;
308                                 from = to;
309                         }
310                         if (to > from)
311                                 Arrays.sort(types, from, to + 1, compare);
312                         contype = Arrays.asList(types);
313                 }
314                 return contype;
315         }
316
317         /**
318          * Get the studyService.
319          * 
320          * @return the studyService
321          */
322         public StudyService getStudyService() {
323                 return _studyService;
324         }
325
326         /**
327          * Set the studyService.
328          * 
329          * @param studyService
330          *            the studyService to set
331          */
332         public void setStudyService(StudyService studyService) {
333                 _studyService = studyService;
334         }
335
336         /**
337          * Get the simulationContextService.
338          * 
339          * @return the simulationContextService
340          */
341         public SimulationContextService getSimulationContextService() {
342                 return _simulationContextService;
343         }
344
345         /**
346          * Set the simulationContextService.
347          * 
348          * @param simulationContextService
349          *            the simulationContextService to set
350          */
351         public void setSimulationContextService(
352                         SimulationContextService simulationContextService) {
353                 _simulationContextService = simulationContextService;
354         }
355 }