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.StudyService;
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;
30 private StudyService _studyService;
31 private ProjectElementService _projectElementService;
37 private static final long serialVersionUID = -5586724442986956861L;
39 protected enum ToDo { cancel, save };
41 // ==============================================================================================================================
43 // ==============================================================================================================================
45 public String doInitialize () {
46 // -----------------------------
47 mystudy = getOpenStudy();
48 Study study = mystudy.getStudyObject();
49 Scenario[] scene = study.getScenarii();
50 Scenario base = scene[scene.length-1]; // Default base scenario
51 ResourceBundle locale = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
53 myscene = Arrays.asList(scene);
54 scindex = base.getIndex();
55 mytitle = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
56 bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
57 selection = scindex + "." + bastep;
60 //RKV Menu menu = new NewScenarioMenu(study);
61 Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
62 menu.selects(selection);
63 getSession().put("menu.scenario", menu);
67 public String doSelectStep () {
68 // -----------------------------
69 mystudy = getOpenStudy();
70 Study study = mystudy.getStudyObject();
71 Scenario[] scene = study.getScenarii();
73 myscene = Arrays.asList(scene);
74 bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
77 getMenu("scenario").selects(selection);
81 public String doCreate () {
82 // -------------------------
83 if (action == ToDo.cancel) return "cancel";
85 Session session = Database.getSession();
86 Transaction transax = session.beginTransaction();
87 try { mystudy = getOpenStudy();
88 selection = getMenu("scenario").getSelection();
89 Study study = mystudy.getStudyObject();
90 String[] parse = selection.split("\\x2E");
91 int scindex = Integer.valueOf(parse[0]);
92 int number = Integer.valueOf(parse[1]);
94 Scenario[] scene = study.getScenarii();
95 Scenario bascene = scene[0];
96 for (int i=1; i<scene.length; i++) {
98 if (bascene.getIndex() == scindex) break;
101 Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
103 bastep = getProjectElementService().getFirstStep(bascene).getNumber();
104 if (this.sharesStep()) {
105 step = getProjectElementService().getSteps(bascene);
106 sprop.setBaseStep(step[number-bastep]);
108 bascene = getStudyService().addScenario(study, sprop);
111 // Update of the display
112 if (step != null) for (int i=0; i<number-bastep+1; i++) {
113 List<Publication> contents = step[i].getAllDocuments();
114 for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
116 mystudy.setSelection(bascene.getIndex() + "." + number);
117 getSession().remove("menu.scenario");
120 catch (RuntimeException saverror) {
121 logger.error("Reason:", saverror);
122 if (transax != null && transax.isActive()) {
123 // Second try-catch as the rollback could fail as well
126 } catch (HibernateException backerror) {
127 logger.debug("Error rolling back transaction", backerror);
132 catch (Exception error) {
137 // ==============================================================================================================================
138 // Getters and setters
139 // ==============================================================================================================================
141 public List<Scenario> getScenarii () {
142 // ------------------------------------
145 public long getSelectedScenarioIndex () {
146 // --------------------------------------
149 public String getSelection () {
150 // -----------------------------
153 public String getSharedStep () {
154 // -------------------------------
155 String[] parse = selection.split("\\x2E");
158 public String getTitle () {
159 // -------------------------
163 public void setCancel (boolean cancel) {
164 // --------------------------------------
165 action = ToDo.cancel;
167 public void setSave (boolean save) {
168 // ----------------------------------
171 public void setSelectedScenario (String index) {
172 // ----------------------------------------------
173 scindex = Integer.valueOf(index);
175 public void setSelection (String step) {
176 // --------------------------------------
179 public void setTitle (String title) {
180 // -----------------------------------
183 public Boolean sharesStep () {
184 // ----------------------------
185 return (Integer.valueOf(getSharedStep()) > bastep);
188 * Get the studyService.
190 * @return the studyService
192 public StudyService getStudyService() {
193 return _studyService;
197 * Set the studyService.
199 * @param studyService
200 * the studyService to set
202 public void setStudyService(StudyService studyService) {
203 _studyService = studyService;
206 * Get the projectElementService.
208 * @return the projectElementService
210 public ProjectElementService getProjectElementService() {
211 return _projectElementService;
215 * Set the projectElementService.
217 * @param projectElementService
218 * the projectElementService to set
220 public void setProjectElementService(
221 ProjectElementService projectElementService) {
222 _projectElementService = projectElementService;
229 public Menu getMenu() {
235 * @param menu the menu to set
237 public void setMenu(Menu menu) {