2 // File : SALOMEDS_UseCaseBuilder_i.cxx
3 // Created : Mon Oct 7 16:15:25 2002
4 // Author : Yves FRICAUD
8 // Copyright : Open CASCADE 2002
10 #include "SALOMEDS_UseCaseBuilder_i.hxx"
11 #include "SALOMEDS_AttributeComment_i.hxx"
12 #include "SALOMEDS_SObject_i.hxx"
13 #include "SALOMEDS_SComponent_i.hxx"
14 #include "SALOMEDS_UseCaseIterator_i.hxx"
15 #include "utilities.h"
16 #include <TDF_Label.hxx>
17 #include <TDF_Tool.hxx>
18 #include <TDF_Data.hxx>
19 #include <TDF_Reference.hxx>
20 #include <TDF_AttributeList.hxx>
21 #include <TDF_ListIteratorOfAttributeList.hxx>
22 #include <TDataStd_ChildNodeIterator.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TDF_ChildIterator.hxx>
26 #define USE_CASE_LABEL_TAG 2
27 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
30 //============================================================================
31 /*! Function : constructor
34 //============================================================================
35 SALOMEDS_UseCaseBuilder_i::SALOMEDS_UseCaseBuilder_i(const Handle(TDocStd_Document)& theDocument,
39 _orb = CORBA::ORB::_duplicate(orb);
40 if(_doc.IsNull()) return;
42 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
43 if(!aLabel.FindAttribute(Standard_GUID(USE_CASE_GUID), _root)) {
44 _root = TDataStd_TreeNode::Set(aLabel, Standard_GUID(USE_CASE_GUID));
47 Handle(TDF_Reference) aRef;
48 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
49 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
52 Handle(TDataStd_Name) aNameAttr;
53 if(!aLabel.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) {
54 aNameAttr = TDataStd_Name::Set(aLabel, "Use cases");
59 //============================================================================
60 /*! Function : destructor
63 //============================================================================
64 SALOMEDS_UseCaseBuilder_i::~SALOMEDS_UseCaseBuilder_i()
69 //============================================================================
73 //============================================================================
74 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::Append(SALOMEDS::SObject_ptr theObject)
76 if(_root.IsNull() || theObject->_is_nil()) return 0;
80 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
81 if(aLabel.IsNull()) return 0;
83 Handle(TDataStd_TreeNode) aNode, aCurrentNode;
84 aNode = TDataStd_TreeNode::Set(aLabel, _root->ID());
87 Handle(TDF_Reference) aRef;
88 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
89 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
91 TDF_Label aCurrent = aRef->Get();
93 if(aCurrent.IsNull() || !aCurrent.FindAttribute(_root->ID(), aCurrentNode))
96 return aCurrentNode->Append(aNode);
99 //============================================================================
100 /*! Function : Remove
103 //============================================================================
104 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::Remove(SALOMEDS::SObject_ptr theObject)
106 if(_root.IsNull() || theObject->_is_nil()) return 0;
109 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
110 if(aLabel.IsNull()) return 0;
112 Handle(TDataStd_TreeNode) aNode;
113 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
117 TDF_AttributeList aList;
120 Handle(TDF_Reference) aRef;
121 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
122 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
124 TDF_Label aCurrent = aRef->Get();
126 TDataStd_ChildNodeIterator aChildItr(aNode, Standard_True);
127 for(; aChildItr.More(); aChildItr.Next())
128 aList.Append(aChildItr.Value());
130 TDF_ListIteratorOfAttributeList anIterator(aList);
131 for(; anIterator.More(); anIterator.Next()) {
132 if(anIterator.Value()->Label() == aCurrent) { //The current node is removed
133 aRef->Set(_root->Label()); //Reset the current node to the root
135 anIterator.Value()->Label().ForgetAttribute(_root->ID());
142 //============================================================================
143 /*! Function : AppendTo
146 //============================================================================
147 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::AppendTo(SALOMEDS::SObject_ptr theFather,
148 SALOMEDS::SObject_ptr theObject)
150 if(_root.IsNull() || theFather->_is_nil() || theObject->_is_nil()) return 0;
152 TDF_Label aFatherLabel, aLabel;
153 Handle(TDataStd_TreeNode) aFather, aNode;
155 TDF_Tool::Label(_root->Label().Data(), theFather->GetID(), aFatherLabel);
156 if(aFatherLabel.IsNull()) return 0;
157 if(!aFatherLabel.FindAttribute(_root->ID(), aFather)) return 0;
159 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
160 if(aLabel.IsNull()) return 0;
161 if(!aLabel.FindAttribute(_root->ID(), aNode)) {
162 aNode = TDataStd_TreeNode::Set(aLabel, _root->ID());
167 return aFather->Append(aNode);
170 //============================================================================
171 /*! Function : InsertBefore
174 //============================================================================
175 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::InsertBefore(SALOMEDS::SObject_ptr theFirst,
176 SALOMEDS::SObject_ptr theNext)
178 if(_root.IsNull() || theFirst->_is_nil() || theNext->_is_nil()) return 0;
180 TDF_Label aFirstLabel, aLabel;
181 Handle(TDataStd_TreeNode) aFirstNode, aNode;
183 TDF_Tool::Label(_root->Label().Data(), theFirst->GetID(), aFirstLabel);
184 if(aFirstLabel.IsNull()) return 0;
185 if(aFirstLabel.FindAttribute(_root->ID(), aFirstNode)) {
186 aFirstNode->Remove();
187 aFirstLabel.ForgetAttribute(aFirstNode->ID());
190 aFirstNode = TDataStd_TreeNode::Set(aFirstLabel, _root->ID());
193 TDF_Tool::Label(_root->Label().Data(), theNext->GetID(), aLabel);
194 if(aLabel.IsNull()) return 0;
195 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
197 return aNode->InsertBefore(aFirstNode);
201 //============================================================================
202 /*! Function : SetCurrentObject
205 //============================================================================
206 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SetCurrentObject(SALOMEDS::SObject_ptr theObject)
208 if(_root.IsNull() || theObject->_is_nil()) return 0;
211 Handle(TDataStd_TreeNode) aNode;
212 TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
213 if(aLabel.IsNull()) return 0;
214 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
217 Handle(TDF_Reference) aRef;
218 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
219 aRef = TDF_Reference::Set(_root->Label(), aNode->Label());
222 aRef->Set(aNode->Label());
227 //============================================================================
228 /*! Function : SetRootCurrent
231 //============================================================================
232 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SetRootCurrent()
234 if(_root.IsNull()) return 0;
236 Handle(TDF_Reference) aRef;
237 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef))
238 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
240 aRef->Set(_root->Label());
244 //============================================================================
245 /*! Function : HasChildren
248 //============================================================================
249 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::HasChildren(SALOMEDS::SObject_ptr theObject)
251 if(_root.IsNull()) return 0;
254 if (theObject->_is_nil()) aLabel = _root->Label();
255 else TDF_Tool::Label(_root->Label().Data(), theObject->GetID(), aLabel);
256 if(aLabel.IsNull()) return 0;
258 Handle(TDataStd_TreeNode) aNode;
259 if(!aLabel.FindAttribute(_root->ID(), aNode)) return 0;
261 return !(aNode->First().IsNull());
264 //============================================================================
265 /*! Function : SetName
268 //============================================================================
269 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SetName(const char* theName) {
270 if(_root.IsNull()) return 0;
272 Handle(TDataStd_Name) aNameAttrib;
273 TCollection_ExtendedString aName(strdup(theName));
275 if (!_root->FindAttribute(TDataStd_Name::GetID(), aNameAttrib))
276 aNameAttrib = TDataStd_Name::Set(_root->Label(), aName);
278 aNameAttrib->Set(aName);
284 //============================================================================
285 /*! Function : GetCurrentObject
288 //============================================================================
289 SALOMEDS::SObject_ptr SALOMEDS_UseCaseBuilder_i::GetCurrentObject()
291 if(_root.IsNull()) return NULL;
293 Handle(TDF_Reference) aRef;
294 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
295 aRef = TDF_Reference::Set(_root->Label(), _root->Label());
297 TDF_Label aCurrent = aRef->Get();
298 if(aCurrent.IsNull()) return NULL;
300 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (aCurrent, _orb);
301 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
305 //============================================================================
306 /*! Function : GetName
309 //============================================================================
310 char* SALOMEDS_UseCaseBuilder_i::GetName() {
311 CORBA::String_var aString;
312 if(_root.IsNull()) return aString._retn();
314 Handle(TDataStd_Name) aName;
315 if (!_root->FindAttribute(TDataStd_Name::GetID(), aName)) return aString._retn();
316 aString = strdup(TCollection_AsciiString(aName->Get()).ToCString());
317 return aString._retn();
320 //============================================================================
321 /*! Function : IsUseCase
324 //============================================================================
325 CORBA::Boolean SALOMEDS_UseCaseBuilder_i::IsUseCase(SALOMEDS::SObject_ptr theObject)
327 if(theObject->_is_nil()) return false;
328 TDF_Label aFather, aLabel;
329 TDF_Tool::Label(_doc->GetData(), theObject->GetID(), aLabel);
330 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
331 if(aLabel.Father() == aFather) return true;
335 //============================================================================
336 /*! Function : NewUseCase
339 //============================================================================
340 SALOMEDS::SObject_ptr SALOMEDS_UseCaseBuilder_i::AddUseCase(const char* theName)
342 Standard_GUID aBasicGUID(USE_CASE_GUID);
344 //Create a use cases structure if it not exists
346 Handle(TDataStd_TreeNode) aFatherNode, aNode;
347 Handle(TDataStd_Integer) anInteger;
348 Handle(TDF_Reference) aRef;
350 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
352 if(!_root->FindAttribute(TDF_Reference::GetID(), aRef)) {
353 aRef = TDF_Reference::Set(aLabel, aLabel);
356 if(!aRef->Get().FindAttribute(aBasicGUID, aFatherNode)) {
357 aFatherNode = TDataStd_TreeNode::Set(aRef->Get());
360 if(!_root->FindAttribute(TDataStd_Integer::GetID(), anInteger)) {
361 anInteger = TDataStd_Integer::Set(aLabel, 0);
364 //Create a new use case
365 anInteger->Set(anInteger->Get()+1);
366 TDF_Label aChild = aLabel.FindChild(anInteger->Get());
367 aNode = TDataStd_TreeNode::Set(aChild, aBasicGUID);
368 aFatherNode->Append(aNode);
369 TDataStd_Name::Set(aChild, TCollection_ExtendedString(strdup(theName)));
371 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (aChild, _orb);
372 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
377 //============================================================================
378 /*! Function : GetUseCaseIterator
379 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
381 //============================================================================
382 SALOMEDS::UseCaseIterator_ptr SALOMEDS_UseCaseBuilder_i::GetUseCaseIterator(SALOMEDS::SObject_ptr anObject)
386 if(!anObject->_is_nil()) {
387 TDF_Tool::Label(_doc->GetData(), anObject->GetID(), aLabel); //Iterate only sub tree in the use case
390 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
393 SALOMEDS_UseCaseIterator_i* aServant = new SALOMEDS_UseCaseIterator_i(aLabel, USE_CASE_GUID, Standard_False, _orb);
394 SALOMEDS::UseCaseIterator_var anIterator = SALOMEDS::UseCaseIterator::_narrow(aServant->_this());
396 return anIterator._retn();