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