Salome HOME
df6211e979c82f639a6af9b9be6f51f00a883723
[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.splat.dal.bo.som.Scenario;
7 import org.splat.dal.bo.som.SimulationContext;
8 import org.splat.dal.bo.som.SimulationContextType;
9 import org.splat.dal.bo.som.Study;
10 import org.splat.service.ScenarioService;
11 import org.splat.service.SimulationContextService;
12 import org.splat.service.StudyService;
13
14 public class NewStudyAction extends Action {
15
16         private String title = null;
17         private List<SimulationContext> contelm = null;
18         private String context = null;
19
20         private static int number = 0;
21         /**
22          * Serial version ID.
23          */
24         private static final long serialVersionUID = 693943641800113782L;
25
26         /**
27          * The injected Study service.
28          */
29         private StudyService _studyService;
30         private SimulationContextService _simulationContextService;
31         /**
32          * Injected scenario service.
33          */
34         private ScenarioService _scenarioService;
35         
36         /**
37          * Value of the menu property. 
38          * It can be: none, create, open, study, knowledge, sysadmin, help.
39          */
40         private String _menuProperty;
41
42         // ==============================================================================================================================
43         // Action methods
44         // ==============================================================================================================================
45
46         // Fill the values of the drop-down list.
47         public String doInitialize() {
48
49                 // get the list of the simulation contexts of the study
50                 contelm = getSimulationContextService().getSimulationContextList();
51
52                 // set the default name of the new study
53                 ResourceBundle locale = ResourceBundle.getBundle("labels",
54                                 ApplicationSettings.getCurrentLocale());
55                 title = locale.getString("label.study") + " "
56                                 + String.valueOf(number + 1);
57                 
58                 setMenuProperty("create");
59                 initializationScreenContext(_menuProperty);
60
61                 return SUCCESS;
62         }
63
64         public String doCreate() throws Exception {
65                 String[] input = context.split(",");
66                 int valid = Integer.valueOf(input[0]);
67                 String value = ""; // input[1] if exists
68
69                 Study.Properties sprop = new Study.Properties();
70
71                 // Check arguments and creation of the study
72                 try {
73                         if (valid == -1)
74                                 throw new Exception();
75                         if (valid == 0) {
76                                 value = input[1].trim();
77                                 if (value.length() == 0) {
78                                         
79                                         setMenuProperty("create");
80                                         initializationScreenContext(_menuProperty);
81                                         
82                                         return INPUT; // No need to reinitialize the list of existing products
83                                 }
84                         }
85                         sprop.setTitle(title).setManager(getConnectedUser());
86                         sprop.checkValidity();
87                         sprop.disableCheck();
88                 } catch (Exception error) {
89                         SimulationContext.Properties cprop = new SimulationContext.Properties();
90                         SimulationContextType product = getSimulationContextService()
91                                         .selectType("product");
92                         contelm = getSimulationContextService()
93                                         .selectSimulationContextsWhere(cprop.setType(product));
94                         
95                         setMenuProperty("create");
96                         initializationScreenContext(_menuProperty);
97                         
98                         return INPUT; // Title empty, simply wait for input without error message
99                 }
100                 try {
101                         Study study = getStudyService().createStudy(sprop);
102
103                         // Addition of a default scenario
104                         ResourceBundle locale = ResourceBundle.getBundle("labels",
105                                         ApplicationSettings.getCurrentLocale());
106                         Scenario.Properties oprop = new Scenario.Properties();
107                         oprop.setTitle(locale.getString("label.scenario") + " 1");
108                         getScenarioService().addScenario(study, oprop);
109
110                         // Addition of the entered project context
111                         if (valid == 0) { // Input of new project context
112                                 SimulationContext.Properties cprop = new SimulationContext.Properties();
113                                 cprop.setType(
114                                                 getSimulationContextService().selectType("product"))
115                                                 .setValue(value);
116                                 getStudyService().addProjectContext(study, cprop);
117                         } else { // Selection of existing project context
118                                 SimulationContext context = getSimulationContextService()
119                                                 .selectSimulationContext(valid);
120                                 getStudyService().addProjectContext(study, context);
121                         }
122                         // Update of the session
123                         number += 1;
124                         open(study); // Opens the study,
125                         
126                         setMenuProperty("study");
127                         initializationScreenContext(_menuProperty);
128                         
129                         return SUCCESS;
130                 } catch (Exception error) {
131                         logger.error("Unable to save the study, reason:", error);
132                         setMenuProperty("none");
133                         initializationScreenContext(_menuProperty);
134                         return ERROR;
135                 }
136         }
137
138         // ==============================================================================================================================
139         // Getters and setters
140         // ==============================================================================================================================
141
142         public String getProjectContext() {
143                 // ----------------------------------
144                 return context;
145         }
146
147         public List<SimulationContext> getProjectContextValues() {
148                 // ---------------------------------------------------------
149                 return contelm;
150         }
151
152         public String getTitle() {
153                 // ----------------------------
154                 return title;
155         }
156
157         public void setProjectContext(String value) {
158                 // --------------------------------------------
159                 this.context = value;
160         }
161
162         public void setTitle(String value) {
163                 // -----------------------------------
164                 this.title = value;
165         }
166
167         /**
168          * Get the studyService.
169          * 
170          * @return the studyService
171          */
172         public StudyService getStudyService() {
173                 return _studyService;
174         }
175
176         /**
177          * Set the studyService.
178          * 
179          * @param studyService
180          *            the studyService to set
181          */
182         public void setStudyService(StudyService studyService) {
183                 _studyService = studyService;
184         }
185
186         /**
187          * Get the simulationContextService.
188          * 
189          * @return the simulationContextService
190          */
191         public SimulationContextService getSimulationContextService() {
192                 return _simulationContextService;
193         }
194
195         /**
196          * Set the simulationContextService.
197          * 
198          * @param simulationContextService
199          *            the simulationContextService to set
200          */
201         public void setSimulationContextService(
202                         SimulationContextService simulationContextService) {
203                 _simulationContextService = simulationContextService;
204         }
205
206         /**
207          * Get the scenarioService.
208          * @return the scenarioService
209          */
210         public ScenarioService getScenarioService() {
211                 return _scenarioService;
212         }
213
214         /**
215          * Set the scenarioService.
216          * @param scenarioService the scenarioService to set
217          */
218         public void setScenarioService(ScenarioService scenarioService) {
219                 _scenarioService = scenarioService;
220         }
221         
222         /**
223          * Get the menuProperty.
224          * @return the menuProperty
225          */
226         public String getMenuProperty() {
227                 return _menuProperty;
228         }
229
230         /**
231          * Set the menuProperty.
232          * @param menuProperty the menuProperty to set
233          */
234         public void setMenuProperty(String menuProperty) {
235                 this._menuProperty = menuProperty;
236         }
237 }