Salome HOME
Merge from V6_main 11/02/2013
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager_i.cxx
1 // Copyright (C) 2007-2012  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_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 {
114   SALOMEDS::Locker lock;
115
116   SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
117   if(!aStudyImpl) {
118     MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
119     return SALOMEDS::Study::_nil();
120   }
121
122   MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
123
124   SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
125   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
126   SALOMEDS::Study_var Study = Study_servant->_this();
127
128   // Register study in the naming service
129   // Path to acces the study
130   if(!_name_service->Change_Directory("/Study"))
131       MESSAGE( "Unable to access the study directory" )
132   else
133       _name_service->Register(Study, study_name);
134
135   // Assign the value of the IOR in the study->root
136   CORBA::String_var IORStudy = _orb->object_to_string(Study);
137
138   aStudyImpl->SetTransientReference((char*)IORStudy.in());
139
140   _mapOfPOA[Study->StudyId()] = _poa;
141
142   return Study._retn();
143 }
144
145 //============================================================================
146 /*! Function : Open
147  *  Purpose  : Open a Study from it's persistent reference
148  */
149 //============================================================================
150 SALOMEDS::Study_ptr  SALOMEDS_StudyManager_i::Open(const char* aUrl)
151      throw(SALOME::SALOME_Exception)
152 {
153   SALOMEDS::Locker lock;
154
155   Unexpect aCatch(SalomeException);
156   MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
157
158   SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
159
160   if ( !aStudyImpl )
161     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
162
163   MESSAGE("Open : Creating the CORBA servant holding it... ");
164
165   // Temporary aStudyUrl in place of study name
166   SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
167   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
168   SALOMEDS::Study_var Study = Study_servant->_this();
169
170   // Assign the value of the IOR in the study->root
171   CORBA::String_var IORStudy = _orb->object_to_string(Study);
172   aStudyImpl->SetTransientReference((char*)IORStudy.in());
173
174   _mapOfPOA[Study->StudyId()] = _poa;
175
176   // Register study in the naming service
177   // Path to acces the study
178   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
179   else _name_service->Register(Study, aStudyImpl->Name().c_str());
180
181   return Study._retn();
182 }
183
184
185
186 //============================================================================
187 /*! Function : Close
188  *  Purpose  : Close a study.
189  *             If the study hasn't been saved, ask the user to confirm the
190  *             close action without saving
191  */
192 //============================================================================
193 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
194 {
195   SALOMEDS::Locker lock;
196
197   if(aStudy->_is_nil()) return;
198
199   // Destroy study name in the naming service
200   if(_name_service->Change_Directory("/Study")){
201     CORBA::String_var aString(aStudy->Name());
202     _name_service->Destroy_Name(aString.in());
203   }
204
205   SALOMEDS::unlock();
206   aStudy->Close();
207   SALOMEDS::lock();
208
209   //remove study servant
210   PortableServer::POA_ptr poa=GetPOA(aStudy);
211   PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
212   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
213   poa->deactivate_object(anObjectId.in());
214   aservant->_remove_ref(); // decrement for the call to reference_to_servant
215   aservant->_remove_ref(); // to delete the object
216 }
217
218 //============================================================================
219 /*! Function : Save
220  *  Purpose  : Save a Study to it's persistent reference
221  */
222 //============================================================================
223 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
224 {
225   SALOMEDS::Locker lock;
226
227   if(aStudy->_is_nil()) {
228     MESSAGE("Save error: Study is null");
229     return false;
230   }
231
232   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
233   return _impl->Save(aStudyImpl, _factory, theMultiFile);
234 }
235
236 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
237 {
238   SALOMEDS::Locker lock;
239
240   if(aStudy->_is_nil()) {
241     MESSAGE("SaveASCII error: Study is null");
242     return false;
243   }
244
245   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
246   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
247 }
248
249 //=============================================================================
250 /*! Function : SaveAs
251  *  Purpose  : Save a study to the persistent reference aUrl
252  */
253 //============================================================================
254 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
255 {
256   SALOMEDS::Locker lock;
257
258   if(aStudy->_is_nil()) {
259     MESSAGE("SaveASCII error: Study is null");
260     return false;
261   }
262
263   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
264   return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
265 }
266
267 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
268 {
269   SALOMEDS::Locker lock;
270
271   if(aStudy->_is_nil()) {
272     MESSAGE("SaveASCII error: Study is null");
273     return false;
274   }
275
276   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
277   return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
278 }
279
280 //============================================================================
281 /*! Function : GetOpenStudies
282  *  Purpose  : Get name list of open studies in the session
283  */
284 //============================================================================
285 SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
286 {
287   SALOMEDS::Locker lock;
288
289   std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
290   int aLength = anOpened.size();
291
292   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
293   _list_open_studies->length(aLength);
294
295   if(!aLength)
296     {
297       MESSAGE("No active study in this session");
298     }
299   else
300     {
301       for (unsigned int ind=0; ind < aLength; ind++)
302         {
303           _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
304           SCRUTE(_list_open_studies[ind]) ;
305         }
306     }
307   return _list_open_studies._retn();
308 }
309
310 //============================================================================
311 /*! Function : GetStudyByName
312  *  Purpose  : Get a study from its name
313  */
314 //============================================================================
315 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
316 {
317   SALOMEDS::Locker lock;
318
319   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
320
321   if (!aStudyImpl)
322   {
323     MESSAGE(_impl->GetErrorCode().c_str());
324     return SALOMEDS::Study::_nil();
325   }
326
327   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
328   return aStudy_servant->_this();
329 }
330
331 //============================================================================
332 /*! Function : GetStudyByID
333  *  Purpose  : Get a study from its ID
334  */
335 //============================================================================
336 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
337 {
338   SALOMEDS::Locker lock;
339
340   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
341
342   if (!aStudyImpl)
343   {
344     MESSAGE(_impl->GetErrorCode().c_str());
345     return SALOMEDS::Study::_nil();
346   }
347
348   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
349   return aStudy_servant->_this();
350 }
351
352
353 //============================================================================
354 /*! Function : CanCopy
355  *  Purpose  :
356  */
357 //============================================================================
358 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
359 {
360   SALOMEDS::Locker lock;
361
362   SALOMEDS::Study_var aStudy = theObject->GetStudy();
363   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
364   CORBA::String_var anID = theObject->GetID();
365   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
366
367   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
368   bool ret = _impl->CanCopy(anObject, aDriver);
369   delete aDriver;
370   return ret;
371 }
372
373 //============================================================================
374 /*! Function : Copy
375  *  Purpose  :
376  */
377 //============================================================================
378 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
379 {
380   SALOMEDS::Locker lock;
381
382   SALOMEDS::Study_var aStudy = theObject->GetStudy();
383   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
384   CORBA::String_var anID = theObject->GetID();
385   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
386
387   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
388   bool ret = _impl->Copy(anObject, aDriver);
389   delete aDriver;
390   return ret;
391 }
392
393 //============================================================================
394 /*! Function : CanPaste
395  *  Purpose  :
396  */
397 //============================================================================
398 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
399 {
400   SALOMEDS::Locker lock;
401
402   SALOMEDS::Study_var aStudy = theObject->GetStudy();
403   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
404   CORBA::String_var anID = theObject->GetID();
405   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
406
407   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
408   bool ret = _impl->CanPaste(anObject, aDriver);
409   delete aDriver;
410   return ret;
411 }
412
413 //============================================================================
414 /*! Function : Paste
415  *  Purpose  :
416  */
417 //============================================================================
418 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
419      throw(SALOMEDS::StudyBuilder::LockProtection)
420 {
421   SALOMEDS::Locker lock;
422
423   Unexpect aCatch(LockProtection);
424   SALOMEDS::Study_var aStudy = theObject->GetStudy();
425
426   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
427   CORBA::String_var anID = theObject->GetID();
428   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
429   SALOMEDSImpl_SObject aNewSO;
430
431   try {
432     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
433     aNewSO =  _impl->Paste(anObject, aDriver);
434     delete aDriver;
435   }
436   catch (...) {
437     throw SALOMEDS::StudyBuilder::LockProtection();
438   }
439
440   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
441   return so._retn();
442 }
443
444
445 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
446 {
447   SALOMEDS_Driver_i* driver = NULL;
448
449   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
450   if(!aSCO.IsNull()) {
451     std::string IOREngine = aSCO.GetIOR();
452     if(!IOREngine.empty()) {
453       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
454       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
455       driver = new SALOMEDS_Driver_i(Engine, orb);
456     }
457   }
458
459   return driver;
460 }
461
462 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
463   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
464   return PortableServer::POA::_nil();
465 }
466
467 CORBA::Long SALOMEDS_StudyManager_i::getPID()
468
469 #ifdef WIN32
470   return (CORBA::Long)_getpid();
471 #else
472   return (CORBA::Long)getpid();
473 #endif
474 }
475
476 void SALOMEDS_StudyManager_i::ShutdownWithExit()
477 {
478   exit( EXIT_SUCCESS );
479 }
480
481 //===========================================================================
482 //   PRIVATE FUNCTIONS
483 //===========================================================================
484 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
485 {
486 #ifdef WIN32
487   long pid = (long)_getpid();
488 #else
489   long pid = (long)getpid();
490 #endif
491   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
492   return reinterpret_cast<CORBA::LongLong>(_impl);
493 }
494
495 //===========================================================================
496 namespace SALOMEDS
497 {
498   PortableServer::ServantBase_var
499   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
500   {
501     if(CORBA::is_nil(theObject))
502       return NULL;
503     try{
504       return thePOA->reference_to_servant(theObject);
505     }catch(...){
506       return NULL;
507     }
508   }
509
510 }
511
512 //===========================================================================