]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/NewScenarioAction.java
Salome HOME
Create new scenario from existing one is improved
[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 number = Integer.valueOf(parse[1]);
113
114                         Scenario[] scene = study.getScenarii();
115                         Scenario bascene = scene[0];
116                         for(Scenario scenario : scene) {
117                                 bascene = scenario;
118                                 if(bascene.getIndex() == _scindex) {
119                                         break;
120                                 }
121                         }
122                         Step[] step = null;
123                         Scenario.Properties sprop = new Scenario.Properties().setManager(
124                                         getConnectedUser()).setTitle(_title)
125                                         .setInsertAfter(bascene);
126
127                         
128                         long selectedScenarioId = Long.valueOf(parse[0]);
129                         Scenario selectedScenario = scene[0];
130                         for(Scenario scenario : scene) {
131                                 if(scenario.getIndex() == selectedScenarioId) {
132                                         selectedScenario = scenario;
133                                 }
134                         }
135                         _bastep = getProjectElementService().getFirstStep(selectedScenario)
136                                         .getNumber();
137                         if (this.sharesStep()) {
138                                 step = getProjectElementService().getSteps(selectedScenario);
139                                 sprop.setBaseStep(step[number - _bastep]);
140                         }
141                         Scenario createdScenario = getScenarioService().addScenario(study, sprop);
142                         // transax.commit();
143
144                         // Update of the display
145                         if (step != null) {
146                                 for (int i = 0; i < number - _bastep + 1; i++) {
147                                         List<Publication> contents = step[i].getAllDocuments();
148                                         for (Iterator<Publication> j = contents.iterator(); j
149                                                         .hasNext();) {
150                                                 _mystudy.update(j.next());
151                                         }
152                                 }
153                         }
154                         _mystudy.setSelection(createdScenario.getIndex() + "." + number);
155                         getSession().remove("menu.scenario");
156                         return SUCCESS;
157                 } catch (RuntimeException saverror) {
158                         LOG.error("Reason:", saverror);
159
160                         initializationFullScreenContext(Constants.STUDY_MENU,
161                                         Constants.STUDY_MENU, Constants.FALSE,
162                                         Constants.STANDARD_MENU, Constants.STUDY_MENU);
163
164                         return ERROR;
165                 } catch (Exception error) {
166                         return INPUT;
167                 }
168         }
169
170         // ==============================================================================================================================
171         // Getters and setters
172         // ==============================================================================================================================
173
174         public List<Scenario> getScenarii() {
175                 return _myscene;
176         }
177
178         public long getSelectedScenarioIndex() {
179                 return _scindex;
180         }
181
182         public String getSelection() {
183                 return _selection;
184         }
185
186         public String getSharedStep() {
187                 String[] parse = _selection.split("\\x2E");
188                 return parse[1];
189         }
190
191         public String getTitle() {
192                 return _title;
193         }
194
195         public void setCancel(final boolean cancel) {
196                 _action = ToDo.cancel;
197         }
198
199         public void setSave(final boolean save) {
200                 _action = ToDo.save;
201         }
202
203         public void setSelectedScenario(final String index) {
204                 _scindex = Integer.valueOf(index);
205         }
206
207         public void setSelection(final String step) {
208                 _selection = step;
209         }
210
211         public void setTitle(final String title) {
212                 _title = title;
213         }
214
215         public Boolean sharesStep() {
216                 return (Integer.valueOf(getSharedStep()) > _bastep);
217         }
218
219         /**
220          * Get the projectElementService.
221          * 
222          * @return the projectElementService
223          */
224         public ProjectElementService getProjectElementService() {
225                 return _projectElementService;
226         }
227
228         /**
229          * Set the projectElementService.
230          * 
231          * @param projectElementService
232          *            the projectElementService to set
233          */
234         public void setProjectElementService(
235                         final ProjectElementService projectElementService) {
236                 _projectElementService = projectElementService;
237         }
238
239         /**
240          * Get the menu.
241          * 
242          * @return the menu
243          */
244         public Menu getMenu() {
245                 return _menu;
246         }
247
248         /**
249          * Set the menu.
250          * 
251          * @param menu
252          *            the menu to set
253          */
254         public void setMenu(final Menu menu) {
255                 _menu = menu;
256         }
257
258         /**
259          * Get the scenarioService.
260          * 
261          * @return the scenarioService
262          */
263         public ScenarioService getScenarioService() {
264                 return _scenarioService;
265         }
266
267         /**
268          * Set the scenarioService.
269          * 
270          * @param scenarioService
271          *            the scenarioService to set
272          */
273         public void setScenarioService(final ScenarioService scenarioService) {
274                 _scenarioService = scenarioService;
275         }
276 }