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