Salome HOME
Rename Engines::Component to Engines::EngineComponent
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
1 //  Copyright (C) 2007-2010  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.
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 "SALOMEDS_Study_i.hxx"
29 #include "SALOMEDS_StudyManager_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 "Basics_Utils.hxx"
51
52 #ifdef WIN32
53 #include <process.h>
54 #else
55 #include <sys/types.h>
56 #include <unistd.h>
57 #endif
58
59 class Notifier: public SALOMEDSImpl_AbstractCallback
60 {
61 public:
62 Notifier(CORBA::ORB_ptr orb)
63 {
64   _orb = CORBA::ORB::_duplicate(orb);
65 }
66
67 //============================================================================
68 /*! Function : addSO_Notification
69  *  Purpose  : This function tells all the observers that a SO has been added
70  */
71 //============================================================================
72
73   virtual bool addSO_Notification(const SALOMEDSImpl_SObject& theSObject)
74     {
75       std::string anID=theSObject.GetID();
76       const char* cID=anID.c_str();
77       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
78         {
79           it->first->notifyObserverID(cID,1);
80         }
81       return true; // NGE return always true but can be modified if needed
82     }
83
84 //============================================================================
85 /*! Function : removeSO_Notification
86  *  Purpose  : This function tells all the observers that a SO has been removed
87  */
88 //============================================================================
89
90   virtual bool removeSO_Notification(const SALOMEDSImpl_SObject& theSObject)
91     {
92       std::string anID=theSObject.GetID();
93       const char* cID=anID.c_str();
94       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
95         {
96           it->first->notifyObserverID(cID,2);
97         }
98       return true; // NGE return always true but can be modified if needed
99     }
100
101 //============================================================================
102 /*! Function : modifySO_Notification
103  *  Purpose  : This function tells all the observers that a SO has been modified
104  */
105 //============================================================================
106
107   virtual bool modifySO_Notification(const SALOMEDSImpl_SObject& theSObject, int reason)
108     {
109       for (ObsListIter it (myObservers.begin()); it != myObservers.end(); ++it)
110         {
111           if(it->second)
112             {
113               std::string anID=theSObject.GetID();
114               const char* cID=anID.c_str();
115               it->first->notifyObserverID(cID,reason);
116             }
117         }
118       return true; // NGE return always true but can be modified if needed
119     }
120
121 //============================================================================
122 /*! Function : attach
123  *  Purpose  : register an Observer
124  */
125 //============================================================================
126
127   virtual void attach(SALOMEDS::Observer_ptr theObs, bool modify)
128     {
129       myObservers.push_back(std::pair< SALOMEDS::Observer_var, bool > (SALOMEDS::Observer::_duplicate(theObs),modify));
130     }
131
132 private:
133     typedef std::list< std::pair< SALOMEDS::Observer_var, bool > > ObsList;
134     typedef ObsList::iterator ObsListIter;
135     ObsList myObservers;
136     CORBA::ORB_var                    _orb;
137 };
138
139
140 std::map<SALOMEDSImpl_Study* , SALOMEDS_Study_i*> SALOMEDS_Study_i::_mapOfStudies;
141
142 //============================================================================
143 /*! Function : SALOMEDS_Study_i
144  *  Purpose  : SALOMEDS_Study_i constructor
145  */
146 //============================================================================
147 SALOMEDS_Study_i::SALOMEDS_Study_i(SALOMEDSImpl_Study* theImpl,
148                                    CORBA::ORB_ptr orb)
149 {
150   _orb = CORBA::ORB::_duplicate(orb);
151   _impl = theImpl;
152   _notifier = new Notifier(_orb);
153   theImpl->setNotifier(_notifier);
154
155   _builder = new SALOMEDS_StudyBuilder_i(_impl->NewBuilder(), _orb);  
156 }
157   
158 //============================================================================
159 /*! Function : ~SALOMEDS_Study_i
160  *  Purpose  : SALOMEDS_Study_i destructor
161  */
162 //============================================================================
163 SALOMEDS_Study_i::~SALOMEDS_Study_i()
164 {
165   //delete the builder servant
166   PortableServer::POA_var poa=_builder->_default_POA();
167   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_builder);
168   poa->deactivate_object(anObjectId.in());
169   _builder->_remove_ref();
170   
171   //delete implementation
172   delete _impl;
173   _mapOfStudies.erase(_impl);
174 }  
175
176 //============================================================================
177 /*! Function : GetPersistentReference
178  *  Purpose  : Get persistent reference of study (idem URL())
179  */
180 //============================================================================
181 char* SALOMEDS_Study_i::GetPersistentReference()
182 {
183   SALOMEDS::Locker lock; 
184   return CORBA::string_dup(_impl->GetPersistentReference().c_str());
185 }
186 //============================================================================
187 /*! Function : GetTransientReference
188  *  Purpose  : Get IOR of the Study (registred in OCAF document in doc->Root)
189  */
190 //============================================================================
191 char* SALOMEDS_Study_i::GetTransientReference()
192 {
193   SALOMEDS::Locker lock; 
194   return CORBA::string_dup(_impl->GetTransientReference().c_str()); 
195 }
196
197 //============================================================================
198 /*! Function : IsEmpty
199  *  Purpose  : Detect if study is empty
200  */
201 //============================================================================
202 CORBA::Boolean SALOMEDS_Study_i::IsEmpty()
203 {
204   SALOMEDS::Locker lock; 
205   return _impl->IsEmpty();
206 }
207
208 //============================================================================
209 /*! Function : FindComponent
210  *  Purpose  : Find a Component with ComponentDataType = aComponentName
211  */
212 //============================================================================
213 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponent (const char* aComponentName)
214 {
215   SALOMEDS::Locker lock; 
216   
217   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponent(std::string(aComponentName));
218   if(aCompImpl.IsNull()) return SALOMEDS::SComponent::_nil();
219
220   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aCompImpl, _orb);
221   return sco._retn();
222 }
223
224 //============================================================================
225 /*! Function : FindComponentID
226  *  Purpose  : Find a Component from it's ID
227  */
228 //============================================================================
229 SALOMEDS::SComponent_ptr SALOMEDS_Study_i::FindComponentID(const char* aComponentID)
230 {
231   SALOMEDS::Locker lock; 
232   
233   SALOMEDSImpl_SComponent aCompImpl = _impl->FindComponentID(std::string((char*)aComponentID));
234   if(aCompImpl.IsNull()) return SALOMEDS::SComponent::_nil();
235
236   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aCompImpl, _orb);
237   return sco._retn();
238 }
239
240 //============================================================================
241 /*! Function : FindObject
242  *  Purpose  : Find an Object with SALOMEDS::Name = anObjectName
243  */
244 //============================================================================
245 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObject(const char* anObjectName)
246 {
247   SALOMEDS::Locker lock; 
248
249   SALOMEDSImpl_SObject aSO = _impl->FindObject(std::string((char*)anObjectName));
250   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
251
252   if(aSO.IsComponent()) {
253     SALOMEDSImpl_SComponent aSCO = aSO;
254     SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aSCO, _orb);
255     return sco._retn();
256   }
257    
258   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
259  
260  return so._retn();
261 }
262
263 //============================================================================
264 /*! Function : FindObjectID
265  *  Purpose  : Find an Object with ID = anObjectID
266  */
267 //============================================================================
268 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectID(const char* anObjectID)
269 {
270   SALOMEDS::Locker lock; 
271
272   SALOMEDSImpl_SObject aSO = _impl->FindObjectID(std::string((char*)anObjectID));
273   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
274   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
275   return so._retn();
276 }
277
278 //============================================================================
279 /*! Function : CreateObjectID
280  *  Purpose  : Creates an Object with ID = anObjectID
281  */
282 //============================================================================
283 SALOMEDS::SObject_ptr SALOMEDS_Study_i::CreateObjectID(const char* anObjectID)
284 {
285   SALOMEDS::Locker lock; 
286
287   if(!anObjectID || strlen(anObjectID) == 0) return SALOMEDS::SObject::_nil();
288
289   SALOMEDSImpl_SObject aSO = _impl->CreateObjectID((char*)anObjectID);
290   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
291
292   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
293   return so._retn();
294 }
295
296 //============================================================================
297 /*! Function : FindObjectByName
298  *  Purpose  : Find Objects with SALOMEDS::Name = anObjectName in a Component
299  *           : with ComponentDataType = aComponentName
300  */
301 //============================================================================
302 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindObjectByName( const char* anObjectName,
303                                                                     const char* aComponentName )
304 {
305   SALOMEDS::Locker lock; 
306
307   std::vector<SALOMEDSImpl_SObject> aSeq = _impl->FindObjectByName(std::string((char*)anObjectName),
308                                                                std::string((char*)aComponentName));
309   int aLength = aSeq.size();
310   SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject ;
311   listSO->length(aLength);
312   for(int i = 0; i<aLength; i++) {
313     SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSeq[i], _orb);
314     listSO[i] = so ;
315   }
316   return listSO._retn() ;
317 }
318
319 //============================================================================
320 /*! Function : FindObjectIOR
321  *  Purpose  : Find an Object with IOR = anObjectIOR
322  */
323 //============================================================================
324 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectIOR(const char* anObjectIOR)
325 {
326   SALOMEDS::Locker lock; 
327
328   SALOMEDSImpl_SObject aSO = _impl->FindObjectIOR(std::string((char*)anObjectIOR));
329   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
330
331   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
332   return so._retn();
333 }
334
335 //============================================================================
336 /*! Function : FindObjectByPath
337  *  Purpose  : Find an Object by its path = thePath
338  */
339 //============================================================================
340 SALOMEDS::SObject_ptr SALOMEDS_Study_i::FindObjectByPath(const char* thePath)
341 {
342   SALOMEDS::Locker lock; 
343
344   SALOMEDSImpl_SObject aSO = _impl->FindObjectByPath(std::string((char*)thePath));
345   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
346
347   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
348   return so._retn();
349 }
350
351 //============================================================================
352 /*! Function : GetObjectPath
353  *  Purpose  : 
354  */
355 //============================================================================
356 char* SALOMEDS_Study_i::GetObjectPath(CORBA::Object_ptr theObject)
357 {
358   SALOMEDS::Locker lock; 
359
360   std::string aPath("");
361   if(CORBA::is_nil(theObject)) return CORBA::string_dup(aPath.c_str());
362   SALOMEDSImpl_SObject aSO;
363   SALOMEDS::SObject_var aSObj = SALOMEDS::SObject::_narrow(theObject);
364
365   if(!CORBA::is_nil(aSObj)) {
366     aSO = _impl->FindObjectID(aSObj->GetID());
367   }
368   else {
369     aSO  = _impl->FindObjectIOR(_orb->object_to_string(theObject));
370   }
371    
372   if(aSO.IsNull()) return CORBA::string_dup(aPath.c_str());
373   
374   aPath = _impl->GetObjectPath(aSO);
375   return  CORBA::string_dup(aPath.c_str());
376 }
377
378
379 //============================================================================
380 /*! Function : SetContext
381  *  Purpose  : Sets the current context
382  */
383 //============================================================================
384 void SALOMEDS_Study_i::SetContext(const char* thePath) 
385 {
386   SALOMEDS::Locker lock; 
387
388   _impl->SetContext(std::string((char*)thePath));
389   if(_impl->IsError() && _impl->GetErrorCode() == "InvalidContext") 
390     throw SALOMEDS::Study::StudyInvalidContext();  
391 }
392
393 //============================================================================
394 /*! Function : GetContext
395  *  Purpose  : Gets the current context
396  */
397 //============================================================================
398 char* SALOMEDS_Study_i::GetContext() 
399 {
400   SALOMEDS::Locker lock; 
401   
402   if(!_impl->HasCurrentContext()) throw SALOMEDS::Study::StudyInvalidContext();   
403   return CORBA::string_dup(_impl->GetContext().c_str());
404 }
405
406 //============================================================================
407 /*! Function : GetObjectNames
408  *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
409  */
410 //============================================================================
411 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetObjectNames(const char* theContext) 
412 {
413   SALOMEDS::Locker lock; 
414
415   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
416
417   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
418     throw SALOMEDS::Study::StudyInvalidContext();
419
420   std::vector<std::string> aSeq = _impl->GetObjectNames(std::string((char*)theContext));
421   if (_impl->GetErrorCode() == "InvalidContext")
422     throw SALOMEDS::Study::StudyInvalidContext();
423
424   int aLength = aSeq.size();
425   aResult->length(aLength);
426   for (int anIndex = 0; anIndex < aLength; anIndex++) {
427     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
428   }
429
430   return aResult._retn();
431 }
432
433 //============================================================================
434 /*! Function : GetDirectoryNames
435  *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
436  */
437 //============================================================================
438 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetDirectoryNames(const char* theContext) 
439 {
440   SALOMEDS::Locker lock; 
441
442   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
443
444   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
445     throw SALOMEDS::Study::StudyInvalidContext();
446
447   std::vector<std::string> aSeq = _impl->GetDirectoryNames(std::string((char*)theContext));
448   if (_impl->GetErrorCode() == "InvalidContext")
449     throw SALOMEDS::Study::StudyInvalidContext();
450
451   int aLength = aSeq.size();
452   aResult->length(aLength);
453   for (int anIndex = 0; anIndex < aLength; anIndex++) {
454     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
455   }
456
457   return aResult._retn();
458 }
459
460 //============================================================================
461 /*! Function : GetFileNames
462  *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
463  */
464 //============================================================================
465 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetFileNames(const char* theContext) 
466 {
467   SALOMEDS::Locker lock; 
468
469   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
470
471   if (strlen(theContext) == 0 && !_impl->HasCurrentContext())
472     throw SALOMEDS::Study::StudyInvalidContext();
473
474   std::vector<std::string> aSeq = _impl->GetFileNames(std::string((char*)theContext));
475   if (_impl->GetErrorCode() == "InvalidContext")
476     throw SALOMEDS::Study::StudyInvalidContext();
477
478   int aLength = aSeq.size();
479   aResult->length(aLength);
480   for (int anIndex = 0; anIndex < aLength; anIndex++) {
481     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
482   }
483
484   return aResult._retn();
485 }
486
487 //============================================================================
488 /*! Function : GetComponentNames
489  *  Purpose  : method to get all components names
490  *  SRN:       Note, theContext can be any, it doesn't matter
491  */
492 //============================================================================
493 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetComponentNames(const char* theContext) 
494 {
495   SALOMEDS::Locker lock; 
496
497   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
498
499   std::vector<std::string> aSeq = _impl->GetComponentNames(std::string((char*)theContext));
500
501   int aLength = aSeq.size();
502   aResult->length(aLength);
503   for(int anIndex = 0; anIndex < aLength; anIndex++) {
504     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
505   }
506
507   return aResult._retn();
508 }
509
510 //============================================================================
511 /*! Function : NewChildIterator
512  *  Purpose  : Create a ChildIterator from an SObject
513  */
514 //============================================================================
515 SALOMEDS::ChildIterator_ptr SALOMEDS_Study_i::NewChildIterator(SALOMEDS::SObject_ptr theSO)
516 {
517   SALOMEDS::Locker lock; 
518
519   CORBA::String_var anID=theSO->GetID();
520   SALOMEDSImpl_SObject aSO = _impl->GetSObject(anID.in());
521   SALOMEDSImpl_ChildIterator anItr(aSO);
522
523   //Create iterator
524   SALOMEDS_ChildIterator_i* it_servant = new SALOMEDS_ChildIterator_i(anItr, _orb);
525
526   return it_servant->_this();
527 }
528
529
530 //============================================================================
531 /*! Function : NewComponentIterator
532  *  Purpose  : Create a SComponentIterator
533  */
534 //============================================================================
535 SALOMEDS::SComponentIterator_ptr SALOMEDS_Study_i::NewComponentIterator()
536 {
537   SALOMEDS::Locker lock; 
538   SALOMEDS_SComponentIterator_i* _it = new SALOMEDS_SComponentIterator_i(_impl->NewComponentIterator(), _orb);
539   _it->Init();
540   return _it->_this();
541 }
542
543
544 //============================================================================
545 /*! Function : NewBuilder
546  *  Purpose  : Create a StudyBuilder
547  */
548 //============================================================================
549 SALOMEDS::StudyBuilder_ptr SALOMEDS_Study_i::NewBuilder()
550 {
551   SALOMEDS::Locker lock; 
552   return _builder->_this();
553 }
554  
555 //============================================================================
556 /*! Function : Name
557  *  Purpose  : get study name
558  */
559 //============================================================================
560 char* SALOMEDS_Study_i::Name()
561 {
562   SALOMEDS::Locker lock; 
563   return CORBA::string_dup(_impl->Name().c_str());
564 }
565
566 //============================================================================
567 /*! Function : Name
568  *  Purpose  : set study name
569  */
570 //============================================================================
571 void SALOMEDS_Study_i::Name(const char* name)
572 {
573   SALOMEDS::Locker lock;  
574   _impl->Name(std::string((char*)name));
575 }
576
577 //============================================================================
578 /*! Function : IsSaved
579  *  Purpose  : get if study has been saved
580  */
581 //============================================================================
582 CORBA::Boolean  SALOMEDS_Study_i::IsSaved()
583 {
584   SALOMEDS::Locker lock; 
585   return _impl->IsSaved();
586 }
587
588 //============================================================================
589 /*! Function : IsSaved
590  *  Purpose  : set if study has been saved
591  */
592 //============================================================================
593 void SALOMEDS_Study_i::IsSaved(CORBA::Boolean save)
594 {
595   SALOMEDS::Locker lock; 
596   _impl->IsSaved(save);
597 }
598
599 //============================================================================
600 /*! Function : IsModified
601  *  Purpose  : Detect if a Study has been modified since it has been saved
602  */
603 //============================================================================
604 CORBA::Boolean  SALOMEDS_Study_i::IsModified()
605 {
606   SALOMEDS::Locker lock; 
607   return _impl->IsModified();
608 }
609
610 //============================================================================
611 /*! Function : Modified
612  *  Purpose  : Sets a Modified flag of a Study to True
613  */
614 //============================================================================
615 void  SALOMEDS_Study_i::Modified()
616 {
617   SALOMEDS::Locker lock; 
618   return _impl->Modify();
619 }
620
621
622 //============================================================================
623 /*! Function : URL
624  *  Purpose  : get URL of the study (persistent reference of the study)
625  */
626 //============================================================================
627 char* SALOMEDS_Study_i::URL()
628 {
629   SALOMEDS::Locker lock; 
630   return CORBA::string_dup(_impl->URL().c_str());
631 }
632
633 //============================================================================
634 /*! Function : URL
635  *  Purpose  : set URL of the study (persistent reference of the study)
636  */
637 //============================================================================
638 void SALOMEDS_Study_i::URL(const char* url)
639 {
640   SALOMEDS::Locker lock; 
641   _impl->URL(std::string((char*)url));
642 }
643
644
645 CORBA::Short SALOMEDS_Study_i::StudyId()
646 {
647   SALOMEDS::Locker lock; 
648   return _impl->StudyId();
649 }
650
651 void SALOMEDS_Study_i::StudyId(CORBA::Short id)
652
653   SALOMEDS::Locker lock; 
654   _impl->StudyId(id);
655 }
656
657 void SALOMEDS_Study_i::UpdateIORLabelMap(const char* anIOR,const char* anEntry) 
658 {
659   SALOMEDS::Locker lock; 
660   _impl->UpdateIORLabelMap(std::string((char*)anIOR), std::string((char*)anEntry));
661 }
662
663 SALOMEDS::Study_ptr SALOMEDS_Study_i::GetStudy(const DF_Label& theLabel, CORBA::ORB_ptr orb) 
664 {
665   SALOMEDS::Locker lock; 
666
667   SALOMEDSImpl_AttributeIOR* Att = NULL;
668   if ((Att=(SALOMEDSImpl_AttributeIOR*)theLabel.Root().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))){
669     char* IOR = CORBA::string_dup(Att->Value().c_str());
670     CORBA::Object_var obj = orb->string_to_object(IOR);
671     SALOMEDS::Study_ptr aStudy = SALOMEDS::Study::_narrow(obj) ;
672     ASSERT(!CORBA::is_nil(aStudy));
673     return SALOMEDS::Study::_duplicate(aStudy);
674   } else {
675     MESSAGE("GetStudy: Problem to get study");
676   }
677   return SALOMEDS::Study::_nil();
678 }
679
680 SALOMEDS_Study_i* SALOMEDS_Study_i::GetStudyServant(SALOMEDSImpl_Study* aStudyImpl, CORBA::ORB_ptr orb)
681 {
682   if (_mapOfStudies.find(aStudyImpl) != _mapOfStudies.end()) 
683     return _mapOfStudies[aStudyImpl];
684   else
685     {
686       SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, orb);
687       _mapOfStudies[aStudyImpl]=Study_servant;
688       return Study_servant;
689     }
690 }
691
692 void SALOMEDS_Study_i::IORUpdated(SALOMEDSImpl_AttributeIOR* theAttribute) 
693 {
694   SALOMEDS::Locker lock; 
695   SALOMEDSImpl_Study::IORUpdated(theAttribute);
696 }
697
698 SALOMEDS::Study::ListOfSObject* SALOMEDS_Study_i::FindDependances(SALOMEDS::SObject_ptr anObject) 
699 {
700   SALOMEDS::Locker lock; 
701
702   SALOMEDS::GenericAttribute_ptr aTarget;
703   if (anObject->FindAttribute(aTarget,"AttributeTarget")) {
704     return SALOMEDS::AttributeTarget::_narrow(aTarget)->Get();
705   }
706   SALOMEDS::Study::ListOfSObject* aList = new SALOMEDS::Study::ListOfSObject;
707   aList->length(0);
708   return aList;
709 }
710
711
712 SALOMEDS::AttributeStudyProperties_ptr SALOMEDS_Study_i::GetProperties() 
713 {
714   SALOMEDS::Locker lock; 
715   
716   SALOMEDSImpl_AttributeStudyProperties* anAttr = _impl->GetProperties();
717   SALOMEDS_AttributeStudyProperties_i* SP = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
718   return SP->AttributeStudyProperties::_this();
719 }
720
721 char* SALOMEDS_Study_i::GetLastModificationDate() 
722 {
723   SALOMEDS::Locker lock; 
724   return CORBA::string_dup(_impl->GetLastModificationDate().c_str());
725 }
726
727 SALOMEDS::ListOfDates* SALOMEDS_Study_i::GetModificationsDate() 
728 {
729   SALOMEDS::Locker lock; 
730   
731   std::vector<std::string> aSeq = _impl->GetModificationsDate();
732   int aLength = aSeq.size();
733   SALOMEDS::ListOfDates_var aDates = new SALOMEDS::ListOfDates;
734   aDates->length(aLength);
735
736   for(int anIndex = 0; anIndex < aLength; anIndex++) {
737     aDates[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
738   }
739   return aDates._retn();
740 }
741
742
743
744 //============================================================================
745 /*! Function : GetUseCaseBuilder
746  *  Purpose  : Returns a UseCase builder
747  */
748 //============================================================================
749 SALOMEDS::UseCaseBuilder_ptr SALOMEDS_Study_i::GetUseCaseBuilder() 
750 {
751   SALOMEDS::Locker lock; 
752   SALOMEDS_UseCaseBuilder_i* UCBuilder = new SALOMEDS_UseCaseBuilder_i(_impl->GetUseCaseBuilder(), _orb);
753   return UCBuilder->_this();
754 }
755
756
757 //============================================================================
758 /*! Function : Close
759  *  Purpose  : 
760  */
761 //============================================================================
762 void SALOMEDS_Study_i::Close()
763 {
764   SALOMEDS::Locker lock; 
765
766   RemovePostponed(-1);
767
768   SALOMEDS::SComponentIterator_var itcomponent = NewComponentIterator();
769   for (; itcomponent->More(); itcomponent->Next()) {
770     SALOMEDS::SComponent_var sco = itcomponent->Value();
771     CORBA::String_var compodatatype=sco->ComponentDataType();
772     MESSAGE ( "Look for an engine for data type :"<< compodatatype);
773     // if there is an associated Engine call its method for closing
774     CORBA::String_var IOREngine;
775     if (sco->ComponentIOR(IOREngine)) {
776       // we have found the associated engine to write the data 
777       MESSAGE ( "We have found an engine for data type :"<< compodatatype);
778       //_narrow can throw a corba exception
779       try
780         {
781           CORBA::Object_var obj = _orb->string_to_object(IOREngine);
782           if (!CORBA::is_nil(obj)) 
783             {
784               SALOMEDS::Driver_var anEngine = SALOMEDS::Driver::_narrow(obj) ;
785               if (!anEngine->_is_nil()) 
786                 { 
787                   SALOMEDS::unlock();
788                   anEngine->Close(sco);
789                   SALOMEDS::lock();
790                 }
791             }
792         } 
793       catch (CORBA::Exception&) 
794         {/*pass*/ }
795     }
796     sco->Destroy();
797   }
798
799   //Does not need any more this iterator
800   itcomponent->Destroy();
801
802
803   _impl->Close();
804 }
805
806 //============================================================================
807 /*! Function : AddPostponed
808  *  Purpose  : 
809  */
810  //============================================================================
811 void SALOMEDS_Study_i::AddPostponed(const char* theIOR) 
812 {
813   SALOMEDS::Locker lock; 
814   //Not implemented
815 }
816
817 void SALOMEDS_Study_i::AddCreatedPostponed(const char* theIOR) 
818 {
819   SALOMEDS::Locker lock; 
820   //Not implemented
821 }
822
823 //============================================================================
824 /*! Function : RemovePostponed
825  *  Purpose  : 
826  */
827 //============================================================================
828 #ifndef WIN32
829 void SALOMEDS_Study_i::RemovePostponed(const CORBA::Long /*theUndoLimit*/) 
830 #else
831 void SALOMEDS_Study_i::RemovePostponed(CORBA::Long /*theUndoLimit*/) 
832 #endif
833 {  
834   SALOMEDS::Locker lock; 
835
836   std::vector<std::string> anIORs = _impl->GetIORs();
837   int i, aSize = (int)anIORs.size();
838
839   for(i = 0; i < aSize; i++) {
840     try {
841       CORBA::Object_var obj = _orb->string_to_object(anIORs[i].c_str());
842       SALOME::GenericObj_var aGeneric = SALOME::GenericObj::_narrow(obj);
843           //rnv: To avoid double deletion of the Salome Generic Objects:
844           //rnv: 1. First decrement of the reference count in the SALOMEDSImpl_AttributeIOR::~SALOMEDSImpl_AttributeIOR();
845           //rnv: 2. Second decrement of the reference count in the next string : aGeneric->Destroy();
846       //if (!CORBA::is_nil(aGeneric)) aGeneric->Destroy();
847     } catch (...) {}
848   }
849
850   //Not implemented
851 }
852
853 //============================================================================
854 /*! Function : UndoPostponed
855  *  Purpose  : 
856  */
857 //============================================================================
858 #ifndef WIN32
859 void SALOMEDS_Study_i::UndoPostponed(const CORBA::Long theWay) 
860 #else
861 void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
862 #endif
863 {
864   SALOMEDS::Locker lock; 
865   //Not implemented
866 }
867
868
869 //============================================================================
870 /*! Function : DumpStudy
871  *  Purpose  : 
872  */
873 //============================================================================
874 CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath, 
875                                            const char* theBaseName, 
876                                            CORBA::Boolean isPublished)
877 {
878   SALOMEDS::Locker lock; 
879
880   std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
881   SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
882   CORBA::Boolean ret = _impl->DumpStudy(aPath, aBaseName, isPublished, factory);
883   delete factory;
884   return ret;
885 }
886
887 //============================================================================
888 /*! Function : GetCommonParameters
889  *  Purpose  : 
890  */
891 //============================================================================
892 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
893 {
894   SALOMEDS::Locker lock; 
895   
896   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
897   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
898   return SP->AttributeParameter::_this();
899 }
900  
901 //============================================================================
902 /*! Function : GetCommonModuleParameters
903  *  Purpose  : 
904  */
905 //============================================================================
906 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
907                                                                        const char* theModuleName, 
908                                                                        CORBA::Long theSavePoint)
909 {
910   SALOMEDS::Locker lock; 
911   
912   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
913   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
914   return SP->AttributeParameter::_this();
915 }
916
917 //============================================================================
918 /*! Function : SetStudyLock
919  *  Purpose  : 
920  */
921 //============================================================================
922 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
923 {
924   SALOMEDS::Locker lock; 
925   _impl->SetStudyLock(theLockerID);
926 }
927
928 //============================================================================
929 /*! Function : IsStudyLocked
930  *  Purpose  : 
931  */
932 //============================================================================
933 bool SALOMEDS_Study_i::IsStudyLocked()
934 {
935   SALOMEDS::Locker lock; 
936   return _impl->IsStudyLocked();
937 }
938
939 //============================================================================
940 /*! Function : UnLockStudy
941  *  Purpose  : 
942  */
943 //============================================================================
944 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
945 {
946   SALOMEDS::Locker lock; 
947   _impl->UnLockStudy(theLockerID);
948 }
949
950 //============================================================================
951 /*! Function : GetLockerID
952  *  Purpose  : 
953  */
954 //============================================================================
955 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
956 {
957   SALOMEDS::Locker lock; 
958
959   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
960
961   std::vector<std::string> aSeq = _impl->GetLockerID();
962
963   int aLength = aSeq.size();
964   aResult->length(aLength);
965   for(int anIndex = 0; anIndex < aLength; anIndex++) {
966     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
967   }
968   return aResult._retn();
969 }
970 //============================================================================
971 /*! Function : SetReal
972  *  Purpose  : 
973  */
974 //============================================================================
975 void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
976 {
977   _impl->SetVariable(std::string(theVarName), 
978                      theValue,
979                      SALOMEDSImpl_GenericVariable::REAL_VAR);
980 }
981
982 //============================================================================
983 /*! Function : SetInteger
984  *  Purpose  : 
985  */
986 //============================================================================
987 void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
988 {
989   _impl->SetVariable(std::string(theVarName), 
990                      theValue,
991                      SALOMEDSImpl_GenericVariable::INTEGER_VAR);
992 }
993
994 //============================================================================
995 /*! Function : SetBoolean
996  *  Purpose  : 
997  */
998 //============================================================================
999 void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
1000 {
1001   _impl->SetVariable(std::string(theVarName), 
1002                      theValue,
1003                      SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1004 }
1005
1006 //============================================================================
1007 /*! Function : SetString
1008  *  Purpose  : 
1009  */
1010 //============================================================================
1011 void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
1012 {
1013   _impl->SetStringVariable(std::string(theVarName), 
1014                            theValue,
1015                            SALOMEDSImpl_GenericVariable::STRING_VAR);
1016 }
1017
1018 //============================================================================
1019 /*! Function : SetStringAsDouble
1020  *  Purpose  : 
1021  */
1022 //============================================================================
1023 void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
1024 {
1025   _impl->SetStringVariableAsDouble(std::string(theVarName), 
1026                                    theValue,
1027                                    SALOMEDSImpl_GenericVariable::STRING_VAR);
1028 }
1029
1030 //============================================================================
1031 /*! Function : GetReal
1032  *  Purpose  : 
1033  */
1034 //============================================================================
1035 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
1036 {
1037   return _impl->GetVariableValue(std::string(theVarName));
1038 }
1039
1040 //============================================================================
1041 /*! Function : GetInteger
1042  *  Purpose  : 
1043  */
1044 //============================================================================
1045 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
1046 {
1047   return (int)_impl->GetVariableValue(std::string(theVarName));
1048 }
1049
1050 //============================================================================
1051 /*! Function : GetBoolean
1052  *  Purpose  : 
1053  */
1054 //============================================================================
1055 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
1056 {
1057   return (bool)_impl->GetVariableValue(std::string(theVarName));
1058 }
1059
1060 //============================================================================
1061 /*! Function : GetString
1062  *  Purpose  : 
1063  */
1064 //============================================================================
1065 char* SALOMEDS_Study_i::GetString(const char* theVarName)
1066 {
1067   return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
1068 }
1069
1070 //============================================================================
1071 /*! Function : IsReal
1072  *  Purpose  : 
1073  */
1074 //============================================================================
1075 CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
1076 {
1077   return _impl->IsTypeOf(std::string(theVarName),
1078                          SALOMEDSImpl_GenericVariable::REAL_VAR);
1079 }
1080
1081 //============================================================================
1082 /*! Function : IsInteger
1083  *  Purpose  : 
1084  */
1085 //============================================================================
1086 CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
1087 {
1088   return _impl->IsTypeOf(std::string(theVarName),
1089                          SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1090 }
1091
1092 //============================================================================
1093 /*! Function : IsBoolean
1094  *  Purpose  : 
1095  */
1096 //============================================================================
1097 CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
1098 {
1099   return _impl->IsTypeOf(std::string(theVarName),
1100                          SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1101 }
1102
1103 //============================================================================
1104 /*! Function : IsString
1105  *  Purpose  : 
1106  */
1107 //============================================================================
1108 CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
1109 {
1110   return _impl->IsTypeOf(std::string(theVarName),
1111                          SALOMEDSImpl_GenericVariable::STRING_VAR);
1112 }
1113
1114 //============================================================================
1115 /*! Function : IsVariable
1116  *  Purpose  : 
1117  */
1118 //============================================================================
1119 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
1120 {
1121   return _impl->IsVariable(std::string(theVarName));
1122 }
1123
1124 //============================================================================
1125 /*! Function : GetVariableNames
1126  *  Purpose  : 
1127  */
1128 //============================================================================
1129 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
1130 {
1131   std::vector<std::string> aVarNames = _impl->GetVariableNames();
1132   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1133   
1134   int aLen = aVarNames.size();
1135   aResult->length(aLen);
1136   
1137   for (int anInd = 0; anInd < aLen; anInd++)
1138     aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1139   
1140   return aResult._retn();
1141 }
1142
1143 //============================================================================
1144 /*! Function : RemoveVariable
1145  *  Purpose  : 
1146  */
1147 //============================================================================
1148 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
1149 {
1150   return _impl->RemoveVariable(std::string(theVarName));
1151 }
1152
1153 //============================================================================
1154 /*! Function : RenameVariable
1155  *  Purpose  : 
1156  */
1157 //============================================================================
1158 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
1159 {
1160   return _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
1161 }
1162
1163 //============================================================================
1164 /*! Function : IsVariableUsed
1165  *  Purpose  : 
1166  */
1167 //============================================================================
1168 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
1169 {
1170   return _impl->IsVariableUsed(std::string(theVarName));
1171 }
1172
1173
1174 //============================================================================
1175 /*! Function : ParseVariables
1176  *  Purpose  : 
1177  */
1178 //============================================================================
1179 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
1180 {
1181   std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
1182
1183   SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
1184
1185   int aSectionsLen = aSections.size();
1186   aResult->length(aSectionsLen);
1187
1188   for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
1189     std::vector<std::string> aVarNames = aSections[aSectionInd];
1190
1191     SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
1192
1193     int aLen = aVarNames.size();
1194     aList->length(aLen);
1195
1196     for (int anInd = 0; anInd < aLen; anInd++)
1197       aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1198
1199     aResult[aSectionInd] = aList;
1200   }
1201
1202   return aResult._retn();
1203 }
1204
1205 //============================================================================
1206 /*! Function : GetDefaultScript
1207  *  Purpose  : 
1208  */
1209 //============================================================================
1210 char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
1211 {
1212   SALOMEDS::Locker lock; 
1213
1214   std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
1215   return CORBA::string_dup(script.c_str());
1216 }
1217
1218 //============================================================================
1219 /*! Function : EnableUseCaseAutoFilling
1220  *  Purpose  : 
1221  */
1222 //============================================================================
1223 void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
1224
1225   _impl->EnableUseCaseAutoFilling(isEnabled); 
1226   SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
1227   if(builder) {
1228     if(isEnabled) {
1229       builder->SetOnAddSObject(_impl->GetCallback());
1230       builder->SetOnRemoveSObject(_impl->GetCallback());
1231     }
1232     else {
1233       builder->SetOnAddSObject(NULL);
1234       builder->SetOnRemoveSObject(NULL);
1235     }
1236   }
1237 }
1238
1239 //============================================================================
1240 /*! Function : attach
1241  *  Purpose  : This function attach an observer to the study
1242  */
1243 //============================================================================
1244 void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs,CORBA::Boolean modify)
1245 {
1246   if(_notifier)
1247     _notifier->attach(theObs,modify);
1248 }
1249
1250 //===========================================================================
1251 //   PRIVATE FUNCTIONS
1252 //===========================================================================
1253 CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
1254 {
1255 #ifdef WIN32
1256   long pid = (long)_getpid();
1257 #else
1258   long pid = (long)getpid();
1259 #endif  
1260   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
1261   return reinterpret_cast<CORBA::LongLong>(_impl);
1262 }