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