1 // File : SALOMEDS_StudyManager_i.cxx
2 // Author : Sergey RUIN
6 #include "SALOME_LifeCycleCORBA.hxx"
7 #include "SALOMEDS_StudyManager_i.hxx"
8 #include "SALOMEDS_Study_i.hxx"
9 #include "SALOMEDS_SComponent_i.hxx"
10 #include "SALOMEDS_Driver_i.hxx"
11 #include "SALOMEDS.hxx"
13 #include "SALOMEDSImpl_Study.hxx"
14 #include "SALOMEDSImpl_SObject.hxx"
15 #include "SALOMEDSImpl_SComponent.hxx"
16 #include "SALOMEDSImpl_AttributeIOR.hxx"
18 #include <TColStd_HArray1OfCharacter.hxx>
19 #include <TCollection_ExtendedString.hxx>
20 #include <TCollection_AsciiString.hxx>
22 #include "Utils_CorbaException.hxx"
31 #include <sys/types.h>
37 #include "SALOME_GenericObj_i.hh"
39 #include "Utils_ExceptHandlers.hxx"
41 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
42 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
44 static SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
46 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
48 //============================================================================
49 /*! Function : SALOMEDS_StudyManager_i
50 * Purpose : SALOMEDS_StudyManager_i constructor
52 //============================================================================
53 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
55 _orb = CORBA::ORB::_duplicate(orb);
56 _poa = PortableServer::POA::_duplicate(thePOA);
57 _name_service = new SALOME_NamingService(_orb);
58 // Study directory creation in the naming service : to register all
59 // open studies in the session
60 _name_service->Create_Directory("/Study");
61 _impl = new SALOMEDSImpl_StudyManager;
62 _factory = new SALOMEDS_DriverFactory_i(_orb);
65 //============================================================================
66 /*! Function : ~SALOMEDS_StudyManager_i
67 * Purpose : SALOMEDS_StudyManager_i destructor
69 //============================================================================
70 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
72 // Destroy directory to register open studies
73 _name_service->Destroy_Directory("/Study");
77 //============================================================================
78 /*! Function : register_name
79 * Purpose : Register the study Manager in the naming service under the
82 //============================================================================
83 void SALOMEDS_StudyManager_i::register_name(char * name)
85 SALOMEDS::StudyManager_var aManager(_this());
86 _name_service->Register(aManager.in(), name);
90 //============================================================================
91 /*! Function : NewStudy
92 * Purpose : Create a New Study of name study_name
94 //============================================================================
95 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
97 SALOMEDS::Locker lock;
99 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->NewStudy(TCollection_AsciiString((char*)study_name));
100 if(aStudyImpl.IsNull()) {
101 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
102 return SALOMEDS::Study::_nil();
105 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
107 SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
108 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
110 // Register study in the naming service
111 // Path to acces the study
112 if(!_name_service->Change_Directory("/Study"))
113 MESSAGE( "Unable to access the study directory" )
115 _name_service->Register(Study, study_name);
117 // Assign the value of the IOR in the study->root
118 const char* IORStudy = _orb->object_to_string(Study);
120 aStudyImpl->SetTransientReference((char*)IORStudy);
122 _mapOfPOA[Study->StudyId()] = _poa;
127 //============================================================================
129 * Purpose : Open a Study from it's persistent reference
131 //============================================================================
132 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
133 throw(SALOME::SALOME_Exception)
135 SALOMEDS::Locker lock;
137 Unexpect aCatch(SalomeException);
138 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
140 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->Open(TCollection_AsciiString((char*)aUrl));
142 MESSAGE("Open : Creating the CORBA servant holding it... ");
144 // Temporary aStudyUrl in place of study name
145 SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
146 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
148 // Assign the value of the IOR in the study->root
149 CORBA::String_var IORStudy = _orb->object_to_string(Study);
150 aStudyImpl->SetTransientReference((char*)IORStudy);
152 // Register study in the naming service
153 // Path to acces the study
154 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
155 else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().ToCString()));
162 //============================================================================
164 * Purpose : Close a study.
165 * If the study hasn't been saved, ask the user to confirm the
166 * close action without saving
168 //============================================================================
169 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
171 SALOMEDS::Locker lock;
173 if(aStudy->_is_nil()) return;
175 // Destroy study name in the naming service
176 if(_name_service->Change_Directory("/Study")){
177 CORBA::String_var aString(aStudy->Name());
178 _name_service->Destroy_Name(aString.in());
186 //============================================================================
188 * Purpose : Save a Study to it's persistent reference
190 //============================================================================
191 void SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
193 SALOMEDS::Locker lock;
195 if(aStudy->_is_nil()) {
196 MESSAGE("Save error: Study is null");
200 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
201 _impl->Save(aStudyImpl, _factory, theMultiFile);
204 void SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
206 SALOMEDS::Locker lock;
208 if(aStudy->_is_nil()) {
209 MESSAGE("SaveASCII error: Study is null");
213 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
214 _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
217 //=============================================================================
218 /*! Function : SaveAs
219 * Purpose : Save a study to the persistent reference aUrl
221 //============================================================================
222 void SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
224 SALOMEDS::Locker lock;
226 if(aStudy->_is_nil()) {
227 MESSAGE("SaveASCII error: Study is null");
231 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
232 _impl->SaveAs(TCollection_AsciiString((char*)aUrl), aStudyImpl, _factory, theMultiFile);
235 void SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
237 SALOMEDS::Locker lock;
239 if(aStudy->_is_nil()) {
240 MESSAGE("SaveASCII error: Study is null");
244 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
245 _impl->SaveAsASCII(TCollection_AsciiString((char*)aUrl), aStudyImpl, _factory, theMultiFile);
248 //============================================================================
249 /*! Function : GetOpenStudies
250 * Purpose : Get name list of open studies in the session
252 //============================================================================
253 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
255 SALOMEDS::Locker lock;
257 Handle(TColStd_HSequenceOfTransient) anOpened = _impl->GetOpenStudies();
258 int aLength = anOpened->Length();
260 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
261 _list_open_studies->length(aLength);
265 MESSAGE("No active study in this session");
269 for (unsigned int ind=1; ind <= aLength; ind++)
271 Handle(SALOMEDSImpl_Study) aStudy = Handle(SALOMEDSImpl_Study)::DownCast(anOpened->Value(ind));
272 _list_open_studies[ind-1] = CORBA::string_dup(aStudy->Name().ToCString());
273 SCRUTE(_list_open_studies[ind-1]) ;
276 return _list_open_studies._retn();
279 //============================================================================
280 /*! Function : GetStudyByName
281 * Purpose : Get a study from its name
283 //============================================================================
284 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
286 SALOMEDS::Locker lock;
288 Handle(SALOMEDSImpl_Study) aStudyImpl =
289 _impl->GetStudyByName(TCollection_AsciiString((char*)aStudyName));
291 if (aStudyImpl.IsNull())
293 MESSAGE(_impl->GetErrorCode().ToCString());
294 return SALOMEDS::Study::_nil();
297 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
298 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
300 return aStudy._retn();
303 //============================================================================
304 /*! Function : GetStudyByID
305 * Purpose : Get a study from its ID
307 //============================================================================
308 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
310 SALOMEDS::Locker lock;
312 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudyID);
314 if (aStudyImpl.IsNull())
316 MESSAGE(_impl->GetErrorCode().ToCString());
317 return SALOMEDS::Study::_nil();
320 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
321 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
323 return aStudy._retn();
327 //============================================================================
328 /*! Function : CanCopy
331 //============================================================================
332 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
334 SALOMEDS::Locker lock;
336 SALOMEDS::Study_var aStudy = theObject->GetStudy();
337 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
338 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
340 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
341 bool ret = _impl->CanCopy(anObject, aDriver);
346 //============================================================================
350 //============================================================================
351 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
353 SALOMEDS::Locker lock;
355 SALOMEDS::Study_var aStudy = theObject->GetStudy();
356 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
357 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
359 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
360 bool ret = _impl->Copy(anObject, aDriver);
365 //============================================================================
366 /*! Function : CanPaste
369 //============================================================================
370 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
372 SALOMEDS::Locker lock;
374 SALOMEDS::Study_var aStudy = theObject->GetStudy();
375 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
376 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
378 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
379 bool ret = _impl->CanPaste(anObject, aDriver);
384 //============================================================================
388 //============================================================================
389 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
390 throw(SALOMEDS::StudyBuilder::LockProtection)
392 SALOMEDS::Locker lock;
394 Unexpect aCatch(LockProtection);
395 SALOMEDS::Study_var aStudy = theObject->GetStudy();
397 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
398 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
399 Handle(SALOMEDSImpl_SObject) aNewSO;
402 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
403 aNewSO = _impl->Paste(anObject, aDriver);
407 throw SALOMEDS::StudyBuilder::LockProtection();
410 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
415 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
417 SALOMEDS_Driver_i* driver = NULL;
419 Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
421 TCollection_AsciiString IOREngine = aSCO->GetIOR();
422 if(!IOREngine.IsEmpty()) {
423 CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
424 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
425 driver = new SALOMEDS_Driver_i(Engine, orb);
432 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
433 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
434 return PortableServer::POA::_nil();
437 //===========================================================================
439 //===========================================================================
440 long SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
443 long pid = (long)_getpid();
445 long pid = (long)getpid();
447 isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
448 SALOMEDSImpl_StudyManager* aManager = _impl.operator->();
449 return ((long)aManager);
452 //===========================================================================
457 GetObject(const TDF_Label& theLabel, CORBA::ORB_ptr theORB)
460 Handle(SALOMEDS_IORAttribute) anAttr;
461 if(theLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(),anAttr))
462 return theORB->string_to_object(TCollection_AsciiString(anAttr->Get()).ToCString());
465 return CORBA::Object::_nil();
469 PortableServer::ServantBase_var
470 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
472 if(CORBA::is_nil(theObject))
475 return thePOA->reference_to_servant(theObject);
483 //===========================================================================