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