+ /**
+ * 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;
+ }