Salome HOME
[EDF17719] : When comparison fails do not loose the content of data
[modules/yacs.git] / src / SALOMESDS / SALOMESDS_Transaction.cxx
index 79eab952f8dc312806de7ba0708ce237cd59094c..cff92a346264c47ffee6b94cc441ec79c25f9cae 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -56,7 +56,7 @@ TransactionVarCreate::TransactionVarCreate(DataScopeServerTransaction *dsct, con
 }
 
 void TransactionVarCreate::prepareRollBackInCaseOfFailure()
-{//nothing it is not a bug
+{
   checkNotAlreadyExisting();
 }
 
@@ -87,6 +87,55 @@ void TransactionRdExtVarCreate::perform()
   _dsct->createRdExtVarInternal(_var_name,data2);
 }
 
+TransactionRdExtVarFreeStyleCreate::TransactionRdExtVarFreeStyleCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue, const char *compareFuncContent):_cmp_func_content(compareFuncContent),_cmp_func(nullptr),TransactionRdExtVarCreate(dsct,varName,constValue)
+{
+  constexpr char EXPECTED_COMPARE_FUNC_NAME[]="comptchev";
+  SALOME::AutoPyRef context(PyDict_New());
+  SALOME::AutoPyRef res(PyRun_String(compareFuncContent,Py_file_input,_dsct->getGlobals(),context));
+  if(res.isNull())
+    {
+      std::ostringstream oss; oss << "TransactionRdExtVarFreeStyleCreate ctor : Fail to parse and evaluate \"" << compareFuncContent << "\" as python function !";
+      throw Exception(oss.str());
+    }
+  PyObject *func(PyDict_GetItemString(context,EXPECTED_COMPARE_FUNC_NAME));//borrowed
+  if(!func)
+    {
+      std::ostringstream oss; oss << "TransactionRdExtVarFreeStyleCreate ctor : Parsing of func is OK but not func called \"" << EXPECTED_COMPARE_FUNC_NAME << "\" is the given parsed string !";
+      throw Exception(oss.str());
+    }
+  _cmp_func = func; Py_XINCREF(func);
+  if(PyDict_DelItemString(context,EXPECTED_COMPARE_FUNC_NAME)!=0)
+    {
+      std::ostringstream oss; oss << "TransactionRdExtVarFreeStyleCreate ctor : Internal error during suppression of \"" << EXPECTED_COMPARE_FUNC_NAME << "\" key that exepect to be present ! Smells bad !";
+      throw Exception(oss.str());
+    }
+}
+
+void TransactionRdExtVarFreeStyleCreate::prepareRollBackInCaseOfFailure()
+{//nothing it is not a bug
+}
+
+void TransactionRdExtVarFreeStyleCreate::rollBack()
+{// nothing to be done here. Fress style means I don t care. Do not remove var.
+  if(!_null_rollback)
+    TransactionRdExtVarCreate::rollBack();
+}
+
+void TransactionRdExtVarFreeStyleCreate::perform()
+{
+  SALOME::ByteVec data2;
+  FromVBToByteSeq(_data,data2);
+  try
+    {
+      _dsct->createRdExtVarFreeStyleInternal(_var_name,data2,std::move(_cmp_func_content),std::move(_cmp_func));
+    }
+  catch(NotSameException& e)
+    {
+      _null_rollback = true;
+      throw e;
+    }
+}
+
 void TransactionRdExtInitVarCreate::perform()
 {
   SALOME::ByteVec data2;
@@ -276,11 +325,12 @@ void TransactionMultiKeyAddSession::addKeyValueInVarErrorIfAlreadyExistingNow(co
   _dsct->checkVarExistingAndDict(_var_name);
   TransactionAddKeyValueErrorIfAlreadyExisting ret(_dsct,_var_name,key,value);
   {
-    bool mustRollback(true);
+    ret.perform();
+    /*bool mustRollback(true);
     TrustTransaction t;
     t.setTransaction(&ret,&mustRollback);
     t.operate();
-    mustRollback=false;//important let this line to notify t that everything was OK
+    mustRollback=false;//important let this line to notify t that everything was OK*/
   }
   ret.notify();
 }