Salome HOME
Revert "Merge remote-tracking branch 'origin/master'"
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
index ae4a1f0768fa1f9cebb1f5044b27831fea7bcdbb..d0b936acc08a635d67697e4f9b2a857f180ef0fc 100644 (file)
-using namespace std;
-//  File      : SALOMEDS_Study_i.cxx
-//  Created   : Wed Nov 28 16:27:23 2001
-//  Author    : Yves FRICAUD
-
-//  Project   : SALOME
-//  Module    : SALOMEDS
-//  Copyright : Open CASCADE 2001
-//  $Header$
-
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+//  File   : SALOMEDS_Study_i.cxx
+//  Author : Sergey RUIN
+//  Module : SALOME
+//
 #include "utilities.h"
+#include <sstream>
 #include "SALOMEDS_Study_i.hxx"
-
-#include "SALOMEDS_DataMapIteratorOfDataMapStringLabel.hxx"
-#include <TColStd_SequenceOfExtendedString.hxx>
-#include <TCollection_AsciiString.hxx>
-#include <TCollection_ExtendedString.hxx>
-#include <TDataStd_ChildNodeIterator.hxx>
-#include <TDocStd_Application.hxx>
-#include <TDocStd_Owner.hxx>
-#include <CDM_Document.hxx>
-#include <CDM_Application.hxx>
-#include "SALOMEDS_LocalIDAttribute.hxx"
+#include "SALOMEDS_StudyManager_i.hxx"
 #include "SALOMEDS_UseCaseIterator_i.hxx"
+#include "SALOMEDS_GenericAttribute_i.hxx"
+#include "SALOMEDS_AttributeStudyProperties_i.hxx"
+#include "SALOMEDS_AttributeParameter_i.hxx"
+#include "SALOMEDS_ChildIterator_i.hxx"
+#include "SALOMEDS_Driver_i.hxx"
+#include "SALOMEDS.hxx"
+
+#include "SALOMEDSImpl_SObject.hxx"
+#include "SALOMEDSImpl_SComponent.hxx"
+#include "SALOMEDSImpl_UseCaseBuilder.hxx"
+#include "SALOMEDSImpl_AttributeStudyProperties.hxx"
+#include "SALOMEDSImpl_AttributeParameter.hxx"
+#include "SALOMEDSImpl_ChildIterator.hxx"
+#include "SALOMEDSImpl_IParameters.hxx"
+#include "SALOMEDSImpl_Callback.hxx"
+
+#include "DF_Label.hxx"
+#include "DF_Attribute.hxx"
+
+#include "Basics_Utils.hxx"
+#include "SALOME_KernelServices.hxx"
+
+#ifdef WIN32
+#include <process.h>
+#else
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+namespace SALOMEDS
+{
+  class Notifier: public SALOMEDSImpl_AbstractCallback
+  {
+  public:
+    Notifier(CORBA::ORB_ptr orb)
+    {
+      _orb = CORBA::ORB::_duplicate(orb);
+    }
+
+    //============================================================================
+    /*! Function : addSO_Notification
+     *  Purpose  : This function tells all the observers that a SO has been added
+     */
+    //============================================================================
+
+    virtual bool addSO_Notification(const SALOMEDSImpl_SObject& theSObject)
+    {
+      std::string anID=theSObject.GetID();
+      const char* cID=anID.c_str();
+      for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
+      {
+        it->first->notifyObserverID(cID,1);
+      }
+      return true; // NGE return always true but can be modified if needed
+    }
+
+    //============================================================================
+    /*! Function : removeSO_Notification
+     *  Purpose  : This function tells all the observers that a SO has been removed
+     */
+    //============================================================================
+
+    virtual bool removeSO_Notification(const SALOMEDSImpl_SObject& theSObject)
+    {
+      std::string anID=theSObject.GetID();
+      const char* cID=anID.c_str();
+      for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
+      {
+        it->first->notifyObserverID(cID,2);
+      }
+      return true; // NGE return always true but can be modified if needed
+    }
+
+    //============================================================================
+    /*! Function : modifySO_Notification
+     *  Purpose  : This function tells all the observers that a SO has been modified
+     */
+    //============================================================================
+
+    virtual bool modifySO_Notification(const SALOMEDSImpl_SObject& theSObject, int reason)
+    {
+      for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
+      {
+        if(it->second)
+        {
+          std::string anID=theSObject.GetID();
+          const char* cID=anID.c_str();
+          it->first->notifyObserverID(cID,reason);
+        }
+      }
+      return true; // NGE return always true but can be modified if needed
+    }
+
+    //============================================================================
+    /*! Function : modifyNB_Notification
+     *  Purpose  : This function tells all the observers that 
+     *             a NoteBook variable has been added/modified/removed.
+     */
+    //============================================================================
+    
+    virtual bool modifyNB_Notification(const char* theVarName)
+    {
+      for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
+        {
+          it->first->notifyObserverID(theVarName,6);
+        }
+      return true; // NGE return always true but can be modified if needed
+    }
+
+    //============================================================================
+    /*! Function : attach
+     *  Purpose  : register an Observer
+     */
+    //============================================================================
+
+    virtual void attach(SALOMEDS::Observer_ptr theObs, bool modify)
+    {
+      myObservers.push_back(std::make_pair(SALOMEDS::Observer::_duplicate(theObs),modify));
+    }
 
+    //============================================================================
+    /*! Function : detach
+     *  Purpose  : unregister an Observer
+     */
+    //============================================================================
 
-#define DIRECTORYID "DIRECTORY:"
-#define FILEID "FILE: "
+    virtual void detach(SALOMEDS::Observer_ptr theObs)
+    {
+      for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
+      {
+       if ( it->first->_is_equivalent(theObs) ) {
+         myObservers.erase( it );
+         break;
+       }
+      }
+    }
+
+  private:
+    typedef std::list< std::pair< SALOMEDS::Observer_var, bool > > ObsList;
+    typedef ObsList::iterator ObsListIter;
+    ObsList myObservers;
+    CORBA::ORB_var                    _orb;
+  };
+
+  class GenObjRegister: public SALOMEDSImpl_AbstractCallback
+  {
+  public:
+    GenObjRegister(CORBA::ORB_ptr orb)
+    {
+      _orb = CORBA::ORB::_duplicate(orb);
+    }
+    virtual void RegisterGenObj  (const std::string& theIOR)
+    {
+      try
+      {
+        CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
+        if ( obj->_non_existent() ) return;
+        SALOME::GenericObj_var gobj = SALOME::GenericObj::_narrow(obj);
+        if(! CORBA::is_nil(gobj) )
+        {
+          gobj->Register();
+        }
+      }
+      catch(const CORBA::Exception& e)
+      {
+      }
+    }
+    virtual void UnRegisterGenObj(const std::string& theIOR)
+    {
+      try
+      {
+        CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
+        if ( obj->_non_existent() ) return;
+        SALOME::GenericObj_var gobj = SALOME::GenericObj::_narrow(obj);
+        if(! CORBA::is_nil(gobj) )
+        {
+          gobj->UnRegister();
+        }
+      }
+      catch(const CORBA::Exception& e)
+      {
+      }
+    }
+
+  private:
+    CORBA::ORB_var _orb;
+  };
+
+} // namespace SALOMEDS
+
+std::map<SALOMEDSImpl_Study* , SALOMEDS_Study_i*> SALOMEDS_Study_i::_mapOfStudies;
 
 //============================================================================
 /*! Function : SALOMEDS_Study_i
  *  Purpose  : SALOMEDS_Study_i constructor
  */
 //============================================================================
-SALOMEDS_Study_i::SALOMEDS_Study_i(const Handle(TDocStd_Document) doc,
-                                  CORBA::ORB_ptr                 orb,
-                                  const char* study_name)
+SALOMEDS_Study_i::SALOMEDS_Study_i(SALOMEDSImpl_Study* theImpl,
+                                   CORBA::ORB_ptr      orb)
 {
-  _orb = CORBA::ORB::_duplicate(orb);
-  _doc = doc;
-  _name = new char[strlen(study_name) +1];
-  strcpy(_name,study_name);
-  _isSaved = false ;
-  _URL = NULL;
-  _StudyId = -1;
-  _autoFill = true;
+  _orb            = CORBA::ORB::_duplicate(orb);
+  _impl           = theImpl;
+  _builder        = new SALOMEDS_StudyBuilder_i(_impl->NewBuilder(), _orb);  
+  _notifier       = new SALOMEDS::Notifier(_orb);
+  _genObjRegister = new SALOMEDS::GenObjRegister(_orb);
+  _closed         = false;
+
+  theImpl->setNotifier(_notifier);
+  theImpl->setGenObjRegister( _genObjRegister );
+
+  // Notify GUI that study was created
+  SALOME_NamingService *aNamingService = KERNEL::getNamingService();
+  CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
+  SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
+  if ( !CORBA::is_nil(aSession) ) {
+    std::stringstream ss;
+    ss << "studyCreated:" << theImpl->StudyId();
+    std::string str = ss.str();
+    SALOMEDS::unlock();
+    aSession->emitMessageOneWay(str.c_str());
+    SALOMEDS::lock();
+  }
 }
   
 //============================================================================
@@ -53,8 +260,18 @@ SALOMEDS_Study_i::SALOMEDS_Study_i(const Handle(TDocStd_Document) doc,
 //============================================================================
 SALOMEDS_Study_i::~SALOMEDS_Study_i()
 {
-  delete [] _name ;
-  delete [] _URL ;
+  //delete the builder servant
+  PortableServer::POA_var poa=_builder->_default_POA();
+  PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_builder);
+  poa->deactivate_object(anObjectId.in());
+  _builder->_remove_ref();
+  
+  _impl->setNotifier(0);
+  delete _notifier;
+  delete _genObjRegister;
+  //delete implementation
+  delete _impl;
+  _mapOfStudies.erase(_impl);
 }  
 
 //============================================================================
@@ -64,27 +281,22 @@ SALOMEDS_Study_i::~SALOMEDS_Study_i()
 //============================================================================
 char* SALOMEDS_Study_i::GetPersistentReference()
 {
-  return URL();
+  SALOMEDS::Locker lock; 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+  return CORBA::string_dup(_impl->GetPersistentReference().c_str());
 }
 //============================================================================
 /*! Function : GetTransientReference
- *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
+ *  Purpose  : Get IOR of the Study (registered in OCAF document in doc->Root)
  */
 //============================================================================
 char* SALOMEDS_Study_i::GetTransientReference()
 {
-  CORBA::String_var IOR;
-
-  Handle(SALOMEDS_IORAttribute) Att;
-  TDF_Label _lab = _doc->GetData()->Root();
-  if (!_lab.FindAttribute(SALOMEDS_IORAttribute::GetID(),Att)){
-
-    TCollection_AsciiString ch(Att->Get());
-    IOR = CORBA::string_dup(ch.ToCString());
-  }
-  else IOR = CORBA::string_dup(""); // NULL ?
-
-  return CORBA::string_dup(IOR); 
+  SALOMEDS::Locker lock; 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+  return CORBA::string_dup(_impl->GetTransientReference().c_str());
 }
 
 //============================================================================
@@ -94,8 +306,10 @@ char* SALOMEDS_Study_i::GetTransientReference()
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
 {
-  if (_doc.IsNull()) return true;
-  return _doc->IsEmpty();
+  SALOMEDS::Locker lock; 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+  return _impl->IsEmpty();
 }
 
 //============================================================================
@@ -105,25 +319,18 @@ CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
 //============================================================================
 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponentName)
 {
-  bool _find = false;
-  Standard_CString name;
-  SALOMEDS::SComponentIterator_var itcomp = NewComponentIterator();
-  SALOMEDS::SComponent_var compo;
-
-  for (; itcomp->More(); itcomp->Next()) {
-    SALOMEDS::SComponent_var SC = itcomp->Value();
-    name = SC->ComponentDataType();
-    //ED if ( TCollection_AsciiString(name).IsEqual(TCollection_AsciiString(strdup(aComponentName))) ) {
-    if(strcmp(aComponentName,name) == 0){
-      _find = true;
-      return SALOMEDS::SComponent::_narrow(SC); 
-    }
-  }
-  if(!_find)
-    {
-      return SALOMEDS::SComponent::_nil();
-    }
-  return compo;
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SComponent_var sco;
+
+  SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponent(std::string(aComponentName));
+  if (!aCompImpl.IsNull())
+    sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
+
+  return sco._retn();
 }
 
 //============================================================================
@@ -133,28 +340,18 @@ SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponent
 //============================================================================
 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponentID)
 {
-  // Iterate on each components defined in the study
-  // Get the component ID and compare with aComponentID 
-  bool _find = false;
-  char *ID;
-  SALOMEDS::SComponent_ptr compo;
-
-  SALOMEDS::SComponentIterator_var itcomp = NewComponentIterator();
-  for (; itcomp->More(); itcomp->Next()) {
-    SALOMEDS::SComponent_var SC = itcomp->Value();
-    ID = SC->GetID();
-    if(strcmp(aComponentID,ID)==0)
-      {
-       // ComponentID found
-       _find = true;
-        compo = SALOMEDS::SComponent::_narrow(SC);
-      }
-  }
-  if(!_find)
-    {
-      compo = SALOMEDS::SComponent::_nil();
-    }
-  return compo;
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SComponent_var sco;
+
+  SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponentID(std::string((char*)aComponentID));
+  if (!aCompImpl.IsNull())
+    sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
+
+  return sco._retn();
 }
 
 //============================================================================
@@ -164,32 +361,25 @@ SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponen
 //============================================================================
 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
 {
-  // Iterate to all components defined in the study
-  // After testing the component name, iterate in all objects defined under
-  // components (function _FindObject)
-  bool _find = false;
-  SALOMEDS::SObject_ptr RefSO = SALOMEDS::SObject::_nil();
+  SALOMEDS::Locker lock; 
 
-  SALOMEDS::SComponentIterator_var it = NewComponentIterator();
-  for (; it->More();it->Next()){
-    if(!_find)
-      {
-       SALOMEDS::SComponent_var SC = it->Value();
-       SALOMEDS::GenericAttribute_var anAttr;
-       if (SC->FindAttribute(anAttr,"AttributeName")) 
-       {
-         SALOMEDS::AttributeName_var Name = SALOMEDS::AttributeName::_narrow(anAttr);
-         CORBA::String_var Val = Name->Value();
-         if (strcmp(Val, anObjectName) == 0)
-           {
-             _find = true;
-             RefSO = SALOMEDS::SObject::_narrow(SC);
-           }
-       }
-       if (!_find) RefSO =  _FindObject(SC,anObjectName, _find);
-      }
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SObject_var so;
+
+  SALOMEDSImpl_SObject aSO = _impl->FindObject(std::string((char*)anObjectName));
+  if (!aSO.IsNull()) {
+    if (aSO.IsComponent()) {
+      SALOMEDSImpl_SComponent aSCO = aSO;
+      so = SALOMEDS_SComponent_i::New(aSCO, _orb);
+    }
+    else {
+      so = SALOMEDS_SObject_i::New(aSO, _orb);
+    }
   }
-  return RefSO;
+
+  return so._retn();
 }
 
 //============================================================================
@@ -199,15 +389,41 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
 //============================================================================
 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
 {
-  // Convert aSO->GetID in TDF_Label.
-  TDF_Label Lab;
-  TDF_Tool::Label(_doc->GetData(), strdup(anObjectID), Lab);
-  
-  if (Lab.IsNull()) return SALOMEDS::SObject::_nil();
-  SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (Lab,_orb);
-  SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
-  return so;
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SObject_var so;
+
+  SALOMEDSImpl_SObject aSO = _impl->FindObjectID(std::string((char*)anObjectID));
+  if (!aSO.IsNull())
+    so = SALOMEDS_SObject_i::New(aSO, _orb);
+
+  return so._retn();
+}
+
+//============================================================================
+/*! Function : CreateObjectID
+ *  Purpose  : Creates an Object with ID = anObjectID
+ */
+//============================================================================
+SALOMEDS::SObject_ptr SALOMEDS_Study_i::CreateObjectID(const char* anObjectID)
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SObject_var so;
 
+  if (anObjectID && strlen(anObjectID) > 0) {
+    SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
+    if (!aSO.IsNull())
+      so = SALOMEDS_SObject_i::New(aSO, _orb);
+  }
+
+  return so._retn();
 }
 
 //============================================================================
@@ -217,57 +433,27 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
  */
 //============================================================================
 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char* anObjectName,
-                                                                   const char* aComponentName )
+                                                                    const char* aComponentName )
 {
-  SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject ;
-  listSO->length(0);
-  
-  SALOMEDS::SComponent_ptr compo = FindComponent(aComponentName) ;
-  if ( compo->_is_nil() ) {
-    MESSAGE ("In FindObjectByName() :  Component named " << aComponentName << " not found ");
-    return listSO._retn();
-  }
+  SALOMEDS::Locker lock; 
 
-  // Iterate on each object and subobject of the component
-  // If objectName is found add it to the list of SObjects 
-  char *name;
-  char *childName ;
-  SALOMEDS::SObject_ptr addSO = SALOMEDS::SObject::_nil();
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  CORBA::String_var compoId = compo->GetID();
-  SALOMEDS::ChildIterator_var it = NewChildIterator(compo);
-  int length = 0 ;
-  for ( ; it->More();it->Next() ) {
-    
-    SALOMEDS::SObject_var CSO = it->Value();
-    SALOMEDS::GenericAttribute_var anAttr;
-    SALOMEDS::AttributeName_var    aName;
-    if ( CSO->FindAttribute(anAttr, "AttributeName") ) {
-      aName = SALOMEDS::AttributeName::_narrow(anAttr);
-      name = aName->Value();
-      if ( strcmp( name, anObjectName ) == 0) {
-       addSO = SALOMEDS::SObject::_narrow(CSO);
-       /* add to list */
-       length++ ;
-       listSO->length(length);
-       listSO[length-1] = addSO ;
-      }
-      
-      /* looks also for eventual children */
-      bool found = false ;
-      addSO = _FindObject( CSO, anObjectName, found ) ;
-      if( found) {
-       length++ ;
-       listSO->length(length);
-       listSO[length-1] = addSO ;
-      }
-    }
+  std::vector<SALOMEDSImpl_SObject> aSeq = _impl->FindObjectByName(std::string((char*)anObjectName),
+                                                                  std::string((char*)aComponentName));
+
+  SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
+  int aLength = aSeq.size();
+  listSO->length(aLength);
+  for (int i = 0; i < aLength; i++) {
+    SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New(aSeq[i], _orb);
+    listSO[i] = so;
   }
-  return listSO._retn() ;
+  
+  return listSO._retn();
 }
 
-
-
 //============================================================================
 /*! Function : FindObjectIOR
  *  Purpose  : Find an Object with IOR = anObjectIOR
@@ -275,45 +461,18 @@ SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char*
 //============================================================================
 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
 {
-  // firstly searching in the datamap for optimization
-  CORBA::String_var anIOR = CORBA::string_dup(anObjectIOR);
-  if (myIORLabels.IsBound(TCollection_ExtendedString(anIOR))) {
-    SALOMEDS_SObject_i* aResult = new SALOMEDS_SObject_i(myIORLabels.Find(TCollection_ExtendedString(anIOR)),_orb);
-    // 11 oct 2002: forbidden attributes must be checked here
-    SALOMEDS::GenericAttribute_var anAttr;
-    if (!aResult->FindAttribute(anAttr,"AttributeIOR")) {
-      myIORLabels.UnBind(TCollection_ExtendedString(anIOR));
-    } else return aResult->_this();
-  }
-  // Iterate to all components defined in the study
-  // After testing the component name, iterate in all objects defined under
-  // components (function _FindObject)
-  bool _find = false;
-  SALOMEDS::SObject_ptr RefSO = SALOMEDS::SObject::_nil();
-
-  SALOMEDS::SComponentIterator_var it = NewComponentIterator();
-  for (; it->More();it->Next()){
-    if(!_find)
-      {
-       SALOMEDS::SComponent_var SC = it->Value();
-       SALOMEDS::GenericAttribute_var anAttr;
-       if (SC->FindAttribute(anAttr,"AttributeIOR")) 
-       {
-         SALOMEDS::AttributeIOR_var IOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
-          CORBA::String_var Val = IOR->Value();
-         if (strcmp(Val, anObjectIOR) == 0)
-           {
-             _find = true;
-             RefSO = SALOMEDS::SObject::_narrow(SC);
-           }
-       }
-       if (!_find) 
-         RefSO =  _FindObjectIOR(SC,anObjectIOR, _find);
-      }
-  }
-  if (!RefSO->_is_nil()) INFOS("SALOMEDS_Study_i::FindObjectIOR: found label with old methods");
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SObject_var so;
+
+  SALOMEDSImpl_SObject aSO = _impl->FindObjectIOR(std::string((char*)anObjectIOR));
+  if (!aSO.IsNull())
+    so = SALOMEDS_SObject_i::New(aSO, _orb);
 
-  return RefSO;
+  return so._retn();
 }
 
 //============================================================================
@@ -323,63 +482,18 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
 //============================================================================
 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
 {
-  TCollection_AsciiString aPath(CORBA::string_dup(thePath)), aToken;
-  SALOMEDS::SObject_var aSO = SALOMEDS::SObject::_nil();
-  int i = 1, aLength = aPath.Length();
-  bool isRelative = false;
-
-  if(aLength == 0) {  //Empty path - return the current context
-    SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (_current, _orb);
-    aSO = SALOMEDS::SObject::_narrow(so_servant->_this()); 
-    return aSO._retn();
-  }
-
-  if(aPath.Value(1) != '/')  //Relative path 
-    isRelative = true;
-
-  TDF_ChildIterator anIterator;
-  TDF_Label aLabel;
-  Handle(TDataStd_Name) anAttr;
-
-  if(isRelative) {
-    if(_current.IsNull()) throw SALOMEDS::Study::StudyInvalidContext(); 
-    anIterator.Initialize(_current, Standard_False);
-  }
-  else {
-    if(aPath.Length() == 1 && aPath.Value(1) == '/') {    //Root
-      SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (_doc->Main(), _orb);
-      aSO = SALOMEDS::SObject::_narrow(so_servant->_this());
-      return aSO._retn();
-    }
-    anIterator.Initialize(_doc->Main(), Standard_False);
-  }
+  SALOMEDS::Locker lock; 
 
-  while(i <= aLength) {
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-    aToken = aPath.Token("/", i);
-    if(aToken.Length() == 0) break;
+  SALOMEDS::SObject_var so;
 
-    for ( ; anIterator.More(); anIterator.Next() ) {
-      aLabel = anIterator.Value();
-      if(aLabel.FindAttribute(TDataStd_Name::GetID(), anAttr)) {
-       if(anAttr->Get() == aToken) {
-         aToken = aPath.Token("/", i+1); //Check if it was the last part of the path
-         if(aToken.Length() == 0) {  //The searched label is found (no part of the path is left)
-             SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (aLabel, _orb);
-             aSO = SALOMEDS::SObject::_narrow(so_servant->_this()); 
-             return aSO._retn();
-         }
-
-         anIterator.Initialize(aLabel, Standard_False);
-         break;
-       }
-      }
-    }
-
-    i++;
-  }
+  SALOMEDSImpl_SObject aSO = _impl->FindObjectByPath(std::string((char*)thePath));
+  if (!aSO.IsNull())
+    so = SALOMEDS_SObject_i::New (aSO, _orb);
 
-  return aSO._retn();
+  return so._retn();
 }
 
 //============================================================================
@@ -389,36 +503,30 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
 //============================================================================
 char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
 {
-  TCollection_AsciiString aPath("");
-  if(CORBA::is_nil(theObject)) return CORBA::string_dup(aPath.ToCString());
+  SALOMEDS::Locker lock; 
 
-  SALOMEDS::SObject_var anObject = SALOMEDS::SObject::_narrow(theObject);
-  if(anObject->_is_nil()) {
-    anObject = FindObjectIOR(_orb->object_to_string(theObject));
-    if(anObject->_is_nil()) return CORBA::string_dup(aPath.ToCString());
-  }
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  SALOMEDS::GenericAttribute_var anAttr;
-  if(anObject->FindAttribute(anAttr, "AttributeName")) {
-    SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
-    if(anAttr->_is_nil()) return CORBA::string_dup(aPath.ToCString());
-    TCollection_AsciiString aValue(aName->Value());
-    aValue.Prepend("/");
-    aValue += aPath;
-    aPath = aValue;
-    SALOMEDS::SObject_ptr aFather = anObject->GetFather();
-    if(!aFather->_is_nil()) {
-      TDF_Label aLabel;
-      Handle(TDataStd_Name) aNameAttrib;
-      TDF_Tool::Label(_doc->GetData(), aFather->GetID(), aLabel);
-      if(aLabel.FindAttribute(TDataStd_Name::GetID(), aNameAttrib)) {
-         aValue = GetObjectPath(aFather);
-         aPath = aValue + aPath;
-      }
+  std::string aPath = "";
+
+  if (!CORBA::is_nil(theObject)) {
+    SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
+    SALOMEDSImpl_SObject aSO;
+
+    if (!CORBA::is_nil(aSObj)) {
+      aSO = _impl->FindObjectID(aSObj->GetID());
+    }
+    else {
+      aSO = _impl->FindObjectIOR(_orb->object_to_string(theObject));
+    }
+    
+    if (!aSO.IsNull()) {    
+      aPath = _impl->GetObjectPath(aSO);
     }
   }
 
-  return  CORBA::string_dup(aPath.ToCString());
+  return CORBA::string_dup(aPath.c_str());
 }
 
 
@@ -429,34 +537,14 @@ char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
 //============================================================================
 void SALOMEDS_Study_i::SetContext(const char* thePath) 
 {
-  if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
-  TCollection_AsciiString aPath(CORBA::string_dup(thePath)), aContext("");
-  bool isInvalid = false, isFound = false;
-  SALOMEDS::SObject_var aSO;
-  
-  if(aPath.Value(1) != '/') { //Relative path 
-    aContext = TCollection_AsciiString(GetContext());
-    aContext += '/';
-    aContext += aPath;
-  }
-  else
-    aContext = aPath;
-  
-  try {
-    aSO = FindObjectByPath(aContext.ToCString());
-  }
-  catch( ... ) {
-    isInvalid = true;
-  }
+  SALOMEDS::Locker lock; 
 
-  if(isInvalid || aSO->_is_nil()) throw SALOMEDS::Study::StudyInvalidContext();
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  TDF_Label aLabel;
-  TDF_Tool::Label(_doc->GetData(), aSO->GetID(), aLabel);
-  if(aLabel.IsNull()) throw SALOMEDS::Study::StudyInvalidContext();
-  else
-    _current = aLabel;  //Set the current context
-  
+  _impl->SetContext(std::string((char*)thePath));
+  if (_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
+    throw SALOMEDS::Study::StudyInvalidContext();  
 }
 
 //============================================================================
@@ -466,10 +554,14 @@ void SALOMEDS_Study_i::SetContext(const char* thePath)
 //============================================================================
 char* SALOMEDS_Study_i::GetContext() 
 {
-  if(_current.IsNull()) throw SALOMEDS::Study::StudyInvalidContext();   
-  SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (_current, _orb);
-  SALOMEDS::SObject_var aSO = SALOMEDS::SObject::_narrow(so_servant->_this()); 
-  return GetObjectPath(aSO._retn());
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  if (!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();
+
+  return CORBA::string_dup(_impl->GetContext().c_str());
 }
 
 //============================================================================
@@ -477,35 +569,28 @@ char* SALOMEDS_Study_i::GetContext()
  *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
  */
 //============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) {
-  TColStd_SequenceOfExtendedString aResultSeq;
+SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) 
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-  TDF_Label aLabel;
-  if (strlen(theContext) == 0) {
-    if(_current.IsNull()) throw SALOMEDS::Study::StudyInvalidContext();   
-    aLabel = _current;
-  } else {
-    TDF_Label aTmp = _current;
-    SetContext(theContext);
-    aLabel = _current;
-    _current = aTmp;
-  }
-  TDF_ChildIterator anIter(aLabel, Standard_False); // iterate all subchildren at all sublevels
-  for(; anIter.More(); anIter.Next()) {
-    TDF_Label aLabel = anIter.Value();
-//      Handle(TDF_Attribute) anAttribute;
-//      if (aLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttribute) ||
-//     aLabel.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anAttribute)) {
-    Handle(TDataStd_Name) aName;
-    if (aLabel.FindAttribute(TDataStd_Name::GetID(), aName)) aResultSeq.Append(aName->Get());
-//    }
-  }
-  // fill the result table
-  int anIndex, aLength = aResultSeq.Length();
+
+  if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
+    throw SALOMEDS::Study::StudyInvalidContext();
+  
+  std::vector<std::string> aSeq = _impl->GetObjectNames(std::string((char*)theContext));
+  if (_impl->GetErrorCode() == "InvalidContext")
+    throw SALOMEDS::Study::StudyInvalidContext();
+
+  int aLength = aSeq.size();
   aResult->length(aLength);
-  for(anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(TCollection_AsciiString(aResultSeq.Value(anIndex + 1)).ToCString());
+  for (int anIndex = 0; anIndex < aLength; anIndex++) {
+    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
   }
+
   return aResult._retn();
 }
 
@@ -514,42 +599,28 @@ SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext
  *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
  */
 //============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) {
-  TColStd_SequenceOfExtendedString aResultSeq;
+SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) 
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-  TDF_Label aLabel;
-  if (strlen(theContext) == 0) {
-    if(_current.IsNull()) throw SALOMEDS::Study::StudyInvalidContext();   
-    aLabel = _current;
-  } else {
-    TDF_Label aTmp = _current;
-    SetContext(theContext);
-    aLabel = _current;
-    _current = aTmp;
-  }
-  TDF_ChildIterator anIter(aLabel, Standard_False); // iterate first-level children at all sublevels
-  for(; anIter.More(); anIter.Next()) {
-    TDF_Label aLabel = anIter.Value();
-//      Handle(TDF_Attribute) anAttribute;
-//      if (!aLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttribute) &&
-//     !aLabel.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anAttribute)) {
-    Handle(SALOMEDS_PersRefAttribute) anID;
-    if (aLabel.FindAttribute(SALOMEDS_PersRefAttribute::GetID(), anID)) {
-      if (anID->Get().Search(TCollection_ExtendedString(DIRECTORYID)) == 1) {
-       Handle(TDataStd_Name) aName;
-       if (aLabel.FindAttribute(TDataStd_Name::GetID(), aName)) {
-         aResultSeq.Append(aName->Get());
-       }
-      }
-    }
-//      }
-  }
-  // fill the result table
-  int anIndex, aLength = aResultSeq.Length();
+
+  if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
+    throw SALOMEDS::Study::StudyInvalidContext();
+  
+  std::vector<std::string> aSeq = _impl->GetDirectoryNames(std::string((char*)theContext));
+  if (_impl->GetErrorCode() == "InvalidContext")
+    throw SALOMEDS::Study::StudyInvalidContext();
+
+  int aLength = aSeq.size();
   aResult->length(aLength);
-  for(anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(TCollection_AsciiString(aResultSeq.Value(anIndex + 1)).ToCString());
+  for (int anIndex = 0; anIndex < aLength; anIndex++) {
+    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
   }
+  
   return aResult._retn();
 }
 
@@ -558,62 +629,54 @@ SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theCont
  *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
  */
 //============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) {
-  TColStd_SequenceOfExtendedString aResultSeq;
+SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) 
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-  TDF_Label aLabel;
-  if (strlen(theContext) == 0) {
-    if(_current.IsNull()) throw SALOMEDS::Study::StudyInvalidContext();   
-    aLabel = _current;
-  } else {
-    TDF_Label aTmp = _current;
-    SetContext(theContext);
-    aLabel = _current;
-    _current = aTmp;
-  }
-  TDF_ChildIterator anIter(aLabel, Standard_False); // iterate all subchildren at all sublevels
-  for(; anIter.More(); anIter.Next()) {
-    TDF_Label aLabel = anIter.Value();
-//      Handle(TDF_Attribute) anAttribute;
-//      if (aLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttribute) ||
-//     aLabel.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anAttribute)) {
-    Handle(SALOMEDS_PersRefAttribute) aName;
-    if (aLabel.FindAttribute(SALOMEDS_PersRefAttribute::GetID(), aName)) {
-      if (aName->Get().Search(TCollection_ExtendedString(FILEID)) == 1) {
-       aResultSeq.Append(aName->Get().Split(strlen(FILEID)));
-      }
-    }
-//      }
-  }
-  // fill the result table
-  int anIndex, aLength = aResultSeq.Length();
+
+  if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
+    throw SALOMEDS::Study::StudyInvalidContext();
+  
+  std::vector<std::string> aSeq = _impl->GetFileNames(std::string((char*)theContext));
+  if (_impl->GetErrorCode() == "InvalidContext")
+    throw SALOMEDS::Study::StudyInvalidContext();
+
+  int aLength = aSeq.size();
   aResult->length(aLength);
-  for(anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(TCollection_AsciiString(aResultSeq.Value(anIndex + 1)).ToCString());
+  for (int anIndex = 0; anIndex < aLength; anIndex++) {
+    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
   }
+
   return aResult._retn();
 }
 
 //============================================================================
 /*! Function : GetComponentNames
  *  Purpose  : method to get all components names
+ *  SRN:       Note, theContext can be any, it doesn't matter
  */
 //============================================================================
-SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) {
-  TColStd_SequenceOfExtendedString aResultSeq;
+SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) 
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-  TDF_ChildIterator anIter(_doc->Main(), Standard_False); // iterate all subchildren at first level
-  for(; anIter.More(); anIter.Next()) {
-    TDF_Label aLabel = anIter.Value();
-    Handle(TDataStd_Name) aName;
-    if (aLabel.FindAttribute(TDataStd_Name::GetID(), aName)) aResultSeq.Append(aName->Get());
-  }
-  // fill the result table
-  int anIndex, aLength = aResultSeq.Length();
+
+  std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
+
+  int aLength = aSeq.size();
   aResult->length(aLength);
-  for(anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(TCollection_AsciiString(aResultSeq.Value(anIndex + 1)).ToCString());
+  for(int anIndex = 0; anIndex < aLength; anIndex++) {
+    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
   }
+
   return aResult._retn();
 }
 
@@ -622,17 +685,20 @@ SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theCont
  *  Purpose  : Create a ChildIterator from an SObject
  */
 //============================================================================
-SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject_ptr aSO)
+SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject_ptr theSO)
 {
-  //Convert aSO->GetID in TDF_Label.
-  TDF_Label Lab;
-  TDF_Tool::Label(_doc->GetData(), strdup(aSO->GetID()), Lab);
+  SALOMEDS::Locker lock; 
 
-  //Create iterator
-  SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(Lab,_orb);
-  SALOMEDS::ChildIterator_var it = SALOMEDS::ChildIterator::_narrow(it_servant->_this()); 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  return it;
+  CORBA::String_var anID = theSO->GetID();
+  SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in());
+  SALOMEDSImpl_ChildIterator anItr(aSO);
+  SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(anItr, _orb);
+  SALOMEDS::ChildIterator_var it = it_servant->_this();
+
+  return it._retn();
 }
 
 
@@ -643,9 +709,16 @@ SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject
 //============================================================================
 SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
 {
-  SALOMEDS_SComponentIterator_i* it_servant = new SALOMEDS_SComponentIterator_i(_doc,_orb);
-  SALOMEDS::SComponentIterator_var it = SALOMEDS::SComponentIterator::_narrow(it_servant->_this()); 
-  return it;
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS_SComponentIterator_i* it_servant = new SALOMEDS_SComponentIterator_i(_impl->NewComponentIterator(), _orb);
+  it_servant->Init();
+  SALOMEDS::SComponentIterator_var it = it_servant->_this();
+
+  return it._retn();
 }
 
 
@@ -656,19 +729,14 @@ SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
 //============================================================================
 SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
 {
-  SALOMEDS_StudyBuilder_i* it_servant = new SALOMEDS_StudyBuilder_i(_doc,_orb);
-  SALOMEDS::StudyBuilder_var it = SALOMEDS::StudyBuilder::_narrow(it_servant->_this()); 
+  SALOMEDS::Locker lock; 
 
-  if(_autoFill) {
-    SALOMEDS_Callback_i* callback = new SALOMEDS_Callback_i(GetUseCaseBuilder(), _orb);
-    SALOMEDS::Callback_var cb = SALOMEDS::Callback::_narrow(callback->_this()); 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-    it->SetOnAddSObject(cb);
-    it->SetOnRemoveSObject(cb);
-  }
-
-  return it;
+  SALOMEDS::StudyBuilder_var sb = SALOMEDS::StudyBuilder::_duplicate(_builder->_this());
 
+  return sb._retn();
 }
  
 //============================================================================
@@ -678,7 +746,9 @@ SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
 //============================================================================
 char* SALOMEDS_Study_i::Name()
 {
-  return CORBA::string_dup(_name);
+  SALOMEDS::Locker lock; 
+  // Name is specified as IDL attribute: user exception cannot be raised
+  return CORBA::string_dup(_impl->Name().c_str());
 }
 
 //============================================================================
@@ -688,8 +758,9 @@ char* SALOMEDS_Study_i::Name()
 //============================================================================
 void SALOMEDS_Study_i::Name(const char* name)
 {
-  _name = new char[strlen(name) +1];
-  strcpy(_name,name);
+  SALOMEDS::Locker lock;  
+  // Name is specified as IDL attribute: user exception cannot be raised
+  _impl->Name(std::string(name));
 }
 
 //============================================================================
@@ -697,9 +768,11 @@ void SALOMEDS_Study_i::Name(const char* name)
  *  Purpose  : get if study has been saved
  */
 //============================================================================
-CORBA::Boolean  SALOMEDS_Study_i::IsSaved()
+CORBA::Boolean SALOMEDS_Study_i::IsSaved()
 {
-  return _isSaved;
+  SALOMEDS::Locker lock; 
+  // IsSaved is specified as IDL attribute: user exception cannot be raised
+  return (!_closed) ? _impl->IsSaved() : false;
 }
 
 //============================================================================
@@ -709,7 +782,10 @@ CORBA::Boolean  SALOMEDS_Study_i::IsSaved()
 //============================================================================
 void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
 {
-  _isSaved = save;
+  SALOMEDS::Locker lock; 
+  // IsSaved is specified as IDL attribute: user exception cannot be raised
+  if (!_closed)
+    _impl->IsSaved(save);
 }
 
 //============================================================================
@@ -717,13 +793,29 @@ void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
  *  Purpose  : Detect if a Study has been modified since it has been saved
  */
 //============================================================================
-CORBA::Boolean  SALOMEDS_Study_i::IsModified()
+CORBA::Boolean SALOMEDS_Study_i::IsModified()
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsModified();
+}
+
+//============================================================================
+/*! Function : Modified
+ *  Purpose  : Sets a Modified flag of a Study to True
+ */
+//============================================================================
+void SALOMEDS_Study_i::Modified()
 {
-  // True if is modified and not saved
-  if (_doc->IsModified())
-    if (!_isSaved) return true;
-    else return false;
-  else return false;
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->Modify();
 }
 
 //============================================================================
@@ -733,7 +825,9 @@ CORBA::Boolean  SALOMEDS_Study_i::IsModified()
 //============================================================================
 char* SALOMEDS_Study_i::URL()
 {
-  return CORBA::string_dup(_URL);
+  SALOMEDS::Locker lock; 
+  // URL is specified as IDL attribute: user exception cannot be raised
+  return CORBA::string_dup(_impl->URL().c_str());
 }
 
 //============================================================================
@@ -743,122 +837,77 @@ char* SALOMEDS_Study_i::URL()
 //============================================================================
 void SALOMEDS_Study_i::URL(const char* url)
 {
-  _URL = new char[strlen(url) +1];
-  strcpy(_URL,url);
-  SCRUTE(_URL);
+  SALOMEDS::Locker lock; 
+  // URL is specified as IDL attribute: user exception cannot be raised
+  _impl->URL(std::string((char*)url));
 }
 
-
-//============================================================================
-/*! Function : _FindObject
- *  Purpose  : Find an Object with SALOMEDS::Name = anObjectName
- */
-//============================================================================
-SALOMEDS::SObject_ptr SALOMEDS_Study_i::_FindObject(SALOMEDS::SObject_ptr SO,
-                                                   const char* anObjectName, 
-                                                   bool& _find)
+CORBA::Short SALOMEDS_Study_i::StudyId()
 {
-  // Iterate on each objects and subobjects of the component
-  // If objectName find, stop the loop and get the object reference
-  SALOMEDS::SObject_ptr RefSO = SALOMEDS::SObject::_nil();
+  SALOMEDS::Locker lock; 
+  // StudyId is specified as IDL attribute: user exception cannot be raised
+  return _impl->StudyId();
+}
 
-  CORBA::String_var soid = SO->GetID();
-  SALOMEDS::ChildIterator_var it = NewChildIterator(SO);
-  for (; it->More();it->Next()){
-    if(!_find)
-      {
-       SALOMEDS::SObject_var CSO = it->Value();
-       SALOMEDS::GenericAttribute_var anAttr;
-       if (CSO->FindAttribute(anAttr,"AttributeName")) 
-       {
-         SALOMEDS::AttributeName_var Name = SALOMEDS::AttributeName::_narrow(anAttr);
-          CORBA::String_var Val = Name->Value();
-         if (strcmp(Val, anObjectName) == 0)
-           {
-             RefSO = SALOMEDS::SObject::_narrow(CSO);
-             _find = true;
-           }
-       }
-       if (!_find) RefSO =  _FindObject(CSO, anObjectName, _find);
-      }
-  }
-  return RefSO;
+void SALOMEDS_Study_i::StudyId(CORBA::Short id)
+{ 
+  SALOMEDS::Locker lock; 
+  // StudyId is specified as IDL attribute: user exception cannot be raised
+  _impl->StudyId(id);
 }
 
-//============================================================================
-/*! Function : _FindObject
- *  Purpose  : Find an Object with SALOMEDS::IOR = anObjectIOR
- */
-//============================================================================
-SALOMEDS::SObject_ptr 
-SALOMEDS_Study_i::_FindObjectIOR(SALOMEDS::SObject_ptr SO,
-                             const char* anObjectIOR, 
-                             bool& _find)
+void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry) 
 {
-  // Iterate on each objects and subobjects of the component
-  // If objectName find, stop the loop and get the object reference
-  SALOMEDS::SObject_ptr RefSO = SALOMEDS::SObject::_nil();
+  SALOMEDS::Locker lock; 
 
-  SALOMEDS::ChildIterator_var it = NewChildIterator(SO);
-  for (; it->More();it->Next()){
-    if(!_find)
-      {
-       SALOMEDS::SObject_var CSO = it->Value();
-       SALOMEDS::GenericAttribute_var anAttr;
-       if (CSO->FindAttribute(anAttr,"AttributeIOR")) 
-       {
-         SALOMEDS::AttributeIOR_var IOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
-          CORBA::String_var Val = IOR->Value();
-         if (strcmp(Val, anObjectIOR) == 0)
-           {
-             RefSO = SALOMEDS::SObject::_narrow(CSO);
-             _find = true;
-           }
-       }
-       if (!_find) RefSO =  _FindObjectIOR(CSO, anObjectIOR, _find);
-      }
-  }
-  return RefSO;
-}
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-CORBA::Short SALOMEDS_Study_i::StudyId()
-{
-  return _StudyId;
+  _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
 }
 
-void SALOMEDS_Study_i::StudyId(CORBA::Short id)
+SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb) 
 {
-  _StudyId = id;
-}
+  SALOMEDS::Locker lock; 
 
-void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR,const char* anEntry) {
-  TDF_Label aLabel;
-  CORBA::String_var anEn = CORBA::string_dup(anEntry);
-  CORBA::String_var IOR = CORBA::string_dup(anIOR);
-  TDF_Tool::Label(_doc->GetData(),anEn,aLabel, Standard_True);
-  if (myIORLabels.IsBound(TCollection_ExtendedString(IOR))) myIORLabels.UnBind(TCollection_ExtendedString(IOR));
-  myIORLabels.Bind(TCollection_ExtendedString(IOR), aLabel);
-}
-
-void SALOMEDS_Study_i::IORUpdated(const Handle(SALOMEDS_IORAttribute) theAttribute, CORBA::ORB_ptr orb) {
-  // get accorded study first
-  Handle(SALOMEDS_IORAttribute) Att;
-  if (theAttribute->Label().Root().FindAttribute(SALOMEDS_IORAttribute::GetID(),Att)){
-    TCollection_AsciiString ch(Att->Get());
-    char* IOR = CORBA::string_dup(ch.ToCString());
+  SALOMEDSImpl_AttributeIOR* Att = NULL;
+  if ((Att=(SALOMEDSImpl_AttributeIOR*)theLabel.Root().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))){
+    char* IOR = CORBA::string_dup(Att->Value().c_str());
     CORBA::Object_var obj = orb->string_to_object(IOR);
     SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj) ;
     ASSERT(!CORBA::is_nil(aStudy));
-    TCollection_AsciiString aString;
-    TDF_Tool::Entry(theAttribute->Label(),aString);
-    aStudy->UpdateIORLabelMap(TCollection_AsciiString(theAttribute->Get()).ToCString(), aString.ToCString());
+    return SALOMEDS::Study::_duplicate(aStudy);
   } else {
-    INFOS("IORUpdated: Problem to get study");
-    return;
+    MESSAGE("GetStudy: Problem to get study");
+  }
+  return SALOMEDS::Study::_nil();
+}
+
+SALOMEDS_Study_i* SALOMEDS_Study_i::GetStudyServant(SALOMEDSImpl_Study* aStudyImpl, CORBA::ORB_ptr orb)
+{
+  if (_mapOfStudies.find(aStudyImpl) != _mapOfStudies.end()) 
+    return _mapOfStudies[aStudyImpl];
+  else
+  {
+    SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, orb);
+    _mapOfStudies[aStudyImpl]=Study_servant;
+    return Study_servant;
   }
 }
 
-SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObject_ptr anObject) {
+void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
+{
+  SALOMEDS::Locker lock; 
+  SALOMEDSImpl_Study::IORUpdated(theAttribute);
+}
+
+SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObject_ptr anObject) 
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDS::GenericAttribute_ptr aTarget;
   if (anObject->FindAttribute(aTarget,"AttributeTarget")) {
     return SALOMEDS::AttributeTarget::_narrow(aTarget)->Get();
@@ -869,45 +918,48 @@ SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObj
 }
 
 
-SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties() {
-  SALOMEDS::GenericAttribute_ptr anAttr =  NewBuilder()->FindOrCreateAttribute(FindObjectID("0:1"),
-                                                                              "AttributeStudyProperties");
-  return SALOMEDS::AttributeStudyProperties::_narrow(anAttr);
-}
+SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties() 
+{
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-char* SALOMEDS_Study_i::GetLastModificationDate() {
-  SALOMEDS::AttributeStudyProperties_var aProp = GetProperties();
-  SALOMEDS::StringSeq_var aNames;
-  SALOMEDS::LongSeq_var aMinutes, aHours, aDays, aMonths, aYears;
-  aProp->GetModificationsList(aNames , aMinutes ,aHours, aDays, aMonths, aYears, true);
-  int aLastIndex = aNames->length() - 1;
-  char aResult[20];
-  sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d", (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]),
-         (int)(aYears[aLastIndex]), (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
-  CORBA::String_var aResStr = CORBA::string_dup(aResult);
-  return aResStr._retn();
+  SALOMEDSImpl_AttributeStudyProperties* anAttr = _impl->GetProperties();
+  SALOMEDS_AttributeStudyProperties_i* SP = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
+  SALOMEDS::AttributeStudyProperties_var aProp = SP->_this();
+  return aProp._retn();
 }
 
-SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() {
-  SALOMEDS::AttributeStudyProperties_var aProp = GetProperties();
-  SALOMEDS::StringSeq_var aNames;
-  SALOMEDS::LongSeq_var aMinutes, aHours, aDays, aMonths, aYears;
-  aProp->GetModificationsList(aNames , aMinutes ,aHours, aDays, aMonths, aYears, false);
+char* SALOMEDS_Study_i::GetLastModificationDate() 
+{
+  SALOMEDS::Locker lock; 
 
-  int anIndex, aLength = aNames->length();
-  SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
-  aDates->length(aLength);
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  for(anIndex = 0; anIndex < aLength; anIndex++) {
-    char aDate[20];
-    sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d", (int)(aDays[anIndex]), (int)(aMonths[anIndex]),
-           (int)(aYears[anIndex]), (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
-    aDates[anIndex] = CORBA::string_dup(aDate);
-  }
-  return aDates._retn();
+  return CORBA::string_dup(_impl->GetLastModificationDate().c_str());
 }
 
+SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() 
+{
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
+
+  std::vector<std::string> aSeq = _impl->GetModificationsDate();
 
+  int aLength = aSeq.size();
+  aDates->length(aLength);
+  for (int anIndex = 0; anIndex < aLength; anIndex++) {
+    aDates[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
+  }
+
+  return aDates._retn();
+}
 
 //============================================================================
 /*! Function : GetUseCaseBuilder
@@ -916,9 +968,14 @@ SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() {
 //============================================================================
 SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder() 
 {
-  SALOMEDS_UseCaseBuilder_i* _caseBuilder = new SALOMEDS_UseCaseBuilder_i(_doc, _orb);
-  SALOMEDS::UseCaseBuilder_var aBuilder = SALOMEDS::UseCaseBuilder::_narrow(_caseBuilder->_this());
-  return aBuilder._retn();
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
+  SALOMEDS::UseCaseBuilder_var uc = UCBuilder->_this();
+  return uc._retn();
 }
 
 
@@ -929,31 +986,633 @@ SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder()
 //============================================================================
 void SALOMEDS_Study_i::Close()
 {
-  SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
+  SALOMEDS::Locker lock; 
 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  RemovePostponed(-1);
+  
+  SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
   for (; itcomponent->More(); itcomponent->Next()) {
     SALOMEDS::SComponent_var sco = itcomponent->Value();
-         
-    MESSAGE ( "Look for an engine for data type :"<< sco->ComponentDataType());
+    CORBA::String_var compodatatype=sco->ComponentDataType();
+    MESSAGE ( "Look for an engine for data type :"<< compodatatype);
     // if there is an associated Engine call its method for closing
     CORBA::String_var IOREngine;
     if (sco->ComponentIOR(IOREngine)) {
       // we have found the associated engine to write the data 
-      MESSAGE ( "We have found an engine for data type :"<< sco->ComponentDataType());
-      CORBA::Object_var obj = _orb->string_to_object(IOREngine);
-      SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
-             
-      if (!anEngine->_is_nil())  
-       anEngine->Close(sco);
+      MESSAGE ( "We have found an engine for data type :"<< compodatatype);
+      //_narrow can throw a corba exception
+      try {
+       CORBA::Object_var obj = _orb->string_to_object(IOREngine);
+       if (!CORBA::is_nil(obj)) {
+         SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
+         if (!anEngine->_is_nil())  { 
+           SALOMEDS::unlock();
+           anEngine->Close(sco);
+           SALOMEDS::lock();
+         }
+       }
+      } 
+      catch (CORBA::Exception&) {
+      }
     }
+    sco->UnRegister();
+  }
+  
+  //Does not need any more this iterator
+  itcomponent->UnRegister();
+  
+  // Notify GUI that study is closed
+  SALOME_NamingService *aNamingService = KERNEL::getNamingService();
+  CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
+  SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
+  if ( !CORBA::is_nil(aSession) ) {
+    std::stringstream ss;
+    ss << "studyClosed:" << _impl->StudyId();
+    std::string str = ss.str();
+    SALOMEDS::unlock();
+    aSession->emitMessageOneWay(str.c_str());
+    SALOMEDS::lock();
+  }
+  
+  _impl->Close();
+  _closed = true;
+}
+
+//============================================================================
+/*! Function : AddPostponed
+ *  Purpose  : 
+ */
+ //============================================================================
+void SALOMEDS_Study_i::AddPostponed(const char* theIOR) 
+{
+  SALOMEDS::Locker lock; 
+  //Not implemented
+}
+
+void SALOMEDS_Study_i::AddCreatedPostponed(const char* theIOR) 
+{
+  SALOMEDS::Locker lock; 
+  //Not implemented
+}
+
+//============================================================================
+/*! Function : RemovePostponed
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/) 
+{  
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  std::vector<std::string> anIORs = _impl->GetIORs();
+  int i, aSize = (int)anIORs.size();
+  
+  for (i = 0; i < aSize; i++) {
+    try {
+      CORBA::Object_var obj = _orb->string_to_object(anIORs[i].c_str());
+      SALOME::GenericObj_var aGeneric = SALOME::GenericObj::_narrow(obj);
+      //rnv: To avoid double deletion of the Salome Generic Objects:
+      //rnv: 1. First decrement of the reference count in the SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR();
+      //rnv: 2. Second decrement of the reference count in the next string : aGeneric->UnRegister();
+      //if (!CORBA::is_nil(aGeneric)) aGeneric->UnRegister();
+    } catch (...) {}
   }
 
-  Handle(TDocStd_Application) anApp = Handle(TDocStd_Application)::DownCast(_doc->Application());
-//    Handle(TDocStd_Owner) anOwner;
-//    if (_doc->Main().Root().FindAttribute(TDocStd_Owner::GetID(), anOwner)) {
-//      Handle(TDocStd_Document) anEmptyDoc;
-//      anOwner->SetDocument(anEmptyDoc);
-//    }
-  if(!anApp.IsNull()) anApp->Close(_doc);
-  _doc.Nullify();
+  //Not implemented
+}
+
+//============================================================================
+/*! Function : UndoPostponed
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
+{
+  SALOMEDS::Locker lock; 
+  //Not implemented
+}
+
+
+//============================================================================
+/*! Function : DumpStudy
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath, 
+                                           const char* theBaseName, 
+                                           CORBA::Boolean isPublished,
+                                           CORBA::Boolean isMultiFile)
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
+  SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
+  bool ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
+  delete factory;
+
+  return ret;
+}
+
+//============================================================================
+/*! Function : GetCommonParameters
+ *  Purpose  : 
+ */
+//============================================================================
+SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
+{
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
+  SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
+  SALOMEDS::AttributeParameter_var aParam = SP->_this();
+
+  return aParam._retn();
+}
+//============================================================================
+/*! Function : GetCommonModuleParameters
+ *  Purpose  : 
+ */
+//============================================================================
+SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
+                                                                       const char* theModuleName, 
+                                                                       CORBA::Long theSavePoint)
+{
+  SALOMEDS::Locker lock; 
+  
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
+  SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
+  SALOMEDS::AttributeParameter_var aParam = SP->_this();
+
+  return aParam._retn();
+}
+
+//============================================================================
+/*! Function : SetStudyLock
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->SetStudyLock(theLockerID);
+}
+
+//============================================================================
+/*! Function : IsStudyLocked
+ *  Purpose  : 
+ */
+//============================================================================
+bool SALOMEDS_Study_i::IsStudyLocked()
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsStudyLocked();
+}
+
+//============================================================================
+/*! Function : UnLockStudy
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->UnLockStudy(theLockerID);
+}
+
+//============================================================================
+/*! Function : GetLockerID
+ *  Purpose  : 
+ */
+//============================================================================
+SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
+
+  std::vector<std::string> aSeq = _impl->GetLockerID();
+
+  int aLength = aSeq.size();
+  aResult->length(aLength);
+  for(int anIndex = 0; anIndex < aLength; anIndex++) {
+    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
+  }
+
+  return aResult._retn();
+}
+//============================================================================
+/*! Function : SetReal
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+
+  _impl->SetVariable(std::string(theVarName), 
+                    theValue,
+                    SALOMEDSImpl_GenericVariable::REAL_VAR);
+  if (_notifier)
+    _notifier->modifyNB_Notification(theVarName);
+}
+
+//============================================================================
+/*! Function : SetInteger
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->SetVariable(std::string(theVarName), 
+                    theValue,
+                    SALOMEDSImpl_GenericVariable::INTEGER_VAR);
+  if (_notifier)
+    _notifier->modifyNB_Notification(theVarName);
+}
+
+//============================================================================
+/*! Function : SetBoolean
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->SetVariable(std::string(theVarName), 
+                    theValue,
+                    SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
+  if (_notifier)
+    _notifier->modifyNB_Notification(theVarName);
+}
+
+//============================================================================
+/*! Function : SetString
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->SetStringVariable(std::string(theVarName), 
+                          theValue,
+                          SALOMEDSImpl_GenericVariable::STRING_VAR);
+  if (_notifier)
+    _notifier->modifyNB_Notification(theVarName);
+}
+
+//============================================================================
+/*! Function : SetStringAsDouble
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->SetStringVariableAsDouble(std::string(theVarName), 
+                                  theValue,
+                                  SALOMEDSImpl_GenericVariable::STRING_VAR);
+}
+
+//============================================================================
+/*! Function : GetReal
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->GetVariableValue(std::string(theVarName));
+}
+
+//============================================================================
+/*! Function : GetInteger
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return (long)_impl->GetVariableValue(std::string(theVarName));
+}
+
+//============================================================================
+/*! Function : GetBoolean
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return (bool)_impl->GetVariableValue(std::string(theVarName));
+}
+
+//============================================================================
+/*! Function : GetString
+ *  Purpose  : 
+ */
+//============================================================================
+char* SALOMEDS_Study_i::GetString(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
+}
+
+//============================================================================
+/*! Function : IsReal
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsTypeOf(std::string(theVarName),
+                        SALOMEDSImpl_GenericVariable::REAL_VAR);
+}
+
+//============================================================================
+/*! Function : IsInteger
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsTypeOf(std::string(theVarName),
+                        SALOMEDSImpl_GenericVariable::INTEGER_VAR);
+}
+
+//============================================================================
+/*! Function : IsBoolean
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsTypeOf(std::string(theVarName),
+                        SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
+}
+
+//============================================================================
+/*! Function : IsString
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsTypeOf(std::string(theVarName),
+                        SALOMEDSImpl_GenericVariable::STRING_VAR);
+}
+
+//============================================================================
+/*! Function : IsVariable
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsVariable(std::string(theVarName));
+}
+
+//============================================================================
+/*! Function : GetVariableNames
+ *  Purpose  : 
+ */
+//============================================================================
+SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
+
+  std::vector<std::string> aVarNames = _impl->GetVariableNames();
+
+  int aLen = aVarNames.size();
+  aResult->length(aLen);
+  for (int anInd = 0; anInd < aLen; anInd++)
+    aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
+  
+  return aResult._retn();
+}
+
+//============================================================================
+/*! Function : RemoveVariable
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  bool res = _impl->RemoveVariable(std::string(theVarName));
+  if (res && _notifier)
+    _notifier->modifyNB_Notification(theVarName);
+
+  return res;
+}
+
+//============================================================================
+/*! Function : RenameVariable
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  bool res = _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
+  if (res && _notifier)
+    _notifier->modifyNB_Notification(theVarName);
+
+  return res;
+}
+
+//============================================================================
+/*! Function : IsVariableUsed
+ *  Purpose  : 
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return _impl->IsVariableUsed(std::string(theVarName));
+}
+
+
+//============================================================================
+/*! Function : ParseVariables
+ *  Purpose  : 
+ */
+//============================================================================
+SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
+{
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
+
+  std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
+
+  int aSectionsLen = aSections.size();
+  aResult->length(aSectionsLen);
+
+  for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
+    std::vector<std::string> aVarNames = aSections[aSectionInd];
+
+    SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
+
+    int aLen = aVarNames.size();
+    aList->length(aLen);
+
+    for (int anInd = 0; anInd < aLen; anInd++)
+      aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
+
+    aResult[aSectionInd] = aList;
+  }
+
+  return aResult._retn();
+}
+
+//============================================================================
+/*! Function : GetDefaultScript
+ *  Purpose  : 
+ */
+//============================================================================
+char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
+{
+  SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
+  return CORBA::string_dup(script.c_str());
+}
+
+//============================================================================
+/*! Function : EnableUseCaseAutoFilling
+ *  Purpose  : 
+ */
+//============================================================================
+void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
+{ 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->EnableUseCaseAutoFilling(isEnabled); 
+  SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
+  if (builder) {
+    if (isEnabled) {
+      builder->SetOnAddSObject(_impl->GetCallback());
+      builder->SetOnRemoveSObject(_impl->GetCallback());
+    }
+    else {
+      builder->SetOnAddSObject(NULL);
+      builder->SetOnRemoveSObject(NULL);
+    }
+  }
+}
+
+//============================================================================
+/*! Function : attach
+ *  Purpose  : This function attach an observer to the study
+ */
+//============================================================================
+void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs, CORBA::Boolean modify)
+{
+  if (_notifier)
+    static_cast<SALOMEDS::Notifier*>(_notifier)->attach(theObs, modify);
+}
+
+
+//============================================================================
+/*! Function : detach
+ *  Purpose  : This function detaches an observer from the study
+ */
+//============================================================================
+void SALOMEDS_Study_i::detach(SALOMEDS::Observer_ptr theObs)
+{
+  if (_notifier)
+    static_cast<SALOMEDS::Notifier*>(_notifier)->detach(theObs);
+}
+
+//===========================================================================
+//   PRIVATE FUNCTIONS
+//===========================================================================
+CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
+{
+#ifdef WIN32
+  long pid = (long)_getpid();
+#else
+  long pid = (long)getpid();
+#endif  
+  isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
+  return reinterpret_cast<CORBA::LongLong>(_impl);
 }