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