1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_UseCaseBuilder.cxx
24 // Author : Sergey RUIN
27 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
28 #include "SALOMEDSImpl_SObject.hxx"
29 #include "SALOMEDSImpl_SComponent.hxx"
30 #include "SALOMEDSImpl_Study.hxx"
31 #include "SALOMEDSImpl_Attributes.hxx"
33 #include "DF_ChildIterator.hxx"
35 #define USE_CASE_LABEL_TAG 2
36 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
39 //============================================================================
40 /*! Function : constructor
43 //============================================================================
44 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
49 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
50 if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
51 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
54 SALOMEDSImpl_AttributeReference* aRef = NULL;
55 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
56 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
59 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) {
60 SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
64 //============================================================================
65 /*! Function : destructor
68 //============================================================================
69 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
74 //============================================================================
78 //============================================================================
79 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
81 if(!_root || !theObject) return false;
83 DF_Label aLabel = theObject.GetLabel();
84 if(aLabel.IsNull()) return false;
86 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
87 SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
88 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
91 SALOMEDSImpl_AttributeReference* aRef;
92 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
93 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
96 DF_Label aCurrent = aRef->Get();
97 if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID())))
100 aCurrentNode->Append(aNode);
102 // Mantis issue 0020136: Drag&Drop in OB
103 theObject.GetStudy()->addSO_Notification(theObject);
108 //============================================================================
109 /*! Function : Remove
112 //============================================================================
113 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
115 if(!_root || !theObject) return false;
117 DF_Label aLabel = theObject.GetLabel();
118 if(aLabel.IsNull()) return false;
120 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
121 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
125 std::vector<DF_Attribute*> aList;
126 aList.push_back(aNode);
128 SALOMEDSImpl_AttributeReference* aRef = NULL;
129 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
130 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
133 DF_Label aCurrent = aRef->Get();
135 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
136 for(; aChildItr.More(); aChildItr.Next())
137 aList.push_back(aChildItr.Value());
139 for(int i = 0, len = aList.size(); i<len; i++) {
140 if(aList[i]->Label() == aCurrent) { //The current node is removed
141 aRef->Set(_root->Label()); //Reset the current node to the root
143 aList[i]->Label().ForgetAttribute(_root->ID());
150 //============================================================================
151 /*! Function : AppendTo
154 //============================================================================
155 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather,
156 const SALOMEDSImpl_SObject& theObject)
158 if(!_root || !theFather || !theObject) return false;
160 DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
161 if(aFatherLabel == aLabel) return false;
163 SALOMEDSImpl_AttributeTreeNode *aFather = NULL, *aNode = NULL;
165 if(aFatherLabel.IsNull()) return false;
166 if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
168 if(aLabel.IsNull()) return false;
169 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
170 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
175 bool ret = aFather->Append(aNode);
177 // Mantis issue 0020136: Drag&Drop in OB
178 theObject.GetStudy()->addSO_Notification(theObject);
183 //============================================================================
184 /*! Function : InsertBefore
187 //============================================================================
188 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst,
189 const SALOMEDSImpl_SObject& theNext)
191 if(!_root || !theFirst || !theNext) return false;
193 DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
194 if(aFirstLabel == aLabel) return false;
196 SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
198 if(aFirstLabel.IsNull()) return false;
199 if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
200 aFirstNode->Remove();
201 aFirstLabel.ForgetAttribute(aFirstNode->ID());
204 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
206 if(aLabel.IsNull()) return false;
207 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
209 aFirstNode->Remove();
211 bool ret = aNode->InsertBefore(aFirstNode);
213 // Mantis issue 0020136: Drag&Drop in OB
214 theFirst.GetStudy()->addSO_Notification(theFirst);
220 //============================================================================
221 /*! Function : SetCurrentObject
224 //============================================================================
225 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
227 if(!_root || !theObject) return false;
229 DF_Label aLabel = theObject.GetLabel();
230 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
231 if(aLabel.IsNull()) return false;
232 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
234 SALOMEDSImpl_AttributeReference* aRef = NULL;
235 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
236 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
239 aRef->Set(aNode->Label());
244 //============================================================================
245 /*! Function : SetRootCurrent
248 //============================================================================
249 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
251 if(!_root) return false;
253 SALOMEDSImpl_AttributeReference* aRef = NULL;
254 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
255 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
257 aRef->Set(_root->Label());
261 //============================================================================
262 /*! Function : HasChildren
265 //============================================================================
266 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
268 if(!_root) return false;
271 if (!theObject) aLabel = _root->Label();
273 aLabel = theObject.GetLabel();
274 if(aLabel.IsNull()) return false;
276 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
277 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
279 return (aNode->GetFirst());
282 //============================================================================
283 /*! Function : GetFather
286 //============================================================================
287 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetFather(const SALOMEDSImpl_SObject& theObject)
289 SALOMEDSImpl_SObject so;
290 if (!_root || !theObject) return so;
292 DF_Label aLabel = theObject.GetLabel();
293 if (aLabel.IsNull()) return so;
295 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
296 if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return so;
298 SALOMEDSImpl_AttributeTreeNode* aFatherNode = aNode->GetFather();
299 if (!aFatherNode) return so;
301 return aFatherNode->GetSObject();
304 //============================================================================
305 /*! Function : SetName
308 //============================================================================
309 bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
310 if(!_root) return false;
312 SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
314 if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
315 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
317 aNameAttrib->SetValue(theName);
323 //============================================================================
324 /*! Function : GetCurrentObject
327 //============================================================================
328 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
330 SALOMEDSImpl_SObject so;
331 if(!_root) return so;
333 SALOMEDSImpl_AttributeReference* aRef = NULL;
334 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
335 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
338 DF_Label aCurrent = aRef->Get();
339 if(aCurrent.IsNull()) return so;
341 return SALOMEDSImpl_Study::SObject(aCurrent);
344 //============================================================================
345 /*! Function : GetName
348 //============================================================================
349 std::string SALOMEDSImpl_UseCaseBuilder::GetName()
352 if(!_root) return aString;
354 SALOMEDSImpl_AttributeName* aName = NULL;
355 if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
356 return aName->Value();
359 //============================================================================
360 /*! Function : IsUseCase
363 //============================================================================
364 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
366 if(!theObject) return false;
367 DF_Label aFather, aLabel = theObject.GetLabel();
368 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
369 if(aLabel.Father() == aFather) return true;
373 //============================================================================
374 /*! Function : IsUseCaseNode
377 //============================================================================
378 bool SALOMEDSImpl_UseCaseBuilder::IsUseCaseNode(const SALOMEDSImpl_SObject& theObject)
380 if(!_root) return false;
383 if (!theObject) aLabel = _root->Label();
385 aLabel = theObject.GetLabel();
386 if(aLabel.IsNull()) return false;
388 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
389 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
394 //============================================================================
395 /*! Function : NewUseCase
398 //============================================================================
399 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
401 std::string aBasicGUID(USE_CASE_GUID);
403 //Create a use cases structure if it not exists
405 SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
406 SALOMEDSImpl_AttributeInteger* anInteger = NULL;
407 SALOMEDSImpl_AttributeReference* aRef = NULL;
409 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
411 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
412 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
415 if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
416 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
419 if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
420 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
423 //Create a new use case
424 anInteger->SetValue(anInteger->Value()+1);
425 DF_Label aChild = aLabel.FindChild(anInteger->Value());
426 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
428 aFatherNode->Append(aNode);
429 SALOMEDSImpl_AttributeName::Set(aChild, theName);
431 return SALOMEDSImpl_Study::SObject(aChild);
434 //============================================================================
435 /*! Function : GetUseCaseIterator
436 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
438 //============================================================================
439 SALOMEDSImpl_UseCaseIterator
440 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject)
445 aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
448 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
451 return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
455 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
457 DF_Label L = DF_Label::Label(_root->Label(), theEntry);
458 return SALOMEDSImpl_Study::SObject(L);