Salome HOME
Modifications to respect PMD rules.
[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       return SUCCESS;
110     }
111
112     public String doSelectStep () {
113 //  -----------------------------
114                  mystudy = getOpenStudy();
115       Study      study   = mystudy.getStudyObject();
116       Scenario[] scene   = study.getScenarii();
117
118       myscene   = Arrays.asList(scene);
119       bastep    = getProjectElementService().getFirstStep(scene[0]).getNumber();  // All scenarios have the same first step number
120       action    = null;
121         
122       getMenu("scenario").selects(selection);
123       
124       setMenuProperty("study");
125       setTitleProperty("study");
126       setEditDisabledProperty("true");
127       setToolProperty("none");
128       setLeftMenuProperty("scenario");
129       initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
130       
131       return SUCCESS;
132     }
133
134     public String doCreate () {
135 //  -------------------------
136       if (action == ToDo.cancel) return "cancel";
137
138 //      Session      session   = Database.getCurSession();
139 //        Transaction  transax   = session.beginTransaction();  
140           try {        mystudy   = getOpenStudy();
141                    selection = getMenu("scenario").getSelection();
142         Study      study     = mystudy.getStudyObject();
143         String[]   parse     = selection.split("\\x2E");
144         int        scindex   = Integer.valueOf(parse[0]);
145         int        number    = Integer.valueOf(parse[1]);
146
147         Scenario[] scene     = study.getScenarii(); 
148         Scenario   bascene   = scene[0];
149         for (int i=1; i<scene.length; i++) {
150           bascene = scene[i];
151           if (bascene.getIndex() == scindex) break;
152         }
153         Step[]              step  = null;        
154         Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
155
156         bastep = getProjectElementService().getFirstStep(bascene).getNumber();
157         if (this.sharesStep()) {
158           step = getProjectElementService().getSteps(bascene);
159           sprop.setBaseStep(step[number-bastep]);
160         }
161         bascene = getScenarioService().addScenario(study, sprop);
162 //        transax.commit();
163
164 //      Update of the display
165         if (step != null) for (int i=0; i<number-bastep+1; i++) {
166           List<Publication>  contents = step[i].getAllDocuments();
167           for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
168         }
169         mystudy.setSelection(bascene.getIndex() + "." + number);
170         getSession().remove("menu.scenario");
171             return SUCCESS;
172           }
173       catch (RuntimeException saverror) {
174         LOG.error("Reason:", saverror);
175         
176         setMenuProperty("study");
177         setTitleProperty("study");
178                 setToolProperty("standard");
179                 setLeftMenuProperty("study");
180                 initializationFullScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty, _leftMenuProperty);
181                 
182         return ERROR;
183       }
184           catch (Exception error) {
185         return INPUT;
186       }
187     }
188
189 //  ==============================================================================================================================
190 //  Getters and setters
191 //  ==============================================================================================================================
192
193     public List<Scenario> getScenarii () {
194 //  ------------------------------------
195       return myscene;
196     }
197     public long getSelectedScenarioIndex () {
198 //  --------------------------------------
199       return scindex;
200     }
201     public String getSelection () {
202 //  -----------------------------
203       return selection;
204     }
205     public String getSharedStep () {
206 //  -------------------------------
207       String[] parse = selection.split("\\x2E");
208       return   parse[1];
209     }
210     public String getTitle () {
211 //  -------------------------
212       return mytitle;
213     }
214
215     public void setCancel (boolean cancel) {
216 //  --------------------------------------
217       action = ToDo.cancel;
218     }
219     public void setSave (boolean save) {
220 //  ----------------------------------
221       action = ToDo.save;
222     }
223     public void setSelectedScenario (String index) {
224 //  ----------------------------------------------
225       scindex = Integer.valueOf(index);
226     }
227     public void setSelection (String step) {
228 //  --------------------------------------
229       selection = step;
230     }
231     public void setTitle (String title) {
232 //  -----------------------------------
233       mytitle = title;
234     }
235     public Boolean sharesStep () {
236 //  ----------------------------
237       return (Integer.valueOf(getSharedStep()) > bastep);
238     }
239         /**
240          * Get the projectElementService.
241          * 
242          * @return the projectElementService
243          */
244         public ProjectElementService getProjectElementService() {
245                 return _projectElementService;
246         }
247
248         /**
249          * Set the projectElementService.
250          * 
251          * @param projectElementService
252          *            the projectElementService to set
253          */
254         public void setProjectElementService(
255                         ProjectElementService projectElementService) {
256                 _projectElementService = projectElementService;
257         }
258
259         /**
260          * Get the menu.
261          * @return the menu
262          */
263         public Menu getMenu() {
264                 return _menu;
265         }
266
267         /**
268          * Set the menu.
269          * @param menu the menu to set
270          */
271         public void setMenu(Menu menu) {
272                 _menu = menu;
273         }
274
275         /**
276          * Get the scenarioService.
277          * @return the scenarioService
278          */
279         public ScenarioService getScenarioService() {
280                 return _scenarioService;
281         }
282
283         /**
284          * Set the scenarioService.
285          * @param scenarioService the scenarioService to set
286          */
287         public void setScenarioService(ScenarioService scenarioService) {
288                 _scenarioService = scenarioService;
289         }
290         
291         /**
292          * Get the menuProperty.
293          * @return the menuProperty
294          */
295         public String getMenuProperty() {
296                 return _menuProperty;
297         }
298
299         /**
300          * Set the menuProperty.
301          * @param menuProperty the menuProperty to set
302          */
303         public void setMenuProperty(String menuProperty) {
304                 this._menuProperty = menuProperty;
305         }
306         
307         /**
308          * Get the _titleProperty.
309          * @return the _titleProperty
310          */
311         public String getTitleProperty() {
312                 return _titleProperty;
313         }
314
315         /**
316          * Set the _titleProperty.
317          * @param _titleProperty the titleProperty to set
318          */
319         public void setTitleProperty(String titleProperty) {
320                 _titleProperty = titleProperty;
321         }
322
323         /**
324          * Get the _editDisabledProperty.
325          * @return the _editDisabledProperty
326          */
327         public String getEditDisabledProperty() {
328                 return _editDisabledProperty;
329         }
330
331         /**
332          * Set the _editDisabledProperty.
333          * @param _editDisabledProperty the _editDisabledProperty to set
334          */
335         public void setEditDisabledProperty(String _editDisabledProperty) {
336                 this._editDisabledProperty = _editDisabledProperty;
337         }
338         
339         /**
340          * Get the toolProperty.
341          * @return the toolProperty
342          */
343         public String getToolProperty() {
344                 return _toolProperty;
345         }
346
347         /**
348          * Set the toolProperty.
349          * @param toolProperty the toolProperty to set
350          */
351         public void setToolProperty(final String toolProperty) {
352                 _toolProperty = toolProperty;
353         }
354         
355         /**
356          * Get the leftMenuProperty.
357          * @return the leftMenuProperty
358          */
359         public String getLeftMenuProperty() {
360                 return _leftMenuProperty;
361         }
362
363         /**
364          * Set the leftMenuProperty.
365          * @param leftMenuProperty the leftMenuProperty to set
366          */
367         public void setLeftMenuProperty(final String leftMenuProperty) {
368                 _leftMenuProperty = leftMenuProperty;
369         }
370 }