Salome HOME
45fdac90827a37b7fdf6bd0d7619244511d975f4
[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          * Value of the menu property. 
42          * It can be: none, create, open, study, knowledge, sysadmin, help.
43          */
44         private String _menuProperty;
45         
46         /**
47          * Value of the title bar property. 
48          * It can be: study, knowledge.
49          */
50         private String _titleProperty;
51         
52         /**
53          * Value of the tool bar property. 
54          * It can be: none, standard, study, back.
55          */
56         private String _toolProperty;
57         
58         /**
59          * Property that indicates whether the current open study is editable or not.
60          * On the screen it looks like pen on the status icon, pop-up menu also can be called.
61          * It is necessary for correct building the title bar.
62          */
63         private String _editDisabledProperty = "false";
64
65         /**
66          * Serial version ID.
67          */
68         private static final long serialVersionUID = -5586724442986956861L;
69
70     protected enum  ToDo { cancel, save };
71
72 //  ==============================================================================================================================
73 //  Action methods
74 //  ==============================================================================================================================
75
76     public String doInitialize () {
77 //  -----------------------------    
78                      mystudy = getOpenStudy();
79       Study          study   = mystudy.getStudyObject();
80       Scenario[]     scene   = study.getScenarii();
81       Scenario       base    = scene[scene.length-1];   // Default base scenario
82       ResourceBundle locale  = ResourceBundle.getBundle("labels", getApplicationSettings().getCurrentLocale());
83
84       myscene   = Arrays.asList(scene);
85       scindex   = base.getIndex();
86       mytitle   = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
87       bastep    = getProjectElementService().getFirstStep(base).getNumber();      // Better use the last current step ?
88       selection = scindex + "." + bastep;
89       action    = null;
90       
91 //RKV      Menu menu = new NewScenarioMenu(study);
92       Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
93       menu.selects(selection);
94       getSession().put("menu.scenario", menu);
95       
96       setMenuProperty("study");
97       setTitleProperty("study");
98       setEditDisabledProperty("true");
99       setToolProperty("none");
100       initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
101                 
102       return SUCCESS;
103     }
104
105     public String doSelectStep () {
106 //  -----------------------------
107                  mystudy = getOpenStudy();
108       Study      study   = mystudy.getStudyObject();
109       Scenario[] scene   = study.getScenarii();
110
111       myscene   = Arrays.asList(scene);
112       bastep    = getProjectElementService().getFirstStep(scene[0]).getNumber();  // All scenarios have the same first step number
113       action    = null;
114         
115       getMenu("scenario").selects(selection);
116       
117       setMenuProperty("study");
118       setTitleProperty("study");
119       setEditDisabledProperty("true");
120       setToolProperty("none");
121       initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
122       
123       return SUCCESS;
124     }
125
126     public String doCreate () {
127 //  -------------------------
128       if (action == ToDo.cancel) return "cancel";
129
130 //      Session      session   = Database.getCurSession();
131 //        Transaction  transax   = session.beginTransaction();  
132           try {        mystudy   = getOpenStudy();
133                    selection = getMenu("scenario").getSelection();
134         Study      study     = mystudy.getStudyObject();
135         String[]   parse     = selection.split("\\x2E");
136         int        scindex   = Integer.valueOf(parse[0]);
137         int        number    = Integer.valueOf(parse[1]);
138
139         Scenario[] scene     = study.getScenarii(); 
140         Scenario   bascene   = scene[0];
141         for (int i=1; i<scene.length; i++) {
142           bascene = scene[i];
143           if (bascene.getIndex() == scindex) break;
144         }
145         Step[]              step  = null;        
146         Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
147
148         bastep = getProjectElementService().getFirstStep(bascene).getNumber();
149         if (this.sharesStep()) {
150           step = getProjectElementService().getSteps(bascene);
151           sprop.setBaseStep(step[number-bastep]);
152         }
153         bascene = getScenarioService().addScenario(study, sprop);
154 //        transax.commit();
155
156 //      Update of the display
157         if (step != null) for (int i=0; i<number-bastep+1; i++) {
158           List<Publication>  contents = step[i].getAllDocuments();
159           for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
160         }
161         mystudy.setSelection(bascene.getIndex() + "." + number);
162         getSession().remove("menu.scenario");
163             return SUCCESS;
164           }
165       catch (RuntimeException saverror) {
166         logger.error("Reason:", saverror);
167         
168         setMenuProperty("study");
169         setTitleProperty("study");
170                 setToolProperty("standard");
171                 initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
172                 
173         return ERROR;
174       }
175           catch (Exception error) {
176         return INPUT;
177       }
178     }
179
180 //  ==============================================================================================================================
181 //  Getters and setters
182 //  ==============================================================================================================================
183
184     public List<Scenario> getScenarii () {
185 //  ------------------------------------
186       return myscene;
187     }
188     public long getSelectedScenarioIndex () {
189 //  --------------------------------------
190       return scindex;
191     }
192     public String getSelection () {
193 //  -----------------------------
194       return selection;
195     }
196     public String getSharedStep () {
197 //  -------------------------------
198       String[] parse = selection.split("\\x2E");
199       return   parse[1];
200     }
201     public String getTitle () {
202 //  -------------------------
203       return mytitle;
204     }
205
206     public void setCancel (boolean cancel) {
207 //  --------------------------------------
208       action = ToDo.cancel;
209     }
210     public void setSave (boolean save) {
211 //  ----------------------------------
212       action = ToDo.save;
213     }
214     public void setSelectedScenario (String index) {
215 //  ----------------------------------------------
216       scindex = Integer.valueOf(index);
217     }
218     public void setSelection (String step) {
219 //  --------------------------------------
220       selection = step;
221     }
222     public void setTitle (String title) {
223 //  -----------------------------------
224       mytitle = title;
225     }
226     public Boolean sharesStep () {
227 //  ----------------------------
228       return (Integer.valueOf(getSharedStep()) > bastep);
229     }
230         /**
231          * Get the projectElementService.
232          * 
233          * @return the projectElementService
234          */
235         public ProjectElementService getProjectElementService() {
236                 return _projectElementService;
237         }
238
239         /**
240          * Set the projectElementService.
241          * 
242          * @param projectElementService
243          *            the projectElementService to set
244          */
245         public void setProjectElementService(
246                         ProjectElementService projectElementService) {
247                 _projectElementService = projectElementService;
248         }
249
250         /**
251          * Get the menu.
252          * @return the menu
253          */
254         public Menu getMenu() {
255                 return _menu;
256         }
257
258         /**
259          * Set the menu.
260          * @param menu the menu to set
261          */
262         public void setMenu(Menu menu) {
263                 _menu = menu;
264         }
265
266         /**
267          * Get the scenarioService.
268          * @return the scenarioService
269          */
270         public ScenarioService getScenarioService() {
271                 return _scenarioService;
272         }
273
274         /**
275          * Set the scenarioService.
276          * @param scenarioService the scenarioService to set
277          */
278         public void setScenarioService(ScenarioService scenarioService) {
279                 _scenarioService = scenarioService;
280         }
281         
282         /**
283          * Get the menuProperty.
284          * @return the menuProperty
285          */
286         public String getMenuProperty() {
287                 return _menuProperty;
288         }
289
290         /**
291          * Set the menuProperty.
292          * @param menuProperty the menuProperty to set
293          */
294         public void setMenuProperty(String menuProperty) {
295                 this._menuProperty = menuProperty;
296         }
297         
298         /**
299          * Get the _titleProperty.
300          * @return the _titleProperty
301          */
302         public String getTitleProperty() {
303                 return _titleProperty;
304         }
305
306         /**
307          * Set the _titleProperty.
308          * @param _titleProperty the titleProperty to set
309          */
310         public void setTitleProperty(String titleProperty) {
311                 _titleProperty = titleProperty;
312         }
313
314         /**
315          * Get the _editDisabledProperty.
316          * @return the _editDisabledProperty
317          */
318         public String getEditDisabledProperty() {
319                 return _editDisabledProperty;
320         }
321
322         /**
323          * Set the _editDisabledProperty.
324          * @param _editDisabledProperty the _editDisabledProperty to set
325          */
326         public void setEditDisabledProperty(String _editDisabledProperty) {
327                 this._editDisabledProperty = _editDisabledProperty;
328         }
329         
330         /**
331          * Get the toolProperty.
332          * @return the toolProperty
333          */
334         public String getToolProperty() {
335                 return _toolProperty;
336         }
337
338         /**
339          * Set the toolProperty.
340          * @param toolProperty the toolProperty to set
341          */
342         public void setToolProperty(final String toolProperty) {
343                 _toolProperty = toolProperty;
344         }
345 }