]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/NewScenarioAction.java
Salome HOME
1ff8b0abe4545fa4b94fc35282b0d994e28e313e
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewScenarioAction.java
1 package org.splat.simer;
2
3 import java.util.Arrays;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.ResourceBundle;
7
8 import org.splat.dal.bo.som.Publication;
9 import org.splat.dal.bo.som.Scenario;
10 import org.splat.dal.bo.som.Study;
11 import org.splat.service.ProjectElementService;
12 import org.splat.service.ScenarioService;
13 import org.splat.som.Step;
14 import org.splat.wapp.Constants;
15 import org.splat.wapp.Menu;
16
17 /**
18  * New scenario creation action.
19  */
20 public class NewScenarioAction extends Action {
21
22         private transient OpenStudy _mystudy;
23         private transient List<Scenario> _myscene;
24         private String _title;
25         private String _selection;
26         private transient long _scindex;
27         private transient int _bastep;
28         private transient ToDo _action;
29         /**
30          * Injected scenario service.
31          */
32         private ScenarioService _scenarioService;
33         /**
34          * Injected project element service.
35          */
36         private ProjectElementService _projectElementService;
37         private Menu _menu;
38
39         /**
40          * Serial version ID.
41          */
42         private static final long serialVersionUID = -5586724442986956861L;
43
44         protected enum ToDo {
45                 cancel, save
46         };
47
48         // ==============================================================================================================================
49         // Action methods
50         // ==============================================================================================================================
51
52         public String doInitialize() {
53                 _mystudy = getOpenStudy();
54                 Study study = _mystudy.getStudyObject();
55                 Scenario[] scene = study.getScenarii();
56                 Scenario base = scene[scene.length - 1]; // Default base scenario
57                 ResourceBundle locale = ResourceBundle.getBundle("labels",
58                                 getApplicationSettings().getCurrentLocale());
59
60                 _myscene = Arrays.asList(scene);
61                 _scindex = base.getIndex();
62                 _title = locale.getString("label.scenario") + " "
63                                 + String.valueOf(scene.length + 1);
64                 _bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
65                 _selection = _scindex + "." + _bastep;
66                 _action = null;
67
68                 // RKV Menu menu = new NewScenarioMenu(study);
69                 Menu menu = ((NewScenarioMenu) getMenu()).init(study); // RKV
70                 menu.selects(_selection);
71                 getSession().put("menu.scenario", menu);
72
73                 initializationFullScreenContext(Constants.STUDY_MENU,
74                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
75                                 Constants.SCENARIO_MENU);
76
77                 setActionType("focus");
78
79                 return SUCCESS;
80         }
81
82         public String doSelectStep() {
83                 _mystudy = getOpenStudy();
84                 Study study = _mystudy.getStudyObject();
85                 Scenario[] scene = study.getScenarii();
86
87                 _myscene = Arrays.asList(scene);
88                 _bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
89                 _action = null;
90
91                 getMenu("scenario").selects(_selection);
92
93                 initializationFullScreenContext(Constants.STUDY_MENU,
94                                 Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
95                                 Constants.SCENARIO_MENU);
96
97                 setActionType("none");
98
99                 return SUCCESS;
100         }
101
102         public String doCreate() {
103                 if (_action == ToDo.cancel) {
104                         return "cancel";
105                 }
106
107                 try {
108                         _mystudy = getOpenStudy();
109                         _selection = getMenu("scenario").getSelection();
110                         Study study = _mystudy.getStudyObject();
111                         String[] parse = _selection.split("\\x2E");
112                         int scindex = Integer.valueOf(parse[0]);
113                         int number = Integer.valueOf(parse[1]);
114
115                         Scenario[] scene = study.getScenarii();
116                         Scenario bascene = scene[0];
117                         for (int i = 1; i < scene.length; i++) {
118                                 bascene = scene[i];
119                                 if (bascene.getIndex() == scindex) {
120                                         break;
121                                 }
122                         }
123                         Step[] step = null;
124                         Scenario.Properties sprop = new Scenario.Properties().setManager(
125                                         getConnectedUser()).setTitle(_title)
126                                         .setInsertAfter(bascene);
127
128                         _bastep = getProjectElementService().getFirstStep(bascene)
129                                         .getNumber();
130                         if (this.sharesStep()) {
131                                 step = getProjectElementService().getSteps(bascene);
132                                 sprop.setBaseStep(step[number - _bastep]);
133                         }
134                         bascene = getScenarioService().addScenario(study, sprop);
135                         // transax.commit();
136
137                         // Update of the display
138                         if (step != null) {
139                                 for (int i = 0; i < number - _bastep + 1; i++) {
140                                         List<Publication> contents = step[i].getAllDocuments();
141                                         for (Iterator<Publication> j = contents.iterator(); j
142                                                         .hasNext();) {
143                                                 _mystudy.update(j.next());
144                                         }
145                                 }
146                         }
147                         _mystudy.setSelection(bascene.getIndex() + "." + number);
148                         getSession().remove("menu.scenario");
149                         return SUCCESS;
150                 } catch (RuntimeException saverror) {
151                         LOG.error("Reason:", saverror);
152
153                         initializationFullScreenContext(Constants.STUDY_MENU,
154                                         Constants.STUDY_MENU, Constants.FALSE,
155                                         Constants.STANDARD_MENU, Constants.STUDY_MENU);
156
157                         return ERROR;
158                 } catch (Exception error) {
159                         return INPUT;
160                 }
161         }
162
163         // ==============================================================================================================================
164         // Getters and setters
165         // ==============================================================================================================================
166
167         public List<Scenario> getScenarii() {
168                 return _myscene;
169         }
170
171         public long getSelectedScenarioIndex() {
172                 return _scindex;
173         }
174
175         public String getSelection() {
176                 return _selection;
177         }
178
179         public String getSharedStep() {
180                 String[] parse = _selection.split("\\x2E");
181                 return parse[1];
182         }
183
184         public String getTitle() {
185                 return _title;
186         }
187
188         public void setCancel(final boolean cancel) {
189                 _action = ToDo.cancel;
190         }
191
192         public void setSave(final boolean save) {
193                 _action = ToDo.save;
194         }
195
196         public void setSelectedScenario(final String index) {
197                 _scindex = Integer.valueOf(index);
198         }
199
200         public void setSelection(final String step) {
201                 _selection = step;
202         }
203
204         public void setTitle(final String title) {
205                 _title = title;
206         }
207
208         public Boolean sharesStep() {
209                 return (Integer.valueOf(getSharedStep()) > _bastep);
210         }
211
212         /**
213          * Get the projectElementService.
214          * 
215          * @return the projectElementService
216          */
217         public ProjectElementService getProjectElementService() {
218                 return _projectElementService;
219         }
220
221         /**
222          * Set the projectElementService.
223          * 
224          * @param projectElementService
225          *            the projectElementService to set
226          */
227         public void setProjectElementService(
228                         final ProjectElementService projectElementService) {
229                 _projectElementService = projectElementService;
230         }
231
232         /**
233          * Get the menu.
234          * 
235          * @return the menu
236          */
237         public Menu getMenu() {
238                 return _menu;
239         }
240
241         /**
242          * Set the menu.
243          * 
244          * @param menu
245          *            the menu to set
246          */
247         public void setMenu(final Menu menu) {
248                 _menu = menu;
249         }
250
251         /**
252          * Get the scenarioService.
253          * 
254          * @return the scenarioService
255          */
256         public ScenarioService getScenarioService() {
257                 return _scenarioService;
258         }
259
260         /**
261          * Set the scenarioService.
262          * 
263          * @param scenarioService
264          *            the scenarioService to set
265          */
266         public void setScenarioService(final ScenarioService scenarioService) {
267                 _scenarioService = scenarioService;
268         }
269 }