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);
109 setActionType("focus");
114 public String doSelectStep () {
115 // -----------------------------
116 mystudy = getOpenStudy();
117 Study study = mystudy.getStudyObject();
118 Scenario[] scene = study.getScenarii();
120 myscene = Arrays.asList(scene);
121 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
124 getMenu("scenario").selects(selection);
126 setMenuProperty("study");
127 setTitleProperty("study");
128 setEditDisabledProperty("true");
129 setToolProperty("none");
130 setLeftMenuProperty("scenario");
131 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
133 setActionType("none");
138 public String doCreate () {
139 // -------------------------
140 if (action == ToDo.cancel) return "cancel";
142 // Session session = Database.getCurSession();
143 // Transaction transax = session.beginTransaction();
144 try { mystudy = getOpenStudy();
145 selection = getMenu("scenario").getSelection();
146 Study study = mystudy.getStudyObject();
147 String[] parse = selection.split("\\x2E");
148 int scindex = Integer.valueOf(parse[0]);
149 int number = Integer.valueOf(parse[1]);
151 Scenario[] scene = study.getScenarii();
152 Scenario bascene = scene[0];
153 for (int i=1; i<scene.length; i++) {
155 if (bascene.getIndex() == scindex) break;
158 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
160 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
161 if (this.sharesStep()) {
162 step = getProjectElementService().getSteps(bascene);
163 sprop.setBaseStep(step[number-bastep]);
165 bascene = getScenarioService().addScenario(study, sprop);
168 // Update of the display
169 if (step != null) for (int i=0; i<number-bastep+1; i++) {
170 List<Publication> contents = step[i].getAllDocuments();
171 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
173 mystudy.setSelection(bascene.getIndex() + "." + number);
174 getSession().remove("menu.scenario");
177 catch (RuntimeException saverror) {
178 LOG.error("Reason:", saverror);
180 setMenuProperty("study");
181 setTitleProperty("study");
182 setToolProperty("standard");
183 setLeftMenuProperty("study");
184 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
188 catch (Exception error) {
193 // ==============================================================================================================================
194 // Getters and setters
195 // ==============================================================================================================================
197 public List<Scenario> getScenarii () {
198 // ------------------------------------
201 public long getSelectedScenarioIndex () {
202 // --------------------------------------
205 public String getSelection () {
206 // -----------------------------
209 public String getSharedStep () {
210 // -------------------------------
211 String[] parse = selection.split("\\x2E");
214 public String getTitle () {
215 // -------------------------
219 public void setCancel (final boolean cancel) {
220 // --------------------------------------
221 action = ToDo.cancel;
223 public void setSave (final boolean save) {
224 // ----------------------------------
227 public void setSelectedScenario (final String index) {
228 // ----------------------------------------------
229 scindex = Integer.valueOf(index);
231 public void setSelection (final String step) {
232 // --------------------------------------
235 public void setTitle (final String title) {
236 // -----------------------------------
239 public Boolean sharesStep () {
240 // ----------------------------
241 return (Integer.valueOf(getSharedStep()) > bastep);
244 * Get the projectElementService.
246 * @return the projectElementService
248 public ProjectElementService getProjectElementService() {
249 return _projectElementService;
253 * Set the projectElementService.
255 * @param projectElementService
256 * the projectElementService to set
258 public void setProjectElementService(
259 final ProjectElementService projectElementService) {
260 _projectElementService = projectElementService;
267 public Menu getMenu() {
273 * @param menu the menu to set
275 public void setMenu(final Menu menu) {
280 * Get the scenarioService.
281 * @return the scenarioService
283 public ScenarioService getScenarioService() {
284 return _scenarioService;
288 * Set the scenarioService.
289 * @param scenarioService the scenarioService to set
291 public void setScenarioService(final ScenarioService scenarioService) {
292 _scenarioService = scenarioService;
296 * Get the menuProperty.
297 * @return the menuProperty
299 public String getMenuProperty() {
300 return _menuProperty;
304 * Set the menuProperty.
305 * @param menuProperty the menuProperty to set
307 public void setMenuProperty(final String menuProperty) {
308 this._menuProperty = menuProperty;
312 * Get the _titleProperty.
313 * @return the _titleProperty
315 public String getTitleProperty() {
316 return _titleProperty;
320 * Set the _titleProperty.
321 * @param _titleProperty the titleProperty to set
323 public void setTitleProperty(final String titleProperty) {
324 _titleProperty = titleProperty;
328 * Get the _editDisabledProperty.
329 * @return the _editDisabledProperty
331 public String getEditDisabledProperty() {
332 return _editDisabledProperty;
336 * Set the _editDisabledProperty.
337 * @param _editDisabledProperty the _editDisabledProperty to set
339 public void setEditDisabledProperty(final String _editDisabledProperty) {
340 this._editDisabledProperty = _editDisabledProperty;
344 * Get the toolProperty.
345 * @return the toolProperty
347 public String getToolProperty() {
348 return _toolProperty;
352 * Set the toolProperty.
353 * @param toolProperty the toolProperty to set
355 public void setToolProperty(final String toolProperty) {
356 _toolProperty = toolProperty;
360 * Get the leftMenuProperty.
361 * @return the leftMenuProperty
363 public String getLeftMenuProperty() {
364 return _leftMenuProperty;
368 * Set the leftMenuProperty.
369 * @param leftMenuProperty the leftMenuProperty to set
371 public void setLeftMenuProperty(final String leftMenuProperty) {
372 _leftMenuProperty = leftMenuProperty;