1 package org.splat.simer;
3 import java.util.Arrays;
4 import java.util.Iterator;
6 import java.util.ResourceBundle;
8 import org.hibernate.HibernateException;
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.splat.dal.dao.som.Database;
12 import org.splat.dal.bo.som.Publication;
13 import org.splat.dal.bo.som.Scenario;
14 import org.splat.service.ProjectElementService;
15 import org.splat.service.ScenarioService;
16 import org.splat.som.Step;
17 import org.splat.dal.bo.som.Study;
18 import org.splat.wapp.Menu;
21 public class NewScenarioAction extends Action {
23 private OpenStudy mystudy;
24 private List<Scenario> myscene;
25 private String mytitle;
26 private String selection;
31 * Injected scenario service.
33 private ScenarioService _scenarioService;
35 * Injected project element service.
37 private ProjectElementService _projectElementService;
41 * Value of the menu property.
42 * It can be: none, create, open, study, knowledge, sysadmin, help.
44 private String _menuProperty;
47 * Value of the title bar property.
48 * It can be: study, knowledge.
50 private String _titleProperty;
53 * Value of the tool bar property.
54 * It can be: none, standard, study, back.
56 private String _toolProperty;
59 * Value of the left menu property.
60 * It can be: open, study, knowledge, scenario.
62 private String _leftMenuProperty;
65 * Property that indicates whether the current open study is editable or not.
66 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
67 * It is necessary for correct building the title bar.
69 private String _editDisabledProperty = "false";
74 private static final long serialVersionUID = -5586724442986956861L;
76 protected enum ToDo { cancel, save };
78 // ==============================================================================================================================
80 // ==============================================================================================================================
82 public String doInitialize () {
83 // -----------------------------
84 mystudy = getOpenStudy();
85 Study study = mystudy.getStudyObject();
86 Scenario[] scene = study.getScenarii();
87 Scenario base = scene[scene.length-1]; // Default base scenario
88 ResourceBundle locale = ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale());
90 myscene = Arrays.asList(scene);
91 scindex = base.getIndex();
92 mytitle = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
93 bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
94 selection = scindex + "." + bastep;
97 //RKV Menu menu = new NewScenarioMenu(study);
98 Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
99 menu.selects(selection);
100 getSession().put("menu.scenario", menu);
102 setMenuProperty("study");
103 setTitleProperty("study");
104 setEditDisabledProperty("true");
105 setToolProperty("none");
106 setLeftMenuProperty("scenario");
107 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
112 public String doSelectStep () {
113 // -----------------------------
114 mystudy = getOpenStudy();
115 Study study = mystudy.getStudyObject();
116 Scenario[] scene = study.getScenarii();
118 myscene = Arrays.asList(scene);
119 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
122 getMenu("scenario").selects(selection);
124 setMenuProperty("study");
125 setTitleProperty("study");
126 setEditDisabledProperty("true");
127 setToolProperty("none");
128 setLeftMenuProperty("scenario");
129 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
134 public String doCreate () {
135 // -------------------------
136 if (action == ToDo.cancel) return "cancel";
138 // Session session = Database.getCurSession();
139 // Transaction transax = session.beginTransaction();
140 try { mystudy = getOpenStudy();
141 selection = getMenu("scenario").getSelection();
142 Study study = mystudy.getStudyObject();
143 String[] parse = selection.split("\\x2E");
144 int scindex = Integer.valueOf(parse[0]);
145 int number = Integer.valueOf(parse[1]);
147 Scenario[] scene = study.getScenarii();
148 Scenario bascene = scene[0];
149 for (int i=1; i<scene.length; i++) {
151 if (bascene.getIndex() == scindex) break;
154 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
156 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
157 if (this.sharesStep()) {
158 step = getProjectElementService().getSteps(bascene);
159 sprop.setBaseStep(step[number-bastep]);
161 bascene = getScenarioService().addScenario(study, sprop);
164 // Update of the display
165 if (step != null) for (int i=0; i<number-bastep+1; i++) {
166 List<Publication> contents = step[i].getAllDocuments();
167 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
169 mystudy.setSelection(bascene.getIndex() + "." + number);
170 getSession().remove("menu.scenario");
173 catch (RuntimeException saverror) {
174 LOG.error("Reason:", saverror);
176 setMenuProperty("study");
177 setTitleProperty("study");
178 setToolProperty("standard");
179 setLeftMenuProperty("study");
180 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
184 catch (Exception error) {
189 // ==============================================================================================================================
190 // Getters and setters
191 // ==============================================================================================================================
193 public List<Scenario> getScenarii () {
194 // ------------------------------------
197 public long getSelectedScenarioIndex () {
198 // --------------------------------------
201 public String getSelection () {
202 // -----------------------------
205 public String getSharedStep () {
206 // -------------------------------
207 String[] parse = selection.split("\\x2E");
210 public String getTitle () {
211 // -------------------------
215 public void setCancel (boolean cancel) {
216 // --------------------------------------
217 action = ToDo.cancel;
219 public void setSave (boolean save) {
220 // ----------------------------------
223 public void setSelectedScenario (String index) {
224 // ----------------------------------------------
225 scindex = Integer.valueOf(index);
227 public void setSelection (String step) {
228 // --------------------------------------
231 public void setTitle (String title) {
232 // -----------------------------------
235 public Boolean sharesStep () {
236 // ----------------------------
237 return (Integer.valueOf(getSharedStep()) > bastep);
240 * Get the projectElementService.
242 * @return the projectElementService
244 public ProjectElementService getProjectElementService() {
245 return _projectElementService;
249 * Set the projectElementService.
251 * @param projectElementService
252 * the projectElementService to set
254 public void setProjectElementService(
255 ProjectElementService projectElementService) {
256 _projectElementService = projectElementService;
263 public Menu getMenu() {
269 * @param menu the menu to set
271 public void setMenu(Menu menu) {
276 * Get the scenarioService.
277 * @return the scenarioService
279 public ScenarioService getScenarioService() {
280 return _scenarioService;
284 * Set the scenarioService.
285 * @param scenarioService the scenarioService to set
287 public void setScenarioService(ScenarioService scenarioService) {
288 _scenarioService = scenarioService;
292 * Get the menuProperty.
293 * @return the menuProperty
295 public String getMenuProperty() {
296 return _menuProperty;
300 * Set the menuProperty.
301 * @param menuProperty the menuProperty to set
303 public void setMenuProperty(String menuProperty) {
304 this._menuProperty = menuProperty;
308 * Get the _titleProperty.
309 * @return the _titleProperty
311 public String getTitleProperty() {
312 return _titleProperty;
316 * Set the _titleProperty.
317 * @param _titleProperty the titleProperty to set
319 public void setTitleProperty(String titleProperty) {
320 _titleProperty = titleProperty;
324 * Get the _editDisabledProperty.
325 * @return the _editDisabledProperty
327 public String getEditDisabledProperty() {
328 return _editDisabledProperty;
332 * Set the _editDisabledProperty.
333 * @param _editDisabledProperty the _editDisabledProperty to set
335 public void setEditDisabledProperty(String _editDisabledProperty) {
336 this._editDisabledProperty = _editDisabledProperty;
340 * Get the toolProperty.
341 * @return the toolProperty
343 public String getToolProperty() {
344 return _toolProperty;
348 * Set the toolProperty.
349 * @param toolProperty the toolProperty to set
351 public void setToolProperty(final String toolProperty) {
352 _toolProperty = toolProperty;
356 * Get the leftMenuProperty.
357 * @return the leftMenuProperty
359 public String getLeftMenuProperty() {
360 return _leftMenuProperty;
364 * Set the leftMenuProperty.
365 * @param leftMenuProperty the leftMenuProperty to set
367 public void setLeftMenuProperty(final String leftMenuProperty) {
368 _leftMenuProperty = leftMenuProperty;