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