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