Salome HOME
- Deleted Study as an input parameter and class field.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
index 0348fbd73b69827bcf35a41fdd5f8019ffe4e336..cd586ec2f80023d78afc04392e2afcadf5bd4b98 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// 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
@@ -6,7 +6,7 @@
 // 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.
+// 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
@@ -25,8 +25,8 @@
 //  Module : SALOME
 //
 #include "utilities.h"
+#include <sstream>
 #include "SALOMEDS_Study_i.hxx"
-#include "SALOMEDS_StudyManager_i.hxx"
 #include "SALOMEDS_UseCaseIterator_i.hxx"
 #include "SALOMEDS_GenericAttribute_i.hxx"
 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
 #include "DF_Label.hxx"
 #include "DF_Attribute.hxx"
 
+#include "Utils_ExceptHandlers.hxx"
+
 #include "Basics_Utils.hxx"
+#include "SALOME_KernelServices.hxx"
 
 #ifdef WIN32
 #include <process.h>
 #include <unistd.h>
 #endif
 
+UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
+UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
+
+static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
+
 namespace SALOMEDS
 {
   class Notifier: public SALOMEDSImpl_AbstractCallback
@@ -147,6 +155,23 @@ namespace SALOMEDS
       myObservers.push_back(std::make_pair(SALOMEDS::Observer::_duplicate(theObs),modify));
     }
 
+    //============================================================================
+    /*! Function : detach
+     *  Purpose  : unregister an Observer
+     */
+    //============================================================================
+
+    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;
@@ -200,26 +225,21 @@ namespace SALOMEDS
 
 } // 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(SALOMEDSImpl_Study* theImpl,
-                                   CORBA::ORB_ptr      orb)
+SALOMEDS_Study_i::SALOMEDS_Study_i(CORBA::ORB_ptr orb)
 {
-  _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);
+  _orb     = CORBA::ORB::_duplicate(orb);
+  _impl    = new SALOMEDSImpl_Study();
+  _factory = new SALOMEDS_DriverFactory_i(_orb);
+  _closed  = true;
 
-  theImpl->setNotifier(_notifier);
-  theImpl->setGenObjRegister( _genObjRegister );
+  Init();
 }
-  
+
 //============================================================================
 /*! Function : ~SALOMEDS_Study_i
  *  Purpose  : SALOMEDS_Study_i destructor
@@ -227,39 +247,291 @@ SALOMEDS_Study_i::SALOMEDS_Study_i(SALOMEDSImpl_Study* theImpl,
 //============================================================================
 SALOMEDS_Study_i::~SALOMEDS_Study_i()
 {
+  Clear();
+  delete _factory;
+  delete _impl;
+}
+
+//============================================================================
+/*! Function : Init
+ *  Purpose  : Initialize study components
+ */
+//============================================================================
+void SALOMEDS_Study_i::Init()
+{
+  if ( !_impl->GetDocument() )
+    _impl->Init();
+
+  if (!_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+
+  _builder        = new SALOMEDS_StudyBuilder_i(_impl->NewBuilder(), _orb);  
+  _notifier       = new SALOMEDS::Notifier(_orb);
+  _genObjRegister = new SALOMEDS::GenObjRegister(_orb);
+  _closed         = false;
+
+  _impl->setNotifier(_notifier);
+  _impl->setGenObjRegister( _genObjRegister );
+
+  // update desktop title with new study name
+  NameChanged();
+
+  // 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";
+    std::string str = ss.str();
+    SALOMEDS::unlock();
+    aSession->emitMessageOneWay(str.c_str());
+    SALOMEDS::lock();
+  }
+}
+
+//============================================================================
+/*! Function : Clear
+ *  Purpose  : Clear study components
+ */
+//============================================================================
+void SALOMEDS_Study_i::Clear()
+{
+  SALOMEDS::Locker lock;
+  if (_closed)
+    return;
   //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();
-  
+
+  RemovePostponed(-1);
+
+  if (_impl->GetDocument()) {
+    SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
+    for (; itcomponent->More(); itcomponent->Next()) {
+      SALOMEDS::SComponent_var sco = itcomponent->Value();
+      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 :"<< 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 cleared
+  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 << "studyCleared";
+    std::string str = ss.str();
+    SALOMEDS::unlock();
+    aSession->emitMessageOneWay(str.c_str());
+    SALOMEDS::lock();
+  }
+
+  _impl->Clear();
   _impl->setNotifier(0);
   delete _notifier;
   delete _genObjRegister;
-  //delete implementation
-  delete _impl;
-  _mapOfStudies.erase(_impl);
-}  
+  _notifier = NULL;
+
+  _closed = true;
+}
 
 //============================================================================
-/*! Function : GetPersistentReference
- *  Purpose  : Get persistent reference of study (idem URL())
+/*! Function : Open
+ *  Purpose  : Open a Study from it's persistent reference
  */
 //============================================================================
-char* SALOMEDS_Study_i::GetPersistentReference()
+bool SALOMEDS_Study_i::Open(const char* aUrl)
+  throw(SALOME::SALOME_Exception)
 {
-  SALOMEDS::Locker lock; 
-  return CORBA::string_dup(_impl->GetPersistentReference().c_str());
+  if (!_closed)
+    Clear();
+  Init();
+  SALOMEDS::Locker lock;
+
+  Unexpect aCatch(SalomeException);
+  MESSAGE("Begin of SALOMEDS_Study_i::Open");
+
+  bool res = _impl->Open(std::string(aUrl));
+
+  // update desktop title with new study name
+  NameChanged();
+
+  if ( !res )
+    THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
+  return res;
+}
+
+//============================================================================
+/*! Function : Save
+ *  Purpose  : Save a Study to it's persistent reference
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::Save(CORBA::Boolean theMultiFile, CORBA::Boolean theASCII)
+{
+  SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+  return _impl->Save(_factory, theMultiFile, theASCII);
+}
+
+//=============================================================================
+/*! Function : SaveAs
+ *  Purpose  : Save a study to the persistent reference aUrl
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::SaveAs(const char* aUrl, CORBA::Boolean theMultiFile, CORBA::Boolean theASCII)
+{
+  SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+  return _impl->SaveAs(std::string(aUrl), _factory, theMultiFile, theASCII);
 }
+
 //============================================================================
-/*! Function : GetTransientReference
- *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
+/*! Function : CanCopy
+ *  Purpose  :
  */
 //============================================================================
-char* SALOMEDS_Study_i::GetTransientReference()
+CORBA::Boolean SALOMEDS_Study_i::CanCopy(SALOMEDS::SObject_ptr theObject)
+{
+  SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
+
+  SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
+  bool ret = _impl->CanCopy(anObject, aDriver);
+  delete aDriver;
+  return ret;
+}
+
+//============================================================================
+/*! Function : Copy
+ *  Purpose  :
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::Copy(SALOMEDS::SObject_ptr theObject)
+{
+  SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
+
+  SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
+  bool ret = _impl->Copy(anObject, aDriver);
+  delete aDriver;
+  return ret;
+}
+
+//============================================================================
+/*! Function : CanPaste
+ *  Purpose  :
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_Study_i::CanPaste(SALOMEDS::SObject_ptr theObject)
+{
+  SALOMEDS::Locker lock;
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();
+
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
+
+  SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
+  bool ret = _impl->CanPaste(anObject, aDriver);
+  delete aDriver;
+  return ret;
+}
+
+//============================================================================
+/*! Function : Paste
+ *  Purpose  :
+ */
+//============================================================================
+SALOMEDS::SObject_ptr SALOMEDS_Study_i::Paste(SALOMEDS::SObject_ptr theObject)
+     throw(SALOMEDS::StudyBuilder::LockProtection)
+{
+  SALOMEDS::Locker lock;
+
+  Unexpect aCatch(LockProtection);
+
+  CORBA::String_var anID = theObject->GetID();
+  SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
+  SALOMEDSImpl_SObject aNewSO;
+
+  try {
+    SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
+    aNewSO =  _impl->Paste(anObject, aDriver);
+    delete aDriver;
+  }
+  catch (...) {
+    throw SALOMEDS::StudyBuilder::LockProtection();
+  }
+
+  SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
+  return so._retn();
+}
+
+SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
+{
+  SALOMEDS_Driver_i* driver = NULL;
+
+  SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
+  if(!aSCO.IsNull()) {
+    std::string IOREngine = aSCO.GetIOR();
+    if(!IOREngine.empty()) {
+      CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
+      Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
+      driver = new SALOMEDS_Driver_i(Engine, orb);
+    }
+  }
+
+  return driver;
+}
+
+//============================================================================
+/*! Function : GetPersistentReference
+ *  Purpose  : Get persistent reference of study (idem URL())
+ */
+//============================================================================
+char* SALOMEDS_Study_i::GetPersistentReference()
 {
   SALOMEDS::Locker lock; 
-  return CORBA::string_dup(_impl->GetTransientReference().c_str()); 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+  return CORBA::string_dup(_impl->GetPersistentReference().c_str());
 }
 
 //============================================================================
@@ -270,6 +542,8 @@ char* SALOMEDS_Study_i::GetTransientReference()
 CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
 {
   SALOMEDS::Locker lock; 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
   return _impl->IsEmpty();
 }
 
@@ -282,10 +556,15 @@ SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponent
 {
   SALOMEDS::Locker lock; 
   
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SComponent_var sco;
+
   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponent(std::string(aComponentName));
-  if(aCompImpl.IsNull()) return SALOMEDS::SComponent::_nil();
+  if (!aCompImpl.IsNull())
+    sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
 
-  SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aCompImpl, _orb);
   return sco._retn();
 }
 
@@ -298,10 +577,15 @@ SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponen
 {
   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()) return SALOMEDS::SComponent::_nil();
+  if (!aCompImpl.IsNull())
+    sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
 
-  SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aCompImpl, _orb);
   return sco._retn();
 }
 
@@ -314,18 +598,23 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
 {
   SALOMEDS::Locker lock; 
 
-  SALOMEDSImpl_SObject aSO = _impl->FindObject(std::string((char*)anObjectName));
-  if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  if(aSO.IsComponent()) {
-    SALOMEDSImpl_SComponent aSCO = aSO;
-    SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aSCO, _orb);
-    return sco._retn();
+  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);
+    }
   }
-   
-  SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
- return so._retn();
+
+  return so._retn();
 }
 
 //============================================================================
@@ -337,9 +626,15 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
 {
   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()) return SALOMEDS::SObject::_nil();
-  SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
+  if (!aSO.IsNull())
+    so = SALOMEDS_SObject_i::New(aSO, _orb);
+
   return so._retn();
 }
 
@@ -352,12 +647,17 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::CreateObjectID(const char* anObjectID)
 {
   SALOMEDS::Locker lock; 
 
-  if(!anObjectID || strlen(anObjectID) == 0) return SALOMEDS::SObject::_nil();
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SObject_var so;
 
-  SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
-  if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
+  if (anObjectID && strlen(anObjectID) > 0) {
+    SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
+    if (!aSO.IsNull())
+      so = SALOMEDS_SObject_i::New(aSO, _orb);
+  }
 
-  SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
   return so._retn();
 }
 
@@ -372,16 +672,21 @@ SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char*
 {
   SALOMEDS::Locker lock; 
 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   std::vector<SALOMEDSImpl_SObject> aSeq = _impl->FindObjectByName(std::string((char*)anObjectName),
-                                                               std::string((char*)aComponentName));
+                                                                  std::string((char*)aComponentName));
+
+  SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
   int aLength = aSeq.size();
-  SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject ;
   listSO->length(aLength);
-  for(int i = 0; i<aLength; i++) {
-    SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSeq[i], _orb);
-    listSO[i] = so ;
+  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();
 }
 
 //============================================================================
@@ -393,10 +698,15 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
 {
   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()) return SALOMEDS::SObject::_nil();
+  if (!aSO.IsNull())
+    so = SALOMEDS_SObject_i::New(aSO, _orb);
 
-  SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
   return so._retn();
 }
 
@@ -409,10 +719,15 @@ SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
 {
   SALOMEDS::Locker lock; 
 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::SObject_var so;
+
   SALOMEDSImpl_SObject aSO = _impl->FindObjectByPath(std::string((char*)thePath));
-  if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
+  if (!aSO.IsNull())
+    so = SALOMEDS_SObject_i::New (aSO, _orb);
 
-  SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
   return so._retn();
 }
 
@@ -425,154 +740,28 @@ char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
 {
   SALOMEDS::Locker lock; 
 
-  std::string aPath("");
-  if(CORBA::is_nil(theObject)) return CORBA::string_dup(aPath.c_str());
-  SALOMEDSImpl_SObject aSO;
-  SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
-
-  if(!CORBA::is_nil(aSObj)) {
-    aSO = _impl->FindObjectID(aSObj->GetID());
-  }
-  else {
-    aSO  = _impl->FindObjectIOR(_orb->object_to_string(theObject));
-  }
-   
-  if(aSO.IsNull()) return CORBA::string_dup(aPath.c_str());
-  
-  aPath = _impl->GetObjectPath(aSO);
-  return  CORBA::string_dup(aPath.c_str());
-}
-
-
-//============================================================================
-/*! Function : SetContext
- *  Purpose  : Sets the current context
- */
-//============================================================================
-void SALOMEDS_Study_i::SetContext(const char* thePath) 
-{
-  SALOMEDS::Locker lock; 
-
-  _impl->SetContext(std::string((char*)thePath));
-  if(_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
-    throw SALOMEDS::Study::StudyInvalidContext();  
-}
-
-//============================================================================
-/*! Function : GetContext
- *  Purpose  : Gets the current context
- */
-//============================================================================
-char* SALOMEDS_Study_i::GetContext() 
-{
-  SALOMEDS::Locker lock; 
-  
-  if(!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();   
-  return CORBA::string_dup(_impl->GetContext().c_str());
-}
-
-//============================================================================
-/*! Function : GetObjectNames
- *  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) 
-{
-  SALOMEDS::Locker lock; 
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  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 (int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-
-  return aResult._retn();
-}
-
-//============================================================================
-/*! Function : GetDirectoryNames
- *  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) 
-{
-  SALOMEDS::Locker lock; 
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  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 (int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-
-  return aResult._retn();
-}
-
-//============================================================================
-/*! Function : GetFileNames
- *  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) 
-{
-  SALOMEDS::Locker lock; 
-
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
-
-  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 (int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
-  }
-
-  return aResult._retn();
-}
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-//============================================================================
-/*! 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) 
-{
-  SALOMEDS::Locker lock; 
+  std::string aPath = "";
 
-  SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
+  if (!CORBA::is_nil(theObject)) {
+    SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
+    SALOMEDSImpl_SObject aSO;
 
-  std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
-
-  int aLength = aSeq.size();
-  aResult->length(aLength);
-  for(int anIndex = 0; anIndex < aLength; anIndex++) {
-    aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
+    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 aResult._retn();
+  return CORBA::string_dup(aPath.c_str());
 }
 
 //============================================================================
@@ -584,14 +773,16 @@ SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject
 {
   SALOMEDS::Locker lock; 
 
-  CORBA::String_var anID=theSO->GetID();
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  CORBA::String_var anID = theSO->GetID();
   SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in());
   SALOMEDSImpl_ChildIterator anItr(aSO);
-
-  //Create iterator
   SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(anItr, _orb);
+  SALOMEDS::ChildIterator_var it = it_servant->_this();
 
-  return it_servant->_this();
+  return it._retn();
 }
 
 
@@ -603,9 +794,15 @@ SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject
 SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
 {
   SALOMEDS::Locker lock; 
-  SALOMEDS_SComponentIterator_i* _it = new SALOMEDS_SComponentIterator_i(_impl->NewComponentIterator(), _orb);
-  _it->Init();
-  return _it->_this();
+
+  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();
 }
 
 
@@ -617,7 +814,13 @@ SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
 SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
 {
   SALOMEDS::Locker lock; 
-  return _builder->_this();
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  SALOMEDS::StudyBuilder_var sb = SALOMEDS::StudyBuilder::_duplicate(_builder->_this());
+
+  return sb._retn();
 }
  
 //============================================================================
@@ -628,29 +831,20 @@ SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
 char* SALOMEDS_Study_i::Name()
 {
   SALOMEDS::Locker lock; 
+  // Name is specified as IDL attribute: user exception cannot be raised
   return CORBA::string_dup(_impl->Name().c_str());
 }
 
-//============================================================================
-/*! Function : Name
- *  Purpose  : set study name
- */
-//============================================================================
-void SALOMEDS_Study_i::Name(const char* name)
-{
-  SALOMEDS::Locker lock;  
-  _impl->Name(std::string(name));
-}
-
 //============================================================================
 /*! Function : IsSaved
  *  Purpose  : get if study has been saved
  */
 //============================================================================
-CORBA::Boolean  SALOMEDS_Study_i::IsSaved()
+CORBA::Boolean SALOMEDS_Study_i::IsSaved()
 {
   SALOMEDS::Locker lock; 
-  return _impl->IsSaved();
+  // IsSaved is specified as IDL attribute: user exception cannot be raised
+  return (!_closed) ? _impl->IsSaved() : false;
 }
 
 //============================================================================
@@ -661,7 +855,9 @@ CORBA::Boolean  SALOMEDS_Study_i::IsSaved()
 void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
 {
   SALOMEDS::Locker lock; 
-  _impl->IsSaved(save);
+  // IsSaved is specified as IDL attribute: user exception cannot be raised
+  if (!_closed)
+    _impl->IsSaved(save);
 }
 
 //============================================================================
@@ -669,9 +865,13 @@ 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();
 }
 
@@ -680,12 +880,15 @@ CORBA::Boolean  SALOMEDS_Study_i::IsModified()
  *  Purpose  : Sets a Modified flag of a Study to True
  */
 //============================================================================
-void  SALOMEDS_Study_i::Modified()
+void SALOMEDS_Study_i::Modified()
 {
   SALOMEDS::Locker lock; 
-  return _impl->Modify();
-}
 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  _impl->Modify();
+}
 
 //============================================================================
 /*! Function : URL
@@ -695,6 +898,7 @@ void  SALOMEDS_Study_i::Modified()
 char* SALOMEDS_Study_i::URL()
 {
   SALOMEDS::Locker lock; 
+  // URL is specified as IDL attribute: user exception cannot be raised
   return CORBA::string_dup(_impl->URL().c_str());
 }
 
@@ -706,55 +910,21 @@ char* SALOMEDS_Study_i::URL()
 void SALOMEDS_Study_i::URL(const char* url)
 {
   SALOMEDS::Locker lock; 
+  // URL is specified as IDL attribute: user exception cannot be raised
   _impl->URL(std::string((char*)url));
-}
-
-
-CORBA::Short SALOMEDS_Study_i::StudyId()
-{
-  SALOMEDS::Locker lock; 
-  return _impl->StudyId();
-}
 
-void SALOMEDS_Study_i::StudyId(CORBA::Short id)
-{ 
-  SALOMEDS::Locker lock; 
-  _impl->StudyId(id);
+  // update desktop title with new study name
+  NameChanged();
 }
 
-void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR,const char* anEntry) 
+void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry) 
 {
   SALOMEDS::Locker lock; 
-  _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
-}
 
-SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb) 
-{
-  SALOMEDS::Locker lock; 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-  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));
-    return SALOMEDS::Study::_duplicate(aStudy);
-  } else {
-    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;
-    }
+  _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
 }
 
 void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
@@ -767,6 +937,9 @@ SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObj
 {
   SALOMEDS::Locker lock; 
 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDS::GenericAttribute_ptr aTarget;
   if (anObject->FindAttribute(aTarget,"AttributeTarget")) {
     return SALOMEDS::AttributeTarget::_narrow(aTarget)->Get();
@@ -781,14 +954,22 @@ SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties()
 {
   SALOMEDS::Locker lock; 
   
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   SALOMEDSImpl_AttributeStudyProperties* anAttr = _impl->GetProperties();
   SALOMEDS_AttributeStudyProperties_i* SP = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
-  return SP->AttributeStudyProperties::_this();
+  SALOMEDS::AttributeStudyProperties_var aProp = SP->_this();
+  return aProp._retn();
 }
 
 char* SALOMEDS_Study_i::GetLastModificationDate() 
 {
   SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   return CORBA::string_dup(_impl->GetLastModificationDate().c_str());
 }
 
@@ -796,19 +977,22 @@ 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();
-  SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
   aDates->length(aLength);
-
-  for(int anIndex = 0; anIndex < aLength; anIndex++) {
+  for (int anIndex = 0; anIndex < aLength; anIndex++) {
     aDates[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
   }
+
   return aDates._retn();
 }
 
-
-
 //============================================================================
 /*! Function : GetUseCaseBuilder
  *  Purpose  : Returns a UseCase builder
@@ -817,58 +1001,13 @@ SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate()
 SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder() 
 {
   SALOMEDS::Locker lock; 
-  SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
-  return UCBuilder->_this();
-}
 
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
 
-//============================================================================
-/*! Function : Close
- *  Purpose  : 
- */
-//============================================================================
-void SALOMEDS_Study_i::Close()
-{
-  SALOMEDS::Locker lock; 
-
-  RemovePostponed(-1);
-
-  SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
-  for (; itcomponent->More(); itcomponent->Next()) {
-    SALOMEDS::SComponent_var sco = itcomponent->Value();
-    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 :"<< 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&) 
-        {/*pass*/ }
-    }
-    sco->UnRegister();
-  }
-
-  //Does not need any more this iterator
-  itcomponent->UnRegister();
-
-
-  _impl->Close();
+  SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
+  SALOMEDS::UseCaseBuilder_var uc = UCBuilder->_this();
+  return uc._retn();
 }
 
 //============================================================================
@@ -893,24 +1032,23 @@ void SALOMEDS_Study_i::AddCreatedPostponed(const char* theIOR)
  *  Purpose  : 
  */
 //============================================================================
-#ifndef WIN32
-void SALOMEDS_Study_i::RemovePostponed(const CORBA::Long /*theUndoLimit*/) 
-#else
 void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/) 
-#endif
 {  
   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++) {
+  
+  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();
+      //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 (...) {}
   }
@@ -923,11 +1061,7 @@ void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/)
  *  Purpose  : 
  */
 //============================================================================
-#ifndef WIN32
-void SALOMEDS_Study_i::UndoPostponed(const CORBA::Long theWay) 
-#else
 void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
-#endif
 {
   SALOMEDS::Locker lock; 
   //Not implemented
@@ -946,10 +1080,14 @@ CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath,
 {
   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);
-  CORBA::Boolean ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
+  bool ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
   delete factory;
+
   return ret;
 }
 
@@ -962,9 +1100,14 @@ SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const cha
 {
   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);
-  return SP->AttributeParameter::_this();
+  SALOMEDS::AttributeParameter_var aParam = SP->_this();
+
+  return aParam._retn();
 }
  
 //============================================================================
@@ -978,9 +1121,14 @@ SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const cha
 {
   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);
-  return SP->AttributeParameter::_this();
+  SALOMEDS::AttributeParameter_var aParam = SP->_this();
+
+  return aParam._retn();
 }
 
 //============================================================================
@@ -991,6 +1139,10 @@ SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const cha
 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
 {
   SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   _impl->SetStudyLock(theLockerID);
 }
 
@@ -1002,6 +1154,10 @@ void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
 bool SALOMEDS_Study_i::IsStudyLocked()
 {
   SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   return _impl->IsStudyLocked();
 }
 
@@ -1013,6 +1169,10 @@ bool SALOMEDS_Study_i::IsStudyLocked()
 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
 {
   SALOMEDS::Locker lock; 
+
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   _impl->UnLockStudy(theLockerID);
 }
 
@@ -1025,6 +1185,9 @@ 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();
@@ -1034,6 +1197,7 @@ SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
   for(int anIndex = 0; anIndex < aLength; anIndex++) {
     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
   }
+
   return aResult._retn();
 }
 //============================================================================
@@ -1043,10 +1207,14 @@ SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
 //============================================================================
 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)
+                    theValue,
+                    SALOMEDSImpl_GenericVariable::REAL_VAR);
+  if (_notifier)
     _notifier->modifyNB_Notification(theVarName);
 }
 
@@ -1057,10 +1225,13 @@ void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
 //============================================================================
 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)
+                    theValue,
+                    SALOMEDSImpl_GenericVariable::INTEGER_VAR);
+  if (_notifier)
     _notifier->modifyNB_Notification(theVarName);
 }
 
@@ -1071,10 +1242,13 @@ void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
 //============================================================================
 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)
+                    theValue,
+                    SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
+  if (_notifier)
     _notifier->modifyNB_Notification(theVarName);
 }
 
@@ -1085,10 +1259,13 @@ void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValu
 //============================================================================
 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)
+                          theValue,
+                          SALOMEDSImpl_GenericVariable::STRING_VAR);
+  if (_notifier)
     _notifier->modifyNB_Notification(theVarName);
 }
 
@@ -1099,9 +1276,12 @@ void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
 //============================================================================
 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);
+                                  theValue,
+                                  SALOMEDSImpl_GenericVariable::STRING_VAR);
 }
 
 //============================================================================
@@ -1111,6 +1291,9 @@ void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double t
 //============================================================================
 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
 {
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   return _impl->GetVariableValue(std::string(theVarName));
 }
 
@@ -1121,7 +1304,10 @@ CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
 //============================================================================
 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
 {
-  return (int)_impl->GetVariableValue(std::string(theVarName));
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  return (long)_impl->GetVariableValue(std::string(theVarName));
 }
 
 //============================================================================
@@ -1131,6 +1317,9 @@ CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
 {
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   return (bool)_impl->GetVariableValue(std::string(theVarName));
 }
 
@@ -1141,6 +1330,9 @@ CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
 //============================================================================
 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());
 }
 
@@ -1151,8 +1343,11 @@ char* SALOMEDS_Study_i::GetString(const char* theVarName)
 //============================================================================
 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);
+                        SALOMEDSImpl_GenericVariable::REAL_VAR);
 }
 
 //============================================================================
@@ -1162,8 +1357,11 @@ CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
 //============================================================================
 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);
+                        SALOMEDSImpl_GenericVariable::INTEGER_VAR);
 }
 
 //============================================================================
@@ -1173,8 +1371,11 @@ CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
 //============================================================================
 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);
+                        SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
 }
 
 //============================================================================
@@ -1184,8 +1385,11 @@ CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
 //============================================================================
 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);
+                        SALOMEDSImpl_GenericVariable::STRING_VAR);
 }
 
 //============================================================================
@@ -1195,6 +1399,9 @@ CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
 {
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   return _impl->IsVariable(std::string(theVarName));
 }
 
@@ -1205,12 +1412,15 @@ CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
 //============================================================================
 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
 {
-  std::vector<std::string> aVarNames = _impl->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());
   
@@ -1224,9 +1434,13 @@ SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
 {
-  CORBA::Boolean res = _impl->RemoveVariable(std::string(theVarName));
-  if(res && _notifier)
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
+  bool res = _impl->RemoveVariable(std::string(theVarName));
+  if (res && _notifier)
     _notifier->modifyNB_Notification(theVarName);
+
   return res;
 }
 
@@ -1237,9 +1451,13 @@ CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
 {
-  CORBA::Boolean res = _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
-  if(res && _notifier)
+  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;
 }
 
@@ -1250,6 +1468,9 @@ CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const ch
 //============================================================================
 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
 {
+  if (_closed)
+    throw SALOMEDS::Study::StudyInvalidReference();  
+
   return _impl->IsVariableUsed(std::string(theVarName));
 }
 
@@ -1261,10 +1482,13 @@ CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
 //============================================================================
 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
 {
-  std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(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);
 
@@ -1294,6 +1518,9 @@ char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char*
 {
   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());
 }
@@ -1305,10 +1532,13 @@ char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char*
 //============================================================================
 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) {
+  if (builder) {
+    if (isEnabled) {
       builder->SetOnAddSObject(_impl->GetCallback());
       builder->SetOnRemoveSObject(_impl->GetCallback());
     }
@@ -1319,15 +1549,48 @@ void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled)
   }
 }
 
+
+CORBA::Long SALOMEDS_Study_i::getPID()
+{
+#ifdef WIN32
+  return (CORBA::Long)_getpid();
+#else
+  return (CORBA::Long)getpid();
+#endif
+}
+
+void SALOMEDS_Study_i::ShutdownWithExit()
+{
+  exit( EXIT_SUCCESS );
+}
+
+void SALOMEDS_Study_i::Shutdown()
+{
+  if(!CORBA::is_nil(_orb))
+    _orb->shutdown(0);
+}
+
 //============================================================================
 /*! Function : attach
  *  Purpose  : This function attach an observer to the study
  */
 //============================================================================
-void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs,CORBA::Boolean modify)
+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)->attach(theObs,modify);
+  if (_notifier)
+    static_cast<SALOMEDS::Notifier*>(_notifier)->detach(theObs);
 }
 
 //===========================================================================
@@ -1343,3 +1606,19 @@ CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::L
   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
   return reinterpret_cast<CORBA::LongLong>(_impl);
 }
+
+void SALOMEDS_Study_i::NameChanged()
+{
+  // Notify GUI that the name of study was changed
+  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 << "studyNameChanged";
+    std::string str = ss.str();
+    SALOMEDS::unlock();
+    aSession->emitMessageOneWay(str.c_str());
+    SALOMEDS::lock();
+  }
+}