]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/NewScenarioAction.java
Salome HOME
Menus are improved
[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          * Serial version ID.
48          */
49         private static final long serialVersionUID = -5586724442986956861L;
50
51     protected enum  ToDo { cancel, save };
52
53 //  ==============================================================================================================================
54 //  Action methods
55 //  ==============================================================================================================================
56
57     public String doInitialize () {
58 //  -----------------------------    
59                      mystudy = getOpenStudy();
60       Study          study   = mystudy.getStudyObject();
61       Scenario[]     scene   = study.getScenarii();
62       Scenario       base    = scene[scene.length-1];   // Default base scenario
63       ResourceBundle locale  = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
64
65       myscene   = Arrays.asList(scene);
66       scindex   = base.getIndex();
67       mytitle   = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
68       bastep    = getProjectElementService().getFirstStep(base).getNumber();      // Better use the last current step ?
69       selection = scindex + "." + bastep;
70       action    = null;
71       
72 //RKV      Menu menu = new NewScenarioMenu(study);
73       Menu menu = ((NewScenarioMenu)getMenu()).init(study); //RKV
74       menu.selects(selection);
75       getSession().put("menu.scenario", menu);
76       
77       setMenuProperty("study");
78           initializationScreenContext(_menuProperty);
79                 
80       return SUCCESS;
81     }
82
83     public String doSelectStep () {
84 //  -----------------------------
85                  mystudy = getOpenStudy();
86       Study      study   = mystudy.getStudyObject();
87       Scenario[] scene   = study.getScenarii();
88
89       myscene   = Arrays.asList(scene);
90       bastep    = getProjectElementService().getFirstStep(scene[0]).getNumber();  // All scenarios have the same first step number
91       action    = null;
92         
93       getMenu("scenario").selects(selection);
94       
95       setMenuProperty("study");
96       initializationScreenContext(_menuProperty);
97       
98       return SUCCESS;
99     }
100
101     public String doCreate () {
102 //  -------------------------
103       if (action == ToDo.cancel) return "cancel";
104
105 //      Session      session   = Database.getCurSession();
106 //        Transaction  transax   = session.beginTransaction();  
107           try {        mystudy   = getOpenStudy();
108                    selection = getMenu("scenario").getSelection();
109         Study      study     = mystudy.getStudyObject();
110         String[]   parse     = selection.split("\\x2E");
111         int        scindex   = Integer.valueOf(parse[0]);
112         int        number    = Integer.valueOf(parse[1]);
113
114         Scenario[] scene     = study.getScenarii(); 
115         Scenario   bascene   = scene[0];
116         for (int i=1; i<scene.length; i++) {
117           bascene = scene[i];
118           if (bascene.getIndex() == scindex) break;
119         }
120         Step[]              step  = null;        
121         Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
122
123         bastep = getProjectElementService().getFirstStep(bascene).getNumber();
124         if (this.sharesStep()) {
125           step = getProjectElementService().getSteps(bascene);
126           sprop.setBaseStep(step[number-bastep]);
127         }
128         bascene = getScenarioService().addScenario(study, sprop);
129 //        transax.commit();
130
131 //      Update of the display
132         if (step != null) for (int i=0; i<number-bastep+1; i++) {
133           List<Publication>  contents = step[i].getAllDocuments();
134           for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
135         }
136         mystudy.setSelection(bascene.getIndex() + "." + number);
137         getSession().remove("menu.scenario");
138             return SUCCESS;
139           }
140       catch (RuntimeException saverror) {
141         logger.error("Reason:", saverror);
142         
143         setMenuProperty("study");
144                 initializationScreenContext(_menuProperty);
145                 
146         return ERROR;
147       }
148           catch (Exception error) {
149         return INPUT;
150       }
151     }
152
153 //  ==============================================================================================================================
154 //  Getters and setters
155 //  ==============================================================================================================================
156
157     public List<Scenario> getScenarii () {
158 //  ------------------------------------
159       return myscene;
160     }
161     public long getSelectedScenarioIndex () {
162 //  --------------------------------------
163       return scindex;
164     }
165     public String getSelection () {
166 //  -----------------------------
167       return selection;
168     }
169     public String getSharedStep () {
170 //  -------------------------------
171       String[] parse = selection.split("\\x2E");
172       return   parse[1];
173     }
174     public String getTitle () {
175 //  -------------------------
176       return mytitle;
177     }
178
179     public void setCancel (boolean cancel) {
180 //  --------------------------------------
181       action = ToDo.cancel;
182     }
183     public void setSave (boolean save) {
184 //  ----------------------------------
185       action = ToDo.save;
186     }
187     public void setSelectedScenario (String index) {
188 //  ----------------------------------------------
189       scindex = Integer.valueOf(index);
190     }
191     public void setSelection (String step) {
192 //  --------------------------------------
193       selection = step;
194     }
195     public void setTitle (String title) {
196 //  -----------------------------------
197       mytitle = title;
198     }
199     public Boolean sharesStep () {
200 //  ----------------------------
201       return (Integer.valueOf(getSharedStep()) > bastep);
202     }
203         /**
204          * Get the projectElementService.
205          * 
206          * @return the projectElementService
207          */
208         public ProjectElementService getProjectElementService() {
209                 return _projectElementService;
210         }
211
212         /**
213          * Set the projectElementService.
214          * 
215          * @param projectElementService
216          *            the projectElementService to set
217          */
218         public void setProjectElementService(
219                         ProjectElementService projectElementService) {
220                 _projectElementService = projectElementService;
221         }
222
223         /**
224          * Get the menu.
225          * @return the menu
226          */
227         public Menu getMenu() {
228                 return _menu;
229         }
230
231         /**
232          * Set the menu.
233          * @param menu the menu to set
234          */
235         public void setMenu(Menu menu) {
236                 _menu = menu;
237         }
238
239         /**
240          * Get the scenarioService.
241          * @return the scenarioService
242          */
243         public ScenarioService getScenarioService() {
244                 return _scenarioService;
245         }
246
247         /**
248          * Set the scenarioService.
249          * @param scenarioService the scenarioService to set
250          */
251         public void setScenarioService(ScenarioService scenarioService) {
252                 _scenarioService = scenarioService;
253         }
254         
255         /**
256          * Get the menuProperty.
257          * @return the menuProperty
258          */
259         public String getMenuProperty() {
260                 return _menuProperty;
261         }
262
263         /**
264          * Set the menuProperty.
265          * @param menuProperty the menuProperty to set
266          */
267         public void setMenuProperty(String menuProperty) {
268                 this._menuProperty = menuProperty;
269         }
270 }