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.getSession();
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);
128 if (transax != null && transax.isActive()) {
129 // Second try-catch as the rollback could fail as well
132 } catch (HibernateException backerror) {
133 logger.debug("Error rolling back transaction", backerror);
138 catch (Exception error) {
143 // ==============================================================================================================================
144 // Getters and setters
145 // ==============================================================================================================================
147 public List<Scenario> getScenarii () {
148 // ------------------------------------
151 public long getSelectedScenarioIndex () {
152 // --------------------------------------
155 public String getSelection () {
156 // -----------------------------
159 public String getSharedStep () {
160 // -------------------------------
161 String[] parse = selection.split("\\x2E");
164 public String getTitle () {
165 // -------------------------
169 public void setCancel (boolean cancel) {
170 // --------------------------------------
171 action = ToDo.cancel;
173 public void setSave (boolean save) {
174 // ----------------------------------
177 public void setSelectedScenario (String index) {
178 // ----------------------------------------------
179 scindex = Integer.valueOf(index);
181 public void setSelection (String step) {
182 // --------------------------------------
185 public void setTitle (String title) {
186 // -----------------------------------
189 public Boolean sharesStep () {
190 // ----------------------------
191 return (Integer.valueOf(getSharedStep()) > bastep);
194 * Get the projectElementService.
196 * @return the projectElementService
198 public ProjectElementService getProjectElementService() {
199 return _projectElementService;
203 * Set the projectElementService.
205 * @param projectElementService
206 * the projectElementService to set
208 public void setProjectElementService(
209 ProjectElementService projectElementService) {
210 _projectElementService = projectElementService;
217 public Menu getMenu() {
223 * @param menu the menu to set
225 public void setMenu(Menu menu) {
230 * Get the scenarioService.
231 * @return the scenarioService
233 public ScenarioService getScenarioService() {
234 return _scenarioService;
238 * Set the scenarioService.
239 * @param scenarioService the scenarioService to set
241 public void setScenarioService(ScenarioService scenarioService) {
242 _scenarioService = scenarioService;