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