]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_StudyManager_i.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager_i.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDS_StudyManager_i.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
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"
31
32 #include "SALOMEDSImpl_Study.hxx"
33 #include "SALOMEDSImpl_SObject.hxx"
34 #include "SALOMEDSImpl_SComponent.hxx"
35 #include "SALOMEDSImpl_AttributeIOR.hxx"
36
37 #include "Utils_CorbaException.hxx"
38
39 #include <strstream>
40 #include <vector>
41 #include <map>
42 using namespace std;
43
44 #ifdef WIN32
45 #include <process.h>
46 #else
47 #include <sys/types.h>
48 #include <unistd.h>
49 #endif
50
51 #include "OpUtil.hxx"
52
53 #include "SALOME_GenericObj_i.hh"
54
55 #include "Utils_ExceptHandlers.hxx"
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(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 = new SALOMEDS_Study_i(aStudyImpl, _orb);
126   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(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;
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(string(aUrl));
159
160   MESSAGE("Open : Creating the CORBA servant holding it... ");
161
162   // Temporary aStudyUrl in place of study name
163   SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
164   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
165
166   // Assign the value of the IOR in the study->root
167   CORBA::String_var IORStudy = _orb->object_to_string(Study);
168   aStudyImpl->SetTransientReference((char*)IORStudy.in());
169
170   // Register study in the naming service
171   // Path to acces the study
172   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
173   else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
174
175   return Study;
176 }
177
178
179
180 //============================================================================
181 /*! Function : Close
182  *  Purpose  : Close a study.
183  *             If the study hasn't been saved, ask the user to confirm the
184  *             close action without saving
185  */
186 //============================================================================
187 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
188 {
189   SALOMEDS::Locker lock;
190
191   if(aStudy->_is_nil()) return;
192
193   // Destroy study name in the naming service
194   if(_name_service->Change_Directory("/Study")){
195     CORBA::String_var aString(aStudy->Name());
196     _name_service->Destroy_Name(aString.in());
197   }
198
199   SALOMEDS::unlock();
200   aStudy->Close();
201   SALOMEDS::lock();
202 }
203
204 //============================================================================
205 /*! Function : Save
206  *  Purpose  : Save a Study to it's persistent reference
207  */
208 //============================================================================
209 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
210 {
211   SALOMEDS::Locker lock;
212
213   if(aStudy->_is_nil()) {
214     MESSAGE("Save error: Study is null");
215     return false;
216   }
217
218   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
219   return _impl->Save(aStudyImpl, _factory, theMultiFile);
220 }
221
222 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
223 {
224   SALOMEDS::Locker lock;
225
226   if(aStudy->_is_nil()) {
227     MESSAGE("SaveASCII error: Study is null");
228     return false;
229   }
230
231   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
232   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
233 }
234
235 //=============================================================================
236 /*! Function : SaveAs
237  *  Purpose  : Save a study to the persistent reference aUrl
238  */
239 //============================================================================
240 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
241 {
242   SALOMEDS::Locker lock;
243
244   if(aStudy->_is_nil()) {
245     MESSAGE("SaveASCII error: Study is null");
246     return false;
247   }
248
249   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
250   return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
251 }
252
253 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
254 {
255   SALOMEDS::Locker lock;
256
257   if(aStudy->_is_nil()) {
258     MESSAGE("SaveASCII error: Study is null");
259     return false;
260   }
261
262   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
263   return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
264 }
265
266 //============================================================================
267 /*! Function : GetOpenStudies
268  *  Purpose  : Get name list of open studies in the session
269  */
270 //============================================================================
271 SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
272 {
273   SALOMEDS::Locker lock;
274
275   vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
276   int aLength = anOpened.size();
277
278   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
279   _list_open_studies->length(aLength);
280
281   if(!aLength)
282     {
283       MESSAGE("No active study in this session");
284     }
285   else
286     {
287       for (unsigned int ind=0; ind < aLength; ind++)
288         {
289           _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
290           SCRUTE(_list_open_studies[ind]) ;
291         }
292     }
293   return _list_open_studies._retn();
294 }
295
296 //============================================================================
297 /*! Function : GetStudyByName
298  *  Purpose  : Get a study from its name
299  */
300 //============================================================================
301 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
302 {
303   SALOMEDS::Locker lock;
304
305   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
306
307   if (!aStudyImpl)
308   {
309     MESSAGE(_impl->GetErrorCode().c_str());
310     return SALOMEDS::Study::_nil();
311   }
312
313   SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
314   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
315
316   return aStudy._retn();
317 }
318
319 //============================================================================
320 /*! Function : GetStudyByID
321  *  Purpose  : Get a study from its ID
322  */
323 //============================================================================
324 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
325 {
326   SALOMEDS::Locker lock;
327
328   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
329
330   if (!aStudyImpl)
331   {
332     MESSAGE(_impl->GetErrorCode().c_str());
333     return SALOMEDS::Study::_nil();
334   }
335
336   SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
337   CORBA::Object_var obj = aStudy_servant->_this();
338   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(obj);
339
340   return aStudy._retn();
341 }
342
343
344 //============================================================================
345 /*! Function : CanCopy
346  *  Purpose  :
347  */
348 //============================================================================
349 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
350 {
351   SALOMEDS::Locker lock;
352
353   SALOMEDS::Study_var aStudy = theObject->GetStudy();
354   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
355   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
356
357   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
358   bool ret = _impl->CanCopy(anObject, aDriver);
359   delete aDriver;
360   return ret;
361 }
362
363 //============================================================================
364 /*! Function : Copy
365  *  Purpose  :
366  */
367 //============================================================================
368 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
369 {
370   SALOMEDS::Locker lock;
371
372   SALOMEDS::Study_var aStudy = theObject->GetStudy();
373   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
374   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
375
376   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
377   bool ret = _impl->Copy(anObject, aDriver);
378   delete aDriver;
379   return ret;
380 }
381
382 //============================================================================
383 /*! Function : CanPaste
384  *  Purpose  :
385  */
386 //============================================================================
387 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
388 {
389   SALOMEDS::Locker lock;
390
391   SALOMEDS::Study_var aStudy = theObject->GetStudy();
392   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
393   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
394
395   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
396   bool ret = _impl->CanPaste(anObject, aDriver);
397   delete aDriver;
398   return ret;
399 }
400
401 //============================================================================
402 /*! Function : Paste
403  *  Purpose  :
404  */
405 //============================================================================
406 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
407      throw(SALOMEDS::StudyBuilder::LockProtection)
408 {
409   SALOMEDS::Locker lock;
410
411   Unexpect aCatch(LockProtection);
412   SALOMEDS::Study_var aStudy = theObject->GetStudy();
413
414   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
415   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
416   SALOMEDSImpl_SObject aNewSO;
417
418   try {
419     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
420     aNewSO =  _impl->Paste(anObject, aDriver);
421     delete aDriver;
422   }
423   catch (...) {
424     throw SALOMEDS::StudyBuilder::LockProtection();
425   }
426
427   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
428   return so._retn();
429 }
430
431
432 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
433 {
434   SALOMEDS_Driver_i* driver = NULL;
435
436   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
437   if(!aSCO.IsNull()) {
438     string IOREngine = aSCO.GetIOR();
439     if(!IOREngine.empty()) {
440       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
441       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
442       driver = new SALOMEDS_Driver_i(Engine, orb);
443     }
444   }
445
446   return driver;
447 }
448
449 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
450   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
451   return PortableServer::POA::_nil();
452 }
453
454 CORBA::Long SALOMEDS_StudyManager_i::getPID()
455
456 #ifdef WIN32
457   return (CORBA::Long)_getpid();
458 #else
459   return (CORBA::Long)getpid();
460 #endif
461 }
462
463 void SALOMEDS_StudyManager_i::ShutdownWithExit()
464 {
465   exit( EXIT_SUCCESS );
466 }
467
468 //===========================================================================
469 //   PRIVATE FUNCTIONS
470 //===========================================================================
471 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
472 {
473 #ifdef WIN32
474   long pid = (long)_getpid();
475 #else
476   long pid = (long)getpid();
477 #endif
478   isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
479   return ((CORBA::LongLong)(void*)_impl);
480 }
481
482 //===========================================================================
483 namespace SALOMEDS
484 {
485   PortableServer::ServantBase_var
486   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
487   {
488     if(CORBA::is_nil(theObject))
489       return NULL;
490     try{
491       return thePOA->reference_to_servant(theObject);
492     }catch(...){
493       return NULL;
494     }
495   }
496
497 }
498
499 //===========================================================================