]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_StudyManager_i.cxx
Salome HOME
Merge branch 'master' into agr/start_procedure
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager_i.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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_StudyManager_i.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28 #include "SALOME_LifeCycleCORBA.hxx"
29 #include "SALOMEDS_StudyManager_i.hxx"
30 #include "SALOMEDS_Study_i.hxx"
31 #include "SALOMEDS_SComponent_i.hxx"
32 #include "SALOMEDS_Driver_i.hxx"
33 #include "SALOMEDS_SimanStudy_i.hxx"
34 #include "SALOMEDS.hxx"
35
36 #include "SALOMEDSImpl_Study.hxx"
37 #include "SALOMEDSImpl_SObject.hxx"
38 #include "SALOMEDSImpl_SComponent.hxx"
39 #include "SALOMEDSImpl_AttributeIOR.hxx"
40
41 #include "Utils_CorbaException.hxx"
42 #include "Utils_ExceptHandlers.hxx"
43 #include "Basics_Utils.hxx"
44 #include "SALOME_GenericObj_i.hh"
45
46 #include <sstream>
47 #include <vector>
48 #include <map>
49
50 #ifdef WIN32
51 #include <process.h>
52 #else
53 #include <sys/types.h>
54 #include <unistd.h>
55 #endif
56
57 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
58 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
59
60 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
61
62 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
63
64 //============================================================================
65 /*! Function : SALOMEDS_StudyManager_i
66  *  Purpose  : SALOMEDS_StudyManager_i constructor
67  */
68 //============================================================================
69 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
70 {
71   _orb = CORBA::ORB::_duplicate(orb);
72   _poa = PortableServer::POA::_duplicate(thePOA);
73   _name_service = new SALOME_NamingService(_orb);
74   // Study directory creation in the naming service : to register all
75   // open studies in the session
76   _name_service->Create_Directory("/Study");
77   _impl = new SALOMEDSImpl_StudyManager;
78   _factory = new SALOMEDS_DriverFactory_i(_orb);
79 }
80
81 //============================================================================
82 /*! Function : ~SALOMEDS_StudyManager_i
83  *  Purpose  : SALOMEDS_StudyManager_i destructor
84  */
85 //============================================================================
86 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
87 {
88   // Destroy directory to register open studies
89   _name_service->Destroy_Directory("/Study");
90   delete _name_service;
91   delete _factory;
92   delete _impl;
93   delete SALOMEDS_SimanStudy_i::GetSimanServant(_orb);
94 }
95
96 //============================================================================
97 /*! Function : register_name
98  *  Purpose  : Register the study Manager in the naming service under the
99  *             context name
100  */
101 //============================================================================
102 void SALOMEDS_StudyManager_i::register_name(const char * name)
103 {
104   SALOMEDS::StudyManager_var aManager(_this());
105   _name_service->Register(aManager.in(), name);
106 }
107
108
109 //============================================================================
110 /*! Function : NewStudy
111  *  Purpose  : Create a New Study of name study_name
112  */
113 //============================================================================
114 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
115 {
116   SALOMEDS::Locker lock;
117
118   SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
119   if(!aStudyImpl) {
120     MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
121     return SALOMEDS::Study::_nil();
122   }
123
124   MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
125
126   SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
127   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
128   SALOMEDS::Study_var Study = Study_servant->_this();
129
130   // Register study in the naming service
131   // Path to acces the study
132   if(!_name_service->Change_Directory("/Study"))
133       MESSAGE( "Unable to access the study directory" )
134   else
135       _name_service->Register(Study, study_name);
136
137   // Assign the value of the IOR in the study->root
138   CORBA::String_var IORStudy = _orb->object_to_string(Study);
139
140   aStudyImpl->SetTransientReference((char*)IORStudy.in());
141
142   _mapOfPOA[Study->StudyId()] = _poa;
143
144   return Study._retn();
145 }
146
147 //============================================================================
148 /*! Function : Open
149  *  Purpose  : Open a Study from it's persistent reference
150  */
151 //============================================================================
152 SALOMEDS::Study_ptr  SALOMEDS_StudyManager_i::Open(const char* aUrl)
153      throw(SALOME::SALOME_Exception)
154 {
155   SALOMEDS::Locker lock;
156
157   Unexpect aCatch(SalomeException);
158   MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
159
160   SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
161
162   if ( !aStudyImpl )
163     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
164
165   MESSAGE("Open : Creating the CORBA servant holding it... ");
166
167   // Temporary aStudyUrl in place of study name
168   SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
169   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
170   SALOMEDS::Study_var Study = Study_servant->_this();
171
172   // Assign the value of the IOR in the study->root
173   CORBA::String_var IORStudy = _orb->object_to_string(Study);
174   aStudyImpl->SetTransientReference((char*)IORStudy.in());
175
176   _mapOfPOA[Study->StudyId()] = _poa;
177
178   // Register study in the naming service
179   // Path to acces the study
180   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
181   else _name_service->Register(Study, aStudyImpl->Name().c_str());
182
183   return Study._retn();
184 }
185
186
187
188 //============================================================================
189 /*! Function : Close
190  *  Purpose  : Close a study.
191  *             If the study hasn't been saved, ask the user to confirm the
192  *             close action without saving
193  */
194 //============================================================================
195 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
196 {
197   SALOMEDS::Locker lock;
198
199   if(aStudy->_is_nil()) return;
200
201   // Destroy study name in the naming service
202   if(_name_service->Change_Directory("/Study")){
203     CORBA::String_var aString(aStudy->Name());
204     _name_service->Destroy_Name(aString.in());
205   }
206
207   SALOMEDS::unlock();
208   aStudy->Close();
209   SALOMEDS::lock();
210
211   //remove study servant
212   PortableServer::POA_ptr poa=GetPOA(aStudy);
213   PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
214   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
215   poa->deactivate_object(anObjectId.in());
216   aservant->_remove_ref(); // decrement for the call to reference_to_servant
217   aservant->_remove_ref(); // to delete the object
218 }
219
220 //============================================================================
221 /*! Function : Save
222  *  Purpose  : Save a Study to it's persistent reference
223  */
224 //============================================================================
225 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
226 {
227   SALOMEDS::Locker lock;
228
229   if(aStudy->_is_nil()) {
230     MESSAGE("Save error: Study is null");
231     return false;
232   }
233
234   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
235   return _impl->Save(aStudyImpl, _factory, theMultiFile);
236 }
237
238 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
239 {
240   SALOMEDS::Locker lock;
241
242   if(aStudy->_is_nil()) {
243     MESSAGE("SaveASCII error: Study is null");
244     return false;
245   }
246
247   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
248   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
249 }
250
251 //=============================================================================
252 /*! Function : SaveAs
253  *  Purpose  : Save a study to the persistent reference aUrl
254  */
255 //============================================================================
256 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
257 {
258   SALOMEDS::Locker lock;
259
260   if(aStudy->_is_nil()) {
261     MESSAGE("SaveASCII error: Study is null");
262     return false;
263   }
264
265   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
266   return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
267 }
268
269 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
270 {
271   SALOMEDS::Locker lock;
272
273   if(aStudy->_is_nil()) {
274     MESSAGE("SaveASCII error: Study is null");
275     return false;
276   }
277
278   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
279   return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
280 }
281
282 //============================================================================
283 /*! Function : GetOpenStudies
284  *  Purpose  : Get name list of open studies in the session
285  */
286 //============================================================================
287 SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
288 {
289   SALOMEDS::Locker lock;
290
291   std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
292   int aLength = anOpened.size();
293
294   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
295   _list_open_studies->length(aLength);
296
297   if(!aLength)
298     {
299       MESSAGE("No active study in this session");
300     }
301   else
302     {
303       for (unsigned int ind=0; ind < aLength; ind++)
304         {
305           _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
306           SCRUTE(_list_open_studies[ind]) ;
307         }
308     }
309   return _list_open_studies._retn();
310 }
311
312 //============================================================================
313 /*! Function : GetStudyByName
314  *  Purpose  : Get a study from its name
315  */
316 //============================================================================
317 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
318 {
319   SALOMEDS::Locker lock;
320
321   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
322
323   if (!aStudyImpl)
324   {
325     MESSAGE(_impl->GetErrorCode().c_str());
326     return SALOMEDS::Study::_nil();
327   }
328
329   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
330   return aStudy_servant->_this();
331 }
332
333 //============================================================================
334 /*! Function : GetStudyByID
335  *  Purpose  : Get a study from its ID
336  */
337 //============================================================================
338 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
339 {
340   SALOMEDS::Locker lock;
341
342   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
343
344   if (!aStudyImpl)
345   {
346     MESSAGE(_impl->GetErrorCode().c_str());
347     return SALOMEDS::Study::_nil();
348   }
349
350   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
351   return aStudy_servant->_this();
352 }
353
354
355 //============================================================================
356 /*! Function : CanCopy
357  *  Purpose  :
358  */
359 //============================================================================
360 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
361 {
362   SALOMEDS::Locker lock;
363
364   SALOMEDS::Study_var aStudy = theObject->GetStudy();
365   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
366   CORBA::String_var anID = theObject->GetID();
367   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
368
369   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
370   bool ret = _impl->CanCopy(anObject, aDriver);
371   delete aDriver;
372   return ret;
373 }
374
375 //============================================================================
376 /*! Function : Copy
377  *  Purpose  :
378  */
379 //============================================================================
380 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
381 {
382   SALOMEDS::Locker lock;
383
384   SALOMEDS::Study_var aStudy = theObject->GetStudy();
385   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
386   CORBA::String_var anID = theObject->GetID();
387   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
388
389   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
390   bool ret = _impl->Copy(anObject, aDriver);
391   delete aDriver;
392   return ret;
393 }
394
395 //============================================================================
396 /*! Function : CanPaste
397  *  Purpose  :
398  */
399 //============================================================================
400 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
401 {
402   SALOMEDS::Locker lock;
403
404   SALOMEDS::Study_var aStudy = theObject->GetStudy();
405   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
406   CORBA::String_var anID = theObject->GetID();
407   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
408
409   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
410   bool ret = _impl->CanPaste(anObject, aDriver);
411   delete aDriver;
412   return ret;
413 }
414
415 //============================================================================
416 /*! Function : Paste
417  *  Purpose  :
418  */
419 //============================================================================
420 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
421      throw(SALOMEDS::StudyBuilder::LockProtection)
422 {
423   SALOMEDS::Locker lock;
424
425   Unexpect aCatch(LockProtection);
426   SALOMEDS::Study_var aStudy = theObject->GetStudy();
427
428   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
429   CORBA::String_var anID = theObject->GetID();
430   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
431   SALOMEDSImpl_SObject aNewSO;
432
433   try {
434     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
435     aNewSO =  _impl->Paste(anObject, aDriver);
436     delete aDriver;
437   }
438   catch (...) {
439     throw SALOMEDS::StudyBuilder::LockProtection();
440   }
441
442   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
443   return so._retn();
444 }
445
446
447 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
448 {
449   SALOMEDS_Driver_i* driver = NULL;
450
451   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
452   if(!aSCO.IsNull()) {
453     std::string IOREngine = aSCO.GetIOR();
454     if(!IOREngine.empty()) {
455       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
456       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
457       driver = new SALOMEDS_Driver_i(Engine, orb);
458     }
459   }
460
461   return driver;
462 }
463
464 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
465   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
466   return PortableServer::POA::_nil();
467 }
468
469 CORBA::Long SALOMEDS_StudyManager_i::getPID()
470
471 #ifdef WIN32
472   return (CORBA::Long)_getpid();
473 #else
474   return (CORBA::Long)getpid();
475 #endif
476 }
477
478 void SALOMEDS_StudyManager_i::ShutdownWithExit()
479 {
480   exit( EXIT_SUCCESS );
481 }
482
483 //============================================================================
484 /*! Function : GetSimanStudy
485  *  Purpose  : Retruns the created or cashed SimanStudy
486  */
487 //============================================================================
488 SALOMEDS::SimanStudy_ptr SALOMEDS_StudyManager_i::GetSimanStudy()
489 {
490   SALOMEDS::Locker lock;
491
492   SALOMEDS_SimanStudy_i* aSiman_servant = SALOMEDS_SimanStudy_i::GetSimanServant(_orb);
493   return aSiman_servant->_this();
494 }
495
496 //===========================================================================
497 //   PRIVATE FUNCTIONS
498 //===========================================================================
499 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
500 {
501 #ifdef WIN32
502   long pid = (long)_getpid();
503 #else
504   long pid = (long)getpid();
505 #endif
506   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
507   return reinterpret_cast<CORBA::LongLong>(_impl);
508 }
509
510 //===========================================================================
511 namespace SALOMEDS
512 {
513   PortableServer::ServantBase_var
514   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
515   {
516     if(CORBA::is_nil(theObject))
517       return NULL;
518     try{
519       return thePOA->reference_to_servant(theObject);
520     }catch(...){
521       return NULL;
522     }
523   }
524
525 }
526
527 //===========================================================================