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