1 // Copyright (C) 2007-2011 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
22 // File : SALOMEDSImpl_UseCaseBuilder.cxx
23 // Author : Sergey RUIN
26 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
27 #include "SALOMEDSImpl_SObject.hxx"
28 #include "SALOMEDSImpl_SComponent.hxx"
29 #include "SALOMEDSImpl_Study.hxx"
30 #include "SALOMEDSImpl_Attributes.hxx"
32 #include "DF_ChildIterator.hxx"
34 #define USE_CASE_LABEL_TAG 2
35 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
38 //============================================================================
39 /*! Function : constructor
42 //============================================================================
43 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
48 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
49 if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
50 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
53 SALOMEDSImpl_AttributeReference* aRef = NULL;
54 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
55 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
58 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) {
59 SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
63 //============================================================================
64 /*! Function : destructor
67 //============================================================================
68 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
73 //============================================================================
77 //============================================================================
78 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
80 if(!_root || !theObject) return false;
82 DF_Label aLabel = theObject.GetLabel();
83 if(aLabel.IsNull()) return false;
85 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
86 SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
87 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
90 SALOMEDSImpl_AttributeReference* aRef;
91 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
92 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
95 DF_Label aCurrent = aRef->Get();
96 if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID())))
99 aCurrentNode->Append(aNode);
101 // Mantis issue 0020136: Drag&Drop in OB
102 theObject.GetStudy()->addSO_Notification(theObject);
107 //============================================================================
108 /*! Function : Remove
111 //============================================================================
112 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
114 if(!_root || !theObject) return false;
116 DF_Label aLabel = theObject.GetLabel();
117 if(aLabel.IsNull()) return false;
119 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
120 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
124 std::vector<DF_Attribute*> aList;
125 aList.push_back(aNode);
127 SALOMEDSImpl_AttributeReference* aRef = NULL;
128 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
129 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
132 DF_Label aCurrent = aRef->Get();
134 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
135 for(; aChildItr.More(); aChildItr.Next())
136 aList.push_back(aChildItr.Value());
138 for(int i = 0, len = aList.size(); i<len; i++) {
139 if(aList[i]->Label() == aCurrent) { //The current node is removed
140 aRef->Set(_root->Label()); //Reset the current node to the root
142 aList[i]->Label().ForgetAttribute(_root->ID());
149 //============================================================================
150 /*! Function : AppendTo
153 //============================================================================
154 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather,
155 const SALOMEDSImpl_SObject& theObject)
157 if(!_root || !theFather || !theObject) return false;
159 DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
160 if(aFatherLabel == aLabel) return false;
162 SALOMEDSImpl_AttributeTreeNode *aFather = false, *aNode = false;
164 if(aFatherLabel.IsNull()) return false;
165 if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
167 if(aLabel.IsNull()) return false;
168 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
169 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
174 bool ret = aFather->Append(aNode);
176 // Mantis issue 0020136: Drag&Drop in OB
177 theObject.GetStudy()->addSO_Notification(theObject);
182 //============================================================================
183 /*! Function : InsertBefore
186 //============================================================================
187 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst,
188 const SALOMEDSImpl_SObject& theNext)
190 if(!_root || !theFirst || !theNext) return false;
192 DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
193 if(aFirstLabel == aLabel) return false;
195 SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
197 if(aFirstLabel.IsNull()) return false;
198 if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
199 aFirstNode->Remove();
200 aFirstLabel.ForgetAttribute(aFirstNode->ID());
203 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
205 if(aLabel.IsNull()) return false;
206 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
208 aFirstNode->Remove();
210 bool ret = aNode->InsertBefore(aFirstNode);
212 // Mantis issue 0020136: Drag&Drop in OB
213 theFirst.GetStudy()->addSO_Notification(theFirst);
219 //============================================================================
220 /*! Function : SetCurrentObject
223 //============================================================================
224 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
226 if(!_root || !theObject) return false;
228 DF_Label aLabel = theObject.GetLabel();
229 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
230 if(aLabel.IsNull()) return false;
231 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
233 SALOMEDSImpl_AttributeReference* aRef = NULL;
234 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
235 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
238 aRef->Set(aNode->Label());
243 //============================================================================
244 /*! Function : SetRootCurrent
247 //============================================================================
248 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
250 if(!_root) return false;
252 SALOMEDSImpl_AttributeReference* aRef = NULL;
253 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID())))
254 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
256 aRef->Set(_root->Label());
260 //============================================================================
261 /*! Function : HasChildren
264 //============================================================================
265 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
267 if(!_root) return false;
270 if (!theObject) aLabel = _root->Label();
272 aLabel = theObject.GetLabel();
273 if(aLabel.IsNull()) return false;
275 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
276 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
278 return (aNode->GetFirst());
281 //============================================================================
282 /*! Function : GetFather
285 //============================================================================
286 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetFather(const SALOMEDSImpl_SObject& theObject)
288 SALOMEDSImpl_SObject so;
289 if (!_root || !theObject) return so;
291 DF_Label aLabel = theObject.GetLabel();
292 if (aLabel.IsNull()) return so;
294 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
295 if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return so;
297 SALOMEDSImpl_AttributeTreeNode* aFatherNode = aNode->GetFather();
298 if (!aFatherNode) return so;
300 return aFatherNode->GetSObject();
303 //============================================================================
304 /*! Function : SetName
307 //============================================================================
308 bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
309 if(!_root) return false;
311 SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
313 if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
314 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
316 aNameAttrib->SetValue(theName);
322 //============================================================================
323 /*! Function : GetCurrentObject
326 //============================================================================
327 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
329 SALOMEDSImpl_SObject so;
330 if(!_root) return so;
332 SALOMEDSImpl_AttributeReference* aRef = NULL;
333 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
334 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
337 DF_Label aCurrent = aRef->Get();
338 if(aCurrent.IsNull()) return so;
340 return SALOMEDSImpl_Study::SObject(aCurrent);
343 //============================================================================
344 /*! Function : GetName
347 //============================================================================
348 std::string SALOMEDSImpl_UseCaseBuilder::GetName()
351 if(!_root) return aString;
353 SALOMEDSImpl_AttributeName* aName = NULL;
354 if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
355 return aName->Value();
358 //============================================================================
359 /*! Function : IsUseCase
362 //============================================================================
363 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
365 if(!theObject) return false;
366 DF_Label aFather, aLabel = theObject.GetLabel();
367 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
368 if(aLabel.Father() == aFather) return true;
372 //============================================================================
373 /*! Function : IsUseCaseNode
376 //============================================================================
377 bool SALOMEDSImpl_UseCaseBuilder::IsUseCaseNode(const SALOMEDSImpl_SObject& theObject)
379 if(!_root) return false;
382 if (!theObject) aLabel = _root->Label();
384 aLabel = theObject.GetLabel();
385 if(aLabel.IsNull()) return false;
387 SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
388 if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
393 //============================================================================
394 /*! Function : NewUseCase
397 //============================================================================
398 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
400 std::string aBasicGUID(USE_CASE_GUID);
402 //Create a use cases structure if it not exists
404 SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
405 SALOMEDSImpl_AttributeInteger* anInteger = NULL;
406 SALOMEDSImpl_AttributeReference* aRef = NULL;
408 DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
410 if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
411 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
414 if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
415 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
418 if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
419 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
422 //Create a new use case
423 anInteger->SetValue(anInteger->Value()+1);
424 DF_Label aChild = aLabel.FindChild(anInteger->Value());
425 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
427 aFatherNode->Append(aNode);
428 SALOMEDSImpl_AttributeName::Set(aChild, theName);
430 return SALOMEDSImpl_Study::SObject(aChild);
433 //============================================================================
434 /*! Function : GetUseCaseIterator
435 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
437 //============================================================================
438 SALOMEDSImpl_UseCaseIterator
439 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject)
444 aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
447 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
450 return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
454 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
456 DF_Label L = DF_Label::Label(_root->Label(), theEntry);
457 return SALOMEDSImpl_Study::SObject(L);