]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/NewScenarioAction.java
Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewScenarioAction.java
1 package org.splat.simer;
2
3 import java.util.Arrays;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.ResourceBundle;
7
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;
19
20
21 public class NewScenarioAction extends Action {
22
23         private  OpenStudy        mystudy;
24         private  List<Scenario>   myscene;
25         private  String           mytitle;
26     private  String           selection;
27     private  long              scindex;
28     private  int              bastep;
29     private  ToDo             action;
30         /**
31          * Injected scenario service.
32          */
33         private ScenarioService _scenarioService;
34         /**
35          * Injected project element service.
36          */
37         private ProjectElementService _projectElementService;
38         private Menu _menu;
39
40         /**
41          * Serial version ID.
42          */
43         private static final long serialVersionUID = -5586724442986956861L;
44
45     protected enum  ToDo { cancel, save };
46
47 //  ==============================================================================================================================
48 //  Action methods
49 //  ==============================================================================================================================
50
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());
58
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;
64       action    = null;
65       
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);
70       return SUCCESS;
71     }
72
73     public String doSelectStep () {
74 //  -----------------------------
75                  mystudy = getOpenStudy();
76       Study      study   = mystudy.getStudyObject();
77       Scenario[] scene   = study.getScenarii();
78
79       myscene   = Arrays.asList(scene);
80       bastep    = getProjectElementService().getFirstStep(scene[0]).getNumber();  // All scenarios have the same first step number
81       action    = null;
82         
83       getMenu("scenario").selects(selection);
84       return SUCCESS;
85     }
86
87     public String doCreate () {
88 //  -------------------------
89       if (action == ToDo.cancel) return "cancel";
90
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]);
99
100         Scenario[] scene     = study.getScenarii(); 
101         Scenario   bascene   = scene[0];
102         for (int i=1; i<scene.length; i++) {
103           bascene = scene[i];
104           if (bascene.getIndex() == scindex) break;
105         }
106         Step[]              step  = null;        
107         Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
108
109         bastep = getProjectElementService().getFirstStep(bascene).getNumber();
110         if (this.sharesStep()) {
111           step = getProjectElementService().getSteps(bascene);
112           sprop.setBaseStep(step[number-bastep]);
113         }
114         bascene = getScenarioService().addScenario(study, sprop);
115 //        transax.commit();
116
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());
121         }
122         mystudy.setSelection(bascene.getIndex() + "." + number);
123         getSession().remove("menu.scenario");
124             return SUCCESS;
125           }
126       catch (RuntimeException saverror) {
127         logger.error("Reason:", saverror);
128         return ERROR;
129       }
130           catch (Exception error) {
131         return INPUT;
132       }
133     }
134
135 //  ==============================================================================================================================
136 //  Getters and setters
137 //  ==============================================================================================================================
138
139     public List<Scenario> getScenarii () {
140 //  ------------------------------------
141       return myscene;
142     }
143     public long getSelectedScenarioIndex () {
144 //  --------------------------------------
145       return scindex;
146     }
147     public String getSelection () {
148 //  -----------------------------
149       return selection;
150     }
151     public String getSharedStep () {
152 //  -------------------------------
153       String[] parse = selection.split("\\x2E");
154       return   parse[1];
155     }
156     public String getTitle () {
157 //  -------------------------
158       return mytitle;
159     }
160
161     public void setCancel (boolean cancel) {
162 //  --------------------------------------
163       action = ToDo.cancel;
164     }
165     public void setSave (boolean save) {
166 //  ----------------------------------
167       action = ToDo.save;
168     }
169     public void setSelectedScenario (String index) {
170 //  ----------------------------------------------
171       scindex = Integer.valueOf(index);
172     }
173     public void setSelection (String step) {
174 //  --------------------------------------
175       selection = step;
176     }
177     public void setTitle (String title) {
178 //  -----------------------------------
179       mytitle = title;
180     }
181     public Boolean sharesStep () {
182 //  ----------------------------
183       return (Integer.valueOf(getSharedStep()) > bastep);
184     }
185         /**
186          * Get the projectElementService.
187          * 
188          * @return the projectElementService
189          */
190         public ProjectElementService getProjectElementService() {
191                 return _projectElementService;
192         }
193
194         /**
195          * Set the projectElementService.
196          * 
197          * @param projectElementService
198          *            the projectElementService to set
199          */
200         public void setProjectElementService(
201                         ProjectElementService projectElementService) {
202                 _projectElementService = projectElementService;
203         }
204
205         /**
206          * Get the menu.
207          * @return the menu
208          */
209         public Menu getMenu() {
210                 return _menu;
211         }
212
213         /**
214          * Set the menu.
215          * @param menu the menu to set
216          */
217         public void setMenu(Menu menu) {
218                 _menu = menu;
219         }
220
221         /**
222          * Get the scenarioService.
223          * @return the scenarioService
224          */
225         public ScenarioService getScenarioService() {
226                 return _scenarioService;
227         }
228
229         /**
230          * Set the scenarioService.
231          * @param scenarioService the scenarioService to set
232          */
233         public void setScenarioService(ScenarioService scenarioService) {
234                 _scenarioService = scenarioService;
235         }
236 }