]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_Study_i.cxx
Salome HOME
add method NameChanged to update title name
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDS_Study_i.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28 #include <sstream>
29 #include "SALOMEDS_Study_i.hxx"
30 #include "SALOMEDS_UseCaseIterator_i.hxx"
31 #include "SALOMEDS_GenericAttribute_i.hxx"
32 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
33 #include "SALOMEDS_AttributeParameter_i.hxx"
34 #include "SALOMEDS_ChildIterator_i.hxx"
35 #include "SALOMEDS_Driver_i.hxx"
36 #include "SALOMEDS.hxx"
37
38 #include "SALOMEDSImpl_SObject.hxx"
39 #include "SALOMEDSImpl_SComponent.hxx"
40 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
41 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
42 #include "SALOMEDSImpl_AttributeParameter.hxx"
43 #include "SALOMEDSImpl_ChildIterator.hxx"
44 #include "SALOMEDSImpl_IParameters.hxx"
45 #include "SALOMEDSImpl_Callback.hxx"
46
47 #include "DF_Label.hxx"
48 #include "DF_Attribute.hxx"
49
50 #include "Utils_ExceptHandlers.hxx"
51
52 #include "Basics_Utils.hxx"
53 #include "SALOME_KernelServices.hxx"
54
55 #ifdef WIN32
56 #include <process.h>
57 #else
58 #include <sys/types.h>
59 #include <unistd.h>
60 #endif
61
62 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
63 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
64
65 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
66
67 namespace SALOMEDS
68 {
69   class Notifier: public SALOMEDSImpl_AbstractCallback
70   {
71   public:
72     Notifier(CORBA::ORB_ptr orb)
73     {
74       _orb = CORBA::ORB::_duplicate(orb);
75     }
76
77     //============================================================================
78     /*! Function : addSO_Notification
79      *  Purpose  : This function tells all the observers that a SO has been added
80      */
81     //============================================================================
82
83     virtual bool addSO_Notification(const SALOMEDSImpl_SObject& theSObject)
84     {
85       std::string anID=theSObject.GetID();
86       const char* cID=anID.c_str();
87       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
88       {
89         it->first->notifyObserverID(cID,1);
90       }
91       return true; // NGE return always true but can be modified if needed
92     }
93
94     //============================================================================
95     /*! Function : removeSO_Notification
96      *  Purpose  : This function tells all the observers that a SO has been removed
97      */
98     //============================================================================
99
100     virtual bool removeSO_Notification(const SALOMEDSImpl_SObject& theSObject)
101     {
102       std::string anID=theSObject.GetID();
103       const char* cID=anID.c_str();
104       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
105       {
106         it->first->notifyObserverID(cID,2);
107       }
108       return true; // NGE return always true but can be modified if needed
109     }
110
111     //============================================================================
112     /*! Function : modifySO_Notification
113      *  Purpose  : This function tells all the observers that a SO has been modified
114      */
115     //============================================================================
116
117     virtual bool modifySO_Notification(const SALOMEDSImpl_SObject& theSObject, int reason)
118     {
119       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
120       {
121         if(it->second)
122         {
123           std::string anID=theSObject.GetID();
124           const char* cID=anID.c_str();
125           it->first->notifyObserverID(cID,reason);
126         }
127       }
128       return true; // NGE return always true but can be modified if needed
129     }
130
131     //============================================================================
132     /*! Function : modifyNB_Notification
133      *  Purpose  : This function tells all the observers that 
134      *             a NoteBook variable has been added/modified/removed.
135      */
136     //============================================================================
137     
138     virtual bool modifyNB_Notification(const char* theVarName)
139     {
140       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
141         {
142           it->first->notifyObserverID(theVarName,6);
143         }
144       return true; // NGE return always true but can be modified if needed
145     }
146
147     //============================================================================
148     /*! Function : attach
149      *  Purpose  : register an Observer
150      */
151     //============================================================================
152
153     virtual void attach(SALOMEDS::Observer_ptr theObs, bool modify)
154     {
155       myObservers.push_back(std::make_pair(SALOMEDS::Observer::_duplicate(theObs),modify));
156     }
157
158     //============================================================================
159     /*! Function : detach
160      *  Purpose  : unregister an Observer
161      */
162     //============================================================================
163
164     virtual void detach(SALOMEDS::Observer_ptr theObs)
165     {
166       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
167       {
168         if ( it->first->_is_equivalent(theObs) ) {
169           myObservers.erase( it );
170           break;
171         }
172       }
173     }
174
175   private:
176     typedef std::list< std::pair< SALOMEDS::Observer_var, bool > > ObsList;
177     typedef ObsList::iterator ObsListIter;
178     ObsList myObservers;
179     CORBA::ORB_var                    _orb;
180   };
181
182   class GenObjRegister: public SALOMEDSImpl_AbstractCallback
183   {
184   public:
185     GenObjRegister(CORBA::ORB_ptr orb)
186     {
187       _orb = CORBA::ORB::_duplicate(orb);
188     }
189     virtual void RegisterGenObj  (const std::string& theIOR)
190     {
191       try
192       {
193         CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
194         if ( obj->_non_existent() ) return;
195         SALOME::GenericObj_var gobj = SALOME::GenericObj::_narrow(obj);
196         if(! CORBA::is_nil(gobj) )
197         {
198           gobj->Register();
199         }
200       }
201       catch(const CORBA::Exception& e)
202       {
203       }
204     }
205     virtual void UnRegisterGenObj(const std::string& theIOR)
206     {
207       try
208       {
209         CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
210         if ( obj->_non_existent() ) return;
211         SALOME::GenericObj_var gobj = SALOME::GenericObj::_narrow(obj);
212         if(! CORBA::is_nil(gobj) )
213         {
214           gobj->UnRegister();
215         }
216       }
217       catch(const CORBA::Exception& e)
218       {
219       }
220     }
221
222   private:
223     CORBA::ORB_var _orb;
224   };
225
226 } // namespace SALOMEDS
227
228 //============================================================================
229 /*! Function : SALOMEDS_Study_i
230  *  Purpose  : SALOMEDS_Study_i constructor
231  */
232 //============================================================================
233 SALOMEDS_Study_i::SALOMEDS_Study_i(CORBA::ORB_ptr orb)
234 {
235   _orb     = CORBA::ORB::_duplicate(orb);
236   _impl    = new SALOMEDSImpl_Study();
237   _factory = new SALOMEDS_DriverFactory_i(_orb);
238
239   Init();
240 }
241
242 //============================================================================
243 /*! Function : ~SALOMEDS_Study_i
244  *  Purpose  : SALOMEDS_Study_i destructor
245  */
246 //============================================================================
247 SALOMEDS_Study_i::~SALOMEDS_Study_i()
248 {
249   Clear();
250   delete _factory;
251   delete _impl;
252 }
253
254 //============================================================================
255 /*! Function : Init
256  *  Purpose  : Initialize study components
257  */
258 //============================================================================
259 void SALOMEDS_Study_i::Init()
260 {
261   if ( !_impl->GetDocument() )
262     _impl->Init();
263
264   _builder        = new SALOMEDS_StudyBuilder_i(_impl->NewBuilder(), _orb);  
265   _notifier       = new SALOMEDS::Notifier(_orb);
266   _genObjRegister = new SALOMEDS::GenObjRegister(_orb);
267   _closed         = false;
268
269   _impl->setNotifier(_notifier);
270   _impl->setGenObjRegister( _genObjRegister );
271
272   // update desktop title with new study name
273   NameChanged();
274
275   // Notify GUI that study was created
276   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
277   CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
278   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
279   if ( !CORBA::is_nil(aSession) ) {
280     std::stringstream ss;
281     ss << "studyCreated";
282     std::string str = ss.str();
283     SALOMEDS::unlock();
284     aSession->emitMessageOneWay(str.c_str());
285     SALOMEDS::lock();
286   }
287 }
288
289 //============================================================================
290 /*! Function : Clear
291  *  Purpose  : Clear study components
292  */
293 //============================================================================
294 void SALOMEDS_Study_i::Clear()
295 {
296   //delete the builder servant
297   PortableServer::POA_var poa=_builder->_default_POA();
298   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_builder);
299   poa->deactivate_object(anObjectId.in());
300   _builder->_remove_ref();
301
302   SALOMEDS::Locker lock;
303
304   if (_closed)
305     throw SALOMEDS::Study::StudyInvalidReference();
306
307   RemovePostponed(-1);
308   if (_impl->GetDocument()) {
309     SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
310     for (; itcomponent->More(); itcomponent->Next()) {
311       SALOMEDS::SComponent_var sco = itcomponent->Value();
312       CORBA::String_var compodatatype=sco->ComponentDataType();
313       MESSAGE ( "Look for an engine for data type :"<< compodatatype);
314       // if there is an associated Engine call its method for closing
315       CORBA::String_var IOREngine;
316       if (sco->ComponentIOR(IOREngine)) {
317         // we have found the associated engine to write the data
318         MESSAGE ( "We have found an engine for data type :"<< compodatatype);
319         //_narrow can throw a corba exception
320         try {
321               CORBA::Object_var obj = _orb->string_to_object(IOREngine);
322               if (!CORBA::is_nil(obj)) {
323                 SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
324                 if (!anEngine->_is_nil())  {
325                   SALOMEDS::unlock();
326                   anEngine->Close(sco);
327                   SALOMEDS::lock();
328                 }
329               }
330         }
331         catch (CORBA::Exception&) {
332         }
333       }
334       sco->UnRegister();
335     }
336
337     //Does not need any more this iterator
338     itcomponent->UnRegister();
339   }
340
341   // Notify GUI that study is cleared
342   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
343   CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
344   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
345   if ( !CORBA::is_nil(aSession) ) {
346     std::stringstream ss;
347     ss << "studyCleared";
348     std::string str = ss.str();
349     SALOMEDS::unlock();
350     aSession->emitMessageOneWay(str.c_str());
351     SALOMEDS::lock();
352   }
353
354   _impl->Clear();
355   _impl->setNotifier(0);
356   delete _notifier;
357   delete _genObjRegister;
358
359   _closed = true;
360 }
361
362 //============================================================================
363 /*! Function : Open
364  *  Purpose  : Open a Study from it's persistent reference
365  */
366 //============================================================================
367 bool SALOMEDS_Study_i::Open(const char* aUrl)
368   throw(SALOME::SALOME_Exception)
369 {
370   SALOMEDS::Locker lock;
371
372   Unexpect aCatch(SalomeException);
373   MESSAGE("Begin of SALOMEDS_Study_i::Open");
374
375   bool res = _impl->Open(std::string(aUrl));
376
377   // update desktop title with new study name
378   NameChanged();
379
380   if ( !res )
381     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
382   return res;
383 }
384
385 //============================================================================
386 /*! Function : Save
387  *  Purpose  : Save a Study to it's persistent reference
388  */
389 //============================================================================
390 CORBA::Boolean SALOMEDS_Study_i::Save(CORBA::Boolean theMultiFile)
391 {
392   SALOMEDS::Locker lock;
393   return _impl->Save(_factory, theMultiFile);
394 }
395
396 CORBA::Boolean SALOMEDS_Study_i::SaveASCII(CORBA::Boolean theMultiFile)
397 {
398   SALOMEDS::Locker lock;
399   return _impl->SaveASCII(_factory, theMultiFile);
400 }
401
402 //=============================================================================
403 /*! Function : SaveAs
404  *  Purpose  : Save a study to the persistent reference aUrl
405  */
406 //============================================================================
407 CORBA::Boolean SALOMEDS_Study_i::SaveAs(const char* aUrl, CORBA::Boolean theMultiFile)
408 {
409   SALOMEDS::Locker lock;
410   return _impl->SaveAs(std::string(aUrl), _factory, theMultiFile);
411 }
412
413 CORBA::Boolean SALOMEDS_Study_i::SaveAsASCII(const char* aUrl, CORBA::Boolean theMultiFile)
414 {
415   SALOMEDS::Locker lock;
416   return _impl->SaveAsASCII(std::string(aUrl), _factory, theMultiFile);
417 }
418
419 //============================================================================
420 /*! Function : CanCopy
421  *  Purpose  :
422  */
423 //============================================================================
424 CORBA::Boolean SALOMEDS_Study_i::CanCopy(SALOMEDS::SObject_ptr theObject)
425 {
426   SALOMEDS::Locker lock;
427
428   CORBA::String_var anID = theObject->GetID();
429   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
430
431   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
432   bool ret = _impl->CanCopy(anObject, aDriver);
433   delete aDriver;
434   return ret;
435 }
436
437 //============================================================================
438 /*! Function : Copy
439  *  Purpose  :
440  */
441 //============================================================================
442 CORBA::Boolean SALOMEDS_Study_i::Copy(SALOMEDS::SObject_ptr theObject)
443 {
444   SALOMEDS::Locker lock;
445
446   CORBA::String_var anID = theObject->GetID();
447   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
448
449   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
450   bool ret = _impl->Copy(anObject, aDriver);
451   delete aDriver;
452   return ret;
453 }
454
455 //============================================================================
456 /*! Function : CanPaste
457  *  Purpose  :
458  */
459 //============================================================================
460 CORBA::Boolean SALOMEDS_Study_i::CanPaste(SALOMEDS::SObject_ptr theObject)
461 {
462   SALOMEDS::Locker lock;
463
464   CORBA::String_var anID = theObject->GetID();
465   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
466
467   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
468   bool ret = _impl->CanPaste(anObject, aDriver);
469   delete aDriver;
470   return ret;
471 }
472
473 //============================================================================
474 /*! Function : Paste
475  *  Purpose  :
476  */
477 //============================================================================
478 SALOMEDS::SObject_ptr SALOMEDS_Study_i::Paste(SALOMEDS::SObject_ptr theObject)
479      throw(SALOMEDS::StudyBuilder::LockProtection)
480 {
481   SALOMEDS::Locker lock;
482
483   Unexpect aCatch(LockProtection);
484
485   CORBA::String_var anID = theObject->GetID();
486   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
487   SALOMEDSImpl_SObject aNewSO;
488
489   try {
490     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
491     aNewSO =  _impl->Paste(anObject, aDriver);
492     delete aDriver;
493   }
494   catch (...) {
495     throw SALOMEDS::StudyBuilder::LockProtection();
496   }
497
498   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
499   return so._retn();
500 }
501
502 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
503 {
504   SALOMEDS_Driver_i* driver = NULL;
505
506   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
507   if(!aSCO.IsNull()) {
508     std::string IOREngine = aSCO.GetIOR();
509     if(!IOREngine.empty()) {
510       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
511       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
512       driver = new SALOMEDS_Driver_i(Engine, orb);
513     }
514   }
515
516   return driver;
517 }
518
519 //============================================================================
520 /*! Function : GetPersistentReference
521  *  Purpose  : Get persistent reference of study (idem URL())
522  */
523 //============================================================================
524 char* SALOMEDS_Study_i::GetPersistentReference()
525 {
526   SALOMEDS::Locker lock; 
527   if (_closed)
528     throw SALOMEDS::Study::StudyInvalidReference();  
529   return CORBA::string_dup(_impl->GetPersistentReference().c_str());
530 }
531 //============================================================================
532 /*! Function : GetTransientReference
533  *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
534  */
535 //============================================================================
536 char* SALOMEDS_Study_i::GetTransientReference()
537 {
538   SALOMEDS::Locker lock; 
539   if (_closed)
540     throw SALOMEDS::Study::StudyInvalidReference();  
541   return CORBA::string_dup(_impl->GetTransientReference().c_str());
542 }
543
544 //============================================================================
545 /*! Function : IsEmpty
546  *  Purpose  : Detect if study is empty
547  */
548 //============================================================================
549 CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
550 {
551   SALOMEDS::Locker lock; 
552   if (_closed)
553     throw SALOMEDS::Study::StudyInvalidReference();  
554   return _impl->IsEmpty();
555 }
556
557 //============================================================================
558 /*! Function : FindComponent
559  *  Purpose  : Find a Component with ComponentDataType = aComponentName
560  */
561 //============================================================================
562 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponentName)
563 {
564   SALOMEDS::Locker lock; 
565   
566   if (_closed)
567     throw SALOMEDS::Study::StudyInvalidReference();  
568
569   SALOMEDS::SComponent_var sco;
570
571   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponent(std::string(aComponentName));
572   if (!aCompImpl.IsNull())
573     sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
574
575   return sco._retn();
576 }
577
578 //============================================================================
579 /*! Function : FindComponentID
580  *  Purpose  : Find a Component from it's ID
581  */
582 //============================================================================
583 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponentID)
584 {
585   SALOMEDS::Locker lock; 
586   
587   if (_closed)
588     throw SALOMEDS::Study::StudyInvalidReference();  
589
590   SALOMEDS::SComponent_var sco;
591
592   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponentID(std::string((char*)aComponentID));
593   if (!aCompImpl.IsNull())
594     sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
595
596   return sco._retn();
597 }
598
599 //============================================================================
600 /*! Function : FindObject
601  *  Purpose  : Find an Object with SALOMEDS::Name = anObjectName
602  */
603 //============================================================================
604 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
605 {
606   SALOMEDS::Locker lock; 
607
608   if (_closed)
609     throw SALOMEDS::Study::StudyInvalidReference();  
610
611   SALOMEDS::SObject_var so;
612
613   SALOMEDSImpl_SObject aSO = _impl->FindObject(std::string((char*)anObjectName));
614   if (!aSO.IsNull()) {
615     if (aSO.IsComponent()) {
616       SALOMEDSImpl_SComponent aSCO = aSO;
617       so = SALOMEDS_SComponent_i::New(aSCO, _orb);
618     }
619     else {
620       so = SALOMEDS_SObject_i::New(aSO, _orb);
621     }
622   }
623
624   return so._retn();
625 }
626
627 //============================================================================
628 /*! Function : FindObjectID
629  *  Purpose  : Find an Object with ID = anObjectID
630  */
631 //============================================================================
632 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
633 {
634   SALOMEDS::Locker lock; 
635
636   if (_closed)
637     throw SALOMEDS::Study::StudyInvalidReference();  
638
639   SALOMEDS::SObject_var so;
640
641   SALOMEDSImpl_SObject aSO = _impl->FindObjectID(std::string((char*)anObjectID));
642   if (!aSO.IsNull())
643     so = SALOMEDS_SObject_i::New(aSO, _orb);
644
645   return so._retn();
646 }
647
648 //============================================================================
649 /*! Function : CreateObjectID
650  *  Purpose  : Creates an Object with ID = anObjectID
651  */
652 //============================================================================
653 SALOMEDS::SObject_ptr SALOMEDS_Study_i::CreateObjectID(const char* anObjectID)
654 {
655   SALOMEDS::Locker lock; 
656
657   if (_closed)
658     throw SALOMEDS::Study::StudyInvalidReference();  
659
660   SALOMEDS::SObject_var so;
661
662   if (anObjectID && strlen(anObjectID) > 0) {
663     SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
664     if (!aSO.IsNull())
665       so = SALOMEDS_SObject_i::New(aSO, _orb);
666   }
667
668   return so._retn();
669 }
670
671 //============================================================================
672 /*! Function : FindObjectByName
673  *  Purpose  : Find Objects with SALOMEDS::Name = anObjectName in a Component
674  *           : with ComponentDataType = aComponentName
675  */
676 //============================================================================
677 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char* anObjectName,
678                                                                     const char* aComponentName )
679 {
680   SALOMEDS::Locker lock; 
681
682   if (_closed)
683     throw SALOMEDS::Study::StudyInvalidReference();  
684
685   std::vector<SALOMEDSImpl_SObject> aSeq = _impl->FindObjectByName(std::string((char*)anObjectName),
686                                                                    std::string((char*)aComponentName));
687
688   SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
689   int aLength = aSeq.size();
690   listSO->length(aLength);
691   for (int i = 0; i < aLength; i++) {
692     SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New(aSeq[i], _orb);
693     listSO[i] = so;
694   }
695   
696   return listSO._retn();
697 }
698
699 //============================================================================
700 /*! Function : FindObjectIOR
701  *  Purpose  : Find an Object with IOR = anObjectIOR
702  */
703 //============================================================================
704 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
705 {
706   SALOMEDS::Locker lock; 
707
708   if (_closed)
709     throw SALOMEDS::Study::StudyInvalidReference();  
710
711   SALOMEDS::SObject_var so;
712
713   SALOMEDSImpl_SObject aSO = _impl->FindObjectIOR(std::string((char*)anObjectIOR));
714   if (!aSO.IsNull())
715     so = SALOMEDS_SObject_i::New(aSO, _orb);
716
717   return so._retn();
718 }
719
720 //============================================================================
721 /*! Function : FindObjectByPath
722  *  Purpose  : Find an Object by its path = thePath
723  */
724 //============================================================================
725 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
726 {
727   SALOMEDS::Locker lock; 
728
729   if (_closed)
730     throw SALOMEDS::Study::StudyInvalidReference();  
731
732   SALOMEDS::SObject_var so;
733
734   SALOMEDSImpl_SObject aSO = _impl->FindObjectByPath(std::string((char*)thePath));
735   if (!aSO.IsNull())
736     so = SALOMEDS_SObject_i::New (aSO, _orb);
737
738   return so._retn();
739 }
740
741 //============================================================================
742 /*! Function : GetObjectPath
743  *  Purpose  : 
744  */
745 //============================================================================
746 char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
747 {
748   SALOMEDS::Locker lock; 
749
750   if (_closed)
751     throw SALOMEDS::Study::StudyInvalidReference();  
752
753   std::string aPath = "";
754
755   if (!CORBA::is_nil(theObject)) {
756     SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
757     SALOMEDSImpl_SObject aSO;
758
759     if (!CORBA::is_nil(aSObj)) {
760       aSO = _impl->FindObjectID(aSObj->GetID());
761     }
762     else {
763       aSO = _impl->FindObjectIOR(_orb->object_to_string(theObject));
764     }
765     
766     if (!aSO.IsNull()) {    
767       aPath = _impl->GetObjectPath(aSO);
768     }
769   }
770
771   return CORBA::string_dup(aPath.c_str());
772 }
773
774
775 //============================================================================
776 /*! Function : SetContext
777  *  Purpose  : Sets the current context
778  */
779 //============================================================================
780 void SALOMEDS_Study_i::SetContext(const char* thePath) 
781 {
782   SALOMEDS::Locker lock; 
783
784   if (_closed)
785     throw SALOMEDS::Study::StudyInvalidReference();  
786
787   _impl->SetContext(std::string((char*)thePath));
788   if (_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
789     throw SALOMEDS::Study::StudyInvalidContext();  
790 }
791
792 //============================================================================
793 /*! Function : GetContext
794  *  Purpose  : Gets the current context
795  */
796 //============================================================================
797 char* SALOMEDS_Study_i::GetContext() 
798 {
799   SALOMEDS::Locker lock; 
800   
801   if (_closed)
802     throw SALOMEDS::Study::StudyInvalidReference();  
803
804   if (!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();
805
806   return CORBA::string_dup(_impl->GetContext().c_str());
807 }
808
809 //============================================================================
810 /*! Function : GetObjectNames
811  *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
812  */
813 //============================================================================
814 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) 
815 {
816   SALOMEDS::Locker lock; 
817
818   if (_closed)
819     throw SALOMEDS::Study::StudyInvalidReference();  
820
821   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
822
823   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
824     throw SALOMEDS::Study::StudyInvalidContext();
825   
826   std::vector<std::string> aSeq = _impl->GetObjectNames(std::string((char*)theContext));
827   if (_impl->GetErrorCode() == "InvalidContext")
828     throw SALOMEDS::Study::StudyInvalidContext();
829
830   int aLength = aSeq.size();
831   aResult->length(aLength);
832   for (int anIndex = 0; anIndex < aLength; anIndex++) {
833     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
834   }
835
836   return aResult._retn();
837 }
838
839 //============================================================================
840 /*! Function : GetDirectoryNames
841  *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
842  */
843 //============================================================================
844 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) 
845 {
846   SALOMEDS::Locker lock; 
847
848   if (_closed)
849     throw SALOMEDS::Study::StudyInvalidReference();  
850
851   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
852
853   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
854     throw SALOMEDS::Study::StudyInvalidContext();
855   
856   std::vector<std::string> aSeq = _impl->GetDirectoryNames(std::string((char*)theContext));
857   if (_impl->GetErrorCode() == "InvalidContext")
858     throw SALOMEDS::Study::StudyInvalidContext();
859
860   int aLength = aSeq.size();
861   aResult->length(aLength);
862   for (int anIndex = 0; anIndex < aLength; anIndex++) {
863     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
864   }
865   
866   return aResult._retn();
867 }
868
869 //============================================================================
870 /*! Function : GetFileNames
871  *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
872  */
873 //============================================================================
874 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) 
875 {
876   SALOMEDS::Locker lock; 
877
878   if (_closed)
879     throw SALOMEDS::Study::StudyInvalidReference();  
880
881   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
882
883   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
884     throw SALOMEDS::Study::StudyInvalidContext();
885   
886   std::vector<std::string> aSeq = _impl->GetFileNames(std::string((char*)theContext));
887   if (_impl->GetErrorCode() == "InvalidContext")
888     throw SALOMEDS::Study::StudyInvalidContext();
889
890   int aLength = aSeq.size();
891   aResult->length(aLength);
892   for (int anIndex = 0; anIndex < aLength; anIndex++) {
893     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
894   }
895
896   return aResult._retn();
897 }
898
899 //============================================================================
900 /*! Function : GetComponentNames
901  *  Purpose  : method to get all components names
902  *  SRN:       Note, theContext can be any, it doesn't matter
903  */
904 //============================================================================
905 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) 
906 {
907   SALOMEDS::Locker lock; 
908
909   if (_closed)
910     throw SALOMEDS::Study::StudyInvalidReference();  
911
912   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
913
914   std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
915
916   int aLength = aSeq.size();
917   aResult->length(aLength);
918   for(int anIndex = 0; anIndex < aLength; anIndex++) {
919     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
920   }
921
922   return aResult._retn();
923 }
924
925 //============================================================================
926 /*! Function : NewChildIterator
927  *  Purpose  : Create a ChildIterator from an SObject
928  */
929 //============================================================================
930 SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject_ptr theSO)
931 {
932   SALOMEDS::Locker lock; 
933
934   if (_closed)
935     throw SALOMEDS::Study::StudyInvalidReference();  
936
937   CORBA::String_var anID = theSO->GetID();
938   SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in());
939   SALOMEDSImpl_ChildIterator anItr(aSO);
940   SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(anItr, _orb);
941   SALOMEDS::ChildIterator_var it = it_servant->_this();
942
943   return it._retn();
944 }
945
946
947 //============================================================================
948 /*! Function : NewComponentIterator
949  *  Purpose  : Create a SComponentIterator
950  */
951 //============================================================================
952 SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
953 {
954   SALOMEDS::Locker lock; 
955
956   if (_closed)
957     throw SALOMEDS::Study::StudyInvalidReference();  
958
959   SALOMEDS_SComponentIterator_i* it_servant = new SALOMEDS_SComponentIterator_i(_impl->NewComponentIterator(), _orb);
960   it_servant->Init();
961   SALOMEDS::SComponentIterator_var it = it_servant->_this();
962
963   return it._retn();
964 }
965
966
967 //============================================================================
968 /*! Function : NewBuilder
969  *  Purpose  : Create a StudyBuilder
970  */
971 //============================================================================
972 SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
973 {
974   SALOMEDS::Locker lock; 
975
976   if (_closed)
977     throw SALOMEDS::Study::StudyInvalidReference();  
978
979   SALOMEDS::StudyBuilder_var sb = SALOMEDS::StudyBuilder::_duplicate(_builder->_this());
980
981   return sb._retn();
982 }
983  
984 //============================================================================
985 /*! Function : Name
986  *  Purpose  : get study name
987  */
988 //============================================================================
989 char* SALOMEDS_Study_i::Name()
990 {
991   SALOMEDS::Locker lock; 
992   // Name is specified as IDL attribute: user exception cannot be raised
993   return CORBA::string_dup(_impl->Name().c_str());
994 }
995
996 //============================================================================
997 /*! Function : IsSaved
998  *  Purpose  : get if study has been saved
999  */
1000 //============================================================================
1001 CORBA::Boolean SALOMEDS_Study_i::IsSaved()
1002 {
1003   SALOMEDS::Locker lock; 
1004   // IsSaved is specified as IDL attribute: user exception cannot be raised
1005   return (!_closed) ? _impl->IsSaved() : false;
1006 }
1007
1008 //============================================================================
1009 /*! Function : IsSaved
1010  *  Purpose  : set if study has been saved
1011  */
1012 //============================================================================
1013 void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
1014 {
1015   SALOMEDS::Locker lock; 
1016   // IsSaved is specified as IDL attribute: user exception cannot be raised
1017   if (!_closed)
1018     _impl->IsSaved(save);
1019 }
1020
1021 //============================================================================
1022 /*! Function : IsModified
1023  *  Purpose  : Detect if a Study has been modified since it has been saved
1024  */
1025 //============================================================================
1026 CORBA::Boolean SALOMEDS_Study_i::IsModified()
1027 {
1028   SALOMEDS::Locker lock; 
1029
1030   if (_closed)
1031     throw SALOMEDS::Study::StudyInvalidReference();  
1032
1033   return _impl->IsModified();
1034 }
1035
1036 //============================================================================
1037 /*! Function : Modified
1038  *  Purpose  : Sets a Modified flag of a Study to True
1039  */
1040 //============================================================================
1041 void SALOMEDS_Study_i::Modified()
1042 {
1043   SALOMEDS::Locker lock; 
1044
1045   if (_closed)
1046     throw SALOMEDS::Study::StudyInvalidReference();  
1047
1048   _impl->Modify();
1049 }
1050
1051 //============================================================================
1052 /*! Function : URL
1053  *  Purpose  : get URL of the study (persistent reference of the study)
1054  */
1055 //============================================================================
1056 char* SALOMEDS_Study_i::URL()
1057 {
1058   SALOMEDS::Locker lock; 
1059   // URL is specified as IDL attribute: user exception cannot be raised
1060   return CORBA::string_dup(_impl->URL().c_str());
1061 }
1062
1063 //============================================================================
1064 /*! Function : URL
1065  *  Purpose  : set URL of the study (persistent reference of the study)
1066  */
1067 //============================================================================
1068 void SALOMEDS_Study_i::URL(const char* url)
1069 {
1070   SALOMEDS::Locker lock; 
1071   // URL is specified as IDL attribute: user exception cannot be raised
1072   _impl->URL(std::string((char*)url));
1073
1074   // update desktop title with new study name
1075   NameChanged();
1076 }
1077
1078 void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry) 
1079 {
1080   SALOMEDS::Locker lock; 
1081
1082   if (_closed)
1083     throw SALOMEDS::Study::StudyInvalidReference();  
1084
1085   _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
1086 }
1087
1088 SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb)
1089 {
1090   SALOMEDS::Locker lock;
1091
1092   SALOMEDSImpl_AttributeIOR* Att = NULL;
1093   if ((Att=(SALOMEDSImpl_AttributeIOR*)theLabel.Root().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))){
1094     char* IOR = CORBA::string_dup(Att->Value().c_str());
1095     CORBA::Object_var obj = orb->string_to_object(IOR);
1096     SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj) ;
1097     ASSERT(!CORBA::is_nil(aStudy));
1098     return SALOMEDS::Study::_duplicate(aStudy);
1099   } else {
1100     MESSAGE("GetStudy: Problem to get study");
1101   }
1102   return SALOMEDS::Study::_nil();
1103 }
1104
1105 void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
1106 {
1107   SALOMEDS::Locker lock; 
1108   SALOMEDSImpl_Study::IORUpdated(theAttribute);
1109 }
1110
1111 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObject_ptr anObject) 
1112 {
1113   SALOMEDS::Locker lock; 
1114
1115   if (_closed)
1116     throw SALOMEDS::Study::StudyInvalidReference();  
1117
1118   SALOMEDS::GenericAttribute_ptr aTarget;
1119   if (anObject->FindAttribute(aTarget,"AttributeTarget")) {
1120     return SALOMEDS::AttributeTarget::_narrow(aTarget)->Get();
1121   }
1122   SALOMEDS::Study::ListOfSObject* aList = new SALOMEDS::Study::ListOfSObject;
1123   aList->length(0);
1124   return aList;
1125 }
1126
1127
1128 SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties() 
1129 {
1130   SALOMEDS::Locker lock; 
1131   
1132   if (_closed)
1133     throw SALOMEDS::Study::StudyInvalidReference();  
1134
1135   SALOMEDSImpl_AttributeStudyProperties* anAttr = _impl->GetProperties();
1136   SALOMEDS_AttributeStudyProperties_i* SP = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
1137   SALOMEDS::AttributeStudyProperties_var aProp = SP->_this();
1138   return aProp._retn();
1139 }
1140
1141 char* SALOMEDS_Study_i::GetLastModificationDate() 
1142 {
1143   SALOMEDS::Locker lock; 
1144
1145   if (_closed)
1146     throw SALOMEDS::Study::StudyInvalidReference();  
1147
1148   return CORBA::string_dup(_impl->GetLastModificationDate().c_str());
1149 }
1150
1151 SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() 
1152 {
1153   SALOMEDS::Locker lock; 
1154   
1155   if (_closed)
1156     throw SALOMEDS::Study::StudyInvalidReference();  
1157
1158   SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
1159
1160   std::vector<std::string> aSeq = _impl->GetModificationsDate();
1161
1162   int aLength = aSeq.size();
1163   aDates->length(aLength);
1164   for (int anIndex = 0; anIndex < aLength; anIndex++) {
1165     aDates[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
1166   }
1167
1168   return aDates._retn();
1169 }
1170
1171 //============================================================================
1172 /*! Function : GetUseCaseBuilder
1173  *  Purpose  : Returns a UseCase builder
1174  */
1175 //============================================================================
1176 SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder() 
1177 {
1178   SALOMEDS::Locker lock; 
1179
1180   if (_closed)
1181     throw SALOMEDS::Study::StudyInvalidReference();  
1182
1183   SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
1184   SALOMEDS::UseCaseBuilder_var uc = UCBuilder->_this();
1185   return uc._retn();
1186 }
1187
1188 //============================================================================
1189 /*! Function : AddPostponed
1190  *  Purpose  : 
1191  */
1192  //============================================================================
1193 void SALOMEDS_Study_i::AddPostponed(const char* theIOR) 
1194 {
1195   SALOMEDS::Locker lock; 
1196   //Not implemented
1197 }
1198
1199 void SALOMEDS_Study_i::AddCreatedPostponed(const char* theIOR) 
1200 {
1201   SALOMEDS::Locker lock; 
1202   //Not implemented
1203 }
1204
1205 //============================================================================
1206 /*! Function : RemovePostponed
1207  *  Purpose  : 
1208  */
1209 //============================================================================
1210 void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/) 
1211 {  
1212   SALOMEDS::Locker lock; 
1213
1214   if (_closed)
1215     throw SALOMEDS::Study::StudyInvalidReference();  
1216
1217   std::vector<std::string> anIORs = _impl->GetIORs();
1218   int i, aSize = (int)anIORs.size();
1219   
1220   for (i = 0; i < aSize; i++) {
1221     try {
1222       CORBA::Object_var obj = _orb->string_to_object(anIORs[i].c_str());
1223       SALOME::GenericObj_var aGeneric = SALOME::GenericObj::_narrow(obj);
1224       //rnv: To avoid double deletion of the Salome Generic Objects:
1225       //rnv: 1. First decrement of the reference count in the SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR();
1226       //rnv: 2. Second decrement of the reference count in the next string : aGeneric->UnRegister();
1227       //if (!CORBA::is_nil(aGeneric)) aGeneric->UnRegister();
1228     } catch (...) {}
1229   }
1230
1231   //Not implemented
1232 }
1233
1234 //============================================================================
1235 /*! Function : UndoPostponed
1236  *  Purpose  : 
1237  */
1238 //============================================================================
1239 void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
1240 {
1241   SALOMEDS::Locker lock; 
1242   //Not implemented
1243 }
1244
1245
1246 //============================================================================
1247 /*! Function : DumpStudy
1248  *  Purpose  : 
1249  */
1250 //============================================================================
1251 CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath, 
1252                                            const char* theBaseName, 
1253                                            CORBA::Boolean isPublished,
1254                                            CORBA::Boolean isMultiFile)
1255 {
1256   SALOMEDS::Locker lock; 
1257
1258   if (_closed)
1259     throw SALOMEDS::Study::StudyInvalidReference();  
1260
1261   std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
1262   SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
1263   bool ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
1264   delete factory;
1265
1266   return ret;
1267 }
1268
1269 //============================================================================
1270 /*! Function : GetCommonParameters
1271  *  Purpose  : 
1272  */
1273 //============================================================================
1274 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
1275 {
1276   SALOMEDS::Locker lock; 
1277   
1278   if (_closed)
1279     throw SALOMEDS::Study::StudyInvalidReference();  
1280
1281   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
1282   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
1283   SALOMEDS::AttributeParameter_var aParam = SP->_this();
1284
1285   return aParam._retn();
1286 }
1287  
1288 //============================================================================
1289 /*! Function : GetCommonModuleParameters
1290  *  Purpose  : 
1291  */
1292 //============================================================================
1293 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
1294                                                                        const char* theModuleName, 
1295                                                                        CORBA::Long theSavePoint)
1296 {
1297   SALOMEDS::Locker lock; 
1298   
1299   if (_closed)
1300     throw SALOMEDS::Study::StudyInvalidReference();  
1301
1302   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
1303   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
1304   SALOMEDS::AttributeParameter_var aParam = SP->_this();
1305
1306   return aParam._retn();
1307 }
1308
1309 //============================================================================
1310 /*! Function : SetStudyLock
1311  *  Purpose  : 
1312  */
1313 //============================================================================
1314 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
1315 {
1316   SALOMEDS::Locker lock; 
1317
1318   if (_closed)
1319     throw SALOMEDS::Study::StudyInvalidReference();  
1320
1321   _impl->SetStudyLock(theLockerID);
1322 }
1323
1324 //============================================================================
1325 /*! Function : IsStudyLocked
1326  *  Purpose  : 
1327  */
1328 //============================================================================
1329 bool SALOMEDS_Study_i::IsStudyLocked()
1330 {
1331   SALOMEDS::Locker lock; 
1332
1333   if (_closed)
1334     throw SALOMEDS::Study::StudyInvalidReference();  
1335
1336   return _impl->IsStudyLocked();
1337 }
1338
1339 //============================================================================
1340 /*! Function : UnLockStudy
1341  *  Purpose  : 
1342  */
1343 //============================================================================
1344 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
1345 {
1346   SALOMEDS::Locker lock; 
1347
1348   if (_closed)
1349     throw SALOMEDS::Study::StudyInvalidReference();  
1350
1351   _impl->UnLockStudy(theLockerID);
1352 }
1353
1354 //============================================================================
1355 /*! Function : GetLockerID
1356  *  Purpose  : 
1357  */
1358 //============================================================================
1359 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
1360 {
1361   SALOMEDS::Locker lock; 
1362
1363   if (_closed)
1364     throw SALOMEDS::Study::StudyInvalidReference();  
1365
1366   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1367
1368   std::vector<std::string> aSeq = _impl->GetLockerID();
1369
1370   int aLength = aSeq.size();
1371   aResult->length(aLength);
1372   for(int anIndex = 0; anIndex < aLength; anIndex++) {
1373     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
1374   }
1375
1376   return aResult._retn();
1377 }
1378 //============================================================================
1379 /*! Function : SetReal
1380  *  Purpose  : 
1381  */
1382 //============================================================================
1383 void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
1384 {
1385   if (_closed)
1386     throw SALOMEDS::Study::StudyInvalidReference();  
1387
1388
1389   _impl->SetVariable(std::string(theVarName), 
1390                      theValue,
1391                      SALOMEDSImpl_GenericVariable::REAL_VAR);
1392   if (_notifier)
1393     _notifier->modifyNB_Notification(theVarName);
1394 }
1395
1396 //============================================================================
1397 /*! Function : SetInteger
1398  *  Purpose  : 
1399  */
1400 //============================================================================
1401 void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
1402 {
1403   if (_closed)
1404     throw SALOMEDS::Study::StudyInvalidReference();  
1405
1406   _impl->SetVariable(std::string(theVarName), 
1407                      theValue,
1408                      SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1409   if (_notifier)
1410     _notifier->modifyNB_Notification(theVarName);
1411 }
1412
1413 //============================================================================
1414 /*! Function : SetBoolean
1415  *  Purpose  : 
1416  */
1417 //============================================================================
1418 void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
1419 {
1420   if (_closed)
1421     throw SALOMEDS::Study::StudyInvalidReference();  
1422
1423   _impl->SetVariable(std::string(theVarName), 
1424                      theValue,
1425                      SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1426   if (_notifier)
1427     _notifier->modifyNB_Notification(theVarName);
1428 }
1429
1430 //============================================================================
1431 /*! Function : SetString
1432  *  Purpose  : 
1433  */
1434 //============================================================================
1435 void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
1436 {
1437   if (_closed)
1438     throw SALOMEDS::Study::StudyInvalidReference();  
1439
1440   _impl->SetStringVariable(std::string(theVarName), 
1441                            theValue,
1442                            SALOMEDSImpl_GenericVariable::STRING_VAR);
1443   if (_notifier)
1444     _notifier->modifyNB_Notification(theVarName);
1445 }
1446
1447 //============================================================================
1448 /*! Function : SetStringAsDouble
1449  *  Purpose  : 
1450  */
1451 //============================================================================
1452 void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
1453 {
1454   if (_closed)
1455     throw SALOMEDS::Study::StudyInvalidReference();  
1456
1457   _impl->SetStringVariableAsDouble(std::string(theVarName), 
1458                                    theValue,
1459                                    SALOMEDSImpl_GenericVariable::STRING_VAR);
1460 }
1461
1462 //============================================================================
1463 /*! Function : GetReal
1464  *  Purpose  : 
1465  */
1466 //============================================================================
1467 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
1468 {
1469   if (_closed)
1470     throw SALOMEDS::Study::StudyInvalidReference();  
1471
1472   return _impl->GetVariableValue(std::string(theVarName));
1473 }
1474
1475 //============================================================================
1476 /*! Function : GetInteger
1477  *  Purpose  : 
1478  */
1479 //============================================================================
1480 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
1481 {
1482   if (_closed)
1483     throw SALOMEDS::Study::StudyInvalidReference();  
1484
1485   return (long)_impl->GetVariableValue(std::string(theVarName));
1486 }
1487
1488 //============================================================================
1489 /*! Function : GetBoolean
1490  *  Purpose  : 
1491  */
1492 //============================================================================
1493 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
1494 {
1495   if (_closed)
1496     throw SALOMEDS::Study::StudyInvalidReference();  
1497
1498   return (bool)_impl->GetVariableValue(std::string(theVarName));
1499 }
1500
1501 //============================================================================
1502 /*! Function : GetString
1503  *  Purpose  : 
1504  */
1505 //============================================================================
1506 char* SALOMEDS_Study_i::GetString(const char* theVarName)
1507 {
1508   if (_closed)
1509     throw SALOMEDS::Study::StudyInvalidReference();  
1510
1511   return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
1512 }
1513
1514 //============================================================================
1515 /*! Function : IsReal
1516  *  Purpose  : 
1517  */
1518 //============================================================================
1519 CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
1520 {
1521   if (_closed)
1522     throw SALOMEDS::Study::StudyInvalidReference();  
1523
1524   return _impl->IsTypeOf(std::string(theVarName),
1525                          SALOMEDSImpl_GenericVariable::REAL_VAR);
1526 }
1527
1528 //============================================================================
1529 /*! Function : IsInteger
1530  *  Purpose  : 
1531  */
1532 //============================================================================
1533 CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
1534 {
1535   if (_closed)
1536     throw SALOMEDS::Study::StudyInvalidReference();  
1537
1538   return _impl->IsTypeOf(std::string(theVarName),
1539                          SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1540 }
1541
1542 //============================================================================
1543 /*! Function : IsBoolean
1544  *  Purpose  : 
1545  */
1546 //============================================================================
1547 CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
1548 {
1549   if (_closed)
1550     throw SALOMEDS::Study::StudyInvalidReference();  
1551
1552   return _impl->IsTypeOf(std::string(theVarName),
1553                          SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1554 }
1555
1556 //============================================================================
1557 /*! Function : IsString
1558  *  Purpose  : 
1559  */
1560 //============================================================================
1561 CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
1562 {
1563   if (_closed)
1564     throw SALOMEDS::Study::StudyInvalidReference();  
1565
1566   return _impl->IsTypeOf(std::string(theVarName),
1567                          SALOMEDSImpl_GenericVariable::STRING_VAR);
1568 }
1569
1570 //============================================================================
1571 /*! Function : IsVariable
1572  *  Purpose  : 
1573  */
1574 //============================================================================
1575 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
1576 {
1577   if (_closed)
1578     throw SALOMEDS::Study::StudyInvalidReference();  
1579
1580   return _impl->IsVariable(std::string(theVarName));
1581 }
1582
1583 //============================================================================
1584 /*! Function : GetVariableNames
1585  *  Purpose  : 
1586  */
1587 //============================================================================
1588 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
1589 {
1590   if (_closed)
1591     throw SALOMEDS::Study::StudyInvalidReference();  
1592
1593   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1594
1595   std::vector<std::string> aVarNames = _impl->GetVariableNames();
1596
1597   int aLen = aVarNames.size();
1598   aResult->length(aLen);
1599   for (int anInd = 0; anInd < aLen; anInd++)
1600     aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1601   
1602   return aResult._retn();
1603 }
1604
1605 //============================================================================
1606 /*! Function : RemoveVariable
1607  *  Purpose  : 
1608  */
1609 //============================================================================
1610 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
1611 {
1612   if (_closed)
1613     throw SALOMEDS::Study::StudyInvalidReference();  
1614
1615   bool res = _impl->RemoveVariable(std::string(theVarName));
1616   if (res && _notifier)
1617     _notifier->modifyNB_Notification(theVarName);
1618
1619   return res;
1620 }
1621
1622 //============================================================================
1623 /*! Function : RenameVariable
1624  *  Purpose  : 
1625  */
1626 //============================================================================
1627 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
1628 {
1629   if (_closed)
1630     throw SALOMEDS::Study::StudyInvalidReference();  
1631
1632   bool res = _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
1633   if (res && _notifier)
1634     _notifier->modifyNB_Notification(theVarName);
1635
1636   return res;
1637 }
1638
1639 //============================================================================
1640 /*! Function : IsVariableUsed
1641  *  Purpose  : 
1642  */
1643 //============================================================================
1644 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
1645 {
1646   if (_closed)
1647     throw SALOMEDS::Study::StudyInvalidReference();  
1648
1649   return _impl->IsVariableUsed(std::string(theVarName));
1650 }
1651
1652
1653 //============================================================================
1654 /*! Function : ParseVariables
1655  *  Purpose  : 
1656  */
1657 //============================================================================
1658 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
1659 {
1660   if (_closed)
1661     throw SALOMEDS::Study::StudyInvalidReference();  
1662
1663   SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
1664
1665   std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
1666
1667   int aSectionsLen = aSections.size();
1668   aResult->length(aSectionsLen);
1669
1670   for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
1671     std::vector<std::string> aVarNames = aSections[aSectionInd];
1672
1673     SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
1674
1675     int aLen = aVarNames.size();
1676     aList->length(aLen);
1677
1678     for (int anInd = 0; anInd < aLen; anInd++)
1679       aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1680
1681     aResult[aSectionInd] = aList;
1682   }
1683
1684   return aResult._retn();
1685 }
1686
1687 //============================================================================
1688 /*! Function : GetDefaultScript
1689  *  Purpose  : 
1690  */
1691 //============================================================================
1692 char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
1693 {
1694   SALOMEDS::Locker lock; 
1695
1696   if (_closed)
1697     throw SALOMEDS::Study::StudyInvalidReference();  
1698
1699   std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
1700   return CORBA::string_dup(script.c_str());
1701 }
1702
1703 //============================================================================
1704 /*! Function : EnableUseCaseAutoFilling
1705  *  Purpose  : 
1706  */
1707 //============================================================================
1708 void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
1709
1710   if (_closed)
1711     throw SALOMEDS::Study::StudyInvalidReference();  
1712
1713   _impl->EnableUseCaseAutoFilling(isEnabled); 
1714   SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
1715   if (builder) {
1716     if (isEnabled) {
1717       builder->SetOnAddSObject(_impl->GetCallback());
1718       builder->SetOnRemoveSObject(_impl->GetCallback());
1719     }
1720     else {
1721       builder->SetOnAddSObject(NULL);
1722       builder->SetOnRemoveSObject(NULL);
1723     }
1724   }
1725 }
1726
1727
1728 CORBA::Long SALOMEDS_Study_i::getPID()
1729 {
1730 #ifdef WIN32
1731   return (CORBA::Long)_getpid();
1732 #else
1733   return (CORBA::Long)getpid();
1734 #endif
1735 }
1736
1737 void SALOMEDS_Study_i::ShutdownWithExit()
1738 {
1739   exit( EXIT_SUCCESS );
1740 }
1741
1742 void SALOMEDS_Study_i::Shutdown()
1743 {
1744   if(!CORBA::is_nil(_orb))
1745     _orb->shutdown(0);
1746 }
1747
1748 //============================================================================
1749 /*! Function : attach
1750  *  Purpose  : This function attach an observer to the study
1751  */
1752 //============================================================================
1753 void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs, CORBA::Boolean modify)
1754 {
1755   if (_notifier)
1756     static_cast<SALOMEDS::Notifier*>(_notifier)->attach(theObs, modify);
1757 }
1758
1759
1760 //============================================================================
1761 /*! Function : detach
1762  *  Purpose  : This function detaches an observer from the study
1763  */
1764 //============================================================================
1765 void SALOMEDS_Study_i::detach(SALOMEDS::Observer_ptr theObs)
1766 {
1767   if (_notifier)
1768     static_cast<SALOMEDS::Notifier*>(_notifier)->detach(theObs);
1769 }
1770
1771 //===========================================================================
1772 //   PRIVATE FUNCTIONS
1773 //===========================================================================
1774 CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
1775 {
1776 #ifdef WIN32
1777   long pid = (long)_getpid();
1778 #else
1779   long pid = (long)getpid();
1780 #endif  
1781   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
1782   return reinterpret_cast<CORBA::LongLong>(_impl);
1783 }
1784
1785 void SALOMEDS_Study_i::NameChanged()
1786 {
1787   // Notify GUI that the name of study was changed
1788   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
1789   CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
1790   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
1791   if ( !CORBA::is_nil(aSession) ) {
1792     std::stringstream ss;
1793     ss << "studyNameChanged";
1794     std::string str = ss.str();
1795     SALOMEDS::unlock();
1796     aSession->emitMessageOneWay(str.c_str());
1797     SALOMEDS::lock();
1798   }
1799 }