Salome HOME
refactoring
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewStudyAction.java
1 package org.splat.simer;
2
3 import java.util.List;
4 import java.util.ResourceBundle;
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.Scenario;
11 import org.splat.dal.bo.som.SimulationContext;
12 import org.splat.dal.bo.som.SimulationContextType;
13 import org.splat.dal.bo.som.Study;
14 import org.splat.service.StudyService;
15
16 public class NewStudyAction extends Action {
17
18         private String title = null;
19         private List<SimulationContext> contelm = null;
20         private String context = null;
21
22         private static int number = 0;
23         private static final long serialVersionUID = 693943641800113782L;
24
25         
26         /**
27          * The Study service.
28          */
29         private StudyService _studyService;
30
31         // ==============================================================================================================================
32         // Action methods
33         // ==============================================================================================================================
34
35         //Fill the values of the drop-down list. 
36     public String doInitialize () {     
37         
38       //get the list of the simulation contexts of the study
39       contelm = _studyService.getSimulationContextList();
40         
41       //set the default name of the new study
42       ResourceBundle locale  = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
43       title = locale.getString("label.study") + " " + String.valueOf(number + 1);
44                 
45       return SUCCESS;
46     }
47
48         public String doCreate() throws Exception {
49                 // -------------------------
50                 String[] input = context.split(",");
51                 int valid = Integer.valueOf(input[0]);
52                 String value = ""; // input[1] if exists
53
54                 Session session = Database.getSession();
55                 Transaction transax = session.beginTransaction();
56                 Study.Properties sprop = new Study.Properties();
57
58                 // Check arguments and creation of the study
59                 try {
60                         if (valid == -1)
61                                 throw new Exception();
62                         if (valid == 0) {
63                                 value = input[1].trim();
64                                 if (value.length() == 0)
65                                         return INPUT; // No need to reinitialize the list of existing products
66                         }
67                         sprop.setTitle(title).setManager(getConnectedUser());
68                         sprop.checkValidity();
69                         sprop.disableCheck();
70                 } catch (Exception error) {
71                         SimulationContext.Properties cprop = new SimulationContext.Properties();
72                         SimulationContextType product = SimulationContext
73                                         .selectType("product");
74                         contelm = Database.selectSimulationContextsWhere(cprop
75                                         .setType(product));
76                         transax.commit();
77                         return INPUT; // Title empty, simply wait for input without error message
78                 }
79                 try {
80                         Study study = getStudyService().createStudy(sprop);
81
82                         // Addition of a default scenario
83                         ResourceBundle locale = ResourceBundle.getBundle("labels",
84                                         ApplicationSettings.getCurrentLocale());
85                         Scenario.Properties oprop = new Scenario.Properties();
86                         oprop.setTitle(locale.getString("label.scenario") + " 1");
87                         getStudyService().addScenario(study, oprop);
88
89                         // Addition of the entered project context
90                         if (valid == 0) { // Input of new project context
91                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
92                                 cprop.setType(SimulationContext.selectType("product"))
93                                                 .setValue(value);
94                                 getStudyService().addProjectContext(study, cprop);
95                         } else { // Selection of existing project context
96                                 SimulationContext context = Database
97                                                 .selectSimulationContext(valid);
98                                 getStudyService().addProjectContext(study, context);
99                         }
100                         // Update of the session
101                         number += 1;
102                         open(study); // Opens the study,
103                         transax.commit();
104                         return SUCCESS;
105                 } catch (Exception error) {
106                         logger.error("Unable to save the study, reason:", error);
107                         if (transax != null && transax.isActive()) {
108                                 // Second try-catch as the rollback could fail as well
109                                 try {
110                                         transax.rollback();
111                                 } catch (HibernateException backerror) {
112                                         logger.debug("Error rolling back transaction", backerror);
113                                 }
114                         }
115                         return ERROR;
116                 }
117         }
118
119         // ==============================================================================================================================
120         // Getters and setters
121         // ==============================================================================================================================
122
123         public String getProjectContext() {
124                 // ----------------------------------
125                 return context;
126         }
127
128         public List<SimulationContext> getProjectContextValues() {
129                 // ---------------------------------------------------------
130                 return contelm;
131         }
132
133         public String getTitle() {
134                 // ----------------------------
135                 return title;
136         }
137
138         public void setProjectContext(String value) {
139                 // --------------------------------------------
140                 this.context = value;
141         }
142
143         public void setTitle(String value) {
144                 // -----------------------------------
145                 this.title = value;
146         }
147
148         /**
149          * Get the studyService.
150          * 
151          * @return the studyService
152          */
153         public StudyService getStudyService() {
154                 return _studyService;
155         }
156
157         /**
158          * Set the studyService.
159          * 
160          * @param studyService
161          *            the studyService to set
162          */
163         public void setStudyService(StudyService studyService) {
164                 _studyService = studyService;
165         }
166 }