Salome HOME
Create new scenario from existing one is improved
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / NewScenarioAction.java
index bc9b0ba781dacdc0218f8b60454dd613874b1b86..05404329ed8d2c0d2837af3df89f786e1eb70e41 100644 (file)
@@ -5,198 +5,217 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.ResourceBundle;
 
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.splat.dal.dao.som.Database;
 import org.splat.dal.bo.som.Publication;
 import org.splat.dal.bo.som.Scenario;
+import org.splat.dal.bo.som.Study;
 import org.splat.service.ProjectElementService;
-import org.splat.service.StudyService;
+import org.splat.service.ScenarioService;
 import org.splat.som.Step;
-import org.splat.dal.bo.som.Study;
+import org.splat.wapp.Constants;
 import org.splat.wapp.Menu;
 
-
+/**
+ * New scenario creation action.
+ */
 public class NewScenarioAction extends Action {
 
-       private  OpenStudy        mystudy;
-       private  List<Scenario>   myscene;
-       private  String           mytitle;
-    private  String           selection;
-    private  long              scindex;
-    private  int              bastep;
-    private  ToDo             action;
-       private StudyService _studyService;
+       private transient OpenStudy _mystudy;
+       private transient List<Scenario> _myscene;
+       private String _title;
+       private String _selection;
+       private transient long _scindex;
+       private transient int _bastep;
+       private transient ToDo _action;
+       /**
+        * Injected scenario service.
+        */
+       private ScenarioService _scenarioService;
+       /**
+        * Injected project element service.
+        */
        private ProjectElementService _projectElementService;
+       private Menu _menu;
 
-       private static final long serialVersionUID = -5586724442986956861L;
-
-    protected enum  ToDo { cancel, save };
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------    
-                     mystudy = getOpenStudy();
-      Study          study   = mystudy.getStudyObject();
-      Scenario[]     scene   = study.getScenarii();
-      Scenario       base    = scene[scene.length-1];   // Default base scenario
-      ResourceBundle locale  = ResourceBundle.getBundle("labels", ApplicationSettings.getCurrentLocale());
-
-      myscene   = Arrays.asList(scene);
-      scindex   = base.getIndex();
-      mytitle   = locale.getString("label.scenario") + " " + String.valueOf(scene.length+1);
-      bastep    = getProjectElementService().getFirstStep(base).getNumber();      // Better use the last current step ?
-      selection = scindex + "." + bastep;
-      action    = null;
-      
-      Menu menu = new NewScenarioMenu(study);
-      menu.selects(selection);
-      getSession().put("menu.scenario", menu);
-      return SUCCESS;
-    }
-
-    public String doSelectStep () {
-//  -----------------------------
-                 mystudy = getOpenStudy();
-      Study      study   = mystudy.getStudyObject();
-      Scenario[] scene   = study.getScenarii();
-
-      myscene   = Arrays.asList(scene);
-      bastep    = getProjectElementService().getFirstStep(scene[0]).getNumber();  // All scenarios have the same first step number
-      action    = null;
-        
-      getMenu("scenario").selects(selection);
-      return SUCCESS;
-    }
-
-    public String doCreate () {
-//  -------------------------
-      if (action == ToDo.cancel) return "cancel";
-
-      Session      session   = Database.getSession();
-         Transaction  transax   = session.beginTransaction();  
-         try {        mystudy   = getOpenStudy();
-                   selection = getMenu("scenario").getSelection();
-        Study      study     = mystudy.getStudyObject();
-        String[]   parse     = selection.split("\\x2E");
-        int        scindex   = Integer.valueOf(parse[0]);
-        int        number    = Integer.valueOf(parse[1]);
-
-        Scenario[] scene     = study.getScenarii(); 
-        Scenario   bascene   = scene[0];
-        for (int i=1; i<scene.length; i++) {
-          bascene = scene[i];
-          if (bascene.getIndex() == scindex) break;
-        }
-        Step[]              step  = null;        
-        Scenario.Properties sprop = new Scenario.Properties().setManager(getConnectedUser()).setTitle(mytitle).setInsertAfter(bascene);
-
-        bastep = getProjectElementService().getFirstStep(bascene).getNumber();
-        if (this.sharesStep()) {
-          step = getProjectElementService().getSteps(bascene);
-          sprop.setBaseStep(step[number-bastep]);
-        }
-        bascene = getStudyService().addScenario(study, sprop);
-        transax.commit();
-
-//      Update of the display
-        if (step != null) for (int i=0; i<number-bastep+1; i++) {
-          List<Publication>  contents = step[i].getAllDocuments();
-          for (Iterator<Publication> j=contents.iterator(); j.hasNext(); ) mystudy.update(j.next());
-        }
-        mystudy.setSelection(bascene.getIndex() + "." + number);
-        getSession().remove("menu.scenario");
-           return SUCCESS;
-         }
-      catch (RuntimeException saverror) {
-        logger.error("Reason:", saverror);
-        if (transax != null && transax.isActive()) {
-//        Second try-catch as the rollback could fail as well
-          try {
-               transax.rollback();
-          } catch (HibernateException backerror) {
-            logger.debug("Error rolling back transaction", backerror);
-          }
-        }
-        return ERROR;
-      }
-         catch (Exception error) {
-        return INPUT;
-      }
-    }
-
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-
-    public List<Scenario> getScenarii () {
-//  ------------------------------------
-      return myscene;
-    }
-    public long getSelectedScenarioIndex () {
-//  --------------------------------------
-      return scindex;
-    }
-    public String getSelection () {
-//  -----------------------------
-      return selection;
-    }
-    public String getSharedStep () {
-//  -------------------------------
-      String[] parse = selection.split("\\x2E");
-      return   parse[1];
-    }
-    public String getTitle () {
-//  -------------------------
-      return mytitle;
-    }
-
-    public void setCancel (boolean cancel) {
-//  --------------------------------------
-      action = ToDo.cancel;
-    }
-    public void setSave (boolean save) {
-//  ----------------------------------
-      action = ToDo.save;
-    }
-    public void setSelectedScenario (String index) {
-//  ----------------------------------------------
-      scindex = Integer.valueOf(index);
-    }
-    public void setSelection (String step) {
-//  --------------------------------------
-      selection = step;
-    }
-    public void setTitle (String title) {
-//  -----------------------------------
-      mytitle = title;
-    }
-    public Boolean sharesStep () {
-//  ----------------------------
-      return (Integer.valueOf(getSharedStep()) > bastep);
-    }
        /**
-        * Get the studyService.
-        * 
-        * @return the studyService
+        * Serial version ID.
         */
-       public StudyService getStudyService() {
-               return _studyService;
+       private static final long serialVersionUID = -5586724442986956861L;
+
+       protected enum ToDo {
+               cancel, save
+       };
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       public String doInitialize() {
+               _mystudy = getOpenStudy();
+               Study study = _mystudy.getStudyObject();
+               Scenario[] scene = study.getScenarii();
+               Scenario base = scene[scene.length - 1]; // Default base scenario
+               ResourceBundle locale = ResourceBundle.getBundle("labels",
+                               getApplicationSettings().getCurrentLocale());
+
+               _myscene = Arrays.asList(scene);
+               _scindex = base.getIndex();
+               _title = locale.getString("label.scenario") + " "
+                               + String.valueOf(scene.length + 1);
+               _bastep = getProjectElementService().getFirstStep(base).getNumber(); // Better use the last current step ?
+               _selection = _scindex + "." + _bastep;
+               _action = null;
+
+               // RKV Menu menu = new NewScenarioMenu(study);
+               Menu menu = ((NewScenarioMenu) getMenu()).init(study); // RKV
+               menu.selects(_selection);
+               getSession().put("menu.scenario", menu);
+
+               initializationFullScreenContext(Constants.STUDY_MENU,
+                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                               Constants.SCENARIO_MENU);
+
+               setActionType("focus");
+
+               return SUCCESS;
        }
 
-       /**
-        * Set the studyService.
-        * 
-        * @param studyService
-        *            the studyService to set
-        */
-       public void setStudyService(StudyService studyService) {
-               _studyService = studyService;
+       public String doSelectStep() {
+               _mystudy = getOpenStudy();
+               Study study = _mystudy.getStudyObject();
+               Scenario[] scene = study.getScenarii();
+
+               _myscene = Arrays.asList(scene);
+               _bastep = getProjectElementService().getFirstStep(scene[0]).getNumber(); // All scenarios have the same first step number
+               _action = null;
+
+               getMenu("scenario").selects(_selection);
+
+               initializationFullScreenContext(Constants.STUDY_MENU,
+                               Constants.STUDY_MENU, Constants.TRUE, Constants.NONE,
+                               Constants.SCENARIO_MENU);
+
+               setActionType("none");
+
+               return SUCCESS;
+       }
+
+       public String doCreate() {
+               if (_action == ToDo.cancel) {
+                       return "cancel";
+               }
+
+               try {
+                       _mystudy = getOpenStudy();
+                       _selection = getMenu("scenario").getSelection();
+                       Study study = _mystudy.getStudyObject();
+                       String[] parse = _selection.split("\\x2E");
+                       int number = Integer.valueOf(parse[1]);
+
+                       Scenario[] scene = study.getScenarii();
+                       Scenario bascene = scene[0];
+                       for(Scenario scenario : scene) {
+                               bascene = scenario;
+                               if(bascene.getIndex() == _scindex) {
+                                       break;
+                               }
+                       }
+                       Step[] step = null;
+                       Scenario.Properties sprop = new Scenario.Properties().setManager(
+                                       getConnectedUser()).setTitle(_title)
+                                       .setInsertAfter(bascene);
+
+                       
+                       long selectedScenarioId = Long.valueOf(parse[0]);
+                       Scenario selectedScenario = scene[0];
+                       for(Scenario scenario : scene) {
+                               if(scenario.getIndex() == selectedScenarioId) {
+                                       selectedScenario = scenario;
+                               }
+                       }
+                       _bastep = getProjectElementService().getFirstStep(selectedScenario)
+                                       .getNumber();
+                       if (this.sharesStep()) {
+                               step = getProjectElementService().getSteps(selectedScenario);
+                               sprop.setBaseStep(step[number - _bastep]);
+                       }
+                       Scenario createdScenario = getScenarioService().addScenario(study, sprop);
+                       // transax.commit();
+
+                       // Update of the display
+                       if (step != null) {
+                               for (int i = 0; i < number - _bastep + 1; i++) {
+                                       List<Publication> contents = step[i].getAllDocuments();
+                                       for (Iterator<Publication> j = contents.iterator(); j
+                                                       .hasNext();) {
+                                               _mystudy.update(j.next());
+                                       }
+                               }
+                       }
+                       _mystudy.setSelection(createdScenario.getIndex() + "." + number);
+                       getSession().remove("menu.scenario");
+                       return SUCCESS;
+               } catch (RuntimeException saverror) {
+                       LOG.error("Reason:", saverror);
+
+                       initializationFullScreenContext(Constants.STUDY_MENU,
+                                       Constants.STUDY_MENU, Constants.FALSE,
+                                       Constants.STANDARD_MENU, Constants.STUDY_MENU);
+
+                       return ERROR;
+               } catch (Exception error) {
+                       return INPUT;
+               }
+       }
+
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       public List<Scenario> getScenarii() {
+               return _myscene;
+       }
+
+       public long getSelectedScenarioIndex() {
+               return _scindex;
        }
+
+       public String getSelection() {
+               return _selection;
+       }
+
+       public String getSharedStep() {
+               String[] parse = _selection.split("\\x2E");
+               return parse[1];
+       }
+
+       public String getTitle() {
+               return _title;
+       }
+
+       public void setCancel(final boolean cancel) {
+               _action = ToDo.cancel;
+       }
+
+       public void setSave(final boolean save) {
+               _action = ToDo.save;
+       }
+
+       public void setSelectedScenario(final String index) {
+               _scindex = Integer.valueOf(index);
+       }
+
+       public void setSelection(final String step) {
+               _selection = step;
+       }
+
+       public void setTitle(final String title) {
+               _title = title;
+       }
+
+       public Boolean sharesStep() {
+               return (Integer.valueOf(getSharedStep()) > _bastep);
+       }
+
        /**
         * Get the projectElementService.
         * 
@@ -213,7 +232,45 @@ public class NewScenarioAction extends Action {
         *            the projectElementService to set
         */
        public void setProjectElementService(
-                       ProjectElementService projectElementService) {
+                       final ProjectElementService projectElementService) {
                _projectElementService = projectElementService;
        }
+
+       /**
+        * Get the menu.
+        * 
+        * @return the menu
+        */
+       public Menu getMenu() {
+               return _menu;
+       }
+
+       /**
+        * Set the menu.
+        * 
+        * @param menu
+        *            the menu to set
+        */
+       public void setMenu(final Menu menu) {
+               _menu = menu;
+       }
+
+       /**
+        * Get the scenarioService.
+        * 
+        * @return the scenarioService
+        */
+       public ScenarioService getScenarioService() {
+               return _scenarioService;
+       }
+
+       /**
+        * Set the scenarioService.
+        * 
+        * @param scenarioService
+        *            the scenarioService to set
+        */
+       public void setScenarioService(final ScenarioService scenarioService) {
+               _scenarioService = scenarioService;
+       }
 }
\ No newline at end of file