Salome HOME
Tool bar is improved.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditKnowledgeElementAction.java
index 4b13620e6e665375213d0cc1d5992a9873ffccb1..5e839d72c37943efa28907deeeedb7dec2dbd9c9 100644 (file)
 package org.splat.simer;
 
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
 import org.splat.dal.bo.kernel.User;
-import org.splat.dal.dao.som.Database;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.KnowledgeElementType;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.service.KnowledgeElementService;
+import org.splat.service.KnowledgeElementTypeService;
 import org.splat.service.ScenarioService;
 import org.splat.som.Step;
 
-
+/**
+ * Action for addition and modification of knowledge elements in the selected scenario.
+ */
 public class EditKnowledgeElementAction extends DisplayStudyStepAction {
 
-       private String                      type      = null;     // Edited knowledge type
-       private String                      title     = null;
-       private String                      value     = null;
+       /**
+        * Serial version ID.
+        */
+       private static final long serialVersionUID = 4636919137087687068L;
+
+       /**
+        * Edited knowledge element type.
+        */
+       private String type = null; // Edited knowledge type
+       /**
+        * Edited knowledge element title.
+        */
+       private String title = null;
+       /**
+        * Edited knowledge element value.
+        */
+       private String value = null;
+       /**
+        * Injected scenario service.
+        */
        private ScenarioService _scenarioService;
+       /**
+        * Injected knowledge element service.
+        */
        private KnowledgeElementService _knowledgeElementService;
+       /**
+        * Injected knowledge element type service.
+        */
+       private KnowledgeElementTypeService _knowledgeElementTypeService;
+
+       /**
+        * Value of the menu property. It can be: none, create, open, study, knowledge, sysadmin, help.
+        */
+       private String _menuProperty;
+       
+       /**
+        * Value of the title bar property. 
+        * It can be: study, knowledge.
+        */
+       private String _titleProperty;
+       
+       /**
+        * Property that indicates whether the current open study is editable or not.
+        * On the screen it looks like pen on the status icon, pop-up menu also can be called.
+        * It is necessary for correct building the title bar.
+        */
+       private String _editDisabledProperty = "false";
+       
+       /**
+        * Value of the tool bar property. 
+        * It can be: none, standard, study, back.
+        */
+       private String _toolProperty;
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
+
+       /**
+        * Initialize the action. Get selected study from the session.
+        * 
+        * @return SUCCESS if no exceptions
+        */
+       public String doInitialize() {
+               mystudy = getOpenStudy();
+
+               setMenuProperty("study");
+               setTitleProperty("study");
+               if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
+                       setToolProperty("study");
+               } else {
+                       setToolProperty("standard");
+               }
+               initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
+
+               return SUCCESS;
+       }
+
+       /**
+        * Add a new or update an existing knowledge element of the selected scenario in the open study.
+        * 
+        * @return SUCCESS if operation succeeded, ERROR if Runtime exception, otherwise INPUT
+        */
+       public String doSetKnowledge() {
+
+               setMenuProperty("study");
+               setTitleProperty("study");
+               if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
+                       setToolProperty("study");
+               } else {
+                       setToolProperty("standard");
+               }
+               initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
+
+               try {
+                       User user = getConnectedUser();
+                       mystudy = getOpenStudy();
+
+                       Step step = mystudy.getSelectedStep();
+                       Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
+
+                       if (title != null && value != null) { // Addition of a new Knowledge Element
+                               KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
+                               KnowledgeElementType ktype = getKnowledgeElementTypeService()
+                                               .selectType(Integer.valueOf(type));
+                               kprop.setType(ktype).setTitle(title).setValue(value).setAuthor(
+                                               user);
+                               mystudy.add(getScenarioService().addKnowledgeElement(scene,
+                                               kprop));
+                               getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of first added document
+                       } else if (title != null) { // Renaming of an existing Knowledge Element
+                               KnowledgeElement kelm = scene.getKnowledgeElement(Integer
+                                               .valueOf(type));
+                               getKnowledgeElementService().rename(kelm, title);
+                               // Useless to update the open study
+                       } else if (value != null) { // Edition of a knowledge
+                               KnowledgeElement kelm = scene.getKnowledgeElement(Integer
+                                               .valueOf(type));
+                               getKnowledgeElementService().update(kelm, value);
+                               mystudy.update(kelm); // For updating the truncated value
+                       }
+                       return SUCCESS;
+               } catch (RuntimeException saverror) {
+                       logger.error("Reason:", saverror);
+                       return ERROR;
+               } catch (Exception error) {
+                       logger.error("Exception while saving a knowledge: ", error);
+                       return INPUT;
+               }
+       }
+
+       /**
+        * Delete a knowledge element from the current scenario.
+        * 
+        * @return SUCCESS if no exceptions
+        */
+       public String doDeleteKnowledge() {
+               mystudy = getOpenStudy();
+               Step step = mystudy.getSelectedStep();
+               Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
+
+               KnowledgeElement kelm = scene.getKnowledgeElement(Integer
+                               .valueOf(myindex));
+               getScenarioService().removeKnowledgeElement(scene, kelm); // The knowledge element necessarily exists
+
+               mystudy.remove(kelm);
+               getMenu("study").selects(mystudy.getSelection()); // Updates the menu icon, in case of last removed document
+
+               setMenuProperty("study");
+               setTitleProperty("study");
+               if ("true".equals(getWriteAccess()) &&  getUserRights().canCreateDocument()) {
+                       setToolProperty("study");
+               } else {
+                       setToolProperty("standard");
+               }
+               initializationScreenContext(_menuProperty, _titleProperty, _editDisabledProperty, _toolProperty);
+
+               return SUCCESS;
+       }
+
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       /**
+        * Get the type of the modified knowledge element.
+        * 
+        * @return the knowledge type name
+        */
+       public String getKnowledgeType() {
+               // ---------------------------------
+               return type;
+       }
+
+       /**
+        * Get the id of the modified knowledge element.
+        * 
+        * @return the knowledge element id
+        */
+       public String getSelectedKnowledge() {
+               // -------------------------------------
+               return myindex;
+       }
+
+       /**
+        * Set the type of the modified knowledge element.
+        * 
+        * @param type
+        *            the knowledge type name
+        */
+       public void setKnowledgeType(String type) {
+               // ------------------------------------------
+               this.type = type;
+       }
+
+       /**
+        * Set the title of the modified knowledge element.
+        * 
+        * @param title
+        *            the new knowledge title
+        */
+       public void setKnowledgeTitle(String title) {
+               // --------------------------------------------
+               this.title = title;
+       }
+
+       /**
+        * Set the value of the modified knowledge element.
+        * 
+        * @param value
+        *            the knowledge value
+        */
+       public void setKnowledgeValue(String value) {
+               // --------------------------------------------
+               this.value = value;
+       }
 
-       private static final long    serialVersionUID = 4636919137087687068L;
-
-//  ==============================================================================================================================
-//  Action methods
-//  ==============================================================================================================================
-
-    public String doInitialize () {
-//  -----------------------------
-//    Session      connex  = Database.getSession();
-//       Transaction  transax = connex.beginTransaction();
-
-         mystudy = getOpenStudy();
-        
-//    transax.commit();
-      return SUCCESS;
-    }
-
-    public String doSetKnowledge () {
-//  -------------------------------
-      Session      connex  = Database.getSession();
-         Transaction  transax = connex.beginTransaction();
-         try {
-       User user = getConnectedUser();
-               mystudy   = getOpenStudy();
-
-               Step     step  = mystudy.getSelectedStep();
-        Scenario scene = (Scenario)step.getOwner();          // It is necessarily a Scenario
-
-               if (title != null && value != null) {                // Addition of a new Knowledge Element
-                 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
-         KnowledgeElementType        ktype = KnowledgeElement.selectType(Integer.valueOf(type));
-         kprop.setType(ktype)
-                  .setTitle(title)
-                  .setValue(value)
-                  .setAuthor(user);
-          mystudy.add( getScenarioService().addKnowledgeElement(scene, kprop) );
-          getMenu("study").selects(mystudy.getSelection());  // Updates the menu icon, in case of first added document
-               } else
-               if (title != null) {                                 // Renaming of an existing Knowledge Element
-          KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(type));
-          getKnowledgeElementService().rename(kelm, title);
-//        Useless to update the open study
-               } else
-               if (value != null) {                                 // Edition of a knowledge
-          KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(type));
-          kelm.update(value);
-          mystudy.update(kelm);                              // For updating the truncated value
-               }
-        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 doDeleteKnowledge () {
-//  ----------------------------------
-      Session      connex  = Database.getSession();
-      Transaction  transax = connex.beginTransaction();
-      try {
-               mystudy = getOpenStudy();
-               Step     step  = mystudy.getSelectedStep();
-        Scenario scene = (Scenario)step.getOwner();        // It is necessarily a Scenario
-        
-        KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(myindex));
-        scene.removeKnowledgeElement(kelm);                // The knowledge element necessarily exists
-
-        mystudy.remove(kelm);
-        getMenu("study").selects(mystudy.getSelection());  // Updates the menu icon, in case of last removed document
-        
-        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;
-         }
-    }
-
-//  ==============================================================================================================================
-//  Getters and setters
-//  ==============================================================================================================================
-
-    public String getKnowledgeType () {
-//  ---------------------------------
-      return type;
-    }
-    public String getSelectedKnowledge () {
-//  -------------------------------------
-      return myindex;
-    }
-    
-    public void setKnowledgeType (String type) {
-//  ------------------------------------------
-      this.type = type;
-    }
-    public void setKnowledgeTitle (String title) {
-//  --------------------------------------------
-      this.title = title;
-    }
-    public void setKnowledgeValue (String value) {
-//  --------------------------------------------
-      this.value = value;
-    }
        /**
         * Get the scenarioService.
+        * 
         * @return the scenarioService
         */
        public ScenarioService getScenarioService() {
@@ -157,7 +242,9 @@ public class EditKnowledgeElementAction extends DisplayStudyStepAction {
 
        /**
         * Set the scenarioService.
-        * @param scenarioService the scenarioService to set
+        * 
+        * @param scenarioService
+        *            the scenarioService to set
         */
        public void setScenarioService(ScenarioService scenarioService) {
                _scenarioService = scenarioService;
@@ -165,6 +252,7 @@ public class EditKnowledgeElementAction extends DisplayStudyStepAction {
 
        /**
         * Get the knowledgeElementService.
+        * 
         * @return the knowledgeElementService
         */
        public KnowledgeElementService getKnowledgeElementService() {
@@ -173,10 +261,101 @@ public class EditKnowledgeElementAction extends DisplayStudyStepAction {
 
        /**
         * Set the knowledgeElementService.
-        * @param knowledgeElementService the knowledgeElementService to set
+        * 
+        * @param knowledgeElementService
+        *            the knowledgeElementService to set
         */
        public void setKnowledgeElementService(
                        KnowledgeElementService knowledgeElementService) {
                _knowledgeElementService = knowledgeElementService;
        }
+
+       /**
+        * Get the knowledgeElementTypeService.
+        * 
+        * @return the knowledgeElementTypeService
+        */
+       public KnowledgeElementTypeService getKnowledgeElementTypeService() {
+               return _knowledgeElementTypeService;
+       }
+
+       /**
+        * Set the knowledgeElementTypeService.
+        * 
+        * @param knowledgeElementTypeService
+        *            the knowledgeElementTypeService to set
+        */
+       public void setKnowledgeElementTypeService(
+                       KnowledgeElementTypeService knowledgeElementTypeService) {
+               _knowledgeElementTypeService = knowledgeElementTypeService;
+       }
+
+       /**
+        * Get the menuProperty.
+        * 
+        * @return the menuProperty
+        */
+       public String getMenuProperty() {
+               return _menuProperty;
+       }
+
+       /**
+        * Set the menuProperty.
+        * 
+        * @param menuProperty
+        *            the menuProperty to set
+        */
+       public void setMenuProperty(final String menuProperty) {
+               this._menuProperty = menuProperty;
+       }
+       
+       /**
+        * Get the _titleProperty.
+        * @return the _titleProperty
+        */
+       public String getTitleProperty() {
+               return _titleProperty;
+       }
+
+       /**
+        * Set the titleProperty.
+        * @param titleProperty the titleProperty to set
+        */
+       public void setTitleProperty(final String titleProperty) {
+               _titleProperty = titleProperty;
+       }
+
+       /**
+        * Get the editDisabledProperty.
+        * @return the editDisabledProperty
+        */
+       public String getEditDisabledProperty() {
+               return _editDisabledProperty;
+       }
+
+       /**
+        * Set the editDisabledProperty.
+        * @param editDisabledProperty the editDisabledProperty to set
+        */
+       public void setEditDisabledProperty(final String editDisabledProperty) {
+               _editDisabledProperty = editDisabledProperty;
+       }
+
+       /**
+        * Get the toolProperty.
+        * @return the toolProperty
+        */
+       public String getToolProperty() {
+               return _toolProperty;
+       }
+
+       /**
+        * Set the toolProperty.
+        * @param toolProperty the toolProperty to set
+        */
+       public void setToolProperty(final String toolProperty) {
+               _toolProperty = toolProperty;
+       }
+
+       
 }
\ No newline at end of file