1 // SALOME SALOMEDS : data structure of SALOME and sources of Salome data server
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SALOMEDS_UseCaseBuilder_i.cxx
8 // Author : Yves FRICAUD
12 #include "SALOMEDS_UseCaseBuilder_i.hxx"
13 #include "SALOMEDS_AttributeComment_i.hxx"
14 #include "SALOMEDS_SObject_i.hxx"
15 #include "SALOMEDS_SComponent_i.hxx"
16 #include "SALOMEDS_UseCaseIterator_i.hxx"
17 #include "utilities.h"
18 #include <TDF_Label.hxx>
19 #include <TDF_Tool.hxx>
20 #include <TDF_Data.hxx>
21 #include <TDF_Reference.hxx>
22 #include <TDF_AttributeList.hxx>
23 #include <TDF_ListIteratorOfAttributeList.hxx>
24 #include <TDataStd_ChildNodeIterator.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TDF_ChildIterator.hxx>
28 #define USE_CASE_LABEL_TAG 2
29 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
32 //============================================================================
33 /*! Function : constructor
36 //============================================================================
37 SALOMEDS_UseCaseBuilder_i::SALOMEDS_UseCaseBuilder_i(const Handle(TDocStd_Document)& theDocument,
41 _orb = CORBA::ORB::_duplicate(orb);
42 if(_doc.IsNull()) return;
44 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
45 if(!aLabel.FindAttribute(Standard_GUID(USE_CASE_GUID), _root)) {
46 _root = TDataStd_TreeNode::Set(aLabel, Standard_GUID(USE_CASE_GUID));
49 Handle(TDF_Reference) aRef;
50 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
51 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
54 Handle(TDataStd_Name) aNameAttr;
55 if(!aLabel.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) {
56 aNameAttr = TDataStd_Name::Set(aLabel, "Use cases");
61 //============================================================================
62 /*! Function : destructor
65 //============================================================================
66 SALOMEDS_UseCaseBuilder_i::~SALOMEDS_UseCaseBuilder_i()
71 //============================================================================
75 //============================================================================
76 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::Append(SALOMEDS::SObject_ptr theObject)
78 if(_root.IsNull() || theObject->_is_nil()) return 0;
82 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
83 if(aLabel.IsNull()) return 0;
85 Handle(TDataStd_TreeNode) aNode, aCurrentNode;
86 aNode = TDataStd_TreeNode::Set(aLabel, _root->ID());
89 Handle(TDF_Reference) aRef;
90 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
91 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
93 TDF_Label aCurrent = aRef->Get();
95 if(aCurrent.IsNull() || !aCurrent.FindAttribute(_root->ID(), aCurrentNode))
98 return aCurrentNode->Append(aNode);
101 //============================================================================
102 /*! Function : Remove
105 //============================================================================
106 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::Remove(SALOMEDS::SObject_ptr theObject)
108 if(_root.IsNull() || theObject->_is_nil()) return 0;
111 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
112 if(aLabel.IsNull()) return 0;
114 Handle(TDataStd_TreeNode) aNode;
115 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
119 TDF_AttributeList aList;
122 Handle(TDF_Reference) aRef;
123 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
124 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
126 TDF_Label aCurrent = aRef->Get();
128 TDataStd_ChildNodeIterator aChildItr(aNode, Standard_True);
129 for(; aChildItr.More(); aChildItr.Next())
130 aList.Append(aChildItr.Value());
132 TDF_ListIteratorOfAttributeList anIterator(aList);
133 for(; anIterator.More(); anIterator.Next()) {
134 if(anIterator.Value()->Label() == aCurrent) { //The current node is removed
135 aRef->Set(_root->Label()); //Reset the current node to the root
137 anIterator.Value()->Label().ForgetAttribute(_root->ID());
144 //============================================================================
145 /*! Function : AppendTo
148 //============================================================================
149 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::AppendTo(SALOMEDS::SObject_ptr theFather,
150 SALOMEDS::SObject_ptr theObject)
152 if(_root.IsNull() || theFather->_is_nil() || theObject->_is_nil()) return 0;
154 TDF_Label aFatherLabel, aLabel;
155 Handle(TDataStd_TreeNode) aFather, aNode;
157 TDF_Tool::Label(_root->Label().Data(), theFather->GetID(), aFatherLabel);
158 if(aFatherLabel.IsNull()) return 0;
159 if(!aFatherLabel.FindAttribute(_root->ID(), aFather)) return 0;
161 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
162 if(aLabel.IsNull()) return 0;
163 if(!aLabel.FindAttribute(_root->ID(), aNode)) {
164 aNode = TDataStd_TreeNode::Set(aLabel, _root->ID());
169 return aFather->Append(aNode);
172 //============================================================================
173 /*! Function : InsertBefore
176 //============================================================================
177 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::InsertBefore(SALOMEDS::SObject_ptr theFirst,
178 SALOMEDS::SObject_ptr theNext)
180 if(_root.IsNull() || theFirst->_is_nil() || theNext->_is_nil()) return 0;
182 TDF_Label aFirstLabel, aLabel;
183 Handle(TDataStd_TreeNode) aFirstNode, aNode;
185 TDF_Tool::Label(_root->Label().Data(), theFirst->GetID(), aFirstLabel);
186 if(aFirstLabel.IsNull()) return 0;
187 if(aFirstLabel.FindAttribute(_root->ID(), aFirstNode)) {
188 aFirstNode->Remove();
189 aFirstLabel.ForgetAttribute(aFirstNode->ID());
192 aFirstNode = TDataStd_TreeNode::Set(aFirstLabel, _root->ID());
195 TDF_Tool::Label(_root->Label().Data(), theNext->GetID(), aLabel);
196 if(aLabel.IsNull()) return 0;
197 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
199 return aNode->InsertBefore(aFirstNode);
203 //============================================================================
204 /*! Function : SetCurrentObject
207 //============================================================================
208 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SetCurrentObject(SALOMEDS::SObject_ptr theObject)
210 if(_root.IsNull() || theObject->_is_nil()) return 0;
213 Handle(TDataStd_TreeNode) aNode;
214 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
215 if(aLabel.IsNull()) return 0;
216 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
219 Handle(TDF_Reference) aRef;
220 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
221 aRef = TDF_Reference::Set(_root->Label(), aNode->Label());
224 aRef->Set(aNode->Label());
229 //============================================================================
230 /*! Function : SetRootCurrent
233 //============================================================================
234 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SetRootCurrent()
236 if(_root.IsNull()) return 0;
238 Handle(TDF_Reference) aRef;
239 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef))
240 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
242 aRef->Set(_root->Label());
246 //============================================================================
247 /*! Function : HasChildren
250 //============================================================================
251 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::HasChildren(SALOMEDS::SObject_ptr theObject)
253 if(_root.IsNull()) return 0;
256 if (theObject->_is_nil()) aLabel = _root->Label();
257 else TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
258 if(aLabel.IsNull()) return 0;
260 Handle(TDataStd_TreeNode) aNode;
261 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
263 return !(aNode->First().IsNull());
266 //============================================================================
267 /*! Function : SetName
270 //============================================================================
271 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SetName(const char* theName) {
272 if(_root.IsNull()) return 0;
274 Handle(TDataStd_Name) aNameAttrib;
275 TCollection_ExtendedString aName(strdup(theName));
277 if (!_root->FindAttribute(TDataStd_Name::GetID(), aNameAttrib))
278 aNameAttrib = TDataStd_Name::Set(_root->Label(), aName);
280 aNameAttrib->Set(aName);
286 //============================================================================
287 /*! Function : GetCurrentObject
290 //============================================================================
291 SALOMEDS::SObject_ptr SALOMEDS_UseCaseBuilder_i::GetCurrentObject()
293 if(_root.IsNull()) return NULL;
295 Handle(TDF_Reference) aRef;
296 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
297 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
299 TDF_Label aCurrent = aRef->Get();
300 if(aCurrent.IsNull()) return NULL;
302 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (aCurrent, _orb);
303 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
307 //============================================================================
308 /*! Function : GetName
311 //============================================================================
312 char* SALOMEDS_UseCaseBuilder_i::GetName() {
313 CORBA::String_var aString;
314 if(_root.IsNull()) return aString._retn();
316 Handle(TDataStd_Name) aName;
317 if (!_root->FindAttribute(TDataStd_Name::GetID(), aName)) return aString._retn();
318 aString = strdup(TCollection_AsciiString(aName->Get()).ToCString());
319 return aString._retn();
322 //============================================================================
323 /*! Function : IsUseCase
326 //============================================================================
327 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::IsUseCase(SALOMEDS::SObject_ptr theObject)
329 if(theObject->_is_nil()) return false;
330 TDF_Label aFather, aLabel;
331 TDF_Tool::Label(_doc->GetData(), theObject->GetID(), aLabel);
332 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
333 if(aLabel.Father() == aFather) return true;
337 //============================================================================
338 /*! Function : NewUseCase
341 //============================================================================
342 SALOMEDS::SObject_ptr SALOMEDS_UseCaseBuilder_i::AddUseCase(const char* theName)
344 Standard_GUID aBasicGUID(USE_CASE_GUID);
346 //Create a use cases structure if it not exists
348 Handle(TDataStd_TreeNode) aFatherNode, aNode;
349 Handle(TDataStd_Integer) anInteger;
350 Handle(TDF_Reference) aRef;
352 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
354 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
355 aRef = TDF_Reference::Set(aLabel, aLabel);
358 if(!aRef->Get().FindAttribute(aBasicGUID, aFatherNode)) {
359 aFatherNode = TDataStd_TreeNode::Set(aRef->Get());
362 if(!_root->FindAttribute(TDataStd_Integer::GetID(), anInteger)) {
363 anInteger = TDataStd_Integer::Set(aLabel, 0);
366 //Create a new use case
367 anInteger->Set(anInteger->Get()+1);
368 TDF_Label aChild = aLabel.FindChild(anInteger->Get());
369 aNode = TDataStd_TreeNode::Set(aChild, aBasicGUID);
370 aFatherNode->Append(aNode);
371 TDataStd_Name::Set(aChild, TCollection_ExtendedString(strdup(theName)));
373 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (aChild, _orb);
374 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
379 //============================================================================
380 /*! Function : GetUseCaseIterator
381 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
383 //============================================================================
384 SALOMEDS::UseCaseIterator_ptr SALOMEDS_UseCaseBuilder_i::GetUseCaseIterator(SALOMEDS::SObject_ptr anObject)
388 if(!anObject->_is_nil()) {
389 TDF_Tool::Label(_doc->GetData(), anObject->GetID(), aLabel); //Iterate only sub tree in the use case
392 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
395 SALOMEDS_UseCaseIterator_i* aServant = new SALOMEDS_UseCaseIterator_i(aLabel, USE_CASE_GUID, Standard_False, _orb);
396 SALOMEDS::UseCaseIterator_var anIterator = SALOMEDS::UseCaseIterator::_narrow(aServant->_this());
398 return anIterator._retn();