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