Salome HOME
Wait takes now into account of name of the var.
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_Transaction.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
21 #include "SALOMESDS_Transaction.hxx"
22 #include "SALOMESDS_Exception.hxx"
23 #include "SALOMESDS_PickelizedPyObjServer.hxx"
24 #include "SALOMESDS_PickelizedPyObjRdWrServer.hxx"
25 #include "SALOMESDS_PickelizedPyObjRdExtServer.hxx"
26
27 #include <sstream>
28
29 using namespace SALOMESDS;
30
31 void Transaction::FromByteSeqToVB(const SALOME::ByteVec& bsToBeConv, std::vector<unsigned char>& ret)
32 {
33   std::size_t sz(bsToBeConv.length());
34   ret.resize(sz);
35   unsigned char *buf(const_cast<unsigned char *>(&ret[0]));
36   for(std::size_t i=0;i<sz;i++)
37     buf[i]=bsToBeConv[i];
38 }
39
40 void Transaction::FromVBToByteSeq(const std::vector<unsigned char>& bsToBeConv, SALOME::ByteVec& ret)
41 {
42   std::size_t sz(bsToBeConv.size());
43   ret.length(sz);
44   for(std::size_t i=0;i<sz;i++)
45     ret[i]=bsToBeConv[i];
46 }
47
48 Transaction::~Transaction()
49 {
50 }
51
52 TransactionVarCreate::TransactionVarCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue):Transaction(dsct,varName)
53 {
54   FromByteSeqToVB(constValue,_data);
55 }
56
57 void TransactionVarCreate::prepareRollBackInCaseOfFailure()
58 {//nothing it is not a bug
59   checkNotAlreadyExisting();
60 }
61
62 void TransactionVarCreate::rollBack()
63 {
64   if(_dsct->existVar(_var_name.c_str()))
65     _dsct->deleteVar(_var_name.c_str());
66 }
67
68 /*!
69  * no implementation it is not a bug !
70  */
71 void TransactionVarCreate::notify()
72 {
73 }
74
75 void TransactionRdOnlyVarCreate::perform()
76 {
77   SALOME::ByteVec data2;
78   FromVBToByteSeq(_data,data2);
79   _dsct->createRdOnlyVarInternal(_var_name,data2);
80 }
81
82 void TransactionRdExtVarCreate::perform()
83 {
84   SALOME::ByteVec data2;
85   FromVBToByteSeq(_data,data2);
86   _dsct->createRdExtVarInternal(_var_name,data2);
87 }
88
89 void TransactionRdWrVarCreate::perform()
90 {
91   SALOME::ByteVec data2;
92   FromVBToByteSeq(_data,data2);
93   _dsct->createRdWrVarInternal(_var_name,data2);
94 }
95
96 TransactionDictModify::TransactionDictModify(DataScopeServerTransaction *dsct, const std::string& varName):Transaction(dsct,varName),_varc(0)
97 {
98   _varc=checkVarExistingAndDict();
99 }
100
101 void TransactionDictModify::prepareRollBackInCaseOfFailure()
102 {
103   _zeDataBefore.clear();
104   PyObject *zeDictPy(_varc->getPyObj());
105   Py_XINCREF(zeDictPy);
106   _zeDataBefore=_varc->pickelize(zeDictPy);
107 }
108
109 void TransactionDictModify::rollBack()
110 {
111   PyObject *obj(_varc->getPyObjFromPickled(_zeDataBefore));
112   _varc->setNewPyObj(obj);
113   _zeDataBefore.clear();
114 }
115
116 TransactionAddKeyValue::TransactionAddKeyValue(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):TransactionDictModify(dsct,varName)
117 {
118   std::vector<unsigned char> key2,value2;
119   FromByteSeqToVB(key,key2);
120   FromByteSeqToVB(value,value2);
121   _key=PickelizedPyObjServer::GetPyObjFromPickled(key2,_dsct);
122   _value=PickelizedPyObjServer::GetPyObjFromPickled(value2,_dsct);
123 }
124
125 void TransactionAddKeyValue::prepareRollBackInCaseOfFailure()
126 {
127   TransactionDictModify::prepareRollBackInCaseOfFailure();
128   _dsct->pingKey(_key);// check that key is OK with all waiting keys
129 }
130
131 void TransactionAddKeyValue::notify()
132 {
133   _dsct->notifyKey(getVarName(),_key,_value);
134 }
135
136 TransactionAddKeyValue::~TransactionAddKeyValue()
137 {
138   Py_XDECREF(_key);
139   Py_XDECREF(_value);
140 }
141
142 TransactionAddKeyValueHard::TransactionAddKeyValueHard(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):TransactionAddKeyValue(dsct,varName,key,value)
143 {
144 }
145
146 void TransactionAddKeyValueHard::perform()
147 {
148   _varc->addKeyValueHard(_key,_value);
149 }
150
151 TransactionAddKeyValueErrorIfAlreadyExisting::TransactionAddKeyValueErrorIfAlreadyExisting(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):TransactionAddKeyValue(dsct,varName,key,value)
152 {
153   _varc->checkKeyNotAlreadyPresent(_key);
154 }
155
156 void TransactionAddKeyValueErrorIfAlreadyExisting::perform()
157 {
158   _varc->addKeyValueErrorIfAlreadyExisting(_key,_value);
159 }
160
161 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key):TransactionDictModify(dsct,varName),_key(0)
162 {
163   std::vector<unsigned char> key2;
164   FromByteSeqToVB(key,key2);
165   _key=PickelizedPyObjServer::GetPyObjFromPickled(key2,_dsct);
166 }
167
168 void TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::perform()
169 {
170   _varc->removeKeyInVarErrorIfNotAlreadyExisting(_key);
171 }
172
173 /*!
174  * no implementation it is not a bug !
175  */
176 void TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::notify()
177 {
178 }
179
180 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::~TransactionRemoveKeyInVarErrorIfNotAlreadyExisting()
181 {
182   Py_XDECREF(_key);
183 }
184
185 TransactionMorphRdWrIntoRdOnly::TransactionMorphRdWrIntoRdOnly(DataScopeServerTransaction *dsct, const std::string& varName):Transaction(dsct,varName)
186 {
187 }
188
189 SALOME::PickelizedPyObjRdWrServer_ptr TransactionMorphRdWrIntoRdOnly::getVar()
190 {
191   SALOME::BasicDataServer_var obj(_dsct->retrieveVarInternal(_var_name.c_str()));
192   SALOME::PickelizedPyObjRdWrServer_ptr ret(SALOME::PickelizedPyObjRdWrServer::_narrow(obj));
193   if(CORBA::is_nil(ret))
194     {
195       std::ostringstream oss; oss << "TransactionMorphRdWrIntoRdOnly::getVar : var \"" << _var_name << "\" has not expected PickelizedPyObjRdWrServer type !";
196       throw Exception(oss.str());
197     }
198   return ret;
199 }
200
201 void TransactionMorphRdWrIntoRdOnly::prepareRollBackInCaseOfFailure()
202 {
203   BasicDataServer *var(_dsct->retrieveVarInternal2(_var_name));
204   if(!var)
205     throw Exception("TransactionMorphRdWrIntoRdOnly::prepareRollBackInCaseOfFailure : Returned var is NULL !");
206   PickelizedPyObjRdWrServer *varc(dynamic_cast<PickelizedPyObjRdWrServer *>(var));
207   if(!varc)
208     throw Exception("TransactionMorphRdWrIntoRdOnly::prepareRollBackInCaseOfFailure : Returned var has not expected type !");
209 }
210
211 void TransactionMorphRdWrIntoRdOnly::perform()
212 {
213   _dsct->moveStatusOfVarFromRdWrToRdOnly(_var_name);
214 }
215
216 void TransactionMorphRdWrIntoRdOnly::rollBack()
217 {
218   _dsct->moveStatusOfVarFromRdOnlyToRdWr(_var_name);
219 }
220
221 /*!
222  * no implementation it is not a bug !
223  */
224 void TransactionMorphRdWrIntoRdOnly::notify()
225 {
226 }