1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDS_StudyManager_i.cxx
21 // Author : Sergey RUIN
24 #include "utilities.h"
25 #include "SALOME_LifeCycleCORBA.hxx"
26 #include "SALOMEDS_StudyManager_i.hxx"
27 #include "SALOMEDS_Study_i.hxx"
28 #include "SALOMEDS_SComponent_i.hxx"
29 #include "SALOMEDS_Driver_i.hxx"
30 #include "SALOMEDS.hxx"
32 #include "SALOMEDSImpl_Study.hxx"
33 #include "SALOMEDSImpl_SObject.hxx"
34 #include "SALOMEDSImpl_SComponent.hxx"
35 #include "SALOMEDSImpl_AttributeIOR.hxx"
37 #include <TColStd_HArray1OfCharacter.hxx>
38 #include <TCollection_ExtendedString.hxx>
39 #include <TCollection_AsciiString.hxx>
41 #include "Utils_CorbaException.hxx"
50 #include <sys/types.h>
56 #include "SALOME_GenericObj_i.hh"
58 #include "Utils_ExceptHandlers.hxx"
60 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
61 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
63 static SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
65 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
67 //============================================================================
68 /*! Function : SALOMEDS_StudyManager_i
69 * Purpose : SALOMEDS_StudyManager_i constructor
71 //============================================================================
72 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
74 _orb = CORBA::ORB::_duplicate(orb);
75 _poa = PortableServer::POA::_duplicate(thePOA);
76 _name_service = new SALOME_NamingService(_orb);
77 // Study directory creation in the naming service : to register all
78 // open studies in the session
79 _name_service->Create_Directory("/Study");
80 _impl = new SALOMEDSImpl_StudyManager;
81 _factory = new SALOMEDS_DriverFactory_i(_orb);
84 //============================================================================
85 /*! Function : ~SALOMEDS_StudyManager_i
86 * Purpose : SALOMEDS_StudyManager_i destructor
88 //============================================================================
89 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
91 // Destroy directory to register open studies
92 _name_service->Destroy_Directory("/Study");
96 //============================================================================
97 /*! Function : register_name
98 * Purpose : Register the study Manager in the naming service under the
101 //============================================================================
102 void SALOMEDS_StudyManager_i::register_name(char * name)
104 SALOMEDS::StudyManager_var aManager(_this());
105 _name_service->Register(aManager.in(), name);
109 //============================================================================
110 /*! Function : NewStudy
111 * Purpose : Create a New Study of name study_name
113 //============================================================================
114 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
116 SALOMEDS::Locker lock;
118 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->NewStudy(TCollection_AsciiString((char*)study_name));
119 if(aStudyImpl.IsNull()) {
120 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
121 return SALOMEDS::Study::_nil();
124 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
126 SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
127 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
129 // Register study in the naming service
130 // Path to acces the study
131 if(!_name_service->Change_Directory("/Study"))
132 MESSAGE( "Unable to access the study directory" )
134 _name_service->Register(Study, study_name);
136 // Assign the value of the IOR in the study->root
137 CORBA::String_var IORStudy = _orb->object_to_string(Study);
139 aStudyImpl->SetTransientReference((char*)IORStudy.in());
141 _mapOfPOA[Study->StudyId()] = _poa;
146 //============================================================================
148 * Purpose : Open a Study from it's persistent reference
150 //============================================================================
151 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
152 throw(SALOME::SALOME_Exception)
154 SALOMEDS::Locker lock;
156 Unexpect aCatch(SalomeException);
157 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
159 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->Open(TCollection_AsciiString((char*)aUrl));
161 MESSAGE("Open : Creating the CORBA servant holding it... ");
163 // Temporary aStudyUrl in place of study name
164 SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
165 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
167 // Assign the value of the IOR in the study->root
168 CORBA::String_var IORStudy = _orb->object_to_string(Study);
169 aStudyImpl->SetTransientReference((char*)IORStudy.in());
171 // Register study in the naming service
172 // Path to acces the study
173 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
174 else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().ToCString()));
181 //============================================================================
183 * Purpose : Close a study.
184 * If the study hasn't been saved, ask the user to confirm the
185 * close action without saving
187 //============================================================================
188 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
190 SALOMEDS::Locker lock;
192 if(aStudy->_is_nil()) return;
194 // Destroy study name in the naming service
195 if(_name_service->Change_Directory("/Study")){
196 CORBA::String_var aString(aStudy->Name());
197 _name_service->Destroy_Name(aString.in());
205 //============================================================================
207 * Purpose : Save a Study to it's persistent reference
209 //============================================================================
210 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
212 SALOMEDS::Locker lock;
214 if(aStudy->_is_nil()) {
215 MESSAGE("Save error: Study is null");
219 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
220 return _impl->Save(aStudyImpl, _factory, theMultiFile);
223 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
225 SALOMEDS::Locker lock;
227 if(aStudy->_is_nil()) {
228 MESSAGE("SaveASCII error: Study is null");
232 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
233 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
236 //=============================================================================
237 /*! Function : SaveAs
238 * Purpose : Save a study to the persistent reference aUrl
240 //============================================================================
241 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
243 SALOMEDS::Locker lock;
245 if(aStudy->_is_nil()) {
246 MESSAGE("SaveASCII error: Study is null");
250 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
251 return _impl->SaveAs(TCollection_AsciiString((char*)aUrl), aStudyImpl, _factory, theMultiFile);
254 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
256 SALOMEDS::Locker lock;
258 if(aStudy->_is_nil()) {
259 MESSAGE("SaveASCII error: Study is null");
263 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
264 return _impl->SaveAsASCII(TCollection_AsciiString((char*)aUrl), aStudyImpl, _factory, theMultiFile);
267 //============================================================================
268 /*! Function : GetOpenStudies
269 * Purpose : Get name list of open studies in the session
271 //============================================================================
272 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
274 SALOMEDS::Locker lock;
276 Handle(TColStd_HSequenceOfTransient) anOpened = _impl->GetOpenStudies();
277 int aLength = anOpened->Length();
279 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
280 _list_open_studies->length(aLength);
284 MESSAGE("No active study in this session");
288 for (unsigned int ind=1; ind <= aLength; ind++)
290 Handle(SALOMEDSImpl_Study) aStudy = Handle(SALOMEDSImpl_Study)::DownCast(anOpened->Value(ind));
291 _list_open_studies[ind-1] = CORBA::string_dup(aStudy->Name().ToCString());
292 SCRUTE(_list_open_studies[ind-1]) ;
295 return _list_open_studies._retn();
298 //============================================================================
299 /*! Function : GetStudyByName
300 * Purpose : Get a study from its name
302 //============================================================================
303 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
305 SALOMEDS::Locker lock;
307 Handle(SALOMEDSImpl_Study) aStudyImpl =
308 _impl->GetStudyByName(TCollection_AsciiString((char*)aStudyName));
310 if (aStudyImpl.IsNull())
312 MESSAGE(_impl->GetErrorCode().ToCString());
313 return SALOMEDS::Study::_nil();
316 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
317 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
319 return aStudy._retn();
322 //============================================================================
323 /*! Function : GetStudyByID
324 * Purpose : Get a study from its ID
326 //============================================================================
327 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
329 SALOMEDS::Locker lock;
331 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudyID);
333 if (aStudyImpl.IsNull())
335 MESSAGE(_impl->GetErrorCode().ToCString());
336 return SALOMEDS::Study::_nil();
339 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
340 CORBA::Object_var obj = aStudy_servant->_this();
341 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(obj);
343 return aStudy._retn();
347 //============================================================================
348 /*! Function : CanCopy
351 //============================================================================
352 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
354 SALOMEDS::Locker lock;
356 SALOMEDS::Study_var aStudy = theObject->GetStudy();
357 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
358 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
360 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
361 bool ret = _impl->CanCopy(anObject, aDriver);
366 //============================================================================
370 //============================================================================
371 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
373 SALOMEDS::Locker lock;
375 SALOMEDS::Study_var aStudy = theObject->GetStudy();
376 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
377 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
379 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
380 bool ret = _impl->Copy(anObject, aDriver);
385 //============================================================================
386 /*! Function : CanPaste
389 //============================================================================
390 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
392 SALOMEDS::Locker lock;
394 SALOMEDS::Study_var aStudy = theObject->GetStudy();
395 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
396 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
398 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
399 bool ret = _impl->CanPaste(anObject, aDriver);
404 //============================================================================
408 //============================================================================
409 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
410 throw(SALOMEDS::StudyBuilder::LockProtection)
412 SALOMEDS::Locker lock;
414 Unexpect aCatch(LockProtection);
415 SALOMEDS::Study_var aStudy = theObject->GetStudy();
417 Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
418 Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
419 Handle(SALOMEDSImpl_SObject) aNewSO;
422 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
423 aNewSO = _impl->Paste(anObject, aDriver);
427 throw SALOMEDS::StudyBuilder::LockProtection();
430 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
435 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
437 SALOMEDS_Driver_i* driver = NULL;
439 Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
441 TCollection_AsciiString IOREngine = aSCO->GetIOR();
442 if(!IOREngine.IsEmpty()) {
443 CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
444 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
445 driver = new SALOMEDS_Driver_i(Engine, orb);
452 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
453 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
454 return PortableServer::POA::_nil();
457 //===========================================================================
459 //===========================================================================
460 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
463 long pid = (long)_getpid();
465 long pid = (long)getpid();
467 isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
468 SALOMEDSImpl_StudyManager* aManager = _impl.operator->();
469 return ((CORBA::LongLong)aManager);
472 //===========================================================================
477 GetObject(const TDF_Label& theLabel, CORBA::ORB_ptr theORB)
480 Handle(SALOMEDS_IORAttribute) anAttr;
481 if(theLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(),anAttr))
482 return theORB->string_to_object(TCollection_AsciiString(anAttr->Get()).ToCString());
485 return CORBA::Object::_nil();
489 PortableServer::ServantBase_var
490 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
492 if(CORBA::is_nil(theObject))
495 return thePOA->reference_to_servant(theObject);
503 //===========================================================================