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 * Property that indicates whether the current open study is editable or not.
54 * On the screen it looks like pen on the status icon, pop-up menu also can be called.
55 * It is necessary for correct building the title bar.
57 private String _editDisabledProperty = "false";
62 private static final long serialVersionUID = -5586724442986956861L;
64 protected enum ToDo { cancel, save };
66 // ==============================================================================================================================
68 // ==============================================================================================================================
70 public String doInitialize () {
71 // -----------------------------
72 mystudy = getOpenStudy();
73 Study study = mystudy.getStudyObject();
74 Scenario[] scene = study.getScenarii();
75 Scenario base = scene[scene.length-1]; // Default base scenario
76 ResourceBundle locale = ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale());
78 myscene = Arrays.asList(scene);
79 scindex = base.getIndex();
80 mytitle = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
81 bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
82 selection = scindex + "." + bastep;
85 //RKV Menu menu = new NewScenarioMenu(study);
86 Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
87 menu.selects(selection);
88 getSession().put("menu.scenario", menu);
90 setMenuProperty("study");
91 setTitleProperty("study");
92 setEditDisabledProperty("true");
93 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
98 public String doSelectStep () {
99 // -----------------------------
100 mystudy = getOpenStudy();
101 Study study = mystudy.getStudyObject();
102 Scenario[] scene = study.getScenarii();
104 myscene = Arrays.asList(scene);
105 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
108 getMenu("scenario").selects(selection);
110 setMenuProperty("study");
111 setTitleProperty("study");
112 setEditDisabledProperty("true");
113 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
118 public String doCreate () {
119 // -------------------------
120 if (action == ToDo.cancel) return "cancel";
122 // Session session = Database.getCurSession();
123 // Transaction transax = session.beginTransaction();
124 try { mystudy = getOpenStudy();
125 selection = getMenu("scenario").getSelection();
126 Study study = mystudy.getStudyObject();
127 String[] parse = selection.split("\\x2E");
128 int scindex = Integer.valueOf(parse[0]);
129 int number = Integer.valueOf(parse[1]);
131 Scenario[] scene = study.getScenarii();
132 Scenario bascene = scene[0];
133 for (int i=1; i<scene.length; i++) {
135 if (bascene.getIndex() == scindex) break;
138 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
140 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
141 if (this.sharesStep()) {
142 step = getProjectElementService().getSteps(bascene);
143 sprop.setBaseStep(step[number-bastep]);
145 bascene = getScenarioService().addScenario(study, sprop);
148 // Update of the display
149 if (step != null) for (int i=0; i<number-bastep+1; i++) {
150 List<Publication> contents = step[i].getAllDocuments();
151 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
153 mystudy.setSelection(bascene.getIndex() + "." + number);
154 getSession().remove("menu.scenario");
157 catch (RuntimeException saverror) {
158 logger.error("Reason:", saverror);
160 setMenuProperty("study");
161 setTitleProperty("study");
162 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty);
166 catch (Exception error) {
171 // ==============================================================================================================================
172 // Getters and setters
173 // ==============================================================================================================================
175 public List<Scenario> getScenarii () {
176 // ------------------------------------
179 public long getSelectedScenarioIndex () {
180 // --------------------------------------
183 public String getSelection () {
184 // -----------------------------
187 public String getSharedStep () {
188 // -------------------------------
189 String[] parse = selection.split("\\x2E");
192 public String getTitle () {
193 // -------------------------
197 public void setCancel (boolean cancel) {
198 // --------------------------------------
199 action = ToDo.cancel;
201 public void setSave (boolean save) {
202 // ----------------------------------
205 public void setSelectedScenario (String index) {
206 // ----------------------------------------------
207 scindex = Integer.valueOf(index);
209 public void setSelection (String step) {
210 // --------------------------------------
213 public void setTitle (String title) {
214 // -----------------------------------
217 public Boolean sharesStep () {
218 // ----------------------------
219 return (Integer.valueOf(getSharedStep()) > bastep);
222 * Get the projectElementService.
224 * @return the projectElementService
226 public ProjectElementService getProjectElementService() {
227 return _projectElementService;
231 * Set the projectElementService.
233 * @param projectElementService
234 * the projectElementService to set
236 public void setProjectElementService(
237 ProjectElementService projectElementService) {
238 _projectElementService = projectElementService;
245 public Menu getMenu() {
251 * @param menu the menu to set
253 public void setMenu(Menu menu) {
258 * Get the scenarioService.
259 * @return the scenarioService
261 public ScenarioService getScenarioService() {
262 return _scenarioService;
266 * Set the scenarioService.
267 * @param scenarioService the scenarioService to set
269 public void setScenarioService(ScenarioService scenarioService) {
270 _scenarioService = scenarioService;
274 * Get the menuProperty.
275 * @return the menuProperty
277 public String getMenuProperty() {
278 return _menuProperty;
282 * Set the menuProperty.
283 * @param menuProperty the menuProperty to set
285 public void setMenuProperty(String menuProperty) {
286 this._menuProperty = menuProperty;
290 * Get the _titleProperty.
291 * @return the _titleProperty
293 public String getTitleProperty() {
294 return _titleProperty;
298 * Set the _titleProperty.
299 * @param _titleProperty the titleProperty to set
301 public void setTitleProperty(String titleProperty) {
302 _titleProperty = titleProperty;
306 * Get the _editDisabledProperty.
307 * @return the _editDisabledProperty
309 public String getEditDisabledProperty() {
310 return _editDisabledProperty;
314 * Set the _editDisabledProperty.
315 * @param _editDisabledProperty the _editDisabledProperty to set
317 public void setEditDisabledProperty(String _editDisabledProperty) {
318 this._editDisabledProperty = _editDisabledProperty;