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 * Property that indicates whether the current open study is editable or not.
60 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
61 * It is necessary for correct building the title bar.
63 private String _editDisabledProperty = "false";
68 private static final long serialVersionUID = -5586724442986956861L;
70 protected enum ToDo { cancel, save };
72 // ==============================================================================================================================
74 // ==============================================================================================================================
76 public String doInitialize () {
77 // -----------------------------
78 mystudy = getOpenStudy();
79 Study study = mystudy.getStudyObject();
80 Scenario[] scene = study.getScenarii();
81 Scenario base = scene[scene.length-1]; // Default base scenario
82 ResourceBundle locale = ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale());
84 myscene = Arrays.asList(scene);
85 scindex = base.getIndex();
86 mytitle = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
87 bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
88 selection = scindex + "." + bastep;
91 //RKV Menu menu = new NewScenarioMenu(study);
92 Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
93 menu.selects(selection);
94 getSession().put("menu.scenario", menu);
96 setMenuProperty("study");
97 setTitleProperty("study");
98 setEditDisabledProperty("true");
99 setToolProperty("none");
100 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
105 public String doSelectStep () {
106 // -----------------------------
107 mystudy = getOpenStudy();
108 Study study = mystudy.getStudyObject();
109 Scenario[] scene = study.getScenarii();
111 myscene = Arrays.asList(scene);
112 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
115 getMenu("scenario").selects(selection);
117 setMenuProperty("study");
118 setTitleProperty("study");
119 setEditDisabledProperty("true");
120 setToolProperty("none");
121 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
126 public String doCreate () {
127 // -------------------------
128 if (action == ToDo.cancel) return "cancel";
130 // Session session = Database.getCurSession();
131 // Transaction transax = session.beginTransaction();
132 try { mystudy = getOpenStudy();
133 selection = getMenu("scenario").getSelection();
134 Study study = mystudy.getStudyObject();
135 String[] parse = selection.split("\\x2E");
136 int scindex = Integer.valueOf(parse[0]);
137 int number = Integer.valueOf(parse[1]);
139 Scenario[] scene = study.getScenarii();
140 Scenario bascene = scene[0];
141 for (int i=1; i<scene.length; i++) {
143 if (bascene.getIndex() == scindex) break;
146 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
148 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
149 if (this.sharesStep()) {
150 step = getProjectElementService().getSteps(bascene);
151 sprop.setBaseStep(step[number-bastep]);
153 bascene = getScenarioService().addScenario(study, sprop);
156 // Update of the display
157 if (step != null) for (int i=0; i<number-bastep+1; i++) {
158 List<Publication> contents = step[i].getAllDocuments();
159 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
161 mystudy.setSelection(bascene.getIndex() + "." + number);
162 getSession().remove("menu.scenario");
165 catch (RuntimeException saverror) {
166 logger.error("Reason:", saverror);
168 setMenuProperty("study");
169 setTitleProperty("study");
170 setToolProperty("standard");
171 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
175 catch (Exception error) {
180 // ==============================================================================================================================
181 // Getters and setters
182 // ==============================================================================================================================
184 public List<Scenario> getScenarii () {
185 // ------------------------------------
188 public long getSelectedScenarioIndex () {
189 // --------------------------------------
192 public String getSelection () {
193 // -----------------------------
196 public String getSharedStep () {
197 // -------------------------------
198 String[] parse = selection.split("\\x2E");
201 public String getTitle () {
202 // -------------------------
206 public void setCancel (boolean cancel) {
207 // --------------------------------------
208 action = ToDo.cancel;
210 public void setSave (boolean save) {
211 // ----------------------------------
214 public void setSelectedScenario (String index) {
215 // ----------------------------------------------
216 scindex = Integer.valueOf(index);
218 public void setSelection (String step) {
219 // --------------------------------------
222 public void setTitle (String title) {
223 // -----------------------------------
226 public Boolean sharesStep () {
227 // ----------------------------
228 return (Integer.valueOf(getSharedStep()) > bastep);
231 * Get the projectElementService.
233 * @return the projectElementService
235 public ProjectElementService getProjectElementService() {
236 return _projectElementService;
240 * Set the projectElementService.
242 * @param projectElementService
243 * the projectElementService to set
245 public void setProjectElementService(
246 ProjectElementService projectElementService) {
247 _projectElementService = projectElementService;
254 public Menu getMenu() {
260 * @param menu the menu to set
262 public void setMenu(Menu menu) {
267 * Get the scenarioService.
268 * @return the scenarioService
270 public ScenarioService getScenarioService() {
271 return _scenarioService;
275 * Set the scenarioService.
276 * @param scenarioService the scenarioService to set
278 public void setScenarioService(ScenarioService scenarioService) {
279 _scenarioService = scenarioService;
283 * Get the menuProperty.
284 * @return the menuProperty
286 public String getMenuProperty() {
287 return _menuProperty;
291 * Set the menuProperty.
292 * @param menuProperty the menuProperty to set
294 public void setMenuProperty(String menuProperty) {
295 this._menuProperty = menuProperty;
299 * Get the _titleProperty.
300 * @return the _titleProperty
302 public String getTitleProperty() {
303 return _titleProperty;
307 * Set the _titleProperty.
308 * @param _titleProperty the titleProperty to set
310 public void setTitleProperty(String titleProperty) {
311 _titleProperty = titleProperty;
315 * Get the _editDisabledProperty.
316 * @return the _editDisabledProperty
318 public String getEditDisabledProperty() {
319 return _editDisabledProperty;
323 * Set the _editDisabledProperty.
324 * @param _editDisabledProperty the _editDisabledProperty to set
326 public void setEditDisabledProperty(String _editDisabledProperty) {
327 this._editDisabledProperty = _editDisabledProperty;
331 * Get the toolProperty.
332 * @return the toolProperty
334 public String getToolProperty() {
335 return _toolProperty;
339 * Set the toolProperty.
340 * @param toolProperty the toolProperty to set
342 public void setToolProperty(final String toolProperty) {
343 _toolProperty = toolProperty;