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