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