Salome HOME
Fix compilation with gcc 4.4
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyBuilder.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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_StudyBuilder.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "utilities.h"
28
29 #include "SALOMEDS_StudyBuilder.hxx"
30
31 #include "SALOMEDS.hxx"
32 #include "SALOMEDS_SObject.hxx"
33 #include "SALOMEDS_SComponent.hxx"
34 #include "SALOMEDS_GenericAttribute.hxx"
35 #include "SALOMEDS_StudyBuilder_i.hxx"
36
37 #include "SALOMEDS_Driver_i.hxx"
38
39 #include "SALOMEDSImpl_SObject.hxx"
40 #include "SALOMEDSImpl_SComponent.hxx"
41 #include "SALOMEDSImpl_GenericAttribute.hxx"
42
43 #include <string>
44 #include <stdexcept>
45
46 #include "DF_Attribute.hxx"
47
48 #include "Utils_CorbaException.hxx"
49 #include "Utils_ORB_INIT.hxx" 
50 #include "Utils_SINGLETON.hxx" 
51
52 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDSImpl_StudyBuilder* theBuilder)
53 {
54   _isLocal = true;
55   _local_impl = theBuilder;
56   _corba_impl = SALOMEDS::StudyBuilder::_nil();
57
58   init_orb();
59 }
60
61 SALOMEDS_StudyBuilder::SALOMEDS_StudyBuilder(SALOMEDS::StudyBuilder_ptr theBuilder)
62 {
63   _isLocal = false;
64   _local_impl = NULL;
65   _corba_impl = SALOMEDS::StudyBuilder::_duplicate(theBuilder);
66
67   init_orb();
68 }
69
70 SALOMEDS_StudyBuilder::~SALOMEDS_StudyBuilder() 
71 {
72 }
73
74 _PTR(SComponent) SALOMEDS_StudyBuilder::NewComponent(const std::string& ComponentDataType)
75 {
76   SALOMEDSClient_SComponent* aSCO = NULL;
77
78   if (_isLocal) {
79     CheckLocked();
80     SALOMEDS::Locker lock;
81
82     SALOMEDSImpl_SComponent aSCO_impl =_local_impl->NewComponent(ComponentDataType);
83     if(!aSCO_impl) return _PTR(SComponent)(aSCO);
84     aSCO = new SALOMEDS_SComponent(aSCO_impl);
85   }
86   else {
87     SALOMEDS::SComponent_var aSCO_impl = _corba_impl->NewComponent((char*)ComponentDataType.c_str());
88     if(CORBA::is_nil(aSCO_impl)) return _PTR(SComponent)(aSCO);
89     aSCO = new SALOMEDS_SComponent(aSCO_impl);
90   }
91
92   return _PTR(SComponent)(aSCO);
93 }
94
95 void SALOMEDS_StudyBuilder::DefineComponentInstance (const _PTR(SComponent)& theSCO, 
96                                                      const std::string& ComponentIOR)
97 {
98   if(!theSCO) return;
99
100   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
101   if (_isLocal) {
102     CheckLocked();
103     SALOMEDS::Locker lock;
104
105     _local_impl->DefineComponentInstance(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())),
106                                          ComponentIOR);
107   }
108   else {
109     CORBA::Object_var obj = _orb->string_to_object(ComponentIOR.c_str());
110     _corba_impl->DefineComponentInstance(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), obj);
111   }
112 }
113
114 void SALOMEDS_StudyBuilder::RemoveComponent(const _PTR(SComponent)& theSCO)
115 {
116   if(!theSCO) return;
117   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
118   if (_isLocal) {
119     CheckLocked();
120     SALOMEDS::Locker lock;
121
122     _local_impl->RemoveComponent(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
123   }
124   else _corba_impl->RemoveComponent(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
125 }
126
127 _PTR(SObject) SALOMEDS_StudyBuilder::NewObject(const _PTR(SObject)& theFatherObject)
128 {
129   CheckLocked();
130
131   SALOMEDSClient_SObject* aSO = NULL;
132   SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
133   if (father == NULL) return _PTR(SObject)(aSO);
134   if (_isLocal) {
135     SALOMEDS::Locker lock;
136
137     SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObject(*(father->GetLocalImpl()));
138     if(!aSO_impl) return _PTR(SObject)(aSO);
139     aSO = new SALOMEDS_SObject(aSO_impl);
140   }
141   else {
142     SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObject(father->GetCORBAImpl());
143     if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
144     aSO = new SALOMEDS_SObject(aSO_impl);
145   }
146
147   return _PTR(SObject)(aSO);
148 }
149
150 _PTR(SObject) SALOMEDS_StudyBuilder::NewObjectToTag(const _PTR(SObject)& theFatherObject, int theTag)
151 {  
152   CheckLocked();
153
154   SALOMEDSClient_SObject* aSO = NULL;
155   SALOMEDS_SObject* father = dynamic_cast< SALOMEDS_SObject*>(theFatherObject.get());
156   if (father == NULL) return _PTR(SObject)(aSO);
157   if (_isLocal) {
158     SALOMEDS::Locker lock;
159
160     SALOMEDSImpl_SObject aSO_impl = _local_impl->NewObjectToTag(*(father->GetLocalImpl()), theTag);
161     if(aSO_impl.IsNull()) return _PTR(SObject)(aSO);
162     aSO = new SALOMEDS_SObject(aSO_impl);
163   }
164   else {
165     SALOMEDS::SObject_var aSO_impl = _corba_impl->NewObjectToTag(father->GetCORBAImpl(), theTag);
166     if(CORBA::is_nil(aSO_impl)) return _PTR(SObject)(aSO);
167     aSO = new SALOMEDS_SObject(aSO_impl);
168   }
169
170   return _PTR(SObject)(aSO);
171 }
172
173 void SALOMEDS_StudyBuilder::AddDirectory(const std::string& thePath)
174 {
175   if (_isLocal) {
176     CheckLocked();
177     SALOMEDS::Locker lock;
178
179     _local_impl->AddDirectory((char*)thePath.c_str());
180     if (_local_impl->IsError()) {
181       std::string anErrorCode = _local_impl->GetErrorCode();
182       if (anErrorCode == "StudyNameAlreadyUsed")  throw SALOMEDS::Study::StudyNameAlreadyUsed(); 
183       if (anErrorCode == "StudyInvalidDirectory") throw SALOMEDS::Study::StudyInvalidDirectory(); 
184       if (anErrorCode == "StudyInvalidComponent") throw SALOMEDS::Study::StudyInvalidComponent();  
185     }
186   }
187   else _corba_impl->AddDirectory((char*)thePath.c_str());
188 }
189
190 void SALOMEDS_StudyBuilder::LoadWith(const _PTR(SComponent)& theSCO, const std::string& theIOR)
191 {
192   if(!theSCO) return;
193
194   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
195   CORBA::Object_var obj = _orb->string_to_object(theIOR.c_str());
196   Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
197   SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(obj);
198   
199   if (_isLocal) {
200     SALOMEDS::Locker lock;
201
202     SALOMEDS_Driver_i* drv = new SALOMEDS_Driver_i(anEngine, _orb);    
203     SALOMEDSImpl_SComponent aSCO_impl = *(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl()));
204     bool isDone = _local_impl->LoadWith(aSCO_impl, drv);
205     delete drv;
206     if(!isDone && _local_impl->IsError()) 
207       THROW_SALOME_CORBA_EXCEPTION(_local_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
208   }
209   else {
210     _corba_impl->LoadWith(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()), aDriver);
211   }
212 }
213
214 void SALOMEDS_StudyBuilder::Load(const _PTR(SObject)& theSCO)
215 {
216   SALOMEDS_SComponent* aSCO = dynamic_cast<SALOMEDS_SComponent*>(theSCO.get());
217   if (_isLocal) _local_impl->Load(*(dynamic_cast<SALOMEDSImpl_SComponent*>(aSCO->GetLocalImpl())));
218   else _corba_impl->Load(SALOMEDS::SComponent::_narrow(aSCO->GetCORBAImpl()));
219 }
220
221 void SALOMEDS_StudyBuilder::RemoveObject(const _PTR(SObject)& theSO)
222 {
223   if(!theSO) return;
224
225   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
226   if (_isLocal) {
227     CheckLocked();
228     SALOMEDS::Locker lock;
229
230     _local_impl->RemoveObject(*(aSO->GetLocalImpl()));
231   }
232   else _corba_impl->RemoveObject(aSO->GetCORBAImpl());
233 }
234
235 void SALOMEDS_StudyBuilder::RemoveObjectWithChildren(const _PTR(SObject)& theSO)
236 {
237   if(!theSO) return;
238
239   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
240   if (_isLocal) {
241     CheckLocked();
242     SALOMEDS::Locker lock;
243
244     _local_impl->RemoveObjectWithChildren(*(aSO->GetLocalImpl()));
245   }
246   else _corba_impl->RemoveObjectWithChildren(aSO->GetCORBAImpl());
247 }
248
249 _PTR(GenericAttribute) SALOMEDS_StudyBuilder::FindOrCreateAttribute(const _PTR(SObject)& theSO, 
250                                                                     const std::string& aTypeOfAttribute)
251 {  
252   SALOMEDSClient_GenericAttribute* anAttr = NULL;
253   if(!theSO) return _PTR(GenericAttribute)(anAttr);
254   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
255   if (_isLocal) {
256     SALOMEDS::Locker lock;
257
258     SALOMEDSImpl_GenericAttribute* aGA;
259     try {
260       aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>
261         (_local_impl->FindOrCreateAttribute(*(aSO->GetLocalImpl()), aTypeOfAttribute));
262     }
263     catch (...) {
264       throw SALOMEDS::StudyBuilder::LockProtection();
265     }
266     anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
267   }
268   else {
269     SALOMEDS::GenericAttribute_var aGA =
270       _corba_impl->FindOrCreateAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
271     anAttr = SALOMEDS_GenericAttribute::CreateAttribute(aGA);
272   }
273
274   return _PTR(GenericAttribute)(anAttr);
275 }
276
277 bool SALOMEDS_StudyBuilder::FindAttribute(const _PTR(SObject)& theSO, 
278                                           _PTR(GenericAttribute)& anAttribute, 
279                                           const std::string& aTypeOfAttribute)
280 {
281   bool ret;
282
283   if(!theSO) return false;
284
285   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
286   if (_isLocal) {
287     SALOMEDS::Locker lock;
288
289     DF_Attribute* anAttr = NULL;
290     ret = _local_impl->FindAttribute(*(aSO->GetLocalImpl()), anAttr, aTypeOfAttribute);
291     if(ret) {
292       SALOMEDSImpl_GenericAttribute* aGA = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(anAttr);
293       anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
294     }
295   }
296   else {
297     SALOMEDS::GenericAttribute_var aGA;
298     ret = _corba_impl->FindAttribute(aSO->GetCORBAImpl(), aGA.out(), (char*)aTypeOfAttribute.c_str()); 
299     if(ret) anAttribute = _PTR(GenericAttribute)(SALOMEDS_GenericAttribute::CreateAttribute(aGA));
300   }
301
302   return ret;
303 }
304
305 void SALOMEDS_StudyBuilder::RemoveAttribute(const _PTR(SObject)& theSO, const std::string& aTypeOfAttribute)
306 {
307   if(!theSO) return;
308
309   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
310   if (_isLocal) {
311     CheckLocked();
312     SALOMEDS::Locker lock;
313
314     _local_impl->RemoveAttribute(*(aSO->GetLocalImpl()), (char*)aTypeOfAttribute.c_str());
315   }
316   else _corba_impl->RemoveAttribute(aSO->GetCORBAImpl(), (char*)aTypeOfAttribute.c_str());
317 }
318
319 void SALOMEDS_StudyBuilder::Addreference(const _PTR(SObject)& me, const _PTR(SObject)& thereferencedObject)
320 {
321   if(!me || !thereferencedObject) {
322     throw DFexception("Invalid arguments");
323   }
324
325   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
326   SALOMEDS_SObject* aRefSO = dynamic_cast<SALOMEDS_SObject*>(thereferencedObject.get());
327   if (_isLocal) {
328     CheckLocked();
329     SALOMEDS::Locker lock;
330
331     _local_impl->Addreference(*(aSO->GetLocalImpl()), *(aRefSO->GetLocalImpl()));
332   }
333   else _corba_impl->Addreference(aSO->GetCORBAImpl(), aRefSO->GetCORBAImpl());
334 }
335
336 void SALOMEDS_StudyBuilder::RemoveReference(const _PTR(SObject)& me)
337 {
338   if(!me) return;
339   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(me.get());
340   if (_isLocal) {
341     CheckLocked();
342     SALOMEDS::Locker lock;
343
344     _local_impl->RemoveReference(*(aSO->GetLocalImpl()));
345   }
346   else _corba_impl->RemoveReference(aSO->GetCORBAImpl());
347 }
348
349 void SALOMEDS_StudyBuilder::SetGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
350 {
351   if(!theSO) return;
352
353   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
354   if (_isLocal) {
355     CheckLocked();
356     SALOMEDS::Locker lock;
357
358     _local_impl->SetGUID(*(aSO->GetLocalImpl()), theGUID);
359   }
360   else _corba_impl->SetGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
361 }
362  
363 bool SALOMEDS_StudyBuilder::IsGUID(const _PTR(SObject)& theSO, const std::string& theGUID)
364 {
365   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
366   bool ret;
367   if (_isLocal) {
368     SALOMEDS::Locker lock;
369
370     ret = _local_impl->IsGUID(*(aSO->GetLocalImpl()), (char*)theGUID.c_str());
371   }
372   else ret = _corba_impl->IsGUID(aSO->GetCORBAImpl(), (char*)theGUID.c_str());
373
374   return ret;
375 }
376
377 void SALOMEDS_StudyBuilder::NewCommand()
378 {
379   if (_isLocal) {
380     SALOMEDS::Locker lock;
381     _local_impl->NewCommand();
382   }
383   else _corba_impl->NewCommand();
384 }
385  
386 void SALOMEDS_StudyBuilder::CommitCommand()
387 {
388   if (_isLocal) {
389     SALOMEDS::Locker lock;
390     try {
391       _local_impl->CommitCommand();
392     }
393     catch(...) {
394       throw SALOMEDS::StudyBuilder::LockProtection();
395     }
396   }
397   else _corba_impl->CommitCommand();
398 }
399
400 bool SALOMEDS_StudyBuilder::HasOpenCommand()
401 {
402   bool ret;
403   if (_isLocal) {
404     SALOMEDS::Locker lock;
405     ret = _local_impl->HasOpenCommand();
406   }
407   else ret = _corba_impl->HasOpenCommand();
408   return ret;
409 }
410
411 void SALOMEDS_StudyBuilder::AbortCommand()
412 {
413   if (_isLocal) {
414     SALOMEDS::Locker lock;
415     _local_impl->AbortCommand();
416   }
417   else _corba_impl->AbortCommand();
418 }
419
420 void SALOMEDS_StudyBuilder::Undo()
421 {
422   if (_isLocal) {
423     SALOMEDS::Locker lock;
424     try {
425       _local_impl->Undo();
426     }
427     catch(...) {
428       throw SALOMEDS::StudyBuilder::LockProtection();
429     }
430   }
431   else _corba_impl->Undo();
432 }
433
434 void SALOMEDS_StudyBuilder::Redo()
435 {
436   if (_isLocal) {
437     SALOMEDS::Locker lock;
438     try {
439       _local_impl->Redo();
440     }
441     catch(...) {
442       throw SALOMEDS::StudyBuilder::LockProtection();
443     }
444   }
445   else _corba_impl->Redo(); 
446 }
447
448 bool SALOMEDS_StudyBuilder::GetAvailableUndos()
449 {
450   bool ret;
451   if (_isLocal) {
452     SALOMEDS::Locker lock;
453     ret = _local_impl->GetAvailableUndos();
454   }
455   else ret = _corba_impl->GetAvailableUndos();
456   return ret;
457 }
458
459 bool SALOMEDS_StudyBuilder::GetAvailableRedos()
460 {
461   bool ret;
462   if (_isLocal) {
463     SALOMEDS::Locker lock;
464     ret = _local_impl->GetAvailableRedos();
465   }
466   else ret = _corba_impl->GetAvailableRedos();
467   return ret; 
468 }
469
470 int SALOMEDS_StudyBuilder::UndoLimit()
471 {
472   int aLimit;
473   if (_isLocal) {
474     SALOMEDS::Locker lock;
475     aLimit = _local_impl->UndoLimit();
476   }
477   else aLimit = _corba_impl->UndoLimit();
478   return aLimit;
479 }
480  
481 void SALOMEDS_StudyBuilder::UndoLimit(int theLimit)
482 {
483   if (_isLocal) {
484     CheckLocked();
485     SALOMEDS::Locker lock;
486
487     _local_impl->UndoLimit(theLimit);
488   }
489   else _corba_impl->UndoLimit(theLimit);
490 }
491  
492 void SALOMEDS_StudyBuilder::CheckLocked()
493 {
494   //There is only local part as CORBA part throws the correct exeception
495   if (_isLocal) {
496     SALOMEDS::Locker lock;
497     try {
498       _local_impl->CheckLocked();
499     }
500     catch(...) {
501       throw SALOMEDS::StudyBuilder::LockProtection();
502     }
503   }
504 }
505
506 void SALOMEDS_StudyBuilder::SetName(const _PTR(SObject)& theSO, const std::string& theValue)
507 {
508   if(!theSO) return;
509
510   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
511   if (_isLocal) {
512     CheckLocked();
513     SALOMEDS::Locker lock;
514
515     _local_impl->SetName(*(aSO->GetLocalImpl()), theValue);
516   }
517   else _corba_impl->SetName(aSO->GetCORBAImpl(), (char*)theValue.c_str());
518 }
519
520 void SALOMEDS_StudyBuilder::SetComment(const _PTR(SObject)& theSO, const std::string& theValue)
521 {
522   if(!theSO) return;
523
524   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
525   if (_isLocal) {
526     CheckLocked();
527     SALOMEDS::Locker lock;
528
529     _local_impl->SetComment(*(aSO->GetLocalImpl()), theValue);
530   }
531   else _corba_impl->SetComment(aSO->GetCORBAImpl(), (char*)theValue.c_str());
532 }
533
534 void SALOMEDS_StudyBuilder::SetIOR(const _PTR(SObject)& theSO, const std::string& theValue)
535 {
536   if(!theSO) return;
537
538   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
539   if (_isLocal) {
540     CheckLocked();
541     SALOMEDS::Locker lock;
542
543     _local_impl->SetIOR(*(aSO->GetLocalImpl()), theValue);
544   }
545   else _corba_impl->SetIOR(aSO->GetCORBAImpl(), (char*)theValue.c_str());
546 }
547
548 SALOMEDS::StudyBuilder_ptr SALOMEDS_StudyBuilder::GetBuilder()
549 {
550   if(_isLocal) {
551     if(!CORBA::is_nil(_corba_impl)) return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
552     SALOMEDS_StudyBuilder_i* servant = new SALOMEDS_StudyBuilder_i(_local_impl, _orb);
553     SALOMEDS::StudyBuilder_var aBuilder = servant->StudyBuilder::_this();
554     _corba_impl = SALOMEDS::StudyBuilder::_duplicate(aBuilder);
555     return aBuilder._retn();
556   }
557   else {
558     return SALOMEDS::StudyBuilder::_duplicate(_corba_impl);
559   }
560   return SALOMEDS::StudyBuilder::_nil();
561 }
562
563 void SALOMEDS_StudyBuilder::init_orb()
564 {
565   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
566   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()); 
567   _orb = init(0 , 0 );     
568 }