]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_Study_i.cxx
Salome HOME
Fixed problem with study after re-initialization (Clear, Init) study.
[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     throw SALOMEDS::Study::StudyInvalidReference();
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
371   _closed = true;
372 }
373
374 //============================================================================
375 /*! Function : Open
376  *  Purpose  : Open a Study from it's persistent reference
377  */
378 //============================================================================
379 bool SALOMEDS_Study_i::Open(const char* aUrl)
380   throw(SALOME::SALOME_Exception)
381 {
382   SALOMEDS::Locker lock;
383
384   Unexpect aCatch(SalomeException);
385   MESSAGE("Begin of SALOMEDS_Study_i::Open");
386
387   bool res = _impl->Open(std::string(aUrl));
388
389   // update desktop title with new study name
390   NameChanged();
391
392   if ( !res )
393     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
394   return res;
395 }
396
397 //============================================================================
398 /*! Function : Save
399  *  Purpose  : Save a Study to it's persistent reference
400  */
401 //============================================================================
402 CORBA::Boolean SALOMEDS_Study_i::Save(CORBA::Boolean theMultiFile)
403 {
404   SALOMEDS::Locker lock;
405   if (_closed)
406     throw SALOMEDS::Study::StudyInvalidReference();
407   return _impl->Save(_factory, theMultiFile);
408 }
409
410 CORBA::Boolean SALOMEDS_Study_i::SaveASCII(CORBA::Boolean theMultiFile)
411 {
412   SALOMEDS::Locker lock;
413   if (_closed)
414     throw SALOMEDS::Study::StudyInvalidReference();
415   return _impl->SaveASCII(_factory, theMultiFile);
416 }
417
418 //=============================================================================
419 /*! Function : SaveAs
420  *  Purpose  : Save a study to the persistent reference aUrl
421  */
422 //============================================================================
423 CORBA::Boolean SALOMEDS_Study_i::SaveAs(const char* aUrl, CORBA::Boolean theMultiFile)
424 {
425   SALOMEDS::Locker lock;
426   if (_closed)
427     throw SALOMEDS::Study::StudyInvalidReference();
428   return _impl->SaveAs(std::string(aUrl), _factory, theMultiFile);
429 }
430
431 CORBA::Boolean SALOMEDS_Study_i::SaveAsASCII(const char* aUrl, CORBA::Boolean theMultiFile)
432 {
433   SALOMEDS::Locker lock;
434   if (_closed)
435     throw SALOMEDS::Study::StudyInvalidReference();
436   return _impl->SaveAsASCII(std::string(aUrl), _factory, theMultiFile);
437 }
438
439 //============================================================================
440 /*! Function : CanCopy
441  *  Purpose  :
442  */
443 //============================================================================
444 CORBA::Boolean SALOMEDS_Study_i::CanCopy(SALOMEDS::SObject_ptr theObject)
445 {
446   SALOMEDS::Locker lock;
447   if (_closed)
448     throw SALOMEDS::Study::StudyInvalidReference();
449
450   CORBA::String_var anID = theObject->GetID();
451   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
452
453   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
454   bool ret = _impl->CanCopy(anObject, aDriver);
455   delete aDriver;
456   return ret;
457 }
458
459 //============================================================================
460 /*! Function : Copy
461  *  Purpose  :
462  */
463 //============================================================================
464 CORBA::Boolean SALOMEDS_Study_i::Copy(SALOMEDS::SObject_ptr theObject)
465 {
466   SALOMEDS::Locker lock;
467   if (_closed)
468     throw SALOMEDS::Study::StudyInvalidReference();
469
470   CORBA::String_var anID = theObject->GetID();
471   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
472
473   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
474   bool ret = _impl->Copy(anObject, aDriver);
475   delete aDriver;
476   return ret;
477 }
478
479 //============================================================================
480 /*! Function : CanPaste
481  *  Purpose  :
482  */
483 //============================================================================
484 CORBA::Boolean SALOMEDS_Study_i::CanPaste(SALOMEDS::SObject_ptr theObject)
485 {
486   SALOMEDS::Locker lock;
487   if (_closed)
488     throw SALOMEDS::Study::StudyInvalidReference();
489
490   CORBA::String_var anID = theObject->GetID();
491   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
492
493   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
494   bool ret = _impl->CanPaste(anObject, aDriver);
495   delete aDriver;
496   return ret;
497 }
498
499 //============================================================================
500 /*! Function : Paste
501  *  Purpose  :
502  */
503 //============================================================================
504 SALOMEDS::SObject_ptr SALOMEDS_Study_i::Paste(SALOMEDS::SObject_ptr theObject)
505      throw(SALOMEDS::StudyBuilder::LockProtection)
506 {
507   SALOMEDS::Locker lock;
508
509   Unexpect aCatch(LockProtection);
510
511   CORBA::String_var anID = theObject->GetID();
512   SALOMEDSImpl_SObject anObject = _impl->GetSObject(anID.in());
513   SALOMEDSImpl_SObject aNewSO;
514
515   try {
516     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
517     aNewSO =  _impl->Paste(anObject, aDriver);
518     delete aDriver;
519   }
520   catch (...) {
521     throw SALOMEDS::StudyBuilder::LockProtection();
522   }
523
524   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
525   return so._retn();
526 }
527
528 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
529 {
530   SALOMEDS_Driver_i* driver = NULL;
531
532   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
533   if(!aSCO.IsNull()) {
534     std::string IOREngine = aSCO.GetIOR();
535     if(!IOREngine.empty()) {
536       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
537       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
538       driver = new SALOMEDS_Driver_i(Engine, orb);
539     }
540   }
541
542   return driver;
543 }
544
545 //============================================================================
546 /*! Function : GetPersistentReference
547  *  Purpose  : Get persistent reference of study (idem URL())
548  */
549 //============================================================================
550 char* SALOMEDS_Study_i::GetPersistentReference()
551 {
552   SALOMEDS::Locker lock; 
553   if (_closed)
554     throw SALOMEDS::Study::StudyInvalidReference();  
555   return CORBA::string_dup(_impl->GetPersistentReference().c_str());
556 }
557 //============================================================================
558 /*! Function : GetTransientReference
559  *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
560  */
561 //============================================================================
562 char* SALOMEDS_Study_i::GetTransientReference()
563 {
564   SALOMEDS::Locker lock; 
565   if (_closed)
566     throw SALOMEDS::Study::StudyInvalidReference();  
567   return CORBA::string_dup(_impl->GetTransientReference().c_str());
568 }
569
570 //============================================================================
571 /*! Function : IsEmpty
572  *  Purpose  : Detect if study is empty
573  */
574 //============================================================================
575 CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
576 {
577   SALOMEDS::Locker lock; 
578   if (_closed)
579     throw SALOMEDS::Study::StudyInvalidReference();  
580   return _impl->IsEmpty();
581 }
582
583 //============================================================================
584 /*! Function : FindComponent
585  *  Purpose  : Find a Component with ComponentDataType = aComponentName
586  */
587 //============================================================================
588 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponentName)
589 {
590   SALOMEDS::Locker lock; 
591   
592   if (_closed)
593     throw SALOMEDS::Study::StudyInvalidReference();  
594
595   SALOMEDS::SComponent_var sco;
596
597   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponent(std::string(aComponentName));
598   if (!aCompImpl.IsNull())
599     sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
600
601   return sco._retn();
602 }
603
604 //============================================================================
605 /*! Function : FindComponentID
606  *  Purpose  : Find a Component from it's ID
607  */
608 //============================================================================
609 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponentID)
610 {
611   SALOMEDS::Locker lock; 
612   
613   if (_closed)
614     throw SALOMEDS::Study::StudyInvalidReference();  
615
616   SALOMEDS::SComponent_var sco;
617
618   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponentID(std::string((char*)aComponentID));
619   if (!aCompImpl.IsNull())
620     sco = SALOMEDS_SComponent_i::New(aCompImpl, _orb);
621
622   return sco._retn();
623 }
624
625 //============================================================================
626 /*! Function : FindObject
627  *  Purpose  : Find an Object with SALOMEDS::Name = anObjectName
628  */
629 //============================================================================
630 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
631 {
632   SALOMEDS::Locker lock; 
633
634   if (_closed)
635     throw SALOMEDS::Study::StudyInvalidReference();  
636
637   SALOMEDS::SObject_var so;
638
639   SALOMEDSImpl_SObject aSO = _impl->FindObject(std::string((char*)anObjectName));
640   if (!aSO.IsNull()) {
641     if (aSO.IsComponent()) {
642       SALOMEDSImpl_SComponent aSCO = aSO;
643       so = SALOMEDS_SComponent_i::New(aSCO, _orb);
644     }
645     else {
646       so = SALOMEDS_SObject_i::New(aSO, _orb);
647     }
648   }
649
650   return so._retn();
651 }
652
653 //============================================================================
654 /*! Function : FindObjectID
655  *  Purpose  : Find an Object with ID = anObjectID
656  */
657 //============================================================================
658 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
659 {
660   SALOMEDS::Locker lock; 
661
662   if (_closed)
663     throw SALOMEDS::Study::StudyInvalidReference();  
664
665   SALOMEDS::SObject_var so;
666
667   SALOMEDSImpl_SObject aSO = _impl->FindObjectID(std::string((char*)anObjectID));
668   if (!aSO.IsNull())
669     so = SALOMEDS_SObject_i::New(aSO, _orb);
670
671   return so._retn();
672 }
673
674 //============================================================================
675 /*! Function : CreateObjectID
676  *  Purpose  : Creates an Object with ID = anObjectID
677  */
678 //============================================================================
679 SALOMEDS::SObject_ptr SALOMEDS_Study_i::CreateObjectID(const char* anObjectID)
680 {
681   SALOMEDS::Locker lock; 
682
683   if (_closed)
684     throw SALOMEDS::Study::StudyInvalidReference();  
685
686   SALOMEDS::SObject_var so;
687
688   if (anObjectID && strlen(anObjectID) > 0) {
689     SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
690     if (!aSO.IsNull())
691       so = SALOMEDS_SObject_i::New(aSO, _orb);
692   }
693
694   return so._retn();
695 }
696
697 //============================================================================
698 /*! Function : FindObjectByName
699  *  Purpose  : Find Objects with SALOMEDS::Name = anObjectName in a Component
700  *           : with ComponentDataType = aComponentName
701  */
702 //============================================================================
703 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char* anObjectName,
704                                                                     const char* aComponentName )
705 {
706   SALOMEDS::Locker lock; 
707
708   if (_closed)
709     throw SALOMEDS::Study::StudyInvalidReference();  
710
711   std::vector<SALOMEDSImpl_SObject> aSeq = _impl->FindObjectByName(std::string((char*)anObjectName),
712                                                                    std::string((char*)aComponentName));
713
714   SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
715   int aLength = aSeq.size();
716   listSO->length(aLength);
717   for (int i = 0; i < aLength; i++) {
718     SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New(aSeq[i], _orb);
719     listSO[i] = so;
720   }
721   
722   return listSO._retn();
723 }
724
725 //============================================================================
726 /*! Function : FindObjectIOR
727  *  Purpose  : Find an Object with IOR = anObjectIOR
728  */
729 //============================================================================
730 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
731 {
732   SALOMEDS::Locker lock; 
733
734   if (_closed)
735     throw SALOMEDS::Study::StudyInvalidReference();  
736
737   SALOMEDS::SObject_var so;
738
739   SALOMEDSImpl_SObject aSO = _impl->FindObjectIOR(std::string((char*)anObjectIOR));
740   if (!aSO.IsNull())
741     so = SALOMEDS_SObject_i::New(aSO, _orb);
742
743   return so._retn();
744 }
745
746 //============================================================================
747 /*! Function : FindObjectByPath
748  *  Purpose  : Find an Object by its path = thePath
749  */
750 //============================================================================
751 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
752 {
753   SALOMEDS::Locker lock; 
754
755   if (_closed)
756     throw SALOMEDS::Study::StudyInvalidReference();  
757
758   SALOMEDS::SObject_var so;
759
760   SALOMEDSImpl_SObject aSO = _impl->FindObjectByPath(std::string((char*)thePath));
761   if (!aSO.IsNull())
762     so = SALOMEDS_SObject_i::New (aSO, _orb);
763
764   return so._retn();
765 }
766
767 //============================================================================
768 /*! Function : GetObjectPath
769  *  Purpose  : 
770  */
771 //============================================================================
772 char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
773 {
774   SALOMEDS::Locker lock; 
775
776   if (_closed)
777     throw SALOMEDS::Study::StudyInvalidReference();  
778
779   std::string aPath = "";
780
781   if (!CORBA::is_nil(theObject)) {
782     SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
783     SALOMEDSImpl_SObject aSO;
784
785     if (!CORBA::is_nil(aSObj)) {
786       aSO = _impl->FindObjectID(aSObj->GetID());
787     }
788     else {
789       aSO = _impl->FindObjectIOR(_orb->object_to_string(theObject));
790     }
791     
792     if (!aSO.IsNull()) {    
793       aPath = _impl->GetObjectPath(aSO);
794     }
795   }
796
797   return CORBA::string_dup(aPath.c_str());
798 }
799
800
801 //============================================================================
802 /*! Function : SetContext
803  *  Purpose  : Sets the current context
804  */
805 //============================================================================
806 void SALOMEDS_Study_i::SetContext(const char* thePath) 
807 {
808   SALOMEDS::Locker lock; 
809
810   if (_closed)
811     throw SALOMEDS::Study::StudyInvalidReference();  
812
813   _impl->SetContext(std::string((char*)thePath));
814   if (_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
815     throw SALOMEDS::Study::StudyInvalidContext();  
816 }
817
818 //============================================================================
819 /*! Function : GetContext
820  *  Purpose  : Gets the current context
821  */
822 //============================================================================
823 char* SALOMEDS_Study_i::GetContext() 
824 {
825   SALOMEDS::Locker lock; 
826   
827   if (_closed)
828     throw SALOMEDS::Study::StudyInvalidReference();  
829
830   if (!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();
831
832   return CORBA::string_dup(_impl->GetContext().c_str());
833 }
834
835 //============================================================================
836 /*! Function : GetObjectNames
837  *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
838  */
839 //============================================================================
840 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) 
841 {
842   SALOMEDS::Locker lock; 
843
844   if (_closed)
845     throw SALOMEDS::Study::StudyInvalidReference();  
846
847   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
848
849   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
850     throw SALOMEDS::Study::StudyInvalidContext();
851   
852   std::vector<std::string> aSeq = _impl->GetObjectNames(std::string((char*)theContext));
853   if (_impl->GetErrorCode() == "InvalidContext")
854     throw SALOMEDS::Study::StudyInvalidContext();
855
856   int aLength = aSeq.size();
857   aResult->length(aLength);
858   for (int anIndex = 0; anIndex < aLength; anIndex++) {
859     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
860   }
861
862   return aResult._retn();
863 }
864
865 //============================================================================
866 /*! Function : GetDirectoryNames
867  *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
868  */
869 //============================================================================
870 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) 
871 {
872   SALOMEDS::Locker lock; 
873
874   if (_closed)
875     throw SALOMEDS::Study::StudyInvalidReference();  
876
877   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
878
879   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
880     throw SALOMEDS::Study::StudyInvalidContext();
881   
882   std::vector<std::string> aSeq = _impl->GetDirectoryNames(std::string((char*)theContext));
883   if (_impl->GetErrorCode() == "InvalidContext")
884     throw SALOMEDS::Study::StudyInvalidContext();
885
886   int aLength = aSeq.size();
887   aResult->length(aLength);
888   for (int anIndex = 0; anIndex < aLength; anIndex++) {
889     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
890   }
891   
892   return aResult._retn();
893 }
894
895 //============================================================================
896 /*! Function : GetFileNames
897  *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
898  */
899 //============================================================================
900 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) 
901 {
902   SALOMEDS::Locker lock; 
903
904   if (_closed)
905     throw SALOMEDS::Study::StudyInvalidReference();  
906
907   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
908
909   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
910     throw SALOMEDS::Study::StudyInvalidContext();
911   
912   std::vector<std::string> aSeq = _impl->GetFileNames(std::string((char*)theContext));
913   if (_impl->GetErrorCode() == "InvalidContext")
914     throw SALOMEDS::Study::StudyInvalidContext();
915
916   int aLength = aSeq.size();
917   aResult->length(aLength);
918   for (int anIndex = 0; anIndex < aLength; anIndex++) {
919     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
920   }
921
922   return aResult._retn();
923 }
924
925 //============================================================================
926 /*! Function : GetComponentNames
927  *  Purpose  : method to get all components names
928  *  SRN:       Note, theContext can be any, it doesn't matter
929  */
930 //============================================================================
931 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) 
932 {
933   SALOMEDS::Locker lock; 
934
935   if (_closed)
936     throw SALOMEDS::Study::StudyInvalidReference();  
937
938   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
939
940   std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
941
942   int aLength = aSeq.size();
943   aResult->length(aLength);
944   for(int anIndex = 0; anIndex < aLength; anIndex++) {
945     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
946   }
947
948   return aResult._retn();
949 }
950
951 //============================================================================
952 /*! Function : NewChildIterator
953  *  Purpose  : Create a ChildIterator from an SObject
954  */
955 //============================================================================
956 SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject_ptr theSO)
957 {
958   SALOMEDS::Locker lock; 
959
960   if (_closed)
961     throw SALOMEDS::Study::StudyInvalidReference();  
962
963   CORBA::String_var anID = theSO->GetID();
964   SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in());
965   SALOMEDSImpl_ChildIterator anItr(aSO);
966   SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(anItr, _orb);
967   SALOMEDS::ChildIterator_var it = it_servant->_this();
968
969   return it._retn();
970 }
971
972
973 //============================================================================
974 /*! Function : NewComponentIterator
975  *  Purpose  : Create a SComponentIterator
976  */
977 //============================================================================
978 SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
979 {
980   SALOMEDS::Locker lock; 
981
982   if (_closed)
983     throw SALOMEDS::Study::StudyInvalidReference();  
984
985   SALOMEDS_SComponentIterator_i* it_servant = new SALOMEDS_SComponentIterator_i(_impl->NewComponentIterator(), _orb);
986   it_servant->Init();
987   SALOMEDS::SComponentIterator_var it = it_servant->_this();
988
989   return it._retn();
990 }
991
992
993 //============================================================================
994 /*! Function : NewBuilder
995  *  Purpose  : Create a StudyBuilder
996  */
997 //============================================================================
998 SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
999 {
1000   SALOMEDS::Locker lock; 
1001
1002   if (_closed)
1003     throw SALOMEDS::Study::StudyInvalidReference();  
1004
1005   SALOMEDS::StudyBuilder_var sb = SALOMEDS::StudyBuilder::_duplicate(_builder->_this());
1006
1007   return sb._retn();
1008 }
1009  
1010 //============================================================================
1011 /*! Function : Name
1012  *  Purpose  : get study name
1013  */
1014 //============================================================================
1015 char* SALOMEDS_Study_i::Name()
1016 {
1017   SALOMEDS::Locker lock; 
1018   // Name is specified as IDL attribute: user exception cannot be raised
1019   return CORBA::string_dup(_impl->Name().c_str());
1020 }
1021
1022 //============================================================================
1023 /*! Function : IsSaved
1024  *  Purpose  : get if study has been saved
1025  */
1026 //============================================================================
1027 CORBA::Boolean SALOMEDS_Study_i::IsSaved()
1028 {
1029   SALOMEDS::Locker lock; 
1030   // IsSaved is specified as IDL attribute: user exception cannot be raised
1031   return (!_closed) ? _impl->IsSaved() : false;
1032 }
1033
1034 //============================================================================
1035 /*! Function : IsSaved
1036  *  Purpose  : set if study has been saved
1037  */
1038 //============================================================================
1039 void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
1040 {
1041   SALOMEDS::Locker lock; 
1042   // IsSaved is specified as IDL attribute: user exception cannot be raised
1043   if (!_closed)
1044     _impl->IsSaved(save);
1045 }
1046
1047 //============================================================================
1048 /*! Function : IsModified
1049  *  Purpose  : Detect if a Study has been modified since it has been saved
1050  */
1051 //============================================================================
1052 CORBA::Boolean SALOMEDS_Study_i::IsModified()
1053 {
1054   SALOMEDS::Locker lock; 
1055
1056   if (_closed)
1057     throw SALOMEDS::Study::StudyInvalidReference();  
1058
1059   return _impl->IsModified();
1060 }
1061
1062 //============================================================================
1063 /*! Function : Modified
1064  *  Purpose  : Sets a Modified flag of a Study to True
1065  */
1066 //============================================================================
1067 void SALOMEDS_Study_i::Modified()
1068 {
1069   SALOMEDS::Locker lock; 
1070
1071   if (_closed)
1072     throw SALOMEDS::Study::StudyInvalidReference();  
1073
1074   _impl->Modify();
1075 }
1076
1077 //============================================================================
1078 /*! Function : URL
1079  *  Purpose  : get URL of the study (persistent reference of the study)
1080  */
1081 //============================================================================
1082 char* SALOMEDS_Study_i::URL()
1083 {
1084   SALOMEDS::Locker lock; 
1085   // URL is specified as IDL attribute: user exception cannot be raised
1086   return CORBA::string_dup(_impl->URL().c_str());
1087 }
1088
1089 //============================================================================
1090 /*! Function : URL
1091  *  Purpose  : set URL of the study (persistent reference of the study)
1092  */
1093 //============================================================================
1094 void SALOMEDS_Study_i::URL(const char* url)
1095 {
1096   SALOMEDS::Locker lock; 
1097   // URL is specified as IDL attribute: user exception cannot be raised
1098   _impl->URL(std::string((char*)url));
1099
1100   // update desktop title with new study name
1101   NameChanged();
1102 }
1103
1104 void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR, const char* anEntry) 
1105 {
1106   SALOMEDS::Locker lock; 
1107
1108   if (_closed)
1109     throw SALOMEDS::Study::StudyInvalidReference();  
1110
1111   _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
1112 }
1113
1114 SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb)
1115 {
1116   SALOMEDS::Locker lock;
1117
1118   SALOMEDSImpl_AttributeIOR* Att = NULL;
1119   if ((Att=(SALOMEDSImpl_AttributeIOR*)theLabel.Root().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))){
1120     char* IOR = CORBA::string_dup(Att->Value().c_str());
1121     CORBA::Object_var obj = orb->string_to_object(IOR);
1122     SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj) ;
1123     ASSERT(!CORBA::is_nil(aStudy));
1124     return SALOMEDS::Study::_duplicate(aStudy);
1125   } else {
1126     MESSAGE("GetStudy: Problem to get study");
1127   }
1128   return SALOMEDS::Study::_nil();
1129 }
1130
1131 void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
1132 {
1133   SALOMEDS::Locker lock; 
1134   SALOMEDSImpl_Study::IORUpdated(theAttribute);
1135 }
1136
1137 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObject_ptr anObject) 
1138 {
1139   SALOMEDS::Locker lock; 
1140
1141   if (_closed)
1142     throw SALOMEDS::Study::StudyInvalidReference();  
1143
1144   SALOMEDS::GenericAttribute_ptr aTarget;
1145   if (anObject->FindAttribute(aTarget,"AttributeTarget")) {
1146     return SALOMEDS::AttributeTarget::_narrow(aTarget)->Get();
1147   }
1148   SALOMEDS::Study::ListOfSObject* aList = new SALOMEDS::Study::ListOfSObject;
1149   aList->length(0);
1150   return aList;
1151 }
1152
1153
1154 SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties() 
1155 {
1156   SALOMEDS::Locker lock; 
1157   
1158   if (_closed)
1159     throw SALOMEDS::Study::StudyInvalidReference();  
1160
1161   SALOMEDSImpl_AttributeStudyProperties* anAttr = _impl->GetProperties();
1162   SALOMEDS_AttributeStudyProperties_i* SP = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
1163   SALOMEDS::AttributeStudyProperties_var aProp = SP->_this();
1164   return aProp._retn();
1165 }
1166
1167 char* SALOMEDS_Study_i::GetLastModificationDate() 
1168 {
1169   SALOMEDS::Locker lock; 
1170
1171   if (_closed)
1172     throw SALOMEDS::Study::StudyInvalidReference();  
1173
1174   return CORBA::string_dup(_impl->GetLastModificationDate().c_str());
1175 }
1176
1177 SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() 
1178 {
1179   SALOMEDS::Locker lock; 
1180   
1181   if (_closed)
1182     throw SALOMEDS::Study::StudyInvalidReference();  
1183
1184   SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
1185
1186   std::vector<std::string> aSeq = _impl->GetModificationsDate();
1187
1188   int aLength = aSeq.size();
1189   aDates->length(aLength);
1190   for (int anIndex = 0; anIndex < aLength; anIndex++) {
1191     aDates[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
1192   }
1193
1194   return aDates._retn();
1195 }
1196
1197 //============================================================================
1198 /*! Function : GetUseCaseBuilder
1199  *  Purpose  : Returns a UseCase builder
1200  */
1201 //============================================================================
1202 SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder() 
1203 {
1204   SALOMEDS::Locker lock; 
1205
1206   if (_closed)
1207     throw SALOMEDS::Study::StudyInvalidReference();  
1208
1209   SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
1210   SALOMEDS::UseCaseBuilder_var uc = UCBuilder->_this();
1211   return uc._retn();
1212 }
1213
1214 //============================================================================
1215 /*! Function : AddPostponed
1216  *  Purpose  : 
1217  */
1218  //============================================================================
1219 void SALOMEDS_Study_i::AddPostponed(const char* theIOR) 
1220 {
1221   SALOMEDS::Locker lock; 
1222   //Not implemented
1223 }
1224
1225 void SALOMEDS_Study_i::AddCreatedPostponed(const char* theIOR) 
1226 {
1227   SALOMEDS::Locker lock; 
1228   //Not implemented
1229 }
1230
1231 //============================================================================
1232 /*! Function : RemovePostponed
1233  *  Purpose  : 
1234  */
1235 //============================================================================
1236 void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/) 
1237 {  
1238   SALOMEDS::Locker lock; 
1239
1240   if (_closed)
1241     throw SALOMEDS::Study::StudyInvalidReference();  
1242
1243   std::vector<std::string> anIORs = _impl->GetIORs();
1244   int i, aSize = (int)anIORs.size();
1245   
1246   for (i = 0; i < aSize; i++) {
1247     try {
1248       CORBA::Object_var obj = _orb->string_to_object(anIORs[i].c_str());
1249       SALOME::GenericObj_var aGeneric = SALOME::GenericObj::_narrow(obj);
1250       //rnv: To avoid double deletion of the Salome Generic Objects:
1251       //rnv: 1. First decrement of the reference count in the SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR();
1252       //rnv: 2. Second decrement of the reference count in the next string : aGeneric->UnRegister();
1253       //if (!CORBA::is_nil(aGeneric)) aGeneric->UnRegister();
1254     } catch (...) {}
1255   }
1256
1257   //Not implemented
1258 }
1259
1260 //============================================================================
1261 /*! Function : UndoPostponed
1262  *  Purpose  : 
1263  */
1264 //============================================================================
1265 void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
1266 {
1267   SALOMEDS::Locker lock; 
1268   //Not implemented
1269 }
1270
1271
1272 //============================================================================
1273 /*! Function : DumpStudy
1274  *  Purpose  : 
1275  */
1276 //============================================================================
1277 CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath, 
1278                                            const char* theBaseName, 
1279                                            CORBA::Boolean isPublished,
1280                                            CORBA::Boolean isMultiFile)
1281 {
1282   SALOMEDS::Locker lock; 
1283
1284   if (_closed)
1285     throw SALOMEDS::Study::StudyInvalidReference();  
1286
1287   std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
1288   SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
1289   bool ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
1290   delete factory;
1291
1292   return ret;
1293 }
1294
1295 //============================================================================
1296 /*! Function : GetCommonParameters
1297  *  Purpose  : 
1298  */
1299 //============================================================================
1300 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
1301 {
1302   SALOMEDS::Locker lock; 
1303   
1304   if (_closed)
1305     throw SALOMEDS::Study::StudyInvalidReference();  
1306
1307   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
1308   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
1309   SALOMEDS::AttributeParameter_var aParam = SP->_this();
1310
1311   return aParam._retn();
1312 }
1313  
1314 //============================================================================
1315 /*! Function : GetCommonModuleParameters
1316  *  Purpose  : 
1317  */
1318 //============================================================================
1319 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
1320                                                                        const char* theModuleName, 
1321                                                                        CORBA::Long theSavePoint)
1322 {
1323   SALOMEDS::Locker lock; 
1324   
1325   if (_closed)
1326     throw SALOMEDS::Study::StudyInvalidReference();  
1327
1328   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
1329   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
1330   SALOMEDS::AttributeParameter_var aParam = SP->_this();
1331
1332   return aParam._retn();
1333 }
1334
1335 //============================================================================
1336 /*! Function : SetStudyLock
1337  *  Purpose  : 
1338  */
1339 //============================================================================
1340 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
1341 {
1342   SALOMEDS::Locker lock; 
1343
1344   if (_closed)
1345     throw SALOMEDS::Study::StudyInvalidReference();  
1346
1347   _impl->SetStudyLock(theLockerID);
1348 }
1349
1350 //============================================================================
1351 /*! Function : IsStudyLocked
1352  *  Purpose  : 
1353  */
1354 //============================================================================
1355 bool SALOMEDS_Study_i::IsStudyLocked()
1356 {
1357   SALOMEDS::Locker lock; 
1358
1359   if (_closed)
1360     throw SALOMEDS::Study::StudyInvalidReference();  
1361
1362   return _impl->IsStudyLocked();
1363 }
1364
1365 //============================================================================
1366 /*! Function : UnLockStudy
1367  *  Purpose  : 
1368  */
1369 //============================================================================
1370 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
1371 {
1372   SALOMEDS::Locker lock; 
1373
1374   if (_closed)
1375     throw SALOMEDS::Study::StudyInvalidReference();  
1376
1377   _impl->UnLockStudy(theLockerID);
1378 }
1379
1380 //============================================================================
1381 /*! Function : GetLockerID
1382  *  Purpose  : 
1383  */
1384 //============================================================================
1385 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
1386 {
1387   SALOMEDS::Locker lock; 
1388
1389   if (_closed)
1390     throw SALOMEDS::Study::StudyInvalidReference();  
1391
1392   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1393
1394   std::vector<std::string> aSeq = _impl->GetLockerID();
1395
1396   int aLength = aSeq.size();
1397   aResult->length(aLength);
1398   for(int anIndex = 0; anIndex < aLength; anIndex++) {
1399     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
1400   }
1401
1402   return aResult._retn();
1403 }
1404 //============================================================================
1405 /*! Function : SetReal
1406  *  Purpose  : 
1407  */
1408 //============================================================================
1409 void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
1410 {
1411   if (_closed)
1412     throw SALOMEDS::Study::StudyInvalidReference();  
1413
1414
1415   _impl->SetVariable(std::string(theVarName), 
1416                      theValue,
1417                      SALOMEDSImpl_GenericVariable::REAL_VAR);
1418   if (_notifier)
1419     _notifier->modifyNB_Notification(theVarName);
1420 }
1421
1422 //============================================================================
1423 /*! Function : SetInteger
1424  *  Purpose  : 
1425  */
1426 //============================================================================
1427 void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
1428 {
1429   if (_closed)
1430     throw SALOMEDS::Study::StudyInvalidReference();  
1431
1432   _impl->SetVariable(std::string(theVarName), 
1433                      theValue,
1434                      SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1435   if (_notifier)
1436     _notifier->modifyNB_Notification(theVarName);
1437 }
1438
1439 //============================================================================
1440 /*! Function : SetBoolean
1441  *  Purpose  : 
1442  */
1443 //============================================================================
1444 void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
1445 {
1446   if (_closed)
1447     throw SALOMEDS::Study::StudyInvalidReference();  
1448
1449   _impl->SetVariable(std::string(theVarName), 
1450                      theValue,
1451                      SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1452   if (_notifier)
1453     _notifier->modifyNB_Notification(theVarName);
1454 }
1455
1456 //============================================================================
1457 /*! Function : SetString
1458  *  Purpose  : 
1459  */
1460 //============================================================================
1461 void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
1462 {
1463   if (_closed)
1464     throw SALOMEDS::Study::StudyInvalidReference();  
1465
1466   _impl->SetStringVariable(std::string(theVarName), 
1467                            theValue,
1468                            SALOMEDSImpl_GenericVariable::STRING_VAR);
1469   if (_notifier)
1470     _notifier->modifyNB_Notification(theVarName);
1471 }
1472
1473 //============================================================================
1474 /*! Function : SetStringAsDouble
1475  *  Purpose  : 
1476  */
1477 //============================================================================
1478 void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
1479 {
1480   if (_closed)
1481     throw SALOMEDS::Study::StudyInvalidReference();  
1482
1483   _impl->SetStringVariableAsDouble(std::string(theVarName), 
1484                                    theValue,
1485                                    SALOMEDSImpl_GenericVariable::STRING_VAR);
1486 }
1487
1488 //============================================================================
1489 /*! Function : GetReal
1490  *  Purpose  : 
1491  */
1492 //============================================================================
1493 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
1494 {
1495   if (_closed)
1496     throw SALOMEDS::Study::StudyInvalidReference();  
1497
1498   return _impl->GetVariableValue(std::string(theVarName));
1499 }
1500
1501 //============================================================================
1502 /*! Function : GetInteger
1503  *  Purpose  : 
1504  */
1505 //============================================================================
1506 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
1507 {
1508   if (_closed)
1509     throw SALOMEDS::Study::StudyInvalidReference();  
1510
1511   return (long)_impl->GetVariableValue(std::string(theVarName));
1512 }
1513
1514 //============================================================================
1515 /*! Function : GetBoolean
1516  *  Purpose  : 
1517  */
1518 //============================================================================
1519 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
1520 {
1521   if (_closed)
1522     throw SALOMEDS::Study::StudyInvalidReference();  
1523
1524   return (bool)_impl->GetVariableValue(std::string(theVarName));
1525 }
1526
1527 //============================================================================
1528 /*! Function : GetString
1529  *  Purpose  : 
1530  */
1531 //============================================================================
1532 char* SALOMEDS_Study_i::GetString(const char* theVarName)
1533 {
1534   if (_closed)
1535     throw SALOMEDS::Study::StudyInvalidReference();  
1536
1537   return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
1538 }
1539
1540 //============================================================================
1541 /*! Function : IsReal
1542  *  Purpose  : 
1543  */
1544 //============================================================================
1545 CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
1546 {
1547   if (_closed)
1548     throw SALOMEDS::Study::StudyInvalidReference();  
1549
1550   return _impl->IsTypeOf(std::string(theVarName),
1551                          SALOMEDSImpl_GenericVariable::REAL_VAR);
1552 }
1553
1554 //============================================================================
1555 /*! Function : IsInteger
1556  *  Purpose  : 
1557  */
1558 //============================================================================
1559 CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
1560 {
1561   if (_closed)
1562     throw SALOMEDS::Study::StudyInvalidReference();  
1563
1564   return _impl->IsTypeOf(std::string(theVarName),
1565                          SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1566 }
1567
1568 //============================================================================
1569 /*! Function : IsBoolean
1570  *  Purpose  : 
1571  */
1572 //============================================================================
1573 CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
1574 {
1575   if (_closed)
1576     throw SALOMEDS::Study::StudyInvalidReference();  
1577
1578   return _impl->IsTypeOf(std::string(theVarName),
1579                          SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1580 }
1581
1582 //============================================================================
1583 /*! Function : IsString
1584  *  Purpose  : 
1585  */
1586 //============================================================================
1587 CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
1588 {
1589   if (_closed)
1590     throw SALOMEDS::Study::StudyInvalidReference();  
1591
1592   return _impl->IsTypeOf(std::string(theVarName),
1593                          SALOMEDSImpl_GenericVariable::STRING_VAR);
1594 }
1595
1596 //============================================================================
1597 /*! Function : IsVariable
1598  *  Purpose  : 
1599  */
1600 //============================================================================
1601 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
1602 {
1603   if (_closed)
1604     throw SALOMEDS::Study::StudyInvalidReference();  
1605
1606   return _impl->IsVariable(std::string(theVarName));
1607 }
1608
1609 //============================================================================
1610 /*! Function : GetVariableNames
1611  *  Purpose  : 
1612  */
1613 //============================================================================
1614 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
1615 {
1616   if (_closed)
1617     throw SALOMEDS::Study::StudyInvalidReference();  
1618
1619   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1620
1621   std::vector<std::string> aVarNames = _impl->GetVariableNames();
1622
1623   int aLen = aVarNames.size();
1624   aResult->length(aLen);
1625   for (int anInd = 0; anInd < aLen; anInd++)
1626     aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1627   
1628   return aResult._retn();
1629 }
1630
1631 //============================================================================
1632 /*! Function : RemoveVariable
1633  *  Purpose  : 
1634  */
1635 //============================================================================
1636 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
1637 {
1638   if (_closed)
1639     throw SALOMEDS::Study::StudyInvalidReference();  
1640
1641   bool res = _impl->RemoveVariable(std::string(theVarName));
1642   if (res && _notifier)
1643     _notifier->modifyNB_Notification(theVarName);
1644
1645   return res;
1646 }
1647
1648 //============================================================================
1649 /*! Function : RenameVariable
1650  *  Purpose  : 
1651  */
1652 //============================================================================
1653 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
1654 {
1655   if (_closed)
1656     throw SALOMEDS::Study::StudyInvalidReference();  
1657
1658   bool res = _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
1659   if (res && _notifier)
1660     _notifier->modifyNB_Notification(theVarName);
1661
1662   return res;
1663 }
1664
1665 //============================================================================
1666 /*! Function : IsVariableUsed
1667  *  Purpose  : 
1668  */
1669 //============================================================================
1670 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
1671 {
1672   if (_closed)
1673     throw SALOMEDS::Study::StudyInvalidReference();  
1674
1675   return _impl->IsVariableUsed(std::string(theVarName));
1676 }
1677
1678
1679 //============================================================================
1680 /*! Function : ParseVariables
1681  *  Purpose  : 
1682  */
1683 //============================================================================
1684 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
1685 {
1686   if (_closed)
1687     throw SALOMEDS::Study::StudyInvalidReference();  
1688
1689   SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
1690
1691   std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
1692
1693   int aSectionsLen = aSections.size();
1694   aResult->length(aSectionsLen);
1695
1696   for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
1697     std::vector<std::string> aVarNames = aSections[aSectionInd];
1698
1699     SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
1700
1701     int aLen = aVarNames.size();
1702     aList->length(aLen);
1703
1704     for (int anInd = 0; anInd < aLen; anInd++)
1705       aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1706
1707     aResult[aSectionInd] = aList;
1708   }
1709
1710   return aResult._retn();
1711 }
1712
1713 //============================================================================
1714 /*! Function : GetDefaultScript
1715  *  Purpose  : 
1716  */
1717 //============================================================================
1718 char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
1719 {
1720   SALOMEDS::Locker lock; 
1721
1722   if (_closed)
1723     throw SALOMEDS::Study::StudyInvalidReference();  
1724
1725   std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
1726   return CORBA::string_dup(script.c_str());
1727 }
1728
1729 //============================================================================
1730 /*! Function : EnableUseCaseAutoFilling
1731  *  Purpose  : 
1732  */
1733 //============================================================================
1734 void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
1735
1736   if (_closed)
1737     throw SALOMEDS::Study::StudyInvalidReference();  
1738
1739   _impl->EnableUseCaseAutoFilling(isEnabled); 
1740   SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
1741   if (builder) {
1742     if (isEnabled) {
1743       builder->SetOnAddSObject(_impl->GetCallback());
1744       builder->SetOnRemoveSObject(_impl->GetCallback());
1745     }
1746     else {
1747       builder->SetOnAddSObject(NULL);
1748       builder->SetOnRemoveSObject(NULL);
1749     }
1750   }
1751 }
1752
1753
1754 CORBA::Long SALOMEDS_Study_i::getPID()
1755 {
1756 #ifdef WIN32
1757   return (CORBA::Long)_getpid();
1758 #else
1759   return (CORBA::Long)getpid();
1760 #endif
1761 }
1762
1763 void SALOMEDS_Study_i::ShutdownWithExit()
1764 {
1765   exit( EXIT_SUCCESS );
1766 }
1767
1768 void SALOMEDS_Study_i::Shutdown()
1769 {
1770   if(!CORBA::is_nil(_orb))
1771     _orb->shutdown(0);
1772 }
1773
1774 //============================================================================
1775 /*! Function : attach
1776  *  Purpose  : This function attach an observer to the study
1777  */
1778 //============================================================================
1779 void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs, CORBA::Boolean modify)
1780 {
1781   if (_notifier)
1782     static_cast<SALOMEDS::Notifier*>(_notifier)->attach(theObs, modify);
1783 }
1784
1785
1786 //============================================================================
1787 /*! Function : detach
1788  *  Purpose  : This function detaches an observer from the study
1789  */
1790 //============================================================================
1791 void SALOMEDS_Study_i::detach(SALOMEDS::Observer_ptr theObs)
1792 {
1793   if (_notifier)
1794     static_cast<SALOMEDS::Notifier*>(_notifier)->detach(theObs);
1795 }
1796
1797 //===========================================================================
1798 //   PRIVATE FUNCTIONS
1799 //===========================================================================
1800 CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
1801 {
1802 #ifdef WIN32
1803   long pid = (long)_getpid();
1804 #else
1805   long pid = (long)getpid();
1806 #endif  
1807   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
1808   return reinterpret_cast<CORBA::LongLong>(_impl);
1809 }
1810
1811 void SALOMEDS_Study_i::NameChanged()
1812 {
1813   // Notify GUI that the name of study was changed
1814   SALOME_NamingService *aNamingService = KERNEL::getNamingService();
1815   CORBA::Object_var obj = aNamingService->Resolve("/Kernel/Session");
1816   SALOME::Session_var aSession = SALOME::Session::_narrow(obj);
1817   if ( !CORBA::is_nil(aSession) ) {
1818     std::stringstream ss;
1819     ss << "studyNameChanged";
1820     std::string str = ss.str();
1821     SALOMEDS::unlock();
1822     aSession->emitMessageOneWay(str.c_str());
1823     SALOMEDS::lock();
1824   }
1825 }