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