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;
43 private static final long serialVersionUID = -5586724442986956861L;
45 protected enum ToDo { cancel, save };
47 // ==============================================================================================================================
49 // ==============================================================================================================================
51 public String doInitialize () {
52 // -----------------------------
53 mystudy = getOpenStudy();
54 Study study = mystudy.getStudyObject();
55 Scenario[] scene = study.getScenarii();
56 Scenario base = scene[scene.length-1]; // Default base scenario
57 ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
59 myscene = Arrays.asList(scene);
60 scindex = base.getIndex();
61 mytitle = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
62 bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
63 selection = scindex + "." + bastep;
66 //RKV Menu menu = new NewScenarioMenu(study);
67 Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
68 menu.selects(selection);
69 getSession().put("menu.scenario", menu);
73 public String doSelectStep () {
74 // -----------------------------
75 mystudy = getOpenStudy();
76 Study study = mystudy.getStudyObject();
77 Scenario[] scene = study.getScenarii();
79 myscene = Arrays.asList(scene);
80 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
83 getMenu("scenario").selects(selection);
87 public String doCreate () {
88 // -------------------------
89 if (action == ToDo.cancel) return "cancel";
91 // Session session = Database.getCurSession();
92 // Transaction transax = session.beginTransaction();
93 try { mystudy = getOpenStudy();
94 selection = getMenu("scenario").getSelection();
95 Study study = mystudy.getStudyObject();
96 String[] parse = selection.split("\\x2E");
97 int scindex = Integer.valueOf(parse[0]);
98 int number = Integer.valueOf(parse[1]);
100 Scenario[] scene = study.getScenarii();
101 Scenario bascene = scene[0];
102 for (int i=1; i<scene.length; i++) {
104 if (bascene.getIndex() == scindex) break;
107 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
109 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
110 if (this.sharesStep()) {
111 step = getProjectElementService().getSteps(bascene);
112 sprop.setBaseStep(step[number-bastep]);
114 bascene = getScenarioService().addScenario(study, sprop);
117 // Update of the display
118 if (step != null) for (int i=0; i<number-bastep+1; i++) {
119 List<Publication> contents = step[i].getAllDocuments();
120 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
122 mystudy.setSelection(bascene.getIndex() + "." + number);
123 getSession().remove("menu.scenario");
126 catch (RuntimeException saverror) {
127 logger.error("Reason:", saverror);
130 catch (Exception error) {
135 // ==============================================================================================================================
136 // Getters and setters
137 // ==============================================================================================================================
139 public List<Scenario> getScenarii () {
140 // ------------------------------------
143 public long getSelectedScenarioIndex () {
144 // --------------------------------------
147 public String getSelection () {
148 // -----------------------------
151 public String getSharedStep () {
152 // -------------------------------
153 String[] parse = selection.split("\\x2E");
156 public String getTitle () {
157 // -------------------------
161 public void setCancel (boolean cancel) {
162 // --------------------------------------
163 action = ToDo.cancel;
165 public void setSave (boolean save) {
166 // ----------------------------------
169 public void setSelectedScenario (String index) {
170 // ----------------------------------------------
171 scindex = Integer.valueOf(index);
173 public void setSelection (String step) {
174 // --------------------------------------
177 public void setTitle (String title) {
178 // -----------------------------------
181 public Boolean sharesStep () {
182 // ----------------------------
183 return (Integer.valueOf(getSharedStep()) > bastep);
186 * Get the projectElementService.
188 * @return the projectElementService
190 public ProjectElementService getProjectElementService() {
191 return _projectElementService;
195 * Set the projectElementService.
197 * @param projectElementService
198 * the projectElementService to set
200 public void setProjectElementService(
201 ProjectElementService projectElementService) {
202 _projectElementService = projectElementService;
209 public Menu getMenu() {
215 * @param menu the menu to set
217 public void setMenu(Menu menu) {
222 * Get the scenarioService.
223 * @return the scenarioService
225 public ScenarioService getScenarioService() {
226 return _scenarioService;
230 * Set the scenarioService.
231 * @param scenarioService the scenarioService to set
233 public void setScenarioService(ScenarioService scenarioService) {
234 _scenarioService = scenarioService;