Salome HOME
ENV: Windows porting.
[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 = _impl->GetStudyByName(TCollection_AsciiString((char*)aStudyName));
289
290   if(aStudyImpl.IsNull())
291     {
292       MESSAGE("No active study in this session");
293       ASSERT(false); // Stop here...
294     }
295   
296   SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);  
297   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this()); 
298   
299   return Study;
300 }
301
302 //============================================================================
303 /*! Function : GetStudyByID
304  *  Purpose  : Get a study from its ID
305  */
306 //============================================================================
307 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID) 
308 {
309   SALOMEDS::Locker lock;
310   
311   Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudyID);
312
313   if(aStudyImpl.IsNull())
314     {
315       MESSAGE("No active study in this session");
316       ASSERT(false); // Stop here...
317     }
318   
319   SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);  
320   SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this()); 
321   
322   return Study;
323 }
324
325
326 //============================================================================
327 /*! Function : CanCopy
328  *  Purpose  : 
329  */
330 //============================================================================
331 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject) 
332 {
333   SALOMEDS::Locker lock;
334
335   SALOMEDS::Study_var aStudy = theObject->GetStudy();
336   Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
337   Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
338
339   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
340   bool ret = _impl->CanCopy(anObject, aDriver);
341   delete aDriver;
342   return ret;
343 }
344
345 //============================================================================
346 /*! Function : Copy
347  *  Purpose  :
348  */
349 //============================================================================
350 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject) 
351 {
352   SALOMEDS::Locker lock;
353
354   SALOMEDS::Study_var aStudy = theObject->GetStudy();
355   Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
356   Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
357
358   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
359   bool ret = _impl->Copy(anObject, aDriver);
360   delete aDriver;
361   return ret;
362 }
363
364 //============================================================================
365 /*! Function : CanPaste
366  *  Purpose  :
367  */
368 //============================================================================
369 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject) 
370 {
371   SALOMEDS::Locker lock;
372
373   SALOMEDS::Study_var aStudy = theObject->GetStudy();
374   Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
375   Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
376
377   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
378   bool ret = _impl->CanPaste(anObject, aDriver);
379   delete aDriver;
380   return ret;
381 }
382
383 //============================================================================
384 /*! Function : Paste
385  *  Purpose  :
386  */
387 //============================================================================
388 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
389      throw(SALOMEDS::StudyBuilder::LockProtection)
390 {
391   SALOMEDS::Locker lock;
392
393   Unexpect aCatch(LockProtection);
394   SALOMEDS::Study_var aStudy = theObject->GetStudy();
395
396   Handle(SALOMEDSImpl_Study) aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
397   Handle(SALOMEDSImpl_SObject) anObject = aStudyImpl->GetSObject((char*)theObject->GetID());
398   Handle(SALOMEDSImpl_SObject) aNewSO;
399   
400   try {
401     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
402     aNewSO =  _impl->Paste(anObject, aDriver);
403     delete aDriver;
404   }
405   catch (...) {
406     throw SALOMEDS::StudyBuilder::LockProtection();
407   }
408
409   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
410   return so._retn();
411 }
412
413
414 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
415 {
416   SALOMEDS_Driver_i* driver = NULL;
417   
418   Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
419   if(!aSCO.IsNull()) {
420     TCollection_AsciiString IOREngine = aSCO->GetIOR();
421     if(!IOREngine.IsEmpty()) {
422       CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
423       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
424       driver = new SALOMEDS_Driver_i(Engine, orb);
425     }
426   }  
427
428   return driver;
429 }
430
431 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
432   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
433   return PortableServer::POA::_nil();
434 }
435
436 //===========================================================================
437 //   PRIVATE FUNCTIONS
438 //===========================================================================
439 long SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
440 {
441 #ifdef WIN32
442   long pid = (long)_getpid();
443 #else
444   long pid = (long)getpid();
445 #endif  
446   isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
447   SALOMEDSImpl_StudyManager* aManager = _impl.operator->();
448   return ((long)aManager);
449 }
450
451 //===========================================================================
452 namespace SALOMEDS
453 {
454 /*
455   CORBA::Object_var
456   GetObject(const TDF_Label& theLabel, CORBA::ORB_ptr theORB)
457   {
458     try {
459       Handle(SALOMEDS_IORAttribute) anAttr;
460       if(theLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(),anAttr))
461         return theORB->string_to_object(TCollection_AsciiString(anAttr->Get()).ToCString());
462     }catch(...){
463     }
464     return CORBA::Object::_nil();
465   }
466 */
467
468   PortableServer::ServantBase_var
469   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
470   {
471     if(CORBA::is_nil(theObject))
472       return NULL;
473     try{
474       return thePOA->reference_to_servant(theObject);
475     }catch(...){
476       return NULL;
477     }
478   }
479
480 }
481
482 //===========================================================================