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