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