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