Salome HOME
Merge from V6_main 01/04/2013
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_UseCaseBuilder.cxx
1 // Copyright (C) 2007-2013  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
23 //  File   : SALOMEDSImpl_UseCaseBuilder.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26
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"
32
33 #include "DF_ChildIterator.hxx"
34
35 #define USE_CASE_LABEL_TAG           2
36 #define USE_CASE_GUID                "AA43BB12-D9CD-11d6-945D-0050DA506788"
37
38
39 //============================================================================
40 /*! Function : constructor
41  *  Purpose  :
42  */
43 //============================================================================
44 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
45 :_doc(theDocument)
46 {
47   if(!_doc) return;
48   
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));
52   }
53  
54   SALOMEDSImpl_AttributeReference* aRef = NULL;
55   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
56     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
57   }  
58
59   if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) { 
60     SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases"); 
61   }  
62 }
63
64 //============================================================================
65 /*! Function : destructor
66  *  Purpose  :
67  */
68 //============================================================================
69 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
70 {
71 }
72
73
74 //============================================================================
75 /*! Function : Append
76  *  Purpose  : 
77  */
78 //============================================================================
79 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
80 {
81   if(!_root || !theObject) return false;
82
83   DF_Label aLabel = theObject.GetLabel();
84   if(aLabel.IsNull()) return false;
85
86   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
87   SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
88   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
89   aNode->Remove();
90
91   SALOMEDSImpl_AttributeReference* aRef;
92   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
93     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
94   }  
95
96   DF_Label aCurrent = aRef->Get();
97   if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID()))) 
98     aCurrentNode = _root;
99
100   aCurrentNode->Append(aNode);
101
102   // Mantis issue 0020136: Drag&Drop in OB
103   theObject.GetStudy()->addSO_Notification(theObject);
104
105   return true;
106 }
107
108  //============================================================================
109 /*! Function : Remove
110  *  Purpose  :
111  */
112 //============================================================================
113 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
114 {
115   if(!_root || !theObject) return false;
116
117   DF_Label aLabel = theObject.GetLabel();   
118   if(aLabel.IsNull()) return false;
119
120   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
121   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
122
123   aNode->Remove();
124
125   std::vector<DF_Attribute*> aList;
126   aList.push_back(aNode);
127
128   SALOMEDSImpl_AttributeReference* aRef = NULL;
129   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
130     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
131   }  
132
133   DF_Label aCurrent = aRef->Get();
134
135   SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
136   for(; aChildItr.More(); aChildItr.Next()) 
137     aList.push_back(aChildItr.Value());
138
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
142     }
143     aList[i]->Label().ForgetAttribute(_root->ID());
144   }
145
146   return true;
147 }
148
149
150 //============================================================================
151 /*! Function : AppendTo
152  *  Purpose  :
153  */
154 //============================================================================
155 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather, 
156                                            const SALOMEDSImpl_SObject& theObject)
157 {
158   if(!_root || !theFather || !theObject) return false;
159
160   DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
161   if(aFatherLabel == aLabel) return false;
162
163   SALOMEDSImpl_AttributeTreeNode *aFather = NULL, *aNode = NULL;
164   
165   if(aFatherLabel.IsNull()) return false;
166   if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
167
168   if(aLabel.IsNull()) return false;
169   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
170     aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
171   }
172
173   aNode->Remove();
174
175   bool ret = aFather->Append(aNode);
176
177   // Mantis issue 0020136: Drag&Drop in OB
178   theObject.GetStudy()->addSO_Notification(theObject);
179
180   return ret;
181 }
182
183 //============================================================================
184 /*! Function : InsertBefore
185  *  Purpose  :
186  */
187 //============================================================================
188 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst, 
189                                                const SALOMEDSImpl_SObject& theNext)
190 {
191   if(!_root || !theFirst || !theNext) return false;
192
193   DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
194   if(aFirstLabel == aLabel) return false;
195
196   SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
197   
198   if(aFirstLabel.IsNull()) return false;
199   if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
200     aFirstNode->Remove();
201     aFirstLabel.ForgetAttribute(aFirstNode->ID());
202   }
203
204   aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
205   
206   if(aLabel.IsNull()) return false;
207   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;    
208
209   aFirstNode->Remove();
210
211   bool ret = aNode->InsertBefore(aFirstNode);
212
213   // Mantis issue 0020136: Drag&Drop in OB
214   theFirst.GetStudy()->addSO_Notification(theFirst);
215
216   return ret;
217 }
218
219
220 //============================================================================
221 /*! Function : SetCurrentObject
222  *  Purpose  :
223  */
224 //============================================================================
225 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
226 {
227   if(!_root || !theObject) return false;
228
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;
233
234   SALOMEDSImpl_AttributeReference* aRef = NULL;
235   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
236     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());  
237   }
238   
239   aRef->Set(aNode->Label());
240
241   return true;
242 }
243
244 //============================================================================
245 /*! Function : SetRootCurrent
246  *  Purpose  :
247  */
248 //============================================================================
249 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
250 {
251   if(!_root) return false;
252    
253   SALOMEDSImpl_AttributeReference* aRef = NULL;
254   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) 
255     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
256
257   aRef->Set(_root->Label());
258   return true;
259 }
260
261 //============================================================================
262 /*! Function : HasChildren
263  *  Purpose  :
264  */
265 //============================================================================
266 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
267 {
268   if(!_root) return false;
269
270   DF_Label aLabel;
271   if (!theObject) aLabel = _root->Label();
272   else 
273     aLabel = theObject.GetLabel(); 
274   if(aLabel.IsNull()) return false;
275
276   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
277   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
278   
279   return (aNode->GetFirst());
280 }
281
282 //============================================================================
283 /*! Function : GetFather
284  *  Purpose  :
285  */
286 //============================================================================
287 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetFather(const SALOMEDSImpl_SObject& theObject)
288 {
289   SALOMEDSImpl_SObject so;
290   if (!_root || !theObject) return so;
291
292   DF_Label aLabel = theObject.GetLabel(); 
293   if (aLabel.IsNull()) return so;
294
295   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
296   if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return so; 
297
298   SALOMEDSImpl_AttributeTreeNode* aFatherNode = aNode->GetFather();
299   if (!aFatherNode) return so;
300
301   return aFatherNode->GetSObject();
302 }
303
304 //============================================================================
305 /*! Function : SetName
306  *  Purpose  :
307  */
308 //============================================================================
309 bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
310   if(!_root) return false;
311
312   SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
313
314   if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
315     aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
316      
317   aNameAttrib->SetValue(theName);
318     
319   return true;
320 }
321
322
323 //============================================================================
324 /*! Function : GetCurrentObject
325  *  Purpose  :
326  */
327 //============================================================================
328 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
329 {
330   SALOMEDSImpl_SObject so;
331   if(!_root) return so;
332
333   SALOMEDSImpl_AttributeReference* aRef = NULL;
334   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
335     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
336   }  
337   
338   DF_Label aCurrent = aRef->Get();  
339   if(aCurrent.IsNull()) return so;
340
341   return SALOMEDSImpl_Study::SObject(aCurrent);
342 }
343
344 //============================================================================
345 /*! Function : GetName
346  *  Purpose  :
347  */
348 //============================================================================
349 std::string SALOMEDSImpl_UseCaseBuilder::GetName() 
350 {
351   std::string aString;
352   if(!_root) return aString;
353   
354   SALOMEDSImpl_AttributeName* aName = NULL;
355   if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
356   return aName->Value();
357 }
358
359 //============================================================================ 
360 /*! Function :  IsUseCase
361  *  Purpose  :  
362  */ 
363 //============================================================================ 
364 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
365 {
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;
370   return false;
371 }
372
373 //============================================================================ 
374 /*! Function :  IsUseCaseNode
375  *  Purpose  :  
376  */ 
377 //============================================================================ 
378 bool SALOMEDSImpl_UseCaseBuilder::IsUseCaseNode(const SALOMEDSImpl_SObject& theObject)
379 {
380   if(!_root) return false;
381
382   DF_Label aLabel;
383   if (!theObject) aLabel = _root->Label();
384   else 
385     aLabel = theObject.GetLabel(); 
386   if(aLabel.IsNull()) return false;
387
388   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
389   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
390   
391   return true;
392 }
393
394 //============================================================================ 
395 /*! Function : NewUseCase 
396  *  Purpose  :  
397  */ 
398 //============================================================================ 
399 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
400 {
401   std::string aBasicGUID(USE_CASE_GUID);
402
403   //Create a use cases structure if it not exists 
404
405   SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
406   SALOMEDSImpl_AttributeInteger* anInteger = NULL;
407   SALOMEDSImpl_AttributeReference* aRef = NULL;
408
409   DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
410
411   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
412     aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
413   }
414  
415   if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
416     aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
417   }
418
419   if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
420     anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
421   }    
422
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);
427   aNode->Remove();
428   aFatherNode->Append(aNode);
429   SALOMEDSImpl_AttributeName::Set(aChild, theName);
430
431   return SALOMEDSImpl_Study::SObject(aChild);
432 }
433
434 //============================================================================
435 /*! Function : GetUseCaseIterator
436  *  Purpose  : Creates a new UseCase iterator, if anObject is null all use cases are iterated 
437  */
438 //============================================================================
439 SALOMEDSImpl_UseCaseIterator
440 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject) 
441 {
442   DF_Label aLabel;
443
444   if(theObject) {
445     aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
446   }
447   else {
448     aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
449   }
450
451   return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false); 
452 }
453
454
455 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
456 {
457   DF_Label L = DF_Label::Label(_root->Label(), theEntry);
458   return SALOMEDSImpl_Study::SObject(L);    
459 }