Salome HOME
88069117b5b709d6a8444cf9ff103e583e3bdb30
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / EditKnowledgeElementAction.java
1 package org.splat.simer;
2
3 import org.hibernate.HibernateException;
4 import org.hibernate.Session;
5 import org.hibernate.Transaction;
6 import org.splat.kernel.User;
7 import org.splat.som.Database;
8 import org.splat.som.KnowledgeElement;
9 import org.splat.som.KnowledgeElementType;
10 import org.splat.som.Scenario;
11 import org.splat.som.Step;
12
13
14 public class EditKnowledgeElementAction extends DisplayStudyStepAction {
15
16         private String                      type      = null;     // Edited knowledge type
17         private String                      title     = null;
18         private String                      value     = null;
19
20         private static final long    serialVersionUID = 4636919137087687068L;
21
22 //  ==============================================================================================================================
23 //  Action methods
24 //  ==============================================================================================================================
25
26     public String doInitialize () {
27 //  -----------------------------
28 //    Session      connex  = Database.getSession();
29 //        Transaction  transax = connex.beginTransaction();
30
31           mystudy = getOpenStudy();
32         
33 //    transax.commit();
34       return SUCCESS;
35     }
36
37     public String doSetKnowledge () {
38 //  -------------------------------
39       Session      connex  = Database.getSession();
40           Transaction  transax = connex.beginTransaction();
41           try {
42         User user = getConnectedUser();
43                 mystudy   = getOpenStudy();
44
45         Step     step  = mystudy.getSelectedStep();
46         Scenario scene = (Scenario)step.getOwner();          // It is necessarily a Scenario
47
48         if (title != null && value != null) {                // Addition of a new Knowledge Element
49           KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
50           KnowledgeElementType        ktype = KnowledgeElement.selectType(Integer.valueOf(type));
51           kprop.setType(ktype)
52                    .setTitle(title)
53                    .setValue(value)
54                    .setAuthor(user);
55           mystudy.add( scene.addKnowledgeElement(kprop) );
56           getMenu("study").selects(mystudy.getSelection());  // Updates the menu icon, in case of first added document
57         } else
58         if (title != null) {                                 // Renaming of an existing Knowledge Element
59           KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(type));
60           kelm.rename(title);
61 //        Useless to update the open study
62         } else
63         if (value != null) {                                 // Edition of a knowledge
64           KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(type));
65           kelm.update(value);
66           mystudy.update(kelm);                              // For updating the truncated value
67         }
68         transax.commit();
69         return SUCCESS;
70           }
71       catch (RuntimeException saverror) {
72         logger.error("Reason:", saverror);
73         if (transax != null && transax.isActive()) {
74 //        Second try-catch as the rollback could fail as well
75           try {
76                 transax.rollback();
77           } catch (HibernateException backerror) {
78             logger.debug("Error rolling back transaction", backerror);
79           }
80         }
81         return ERROR;
82           }
83       catch (Exception error) {
84         transax.commit();
85         return INPUT;
86       }
87     }
88
89     public String doDeleteKnowledge () {
90 //  ----------------------------------
91       Session      connex  = Database.getSession();
92       Transaction  transax = connex.beginTransaction();
93       try {
94                 mystudy = getOpenStudy();
95         Step     step  = mystudy.getSelectedStep();
96         Scenario scene = (Scenario)step.getOwner();        // It is necessarily a Scenario
97         
98         KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(myindex));
99         scene.removeKnowledgeElement(kelm);                // The knowledge element necessarily exists
100
101         mystudy.remove(kelm);
102         getMenu("study").selects(mystudy.getSelection());  // Updates the menu icon, in case of last removed document
103         
104         transax.commit();
105         return SUCCESS;
106           }
107       catch (RuntimeException saverror) {
108         logger.error("Reason:", saverror);
109         if (transax != null && transax.isActive()) {
110 //        Second try-catch as the rollback could fail as well
111           try {
112                 transax.rollback();
113           } catch (HibernateException backerror) {
114             logger.debug("Error rolling back transaction", backerror);
115           }
116         }
117         return ERROR;
118           }
119     }
120
121 //  ==============================================================================================================================
122 //  Getters and setters
123 //  ==============================================================================================================================
124
125     public String getKnowledgeType () {
126 //  ---------------------------------
127       return type;
128     }
129     public String getSelectedKnowledge () {
130 //  -------------------------------------
131       return myindex;
132     }
133     
134     public void setKnowledgeType (String type) {
135 //  ------------------------------------------
136       this.type = type;
137     }
138     public void setKnowledgeTitle (String title) {
139 //  --------------------------------------------
140       this.title = title;
141     }
142     public void setKnowledgeValue (String value) {
143 //  --------------------------------------------
144       this.value = value;
145     }
146 }