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