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;
49 private static final long serialVersionUID = -5586724442986956861L;
51 protected enum ToDo { cancel, save };
53 // ==============================================================================================================================
55 // ==============================================================================================================================
57 public String doInitialize () {
58 // -----------------------------
59 mystudy = getOpenStudy();
60 Study study = mystudy.getStudyObject();
61 Scenario[] scene = study.getScenarii();
62 Scenario base = scene[scene.length-1]; // Default base scenario
63 ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
65 myscene = Arrays.asList(scene);
66 scindex = base.getIndex();
67 mytitle = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
68 bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
69 selection = scindex + "." + bastep;
72 //RKV Menu menu = new NewScenarioMenu(study);
73 Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
74 menu.selects(selection);
75 getSession().put("menu.scenario", menu);
77 setMenuProperty("study");
78 initializationScreenContext(_menuProperty);
83 public String doSelectStep () {
84 // -----------------------------
85 mystudy = getOpenStudy();
86 Study study = mystudy.getStudyObject();
87 Scenario[] scene = study.getScenarii();
89 myscene = Arrays.asList(scene);
90 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
93 getMenu("scenario").selects(selection);
95 setMenuProperty("study");
96 initializationScreenContext(_menuProperty);
101 public String doCreate () {
102 // -------------------------
103 if (action == ToDo.cancel) return "cancel";
105 // Session session = Database.getCurSession();
106 // Transaction transax = session.beginTransaction();
107 try { mystudy = getOpenStudy();
108 selection = getMenu("scenario").getSelection();
109 Study study = mystudy.getStudyObject();
110 String[] parse = selection.split("\\x2E");
111 int scindex = Integer.valueOf(parse[0]);
112 int number = Integer.valueOf(parse[1]);
114 Scenario[] scene = study.getScenarii();
115 Scenario bascene = scene[0];
116 for (int i=1; i<scene.length; i++) {
118 if (bascene.getIndex() == scindex) break;
121 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
123 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
124 if (this.sharesStep()) {
125 step = getProjectElementService().getSteps(bascene);
126 sprop.setBaseStep(step[number-bastep]);
128 bascene = getScenarioService().addScenario(study, sprop);
131 // Update of the display
132 if (step != null) for (int i=0; i<number-bastep+1; i++) {
133 List<Publication> contents = step[i].getAllDocuments();
134 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
136 mystudy.setSelection(bascene.getIndex() + "." + number);
137 getSession().remove("menu.scenario");
140 catch (RuntimeException saverror) {
141 logger.error("Reason:", saverror);
143 setMenuProperty("study");
144 initializationScreenContext(_menuProperty);
148 catch (Exception error) {
153 // ==============================================================================================================================
154 // Getters and setters
155 // ==============================================================================================================================
157 public List<Scenario> getScenarii () {
158 // ------------------------------------
161 public long getSelectedScenarioIndex () {
162 // --------------------------------------
165 public String getSelection () {
166 // -----------------------------
169 public String getSharedStep () {
170 // -------------------------------
171 String[] parse = selection.split("\\x2E");
174 public String getTitle () {
175 // -------------------------
179 public void setCancel (boolean cancel) {
180 // --------------------------------------
181 action = ToDo.cancel;
183 public void setSave (boolean save) {
184 // ----------------------------------
187 public void setSelectedScenario (String index) {
188 // ----------------------------------------------
189 scindex = Integer.valueOf(index);
191 public void setSelection (String step) {
192 // --------------------------------------
195 public void setTitle (String title) {
196 // -----------------------------------
199 public Boolean sharesStep () {
200 // ----------------------------
201 return (Integer.valueOf(getSharedStep()) > bastep);
204 * Get the projectElementService.
206 * @return the projectElementService
208 public ProjectElementService getProjectElementService() {
209 return _projectElementService;
213 * Set the projectElementService.
215 * @param projectElementService
216 * the projectElementService to set
218 public void setProjectElementService(
219 ProjectElementService projectElementService) {
220 _projectElementService = projectElementService;
227 public Menu getMenu() {
233 * @param menu the menu to set
235 public void setMenu(Menu menu) {
240 * Get the scenarioService.
241 * @return the scenarioService
243 public ScenarioService getScenarioService() {
244 return _scenarioService;
248 * Set the scenarioService.
249 * @param scenarioService the scenarioService to set
251 public void setScenarioService(ScenarioService scenarioService) {
252 _scenarioService = scenarioService;
256 * Get the menuProperty.
257 * @return the menuProperty
259 public String getMenuProperty() {
260 return _menuProperty;
264 * Set the menuProperty.
265 * @param menuProperty the menuProperty to set
267 public void setMenuProperty(String menuProperty) {
268 this._menuProperty = menuProperty;