Salome HOME
Merge from V5_1_main 14/05/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       if (!CORBA::is_nil(aGeneric)) aGeneric->Destroy();
760     } catch (...) {}
761   }
762
763   //Not implemented
764 }
765
766 //============================================================================
767 /*! Function : UndoPostponed
768  *  Purpose  : 
769  */
770 //============================================================================
771 #ifndef WIN32
772 void SALOMEDS_Study_i::UndoPostponed(const CORBA::Long theWay) 
773 #else
774 void SALOMEDS_Study_i::UndoPostponed(CORBA::Long theWay) 
775 #endif
776 {
777   SALOMEDS::Locker lock; 
778   //Not implemented
779 }
780
781
782 //============================================================================
783 /*! Function : DumpStudy
784  *  Purpose  : 
785  */
786 //============================================================================
787 CORBA::Boolean SALOMEDS_Study_i::DumpStudy(const char* thePath, 
788                                            const char* theBaseName, 
789                                            CORBA::Boolean isPublished)
790 {
791   SALOMEDS::Locker lock; 
792
793   std::string aPath((char*)thePath), aBaseName((char*)theBaseName);
794   SALOMEDS_DriverFactory_i* factory = new SALOMEDS_DriverFactory_i(_orb);
795   CORBA::Boolean ret = _impl->DumpStudy(aPath, aBaseName, isPublished, factory);
796   delete factory;
797   return ret;
798 }
799
800 //============================================================================
801 /*! Function : GetCommonParameters
802  *  Purpose  : 
803  */
804 //============================================================================
805 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetCommonParameters(const char* theID, CORBA::Long theSavePoint)
806 {
807   SALOMEDS::Locker lock; 
808   
809   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetCommonParameters(theID, theSavePoint);
810   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
811   return SP->AttributeParameter::_this();
812 }
813  
814 //============================================================================
815 /*! Function : GetCommonModuleParameters
816  *  Purpose  : 
817  */
818 //============================================================================
819 SALOMEDS::AttributeParameter_ptr SALOMEDS_Study_i::GetModuleParameters(const char* theID, 
820                                                                        const char* theModuleName, 
821                                                                        CORBA::Long theSavePoint)
822 {
823   SALOMEDS::Locker lock; 
824   
825   SALOMEDSImpl_AttributeParameter* anAttr = _impl->GetModuleParameters(theID, theModuleName, theSavePoint);
826   SALOMEDS_AttributeParameter_i* SP = new SALOMEDS_AttributeParameter_i(anAttr, _orb);
827   return SP->AttributeParameter::_this();
828 }
829
830 //============================================================================
831 /*! Function : SetStudyLock
832  *  Purpose  : 
833  */
834 //============================================================================
835 void SALOMEDS_Study_i::SetStudyLock(const char* theLockerID)
836 {
837   SALOMEDS::Locker lock; 
838   _impl->SetStudyLock(theLockerID);
839 }
840
841 //============================================================================
842 /*! Function : IsStudyLocked
843  *  Purpose  : 
844  */
845 //============================================================================
846 bool SALOMEDS_Study_i::IsStudyLocked()
847 {
848   SALOMEDS::Locker lock; 
849   return _impl->IsStudyLocked();
850 }
851
852 //============================================================================
853 /*! Function : UnLockStudy
854  *  Purpose  : 
855  */
856 //============================================================================
857 void SALOMEDS_Study_i::UnLockStudy(const char* theLockerID)
858 {
859   SALOMEDS::Locker lock; 
860   _impl->UnLockStudy(theLockerID);
861 }
862
863 //============================================================================
864 /*! Function : GetLockerID
865  *  Purpose  : 
866  */
867 //============================================================================
868 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetLockerID()
869 {
870   SALOMEDS::Locker lock; 
871
872   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
873
874   std::vector<std::string> aSeq = _impl->GetLockerID();
875
876   int aLength = aSeq.size();
877   aResult->length(aLength);
878   for(int anIndex = 0; anIndex < aLength; anIndex++) {
879     aResult[anIndex] = CORBA::string_dup(aSeq[anIndex].c_str());
880   }
881   return aResult._retn();
882 }
883 //============================================================================
884 /*! Function : SetReal
885  *  Purpose  : 
886  */
887 //============================================================================
888 void SALOMEDS_Study_i::SetReal(const char* theVarName, CORBA::Double theValue)
889 {
890   _impl->SetVariable(std::string(theVarName), 
891                      theValue,
892                      SALOMEDSImpl_GenericVariable::REAL_VAR);
893 }
894
895 //============================================================================
896 /*! Function : SetInteger
897  *  Purpose  : 
898  */
899 //============================================================================
900 void SALOMEDS_Study_i::SetInteger(const char* theVarName, CORBA::Long theValue)
901 {
902   _impl->SetVariable(std::string(theVarName), 
903                      theValue,
904                      SALOMEDSImpl_GenericVariable::INTEGER_VAR);
905 }
906
907 //============================================================================
908 /*! Function : SetBoolean
909  *  Purpose  : 
910  */
911 //============================================================================
912 void SALOMEDS_Study_i::SetBoolean(const char* theVarName, CORBA::Boolean theValue)
913 {
914   _impl->SetVariable(std::string(theVarName), 
915                      theValue,
916                      SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
917 }
918
919 //============================================================================
920 /*! Function : SetString
921  *  Purpose  : 
922  */
923 //============================================================================
924 void SALOMEDS_Study_i::SetString(const char* theVarName, const char* theValue)
925 {
926   _impl->SetStringVariable(std::string(theVarName), 
927                            theValue,
928                            SALOMEDSImpl_GenericVariable::STRING_VAR);
929 }
930
931 //============================================================================
932 /*! Function : SetStringAsDouble
933  *  Purpose  : 
934  */
935 //============================================================================
936 void SALOMEDS_Study_i::SetStringAsDouble(const char* theVarName, CORBA::Double theValue)
937 {
938   _impl->SetStringVariableAsDouble(std::string(theVarName), 
939                                    theValue,
940                                    SALOMEDSImpl_GenericVariable::STRING_VAR);
941 }
942
943 //============================================================================
944 /*! Function : GetReal
945  *  Purpose  : 
946  */
947 //============================================================================
948 CORBA::Double SALOMEDS_Study_i::GetReal(const char* theVarName)
949 {
950   return _impl->GetVariableValue(std::string(theVarName));
951 }
952
953 //============================================================================
954 /*! Function : GetInteger
955  *  Purpose  : 
956  */
957 //============================================================================
958 CORBA::Long SALOMEDS_Study_i::GetInteger(const char* theVarName)
959 {
960   return (int)_impl->GetVariableValue(std::string(theVarName));
961 }
962
963 //============================================================================
964 /*! Function : GetBoolean
965  *  Purpose  : 
966  */
967 //============================================================================
968 CORBA::Boolean SALOMEDS_Study_i::GetBoolean(const char* theVarName)
969 {
970   return (bool)_impl->GetVariableValue(std::string(theVarName));
971 }
972
973 //============================================================================
974 /*! Function : GetString
975  *  Purpose  : 
976  */
977 //============================================================================
978 char* SALOMEDS_Study_i::GetString(const char* theVarName)
979 {
980   return CORBA::string_dup(_impl->GetStringVariableValue(std::string(theVarName)).c_str());
981 }
982
983 //============================================================================
984 /*! Function : IsReal
985  *  Purpose  : 
986  */
987 //============================================================================
988 CORBA::Boolean SALOMEDS_Study_i::IsReal(const char* theVarName)
989 {
990   return _impl->IsTypeOf(std::string(theVarName),
991                          SALOMEDSImpl_GenericVariable::REAL_VAR);
992 }
993
994 //============================================================================
995 /*! Function : IsInteger
996  *  Purpose  : 
997  */
998 //============================================================================
999 CORBA::Boolean SALOMEDS_Study_i::IsInteger(const char* theVarName)
1000 {
1001   return _impl->IsTypeOf(std::string(theVarName),
1002                          SALOMEDSImpl_GenericVariable::INTEGER_VAR);
1003 }
1004
1005 //============================================================================
1006 /*! Function : IsBoolean
1007  *  Purpose  : 
1008  */
1009 //============================================================================
1010 CORBA::Boolean SALOMEDS_Study_i::IsBoolean(const char* theVarName)
1011 {
1012   return _impl->IsTypeOf(std::string(theVarName),
1013                          SALOMEDSImpl_GenericVariable::BOOLEAN_VAR);
1014 }
1015
1016 //============================================================================
1017 /*! Function : IsString
1018  *  Purpose  : 
1019  */
1020 //============================================================================
1021 CORBA::Boolean SALOMEDS_Study_i::IsString(const char* theVarName)
1022 {
1023   return _impl->IsTypeOf(std::string(theVarName),
1024                          SALOMEDSImpl_GenericVariable::STRING_VAR);
1025 }
1026
1027 //============================================================================
1028 /*! Function : IsVariable
1029  *  Purpose  : 
1030  */
1031 //============================================================================
1032 CORBA::Boolean SALOMEDS_Study_i::IsVariable(const char* theVarName)
1033 {
1034   return _impl->IsVariable(std::string(theVarName));
1035 }
1036
1037 //============================================================================
1038 /*! Function : GetVariableNames
1039  *  Purpose  : 
1040  */
1041 //============================================================================
1042 SALOMEDS::ListOfStrings* SALOMEDS_Study_i::GetVariableNames()
1043 {
1044   std::vector<std::string> aVarNames = _impl->GetVariableNames();
1045   SALOMEDS::ListOfStrings_var aResult = new SALOMEDS::ListOfStrings;
1046   
1047   int aLen = aVarNames.size();
1048   aResult->length(aLen);
1049   
1050   for (int anInd = 0; anInd < aLen; anInd++)
1051     aResult[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1052   
1053   return aResult._retn();
1054 }
1055
1056 //============================================================================
1057 /*! Function : RemoveVariable
1058  *  Purpose  : 
1059  */
1060 //============================================================================
1061 CORBA::Boolean SALOMEDS_Study_i::RemoveVariable(const char* theVarName)
1062 {
1063   return _impl->RemoveVariable(std::string(theVarName));
1064 }
1065
1066 //============================================================================
1067 /*! Function : RenameVariable
1068  *  Purpose  : 
1069  */
1070 //============================================================================
1071 CORBA::Boolean SALOMEDS_Study_i::RenameVariable(const char* theVarName, const char* theNewVarName)
1072 {
1073   return _impl->RenameVariable(std::string(theVarName), std::string(theNewVarName));
1074 }
1075
1076 //============================================================================
1077 /*! Function : IsVariableUsed
1078  *  Purpose  : 
1079  */
1080 //============================================================================
1081 CORBA::Boolean SALOMEDS_Study_i::IsVariableUsed(const char* theVarName)
1082 {
1083   return _impl->IsVariableUsed(std::string(theVarName));
1084 }
1085
1086
1087 //============================================================================
1088 /*! Function : ParseVariables
1089  *  Purpose  : 
1090  */
1091 //============================================================================
1092 SALOMEDS::ListOfListOfStrings* SALOMEDS_Study_i::ParseVariables(const char* theVarName)
1093 {
1094   std::vector< std::vector<std::string> > aSections = _impl->ParseVariables(std::string(theVarName));
1095
1096   SALOMEDS::ListOfListOfStrings_var aResult = new SALOMEDS::ListOfListOfStrings;
1097
1098   int aSectionsLen = aSections.size();
1099   aResult->length(aSectionsLen);
1100
1101   for (int aSectionInd = 0; aSectionInd < aSectionsLen; aSectionInd++) {
1102     std::vector<std::string> aVarNames = aSections[aSectionInd];
1103
1104     SALOMEDS::ListOfStrings_var aList = new SALOMEDS::ListOfStrings;
1105
1106     int aLen = aVarNames.size();
1107     aList->length(aLen);
1108
1109     for (int anInd = 0; anInd < aLen; anInd++)
1110       aList[anInd] = CORBA::string_dup(aVarNames[anInd].c_str());
1111
1112     aResult[aSectionInd] = aList;
1113   }
1114
1115   return aResult._retn();
1116 }
1117
1118 //============================================================================
1119 /*! Function : GetDefaultScript
1120  *  Purpose  : 
1121  */
1122 //============================================================================
1123 char* SALOMEDS_Study_i::GetDefaultScript(const char* theModuleName, const char* theShift)
1124 {
1125   SALOMEDS::Locker lock; 
1126
1127   std::string script = SALOMEDSImpl_IParameters::getDefaultScript(_impl, theModuleName, theShift);
1128   return CORBA::string_dup(script.c_str());
1129 }
1130
1131 //============================================================================
1132 /*! Function : EnableUseCaseAutoFilling
1133  *  Purpose  : 
1134  */
1135 //============================================================================
1136 void SALOMEDS_Study_i::EnableUseCaseAutoFilling(CORBA::Boolean isEnabled) 
1137
1138   _impl->EnableUseCaseAutoFilling(isEnabled); 
1139   SALOMEDSImpl_StudyBuilder* builder = _builder->GetImpl();
1140   if(builder) {
1141     if(isEnabled) {
1142       builder->SetOnAddSObject(_impl->GetCallback());
1143       builder->SetOnRemoveSObject(_impl->GetCallback());
1144     }
1145     else {
1146       builder->SetOnAddSObject(NULL);
1147       builder->SetOnRemoveSObject(NULL);
1148     }
1149   }
1150 }
1151
1152 //===========================================================================
1153 //   PRIVATE FUNCTIONS
1154 //===========================================================================
1155 CORBA::LongLong SALOMEDS_Study_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
1156 {
1157 #ifdef WIN32
1158   long pid = (long)_getpid();
1159 #else
1160   long pid = (long)getpid();
1161 #endif  
1162   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
1163   return reinterpret_cast<CORBA::LongLong>(_impl);
1164 }