Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Study_i.cxx
1 // Copyright (C) 2007-2012  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->UnRegister();
797   }
798
799   //Does not need any more this iterator
800   itcomponent->UnRegister();
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->UnRegister();
846       //if (!CORBA::is_nil(aGeneric)) aGeneric->UnRegister();
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                                            CORBA::Boolean isMultiFile)
878 {
879   SALOMEDS::Locker lock; 
880
881   std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
882   SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
883   CORBA::Boolean ret = _impl->DumpStudy(aPath, aBaseName, isPublished, isMultiFile, factory);
884   delete factory;
885   return ret;
886 }
887
888 //============================================================================
889 /*! Function : GetCommonParameters
890  *  Purpose  : 
891  */
892 //============================================================================
893 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
894 {
895   SALOMEDS::Locker lock; 
896   
897   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
898   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
899   return SP->AttributeParameter::_this();
900 }
901  
902 //============================================================================
903 /*! Function : GetCommonModuleParameters
904  *  Purpose  : 
905  */
906 //============================================================================
907 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
908                                                                        const char* theModuleName, 
909                                                                        CORBA::Long theSavePoint)
910 {
911   SALOMEDS::Locker lock; 
912   
913   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
914   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
915   return SP->AttributeParameter::_this();
916 }
917
918 //============================================================================
919 /*! Function : SetStudyLock
920  *  Purpose  : 
921  */
922 //============================================================================
923 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
924 {
925   SALOMEDS::Locker lock; 
926   _impl->SetStudyLock(theLockerID);
927 }
928
929 //============================================================================
930 /*! Function : IsStudyLocked
931  *  Purpose  : 
932  */
933 //============================================================================
934 bool SALOMEDS_Study_i::IsStudyLocked()
935 {
936   SALOMEDS::Locker lock; 
937   return _impl->IsStudyLocked();
938 }
939
940 //============================================================================
941 /*! Function : UnLockStudy
942  *  Purpose  : 
943  */
944 //============================================================================
945 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
946 {
947   SALOMEDS::Locker lock; 
948   _impl->UnLockStudy(theLockerID);
949 }
950
951 //============================================================================
952 /*! Function : GetLockerID
953  *  Purpose  : 
954  */
955 //============================================================================
956 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
957 {
958   SALOMEDS::Locker lock; 
959
960   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
961
962   std::vector<std::string> aSeq = _impl->GetLockerID();
963
964   int aLength = aSeq.size();
965   aResult->length(aLength);
966   for(int anIndex = 0; anIndex < aLength; anIndex++) {
967     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
968   }
969   return aResult._retn();
970 }
971 //============================================================================
972 /*! Function : SetReal
973  *  Purpose  : 
974  */
975 //============================================================================
976 void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
977 {
978   _impl->SetVariable(std::string(theVarName), 
979                      theValue,
980                      SALOMEDSImpl_GenericVariable::REAL_VAR);
981 }
982
983 //============================================================================
984 /*! Function : SetInteger
985  *  Purpose  : 
986  */
987 //============================================================================
988 void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
989 {
990   _impl->SetVariable(std::string(theVarName), 
991                      theValue,
992                      SALOMEDSImpl_GenericVariable::INTEGER_VAR);
993 }
994
995 //============================================================================
996 /*! Function : SetBoolean
997  *  Purpose  : 
998  */
999 //============================================================================
1000 void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
1001 {
1002   _impl->SetVariable(std::string(theVarName), 
1003                      theValue,
1004                      SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1005 }
1006
1007 //============================================================================
1008 /*! Function : SetString
1009  *  Purpose  : 
1010  */
1011 //============================================================================
1012 void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
1013 {
1014   _impl->SetStringVariable(std::string(theVarName), 
1015                            theValue,
1016                            SALOMEDSImpl_GenericVariable::STRING_VAR);
1017 }
1018
1019 //============================================================================
1020 /*! Function : SetStringAsDouble
1021  *  Purpose  : 
1022  */
1023 //============================================================================
1024 void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
1025 {
1026   _impl->SetStringVariableAsDouble(std::string(theVarName), 
1027                                    theValue,
1028                                    SALOMEDSImpl_GenericVariable::STRING_VAR);
1029 }
1030
1031 //============================================================================
1032 /*! Function : GetReal
1033  *  Purpose  : 
1034  */
1035 //============================================================================
1036 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
1037 {
1038   return _impl->GetVariableValue(std::string(theVarName));
1039 }
1040
1041 //============================================================================
1042 /*! Function : GetInteger
1043  *  Purpose  : 
1044  */
1045 //============================================================================
1046 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
1047 {
1048   return (int)_impl->GetVariableValue(std::string(theVarName));
1049 }
1050
1051 //============================================================================
1052 /*! Function : GetBoolean
1053  *  Purpose  : 
1054  */
1055 //============================================================================
1056 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
1057 {
1058   return (bool)_impl->GetVariableValue(std::string(theVarName));
1059 }
1060
1061 //============================================================================
1062 /*! Function : GetString
1063  *  Purpose  : 
1064  */
1065 //============================================================================
1066 char* SALOMEDS_Study_i::GetString(const char* theVarName)
1067 {
1068   return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
1069 }
1070
1071 //============================================================================
1072 /*! Function : IsReal
1073  *  Purpose  : 
1074  */
1075 //============================================================================
1076 CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
1077 {
1078   return _impl->IsTypeOf(std::string(theVarName),
1079                          SALOMEDSImpl_GenericVariable::REAL_VAR);
1080 }
1081
1082 //============================================================================
1083 /*! Function : IsInteger
1084  *  Purpose  : 
1085  */
1086 //============================================================================
1087 CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
1088 {
1089   return _impl->IsTypeOf(std::string(theVarName),
1090                          SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1091 }
1092
1093 //============================================================================
1094 /*! Function : IsBoolean
1095  *  Purpose  : 
1096  */
1097 //============================================================================
1098 CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
1099 {
1100   return _impl->IsTypeOf(std::string(theVarName),
1101                          SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1102 }
1103
1104 //============================================================================
1105 /*! Function : IsString
1106  *  Purpose  : 
1107  */
1108 //============================================================================
1109 CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
1110 {
1111   return _impl->IsTypeOf(std::string(theVarName),
1112                          SALOMEDSImpl_GenericVariable::STRING_VAR);
1113 }
1114
1115 //============================================================================
1116 /*! Function : IsVariable
1117  *  Purpose  : 
1118  */
1119 //============================================================================
1120 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
1121 {
1122   return _impl->IsVariable(std::string(theVarName));
1123 }
1124
1125 //============================================================================
1126 /*! Function : GetVariableNames
1127  *  Purpose  : 
1128  */
1129 //============================================================================
1130 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
1131 {
1132   std::vector<std::string> aVarNames = _impl->GetVariableNames();
1133   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1134   
1135   int aLen = aVarNames.size();
1136   aResult->length(aLen);
1137   
1138   for (int anInd = 0; anInd < aLen; anInd++)
1139     aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1140   
1141   return aResult._retn();
1142 }
1143
1144 //============================================================================
1145 /*! Function : RemoveVariable
1146  *  Purpose  : 
1147  */
1148 //============================================================================
1149 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
1150 {
1151   return _impl->RemoveVariable(std::string(theVarName));
1152 }
1153
1154 //============================================================================
1155 /*! Function : RenameVariable
1156  *  Purpose  : 
1157  */
1158 //============================================================================
1159 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
1160 {
1161   return _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
1162 }
1163
1164 //============================================================================
1165 /*! Function : IsVariableUsed
1166  *  Purpose  : 
1167  */
1168 //============================================================================
1169 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
1170 {
1171   return _impl->IsVariableUsed(std::string(theVarName));
1172 }
1173
1174
1175 //============================================================================
1176 /*! Function : ParseVariables
1177  *  Purpose  : 
1178  */
1179 //============================================================================
1180 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
1181 {
1182   std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
1183
1184   SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
1185
1186   int aSectionsLen = aSections.size();
1187   aResult->length(aSectionsLen);
1188
1189   for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
1190     std::vector<std::string> aVarNames = aSections[aSectionInd];
1191
1192     SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
1193
1194     int aLen = aVarNames.size();
1195     aList->length(aLen);
1196
1197     for (int anInd = 0; anInd < aLen; anInd++)
1198       aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1199
1200     aResult[aSectionInd] = aList;
1201   }
1202
1203   return aResult._retn();
1204 }
1205
1206 //============================================================================
1207 /*! Function : GetDefaultScript
1208  *  Purpose  : 
1209  */
1210 //============================================================================
1211 char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
1212 {
1213   SALOMEDS::Locker lock; 
1214
1215   std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
1216   return CORBA::string_dup(script.c_str());
1217 }
1218
1219 //============================================================================
1220 /*! Function : EnableUseCaseAutoFilling
1221  *  Purpose  : 
1222  */
1223 //============================================================================
1224 void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
1225
1226   _impl->EnableUseCaseAutoFilling(isEnabled); 
1227   SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
1228   if(builder) {
1229     if(isEnabled) {
1230       builder->SetOnAddSObject(_impl->GetCallback());
1231       builder->SetOnRemoveSObject(_impl->GetCallback());
1232     }
1233     else {
1234       builder->SetOnAddSObject(NULL);
1235       builder->SetOnRemoveSObject(NULL);
1236     }
1237   }
1238 }
1239
1240 //============================================================================
1241 /*! Function : attach
1242  *  Purpose  : This function attach an observer to the study
1243  */
1244 //============================================================================
1245 void SALOMEDS_Study_i::attach(SALOMEDS::Observer_ptr theObs,CORBA::Boolean modify)
1246 {
1247   if(_notifier)
1248     _notifier->attach(theObs,modify);
1249 }
1250
1251 //===========================================================================
1252 //   PRIVATE FUNCTIONS
1253 //===========================================================================
1254 CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
1255 {
1256 #ifdef WIN32
1257   long pid = (long)_getpid();
1258 #else
1259   long pid = (long)getpid();
1260 #endif  
1261   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
1262   return reinterpret_cast<CORBA::LongLong>(_impl);
1263 }