Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[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.dal.bo.kernel.User;
7 import org.splat.dal.dao.som.Database;
8 import org.splat.dal.bo.som.KnowledgeElement;
9 import org.splat.dal.bo.som.KnowledgeElementType;
10 import org.splat.dal.bo.som.Scenario;
11 import org.splat.service.KnowledgeElementService;
12 import org.splat.service.ScenarioService;
13 import org.splat.som.Step;
14
15
16 public class EditKnowledgeElementAction extends DisplayStudyStepAction {
17
18         private String                      type      = null;     // Edited knowledge type
19         private String                      title     = null;
20         private String                      value     = null;
21         private ScenarioService _scenarioService;
22         private KnowledgeElementService _knowledgeElementService;
23
24         private static final long    serialVersionUID = 4636919137087687068L;
25
26 //  ==============================================================================================================================
27 //  Action methods
28 //  ==============================================================================================================================
29
30     public String doInitialize () {
31 //  -----------------------------
32 //    Session      connex  = Database.getSession();
33 //        Transaction  transax = connex.beginTransaction();
34
35           mystudy = getOpenStudy();
36         
37 //    transax.commit();
38       return SUCCESS;
39     }
40
41     public String doSetKnowledge () {
42 //  -------------------------------
43       Session      connex  = Database.getSession();
44           Transaction  transax = connex.beginTransaction();
45           try {
46         User user = getConnectedUser();
47                 mystudy   = getOpenStudy();
48
49         Step     step  = mystudy.getSelectedStep();
50         Scenario scene = (Scenario)step.getOwner();          // It is necessarily a Scenario
51
52         if (title != null && value != null) {                // Addition of a new Knowledge Element
53           KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
54           KnowledgeElementType        ktype = KnowledgeElement.selectType(Integer.valueOf(type));
55           kprop.setType(ktype)
56                    .setTitle(title)
57                    .setValue(value)
58                    .setAuthor(user);
59           mystudy.add( getScenarioService().addKnowledgeElement(scene, kprop) );
60           getMenu("study").selects(mystudy.getSelection());  // Updates the menu icon, in case of first added document
61         } else
62         if (title != null) {                                 // Renaming of an existing Knowledge Element
63           KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(type));
64           getKnowledgeElementService().rename(kelm, title);
65 //        Useless to update the open study
66         } else
67         if (value != null) {                                 // Edition of a knowledge
68           KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(type));
69           kelm.update(value);
70           mystudy.update(kelm);                              // For updating the truncated value
71         }
72         transax.commit();
73         return SUCCESS;
74           }
75       catch (RuntimeException saverror) {
76         logger.error("Reason:", saverror);
77         if (transax != null && transax.isActive()) {
78 //        Second try-catch as the rollback could fail as well
79           try {
80                 transax.rollback();
81           } catch (HibernateException backerror) {
82             logger.debug("Error rolling back transaction", backerror);
83           }
84         }
85         return ERROR;
86           }
87       catch (Exception error) {
88         transax.commit();
89         return INPUT;
90       }
91     }
92
93     public String doDeleteKnowledge () {
94 //  ----------------------------------
95       Session      connex  = Database.getSession();
96       Transaction  transax = connex.beginTransaction();
97       try {
98                 mystudy = getOpenStudy();
99         Step     step  = mystudy.getSelectedStep();
100         Scenario scene = (Scenario)step.getOwner();        // It is necessarily a Scenario
101         
102         KnowledgeElement kelm = scene.getKnowledgeElement(Integer.valueOf(myindex));
103         scene.removeKnowledgeElement(kelm);                // The knowledge element necessarily exists
104
105         mystudy.remove(kelm);
106         getMenu("study").selects(mystudy.getSelection());  // Updates the menu icon, in case of last removed document
107         
108         transax.commit();
109         return SUCCESS;
110           }
111       catch (RuntimeException saverror) {
112         logger.error("Reason:", saverror);
113         if (transax != null && transax.isActive()) {
114 //        Second try-catch as the rollback could fail as well
115           try {
116                 transax.rollback();
117           } catch (HibernateException backerror) {
118             logger.debug("Error rolling back transaction", backerror);
119           }
120         }
121         return ERROR;
122           }
123     }
124
125 //  ==============================================================================================================================
126 //  Getters and setters
127 //  ==============================================================================================================================
128
129     public String getKnowledgeType () {
130 //  ---------------------------------
131       return type;
132     }
133     public String getSelectedKnowledge () {
134 //  -------------------------------------
135       return myindex;
136     }
137     
138     public void setKnowledgeType (String type) {
139 //  ------------------------------------------
140       this.type = type;
141     }
142     public void setKnowledgeTitle (String title) {
143 //  --------------------------------------------
144       this.title = title;
145     }
146     public void setKnowledgeValue (String value) {
147 //  --------------------------------------------
148       this.value = value;
149     }
150         /**
151          * Get the scenarioService.
152          * @return the scenarioService
153          */
154         public ScenarioService getScenarioService() {
155                 return _scenarioService;
156         }
157
158         /**
159          * Set the scenarioService.
160          * @param scenarioService the scenarioService to set
161          */
162         public void setScenarioService(ScenarioService scenarioService) {
163                 _scenarioService = scenarioService;
164         }
165
166         /**
167          * Get the knowledgeElementService.
168          * @return the knowledgeElementService
169          */
170         public KnowledgeElementService getKnowledgeElementService() {
171                 return _knowledgeElementService;
172         }
173
174         /**
175          * Set the knowledgeElementService.
176          * @param knowledgeElementService the knowledgeElementService to set
177          */
178         public void setKnowledgeElementService(
179                         KnowledgeElementService knowledgeElementService) {
180                 _knowledgeElementService = knowledgeElementService;
181         }
182 }