Salome HOME
Menus are improved
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditSimulationContextAction.java
index 8e30011792342b55aba5bd3dbd4a28a360f910ac..939d5d1ba1d4d2929ee83facb038721f913382db 100644 (file)
@@ -3,257 +3,368 @@ package org.splat.simer;
 import java.util.Arrays;
 import java.util.List;
 
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.splat.som.Database;
-import org.splat.som.ProjectElement;
-import org.splat.som.SimulationContext;
-import org.splat.som.SimulationContextType;
+import org.splat.dal.bo.som.ProjectElement;
+import org.splat.dal.bo.som.SimulationContext;
+import org.splat.dal.bo.som.SimulationContextType;
+import org.splat.service.SimulationContextService;
+import org.splat.service.SimulationContextTypeService;
+import org.splat.service.StepService;
+import org.splat.service.StudyService;
 import org.splat.som.Step;
-import org.splat.som.Study;
-
+import org.splat.dal.bo.som.Study;
 
 public class EditSimulationContextAction extends DisplayStudyStepAction {
 
-       private List<SimulationContextType> contype   = null;
-       private List<SimulationContext>     contelm   = null;
-       private String                      selectype = null;     // Context type, if selected
-       private String                      newtype   = null;     // Context type, if newed
-       private SimulationContextType       type      = null;     // Corresponding context type object
-       private String                      value     = null;     // Context value
-
-       private static final long    serialVersionUID = -641719644024601042L;
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------
-      Session     connex  = Database.getSession();
-         Transaction transax = connex.beginTransaction();
-
-         mystudy = getOpenStudy();
-      contype = getInvolvedContexts();
-      
-      transax.commit();
-      if (contype.isEmpty()) return "create";
-      else                   return "select";
-    }
-    
-    public String doSelectContext () {
-//  --------------------------------
-      Session     connex  = Database.getSession();
-         Transaction transax = connex.beginTransaction();
-      try {      
-       mystudy   = getOpenStudy();
-        int typid = Integer.valueOf(selectype);      
-        if (typid == 0) return "create";
-      
-           SimulationContext.Properties  cprop = new SimulationContext.Properties();
-           type    = SimulationContext.selectType(typid);
-           newtype = type.getName();
-        contype = getInvolvedContexts();
-        contelm = Database.selectSimulationContextsWhere(cprop.setType(type));
-
-        return "set";
-      }
-      finally {
-        transax.commit();
-      }
-    }
-
-    public String doCreateContext () {
-//  --------------------------------
-      Session     connex  = Database.getSession();
-      Transaction transax = connex.beginTransaction();
-      try {
-       mystudy = getOpenStudy();
-        if (newtype.length() == 0 || value.length() == 0) return INPUT;
-
-       Step            step  = mystudy.getSelectedStep();
-       ProjectElement  owner = step.getOwner();
-
-           SimulationContext.Properties  cprop  = new SimulationContext.Properties();
-           SimulationContext             contex = null;
-           type = SimulationContext.createType(newtype, step.getStep());
-           cprop.setType(type).setValue(value);
-           if (owner instanceof Study) contex = ((Study)owner).addProjectContext(cprop);     // Re-indexes knowledges and the study
-           else                        contex =           step.addSimulationContext(cprop);  // Re-indexes knowledges only
-
-           mystudy.add(contex);
-        transax.commit();
-        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) {
-        transax.commit();
-       return INPUT;
-      }
-    }
-
-    public String doDeleteContext () {
-//  --------------------------------
-      Session     connex  = Database.getSession();
-         Transaction transax = connex.beginTransaction();
-         try {
+       private List<SimulationContextType> contype = null;
+       private List<SimulationContext> contelm = null;
+       private String selectype = null; // Context type, if selected
+       private String newtype = null; // Context type, if newed
+       private SimulationContextType type = null; // Corresponding context type object
+       private String value = null; // Context value
+       /**
+        * Injected study service.
+        */
+       private StudyService _studyService;
+       /**
+        * Injected step service.
+        */
+       private StepService _stepService;
+       /**
+        * Injected simulation context service.
+        */
+       private SimulationContextService _simulationContextService;
+       /**
+        * Injected simulation context type service.
+        */
+       private SimulationContextTypeService _simulationContextTypeService;
+       
+       /**
+        * Value of the menu property. 
+        * It can be: none, create, open, study, knowledge, sysadmin, help.
+        */
+       private String _menuProperty;
+
+       /**
+        * Get the stepService.
+        * 
+        * @return the stepService
+        */
+       public StepService getStepService() {
+               return _stepService;
+       }
+
+       /**
+        * Set the stepService.
+        * 
+        * @param stepService
+        *            the stepService to set
+        */
+       public void setStepService(StepService stepService) {
+               _stepService = stepService;
+       }
+
+       private static final long serialVersionUID = -641719644024601042L;
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       public String doInitialize() {
+
                mystudy = getOpenStudy();
-         
-       Step              step    = mystudy.getSelectedStep();
-       ProjectElement    owner   = step.getOwner();
-        SimulationContext context = step.getSimulationContext(Integer.valueOf(myindex));
-           if (owner instanceof Study) ((Study)owner).removeProjectContext(context);     // Re-indexes knowledges and the study
-           else                                  step.removeSimulationContext(context);  // Re-indexes knowledges only
-
-        mystudy.remove(context);
-        transax.commit();
-        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;
-         }
-    }
-    
-    public String doSetContext () {
-//  -----------------------------      
-         String[]    input   = value.split(",");
-      Session     connex  = Database.getSession();
-         Transaction transax = connex.beginTransaction();
-         try {
+               contype = getInvolvedContexts();
+               
+               setMenuProperty("study");
+               initializationScreenContext(_menuProperty);
+
+               if (contype.isEmpty())
+                       return "create";
+               else
+                       return "select";
+       }
+
+       public String doSelectContext() {
                mystudy = getOpenStudy();
+               
+               setMenuProperty("study");
+               initializationScreenContext(_menuProperty);
+               
+               int typid = Integer.valueOf(selectype);
+               if (typid == 0)
+                       return "create";
+
+               SimulationContext.Properties cprop = new SimulationContext.Properties();
+               type = getSimulationContextService().selectType(typid);
+               newtype = type.getName();
+               contype = getInvolvedContexts();
+               contelm = getSimulationContextService()
+                               .selectSimulationContextsWhere(cprop.setType(type));
+
+               return "set";
+       }
+
+       public String doCreateContext() {
+               // --------------------------------
+               try {
+                       mystudy = getOpenStudy();
+                       
+                       setMenuProperty("study");
+                       initializationScreenContext(_menuProperty);
+                       
+                       if (newtype.length() == 0 || value.length() == 0)
+                               return INPUT;
+
+                       Step step = mystudy.getSelectedStep();
+                       ProjectElement owner = step.getOwner();
+
+                       SimulationContext.Properties cprop = new SimulationContext.Properties();
+                       SimulationContext contex = null;
+                       type = getSimulationContextTypeService().createType(newtype,
+                                       step.getStep());
+                       cprop.setType(type).setValue(value);
+                       if (owner instanceof Study)
+                               contex = getStudyService().addProjectContext(((Study) owner),
+                                               cprop); // Re-indexes knowledges and the study
+                       else
+                               contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
+
+                       mystudy.add(contex);
+                       return SUCCESS;
+               } catch (RuntimeException saverror) {
+                       logger.error("Reason:", saverror);
+                       return ERROR;
+               } catch (Exception error) {
+                       return INPUT;
+               }
+       }
+
+       public String doDeleteContext() {
+               
+               setMenuProperty("study");
+               initializationScreenContext(_menuProperty);
+               
+               try {
+                       mystudy = getOpenStudy();
+
+                       Step step = mystudy.getSelectedStep();
+                       ProjectElement owner = step.getOwner();
+                       SimulationContext context = step.getSimulationContext(Integer
+                                       .valueOf(myindex));
+                       if (owner instanceof Study)
+                               getStudyService()
+                                               .removeProjectContext(((Study) owner), context); // Re-indexes knowledges and the study
+                       else
+                               getStepService().removeSimulationContext(step, context); // Re-indexes knowledges only
+
+                       mystudy.remove(context);
+                       return SUCCESS;
+               } catch (RuntimeException saverror) {
+                       logger.error("Reason:", saverror);
+                       return ERROR;
+               }
+       }
+
+       public String doSetContext() {
+               // -----------------------------
+               String[] input = value.split(",");
+//             Session connex = Database.getCurSession();
+//             Transaction transax = connex.beginTransaction();
+               
+               setMenuProperty("study");
+               initializationScreenContext(_menuProperty);
+               
+               try {
+                       mystudy = getOpenStudy();
+
+                       Step step = mystudy.getSelectedStep();
+                       ProjectElement owner = step.getOwner();
+                       SimulationContext contex = null;
+
+                       if (input.length == 1
+                                       || (input.length == 2 && input[1].equals(" "))) {
+                               // Setting an existing simulation context identified by value (input = rid," ")
+                               int valid = Integer.valueOf(input[0]);
+                               contex = getSimulationContextService().selectSimulationContext(
+                                               valid);
+                               if (owner instanceof Study)
+                                       getStudyService()
+                                                       .addProjectContext(((Study) owner), contex);
+                               else
+                                       getStepService().addSimulationContext(step, contex);
+                       } else {
+                               // Setting a new simulation context value (input = 0,"new context value")
+                               int typid = Integer.valueOf(selectype);
+                               SimulationContext.Properties cprop = new SimulationContext.Properties();
+                               cprop.setType(getSimulationContextService().selectType(typid))
+                                               .setValue(input[1].trim());
+                               if (owner instanceof Study)
+                                       contex = getStudyService().addProjectContext(
+                                                       ((Study) owner), cprop); // Re-indexes knowledges and the study
+                               else
+                                       contex = getStepService().addSimulationContext(step, cprop); // Re-indexes knowledges only
+                       }
+                       mystudy.add(contex);
+                       contype = getInvolvedContexts();
+
+//                     transax.commit();
+                       return SUCCESS;
+               } catch (RuntimeException saverror) {
+                       logger.error("Reason:", saverror);
+                       return ERROR;
+               } catch (Exception error) {
+                       value = input[0];
+                       return INPUT;
+               }
+       }
+
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       public SimulationContextType getContextType() {
+               // ----------------------------------------------
+               return type;
+       }
+
+       public String getContextName() {
+               // -------------------------------
+               return newtype;
+       }
+
+       // public List<SimulationContextFacade> getSimulationContexts () {
+       // -------------------------------------------------------
+       // return mystudy.getSelectedStep().getAllSimulationContexts();
+       // }
+       public List<SimulationContextType> getSimulationContextTypes() {
+               // ---------------------------------------------------------------
+               return contype;
+       }
+
+       public List<SimulationContext> getSimulationContextValues() {
+               // ------------------------------------------------------------
+               return contelm;
+       }
+
+       public void setContextType(String type) {
+               // ----------------------------------------
+               this.selectype = type;
+       }
+
+       public void setContextValue(String value) {
+               // -----------------------------------------
+               this.value = value;
+       }
+
+       public void setNewType(String name) {
+               // ------------------------------------
+               this.newtype = name;
+       }
+
+       // ==============================================================================================================================
+       // Private service
+       // ==============================================================================================================================
+
+       private List<SimulationContextType> getInvolvedContexts() {
+               // ----------------------------------------------------------
+               SimulationContextType.Properties sprop = new SimulationContextType.Properties()
+                               .setStep(mystudy.getSelectedStep().getStep());
+               List<SimulationContextType> contype = getSimulationContextService()
+                               .selectTypesWhere(sprop);
+
+               if (!contype.isEmpty()) {
+                       // Ordering by alphabetical order of localized context types
+                       SimulationContextType[] types = contype
+                                       .toArray(new SimulationContextType[contype.size()]);
+                       ContextTypeComparator compare = new ContextTypeComparator();
+                       boolean state = types[0].isApproved();
+                       int from = 0;
+                       int to = 0;
+                       while (to < types.length - 1) {
+                               to += 1;
+                               if (types[to].isApproved() == state)
+                                       continue;
+
+                               if (to > from + 1)
+                                       Arrays.sort(types, from, to, compare);
+                               state = !state;
+                               from = to;
+                       }
+                       if (to > from)
+                               Arrays.sort(types, from, to + 1, compare);
+                       contype = Arrays.asList(types);
+               }
+               return contype;
+       }
+
+       /**
+        * Get the studyService.
+        * 
+        * @return the studyService
+        */
+       public StudyService getStudyService() {
+               return _studyService;
+       }
+
+       /**
+        * Set the studyService.
+        * 
+        * @param studyService
+        *            the studyService to set
+        */
+       public void setStudyService(StudyService studyService) {
+               _studyService = studyService;
+       }
+
+       /**
+        * Get the simulationContextService.
+        * 
+        * @return the simulationContextService
+        */
+       public SimulationContextService getSimulationContextService() {
+               return _simulationContextService;
+       }
+
+       /**
+        * Set the simulationContextService.
+        * 
+        * @param simulationContextService
+        *            the simulationContextService to set
+        */
+       public void setSimulationContextService(
+                       SimulationContextService simulationContextService) {
+               _simulationContextService = simulationContextService;
+       }
+
+       /**
+        * Get the simulationContextTypeService.
+        * @return the simulationContextTypeService
+        */
+       public SimulationContextTypeService getSimulationContextTypeService() {
+               return _simulationContextTypeService;
+       }
+
+       /**
+        * Set the simulationContextTypeService.
+        * @param simulationContextTypeService the simulationContextTypeService to set
+        */
+       public void setSimulationContextTypeService(
+                       SimulationContextTypeService simulationContextTypeService) {
+               _simulationContextTypeService = simulationContextTypeService;
+       }
+       
+       /**
+        * Get the menuProperty.
+        * @return the menuProperty
+        */
+       public String getMenuProperty() {
+               return _menuProperty;
+       }
 
-       Step              step   = mystudy.getSelectedStep();
-       ProjectElement    owner  = step.getOwner();
-       SimulationContext contex = null;
-
-           if (input.length == 1 || (input.length == 2 && input[1].equals(" "))) {
-//        Setting an existing simulation context identified by value (input = rid," ")
-             int valid = Integer.valueOf(input[0]);
-             contex    = Database.selectSimulationContext(valid);
-             if (owner instanceof Study) ((Study)owner).addProjectContext(contex);
-             else                                  step.addSimulationContext(contex);
-           }
-           else {
-//        Setting a new simulation context value (input = 0,"new context value")
-          int                          typid = Integer.valueOf(selectype);
-          SimulationContext.Properties cprop = new SimulationContext.Properties();
-          cprop.setType(SimulationContext.selectType(typid))
-               .setValue(input[1].trim());          
-          if (owner instanceof Study) contex = ((Study)owner).addProjectContext(cprop);     // Re-indexes knowledges and the study
-          else                        contex =           step.addSimulationContext(cprop);  // Re-indexes knowledges only
-           }
-           mystudy.add(contex);
-        contype = getInvolvedContexts();
-
-        transax.commit();
-           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) {
-        value = input[0];
-        transax.commit();
-       return INPUT;
-      }
-    }
-
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-
-    public SimulationContextType getContextType () {
-//  ----------------------------------------------
-      return type;
-    }
-    public String getContextName () {
-//  -------------------------------
-      return newtype;
-    }
-//    public List<SimulationContextFacade> getSimulationContexts () {
-//  -------------------------------------------------------
-//      return mystudy.getSelectedStep().getAllSimulationContexts();
-//    }
-    public List<SimulationContextType> getSimulationContextTypes () {
-//  ---------------------------------------------------------------
-      return contype;
-    }
-    public List<SimulationContext> getSimulationContextValues () {
-//  ------------------------------------------------------------
-      return contelm;
-    }
-    
-    public void setContextType (String type) {
-//  ----------------------------------------
-      this.selectype = type;
-    }
-    public void setContextValue (String value) {
-//  -----------------------------------------
-      this.value = value;
-    }
-    public void setNewType (String name) {
-//  ------------------------------------
-      this.newtype = name;
-    }
-
-//  ==============================================================================================================================
-//  Private service
-//  ==============================================================================================================================
-
-       private List<SimulationContextType> getInvolvedContexts () {
-//  ----------------------------------------------------------
-      SimulationContextType.Properties sprop   = new SimulationContextType.Properties()
-                                                                          .setStep(mystudy.getSelectedStep().getStep());
-      List<SimulationContextType>      contype = SimulationContext.selectTypesWhere(sprop);
-
-      if (!contype.isEmpty()) {
-//      Ordering by alphabetical order of localized context types
-        SimulationContextType[] types   = contype.toArray( new SimulationContextType[contype.size()] );
-        ContextTypeComparator   compare = new ContextTypeComparator();
-        boolean state = types[0].isApproved();
-        int     from  = 0;
-        int     to    = 0;
-        while (to < types.length-1) {
-          to += 1;
-             if (types[to].isApproved() == state) continue;
-
-          if (to > from+1) Arrays.sort(types, from, to, compare);
-          state = !state;
-          from  = to;
-        }
-        if (to > from) Arrays.sort(types, from, to+1, compare);
-        contype = Arrays.asList(types);
-      }
-      return contype;
+       /**
+        * Set the menuProperty.
+        * @param menuProperty the menuProperty to set
+        */
+       public void setMenuProperty(String menuProperty) {
+               this._menuProperty = menuProperty;
        }
 }
\ No newline at end of file