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;
+
+ // ==============================================================================================================================
+ // Action methods
+ // ==============================================================================================================================
/**
- * Serial version ID.
+ * Initialize the action. Get selected study from the session.
+ *
+ * @return SUCCESS if no exceptions
*/
- 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 String doInitialize() {
+ mystudy = getOpenStudy();
+ 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() {
+ 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) {
+ 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));
+ 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
+
+ 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;
+ }
+
/**
* Get the scenarioService.
+ *
* @return the scenarioService
*/
public ScenarioService getScenarioService() {
/**
* Set the scenarioService.
- * @param scenarioService the scenarioService to set
+ *
+ * @param scenarioService
+ * the scenarioService to set
*/
public void setScenarioService(ScenarioService scenarioService) {
_scenarioService = scenarioService;
/**
* Get the knowledgeElementService.
+ *
* @return the knowledgeElementService
*/
public KnowledgeElementService getKnowledgeElementService() {
/**
* 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;
+ }
}
\ No newline at end of file