Salome HOME
Fix of wrong search results after logout.
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditKnowledgeElementAction.java
index 38f2f1741a9967765fb941c996a94f19d0da7077..6d488e7a9f6db45d1ebeaa1aa6e6695af325688c 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.service.dto.KnowledgeElementDTO;
 import org.splat.som.Step;
+import org.splat.wapp.Constants;
 
-
+/**
+ * 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 _knowledgeType = null; // Edited knowledge type
+       /**
+        * Edited knowledge element title.
+        */
+       private String _knowledgeTitle = null;
+       /**
+        * Edited knowledge element value.
+        */
+       private String _knowledgeValue = null;
+       /**
+        * Injected scenario service.
+        */
        private ScenarioService _scenarioService;
+       /**
+        * Injected knowledge element service.
+        */
        private KnowledgeElementService _knowledgeElementService;
+       /**
+        * Injected knowledge element type service.
+        */
+       private KnowledgeElementTypeService _knowledgeElementTypeService;
+
+       // ==============================================================================================================================
+       // Action methods
+       // ==============================================================================================================================
 
        /**
-        * Serial version ID.
+        * Initialize the action. Get selected study from the session.
+        * 
+        * @return SUCCESS if no exceptions
+        */
+       public String doInitialize() {
+               _openStudy = getOpenStudy();
+               setMenu();
+               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() {
+
+               String res = SUCCESS;
+               setMenu();
+
+               try {
+                       _openStudy = getOpenStudy();
+
+                       if ((_knowledgeTitle == null) || (_knowledgeValue == null)) {
+                               KnowledgeElementDTO kelm = new KnowledgeElementDTO(Integer
+                                               .valueOf(_knowledgeType), _knowledgeTitle,
+                                               _knowledgeValue);
+                               if (_knowledgeValue == null) { // Renaming of an existing Knowledge Element
+                                       getKnowledgeElementService().rename(kelm, _knowledgeTitle);
+                               } else { // Edition of a knowledge
+                                       getKnowledgeElementService().update(kelm, _knowledgeValue);
+                               }
+                               _openStudy.update(kelm); // For updating the truncated value
+                       } else { // Addition of a new Knowledge Element
+                               KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
+                               KnowledgeElementType ktype = getKnowledgeElementTypeService()
+                                               .selectType(Integer.valueOf(_knowledgeType));
+                               kprop.setType(ktype).setTitle(_knowledgeTitle).setValue(
+                                               _knowledgeValue).setAuthor(getConnectedUser());
+                               _openStudy.add(getScenarioService().addKnowledgeElement(
+                                               (Scenario) _openStudy.getSelectedStep().getOwner(),
+                                               kprop));
+                               getMenu(Constants.STUDY_MENU)
+                                               .selects(_openStudy.getSelection()); // Updates the menu icon, in case of first added document
+                       }
+               } catch (RuntimeException saverror) {
+                       LOG.error("Reason:", saverror);
+                       res = ERROR;
+               } catch (Exception error) {
+                       LOG.error("Exception while saving a knowledge: ", error);
+                       res = INPUT;
+               }
+               return res;
+       }
+
+       /**
+        * Delete a knowledge element from the current scenario.
+        * 
+        * @return SUCCESS if no exceptions
+        */
+       public String doDeleteKnowledge() {
+               _openStudy = getOpenStudy();
+               Step step = _openStudy.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
+
+               _openStudy.remove(kelm);
+               updateMenu();
+
+               return SUCCESS;
+       }
+
+       /**
+        * Promote a knowledge element from the current scenario.
+        * 
+        * @return SUCCESS if no exceptions
+        */
+       public String doPromoteKnowledge() {
+               getKnowledgeElementService().promote(getKnowledgeElement()); // The knowledge element necessarily exists
+               updateMenu();
+               return SUCCESS;
+       }
+
+       /**
+        * Demote a knowledge element from the current scenario.
+        * 
+        * @return SUCCESS if no exceptions
+        */
+       public String doDemoteKnowledge() {
+               getKnowledgeElementService().demote(getKnowledgeElement()); // The knowledge element necessarily exists
+               updateMenu();
+               return SUCCESS;
+       }
+
+       /**
+        * Get selected knowledge element.
+        * 
+        * @return the selected knowledge element
+        */
+       private KnowledgeElement getKnowledgeElement() {
+               _openStudy = getOpenStudy();
+               Step step = _openStudy.getSelectedStep();
+               Scenario scene = (Scenario) step.getOwner(); // It is necessarily a Scenario
+               return scene.getKnowledgeElement(Integer.valueOf(_myindex));
+       }
+
+       /**
+        * Update current menu.
+        */
+       private void updateMenu() {
+               getMenu(Constants.STUDY_MENU).selects(_openStudy.getSelection()); // Updates the menu icon, in case of last removed document
+               setMenu();
+       }
+
+       // ==============================================================================================================================
+       // Getters and setters
+       // ==============================================================================================================================
+
+       /**
+        * Get the type of the modified knowledge element.
+        * 
+        * @return the knowledge type name
+        */
+       public String getKnowledgeType() {
+               return _knowledgeType;
+       }
+
+       /**
+        * 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(final String type) {
+               this._knowledgeType = type;
+       }
+
+       /**
+        * Set the title of the modified knowledge element.
+        * 
+        * @param title
+        *            the new knowledge title
         */
-       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;
-    }
+       public void setKnowledgeTitle(final String title) {
+               this._knowledgeTitle = title;
+       }
+
+       /**
+        * Set the value of the modified knowledge element.
+        * 
+        * @param value
+        *            the knowledge value
+        */
+       public void setKnowledgeValue(final String value) {
+               this._knowledgeValue = value;
+       }
+
        /**
         * Get the scenarioService.
+        * 
         * @return the scenarioService
         */
        public ScenarioService getScenarioService() {
@@ -160,14 +230,17 @@ 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) {
+       public void setScenarioService(final ScenarioService scenarioService) {
                _scenarioService = scenarioService;
        }
 
        /**
         * Get the knowledgeElementService.
+        * 
         * @return the knowledgeElementService
         */
        public KnowledgeElementService getKnowledgeElementService() {
@@ -176,10 +249,50 @@ 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) {
+                       final 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(
+                       final KnowledgeElementTypeService knowledgeElementTypeService) {
+               _knowledgeElementTypeService = knowledgeElementTypeService;
+       }
+
+       /**
+        * Get the knowledgeValue.
+        * 
+        * @return the knowledgeValue
+        */
+       public String getKnowledgeValue() {
+               return _knowledgeValue;
+       }
+
+       /**
+        * Get the knowledgeTitle.
+        * 
+        * @return the knowledgeTitle
+        */
+       public String getKnowledgeTitle() {
+               return _knowledgeTitle;
+       }
 }
\ No newline at end of file