Salome HOME
CCAR: remove some memory leaks in non local SALOMEDS
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyBuilder_i.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  File   : SALOMEDS_StudyBuilder_i.cxx
23 //  Author : Seregy RUIN
24 //  Module : SALOME
25 //
26 #include "utilities.h"
27 #include "SALOMEDS_StudyBuilder_i.hxx"
28 #include "SALOMEDS_Study_i.hxx"
29 #include "SALOMEDS_SObject_i.hxx"
30 #include "SALOMEDS_SComponent_i.hxx"
31 #include "SALOMEDS_GenericAttribute_i.hxx"
32 #include "SALOMEDS_Driver_i.hxx"
33 #include "SALOMEDS.hxx"
34
35 #include "SALOMEDSImpl_Study.hxx"
36 #include "SALOMEDSImpl_SObject.hxx"
37 #include "SALOMEDSImpl_SComponent.hxx"
38
39 #include "Utils_CorbaException.hxx"
40 #include "Utils_ExceptHandlers.hxx"
41
42 #include <DF_Attribute.hxx>
43 #include <DF_Document.hxx>
44 #include <stdlib.h> 
45
46 using namespace std;
47
48 UNEXPECT_CATCH(SBSalomeException, SALOME::SALOME_Exception);
49 UNEXPECT_CATCH(SBLockProtection, SALOMEDS::StudyBuilder::LockProtection);
50
51 //============================================================================
52 /*! Function : constructor
53  *  Purpose  :
54  */
55 //============================================================================
56 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(SALOMEDSImpl_StudyBuilder* theImpl, 
57                                                  CORBA::ORB_ptr orb) 
58 {
59   _orb = CORBA::ORB::_duplicate(orb);
60   _impl = theImpl;
61 }
62
63 //============================================================================
64 /*! Function : destructor
65  *  Purpose  :
66  */
67 //============================================================================
68 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
69 {}
70
71 //============================================================================
72 /*! Function : NewComponent
73  *  Purpose  : Create a new component (Scomponent)
74  */
75 //============================================================================
76 SALOMEDS::SComponent_ptr SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
77 {
78   SALOMEDS::Locker lock;
79   CheckLocked();
80   //char* aDataType = CORBA::string_dup(DataType);
81   SALOMEDSImpl_SComponent aSCO = _impl->NewComponent(string(DataType));
82   //CORBA::free_string(aDataType);
83   if(aSCO.IsNull()) return SALOMEDS::SComponent::_nil();
84
85   SALOMEDS::SComponent_var sco = SALOMEDS_SComponent_i::New (aSCO,_orb);
86   return sco._retn();
87 }
88
89 //============================================================================
90 /*! Function : DefineComponentInstance
91  *  Purpose  : Add IOR attribute of a Scomponent
92  */
93 //============================================================================
94 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
95                                                       CORBA::Object_ptr IOR)
96 {
97   SALOMEDS::Locker lock;
98   CheckLocked();
99   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(aComponent->GetID());
100
101   CORBA::String_var iorstr = _orb->object_to_string(IOR);
102   _impl->DefineComponentInstance(aSCO, (char*)iorstr.in());
103 }
104
105 //============================================================================
106 /*! Function : RemoveComponent
107  *  Purpose  : Delete a Scomponent
108  */
109 //============================================================================
110 void SALOMEDS_StudyBuilder_i::RemoveComponent(SALOMEDS::SComponent_ptr aComponent)
111 {
112   SALOMEDS::Locker lock;
113   CheckLocked();
114   ASSERT(!CORBA::is_nil(aComponent));
115   CORBA::String_var cid=aComponent->GetID();
116   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(cid.in());
117   _impl->RemoveComponent(aSCO);
118 }
119
120 //============================================================================
121 /*! Function : NewObject
122  *  Purpose  : Create a new SObject
123  */
124 //============================================================================
125 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
126 {
127   SALOMEDS::Locker lock;
128   CheckLocked();
129   
130   SALOMEDSImpl_SObject aFO, aSO;
131   aFO = _impl->GetOwner()->GetSObject(theFatherObject->GetID());
132   aSO = _impl->NewObject(aFO);
133   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
134   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO,_orb);
135
136   return so._retn();
137 }
138
139 //============================================================================
140 /*! Function : NewObjectToTag
141  *  Purpose  :
142  */
143 //============================================================================
144 SALOMEDS::SObject_ptr SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
145                                                               CORBA::Long atag)
146 {
147   SALOMEDS::Locker lock;
148   CheckLocked();
149   SALOMEDSImpl_SObject aFO, aSO;
150   aFO = _impl->GetOwner()->GetSObject(theFatherObject->GetID());
151   aSO = _impl->NewObjectToTag(aFO, atag);
152   if(aSO.IsNull()) return SALOMEDS::SObject::_nil();
153   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aSO, _orb);
154   return so._retn();
155 }
156
157 //============================================================================
158 /*! Function : RemoveObject
159  *  Purpose  :
160  */
161 //============================================================================
162 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
163 {
164   SALOMEDS::Locker lock;
165   CheckLocked();
166   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
167   _impl->RemoveObject(aSO);
168 }
169
170 //============================================================================
171 /*! Function : RemoveObjectWithChildren
172  *  Purpose  :
173  */
174 //============================================================================
175 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
176 {
177   SALOMEDS::Locker lock;
178   CheckLocked();
179   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
180   _impl->RemoveObjectWithChildren(aSO);
181 }
182
183 //============================================================================
184 /*! Function : LoadWith
185  *  Purpose  : 
186  */
187 //============================================================================
188 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO, 
189                                        SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
190 {
191   SALOMEDS::Locker lock;
192   Unexpect aCatch(SBSalomeException);
193
194   SALOMEDSImpl_SComponent aSCO = _impl->GetOwner()->GetSComponent(anSCO->GetID());
195   SALOMEDS_Driver_i* driver = new SALOMEDS_Driver_i(aDriver, _orb);
196   bool isDone = _impl->LoadWith(aSCO, driver); 
197   delete driver;
198
199   if(!isDone && _impl->IsError()) {
200     THROW_SALOME_CORBA_EXCEPTION(_impl->GetErrorCode().c_str(),SALOME::BAD_PARAM);
201   }
202 }
203
204
205 //============================================================================
206 /*! Function : Load
207  *  Purpose  : 
208  */
209 //============================================================================
210 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
211 {
212   MESSAGE ( "This function is not yet implemented");
213 }
214
215 //============================================================================
216 /*! Function : FindOrCreateAttribute
217  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
218  *  existing one
219  */
220 //============================================================================
221 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject, 
222                                                                               const char* aTypeOfAttribute)
223 {
224   SALOMEDS::Locker lock;
225   CORBA::String_var anID = anObject->GetID();
226   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anID.inout());
227   DF_Attribute* anAttr;
228   try {
229      anAttr = _impl->FindOrCreateAttribute(aSO, string(aTypeOfAttribute));
230   }
231   catch (...) {
232     throw SALOMEDS::StudyBuilder::LockProtection();
233   }
234
235   SALOMEDS::GenericAttribute_var anAttribute;
236   
237   if(anAttr)     
238      anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
239
240   return anAttribute._retn();
241 }
242
243 //============================================================================
244 /*! Function : FindAttribute
245  *  Purpose  : Find attribute of given type assigned SObject, returns true if it is found
246  */
247 //============================================================================
248
249 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject, 
250                                                       SALOMEDS::GenericAttribute_out anAttribute, 
251                                                       const char* aTypeOfAttribute)
252 {
253   SALOMEDS::Locker lock;
254   ASSERT(!CORBA::is_nil(anObject));
255   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
256   DF_Attribute* anAttr;
257
258   if(!_impl->FindAttribute(aSO, anAttr, string(aTypeOfAttribute))) return false;
259     
260   anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(anAttr, _orb));
261   return true;  
262 }
263
264 //============================================================================
265 /*! Function : RemoveAttribute
266  *  Purpose  : Remove attribute of given type assigned SObject
267  */
268 //============================================================================
269
270 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject, 
271                                               const char* aTypeOfAttribute)
272 {
273   SALOMEDS::Locker lock;
274   CheckLocked();
275   ASSERT(!CORBA::is_nil(anObject));
276   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
277   _impl->RemoveAttribute(aSO, string(aTypeOfAttribute));
278 }
279
280 //============================================================================
281 /*! Function : Addreference
282  *  Purpose  : 
283  */
284 //============================================================================
285 void SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me, 
286                                            SALOMEDS::SObject_ptr theReferencedObject)
287 {
288   SALOMEDS::Locker lock;
289   CheckLocked();
290   ASSERT(!CORBA::is_nil(me));
291   ASSERT(!CORBA::is_nil(theReferencedObject));
292  
293   SALOMEDSImpl_SObject aSO, aRefSO;
294   aSO = _impl->GetOwner()->GetSObject(me->GetID());
295   aRefSO = _impl->GetOwner()->GetSObject(theReferencedObject->GetID());
296   _impl->Addreference(aSO, aRefSO);
297 }
298
299 //============================================================================
300 /*! Function : RemoveReference
301  *  Purpose  : 
302  */
303 //============================================================================
304 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
305 {
306   SALOMEDS::Locker lock;
307   CheckLocked();
308   ASSERT(!CORBA::is_nil(me));
309   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(me->GetID());
310   _impl->RemoveReference(aSO);
311 }
312
313
314 //============================================================================
315 /*! Function : AddDirectory
316  *  Purpose  : adds a new directory with a path = thePath
317  */
318 //============================================================================
319 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath) 
320 {
321   SALOMEDS::Locker lock;
322   CheckLocked();
323   if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
324   if(!_impl->AddDirectory(string(thePath))) {
325     string anErrorCode = _impl->GetErrorCode();
326     if(anErrorCode == "StudyNameAlreadyUsed") throw SALOMEDS::Study::StudyNameAlreadyUsed(); 
327     if(anErrorCode == "StudyInvalidDirectory") throw SALOMEDS::Study::StudyInvalidDirectory(); 
328     if(anErrorCode == "StudyInvalidComponent") throw SALOMEDS::Study::StudyInvalidComponent();  
329   }
330 }
331
332
333 //============================================================================
334 /*! Function : SetGUID
335  *  Purpose  : 
336  */
337 //============================================================================
338 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
339 {
340   SALOMEDS::Locker lock;
341   CheckLocked();
342   ASSERT(!CORBA::is_nil(anObject));
343   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
344   _impl->SetGUID(aSO, string(theGUID));
345 }
346
347 //============================================================================
348 /*! Function : IsGUID
349  *  Purpose  : 
350  */
351 //============================================================================
352 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
353 {
354   SALOMEDS::Locker lock;
355   ASSERT(!CORBA::is_nil(anObject));
356   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(anObject->GetID());
357   return _impl->IsGUID(aSO, string(theGUID));
358 }
359
360
361 //============================================================================
362 /*! Function : NewCommand
363  *  Purpose  : 
364  */
365 //============================================================================
366 void SALOMEDS_StudyBuilder_i::NewCommand()
367 {
368   SALOMEDS::Locker lock;
369   _impl->NewCommand();
370 }
371
372 //============================================================================
373 /*! Function : CommitCommand
374  *  Purpose  : 
375  */
376 //============================================================================
377 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
378 {
379   SALOMEDS::Locker lock;
380   Unexpect aCatch(SBLockProtection);
381   try {
382     _impl->CommitCommand();
383   }
384   catch(...) {
385     MESSAGE("Locked document modification !!!");
386     throw SALOMEDS::StudyBuilder::LockProtection();
387   }
388 }
389
390 //============================================================================
391 /*! Function : HasOpenCommand
392  *  Purpose  : 
393  */
394 //============================================================================
395 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
396 {
397   SALOMEDS::Locker lock;
398   return _impl->HasOpenCommand();
399 }
400
401 //============================================================================
402 /*! Function : AbortCommand
403  *  Purpose  : 
404  */
405 //============================================================================
406 void SALOMEDS_StudyBuilder_i::AbortCommand()
407 {
408   SALOMEDS::Locker lock;
409   _impl->AbortCommand();
410 }
411
412 //============================================================================
413 /*! Function : Undo
414  *  Purpose  : 
415  */
416 //============================================================================
417 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
418 {
419   SALOMEDS::Locker lock;
420   Unexpect aCatch(SBLockProtection);
421   try {
422     _impl->Undo();
423   }
424   catch(...) {
425     MESSAGE("Locked document modification !!!");
426     throw SALOMEDS::StudyBuilder::LockProtection();
427   }
428 }
429
430 //============================================================================
431 /*! Function : Redo
432  *  Purpose  : 
433  */
434 //============================================================================
435 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
436 {
437   SALOMEDS::Locker lock;
438   Unexpect aCatch(SBLockProtection);
439   try {
440     _impl->Redo();
441   }
442   catch(...) {
443     MESSAGE("Locked document modification !!!");
444     throw SALOMEDS::StudyBuilder::LockProtection();
445   }
446 }
447
448 //============================================================================
449 /*! Function : GetAvailableUndos
450  *  Purpose  : 
451  */
452 //============================================================================
453 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
454 {
455   SALOMEDS::Locker lock;
456   return _impl->GetAvailableUndos();
457 }
458
459 //============================================================================
460 /*! Function : GetAvailableRedos
461  *  Purpose  : 
462  */
463 //============================================================================
464 CORBA::Boolean  SALOMEDS_StudyBuilder_i::GetAvailableRedos()
465 {
466   SALOMEDS::Locker lock;
467   return _impl->GetAvailableRedos();
468 }
469
470 //============================================================================
471 /*! Function : UndoLimit
472  *  Purpose  : 
473  */
474 //============================================================================
475 CORBA::Long  SALOMEDS_StudyBuilder_i::UndoLimit()
476 {
477   SALOMEDS::Locker lock;
478   return _impl->UndoLimit();
479 }
480
481 //============================================================================
482 /*! Function : UndoLimit
483  *  Purpose  : 
484  */
485 //============================================================================
486 void  SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
487 {
488   SALOMEDS::Locker lock;
489   CheckLocked();
490   _impl->UndoLimit(n);
491 }
492
493 //============================================================================
494 /*! Function : CheckLocked
495  *  Purpose  : 
496  */
497 //============================================================================
498 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) 
499 {
500   SALOMEDS::Locker lock;
501   Unexpect aCatch(SBLockProtection);
502   try {
503     _impl->CheckLocked();
504   }
505   catch(...) {
506     throw SALOMEDS::StudyBuilder::LockProtection();
507   }
508 }
509
510 //============================================================================
511 /*! Function : SetName
512  *  Purpose  : 
513  */
514 //============================================================================
515 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
516      throw(SALOMEDS::StudyBuilder::LockProtection)
517 {
518   SALOMEDS::Locker lock;
519   Unexpect aCatch(SBLockProtection);
520   CheckLocked();
521  
522   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(theSO->GetID());  
523   _impl->SetName(aSO, string(theValue));
524 }
525
526 //============================================================================
527 /*! Function : SetComment
528  *  Purpose  : 
529  */
530 //============================================================================
531 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
532      throw(SALOMEDS::StudyBuilder::LockProtection)
533 {
534   SALOMEDS::Locker lock;
535   Unexpect aCatch(SBLockProtection);
536   CheckLocked();
537
538   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(theSO->GetID());  
539   _impl->SetComment(aSO, string(theValue));
540 }
541
542 //============================================================================
543 /*! Function : SetIOR
544  *  Purpose  : 
545  */
546 //============================================================================
547 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
548  throw(SALOMEDS::StudyBuilder::LockProtection)
549 {
550   SALOMEDS::Locker lock;
551   Unexpect aCatch(SBLockProtection);
552   CheckLocked();
553
554   SALOMEDSImpl_SObject aSO = _impl->GetOwner()->GetSObject(theSO->GetID());  
555   _impl->SetIOR(aSO, string(theValue));
556 }