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