Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_UseCaseBuilder.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //  File   : SALOMEDSImpl_UseCaseBuilder.cxx
21 //  Author : Sergey RUIN
22 //  Module : SALOME
23
24
25 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
26 #include "SALOMEDSImpl_SObject.hxx"
27 #include "SALOMEDSImpl_SComponent.hxx"
28 #include "SALOMEDSImpl_Study.hxx"
29 #include "SALOMEDSImpl_Attributes.hxx"
30
31 #include "DF_ChildIterator.hxx"
32
33 using namespace std;
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   return true;
103 }
104
105  //============================================================================
106 /*! Function : Remove
107  *  Purpose  :
108  */
109 //============================================================================
110 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
111 {
112   if(!_root || !theObject) return false;
113
114   DF_Label aLabel = theObject.GetLabel();   
115   if(aLabel.IsNull()) return false;
116
117   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
118   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
119
120   aNode->Remove();
121
122   vector<DF_Attribute*> aList;
123   aList.push_back(aNode);
124
125   SALOMEDSImpl_AttributeReference* aRef = NULL;
126   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
127     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
128   }  
129
130   DF_Label aCurrent = aRef->Get();
131
132   SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
133   for(; aChildItr.More(); aChildItr.Next()) 
134     aList.push_back(aChildItr.Value());
135
136   for(int i = 0, len = aList.size(); i<len; i++) {
137     if(aList[i]->Label() ==  aCurrent) { //The current node is removed
138       aRef->Set(_root->Label()); //Reset the current node to the root
139     }
140     aList[i]->Label().ForgetAttribute(_root->ID());
141   }
142
143   return true;
144 }
145
146
147 //============================================================================
148 /*! Function : AppendTo
149  *  Purpose  :
150  */
151 //============================================================================
152 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather, 
153                                            const SALOMEDSImpl_SObject& theObject)
154 {
155   if(!_root || !theFather || !theObject) return false;
156
157   DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
158   if(aFatherLabel == aLabel) return false;
159
160   SALOMEDSImpl_AttributeTreeNode *aFather = false, *aNode = false;
161   
162   if(aFatherLabel.IsNull()) return false;
163   if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
164
165   if(aLabel.IsNull()) return false;
166   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
167     aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
168   }
169
170   aNode->Remove();
171
172   return aFather->Append(aNode);
173 }
174
175 //============================================================================
176 /*! Function : InsertBefore
177  *  Purpose  :
178  */
179 //============================================================================
180 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst, 
181                                                const SALOMEDSImpl_SObject& theNext)
182 {
183   if(!_root || !theFirst || !theNext) return false;
184
185   DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
186   if(aFirstLabel == aLabel) return false;
187
188   SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
189   
190   if(aFirstLabel.IsNull()) return false;
191   if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
192     aFirstNode->Remove();
193     aFirstLabel.ForgetAttribute(aFirstNode->ID());
194   }
195
196   aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
197   
198   if(aLabel.IsNull()) return false;
199   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;    
200
201   aFirstNode->Remove();
202
203   return aNode->InsertBefore(aFirstNode);
204 }
205
206
207 //============================================================================
208 /*! Function : SetCurrentObject
209  *  Purpose  :
210  */
211 //============================================================================
212 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
213 {
214   if(!_root || !theObject) return false;
215
216   DF_Label aLabel = theObject.GetLabel();
217   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
218   if(aLabel.IsNull()) return false;
219   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
220
221   SALOMEDSImpl_AttributeReference* aRef = NULL;
222   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
223     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());  
224   }
225   
226   aRef->Set(aNode->Label());
227
228   return true;
229 }
230
231 //============================================================================
232 /*! Function : SetRootCurrent
233  *  Purpose  :
234  */
235 //============================================================================
236 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
237 {
238   if(!_root) return false;
239    
240   SALOMEDSImpl_AttributeReference* aRef = NULL;
241   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) 
242     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
243
244   aRef->Set(_root->Label());
245   return true;
246 }
247
248 //============================================================================
249 /*! Function : HasChildren
250  *  Purpose  :
251  */
252 //============================================================================
253 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
254 {
255   if(!_root) return false;
256
257   DF_Label aLabel;
258   if (!theObject) aLabel = _root->Label();
259   else 
260     aLabel = theObject.GetLabel(); 
261   if(aLabel.IsNull()) return false;
262
263   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
264   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
265   
266   return (aNode->GetFirst());
267 }
268
269 //============================================================================
270 /*! Function : SetName
271  *  Purpose  :
272  */
273 //============================================================================
274 bool SALOMEDSImpl_UseCaseBuilder::SetName(const string& theName) {
275   if(!_root) return false;
276
277   SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
278
279   if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
280     aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
281      
282   aNameAttrib->SetValue(theName);
283     
284   return true;
285 }
286
287
288 //============================================================================
289 /*! Function : GetCurrentObject
290  *  Purpose  :
291  */
292 //============================================================================
293 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
294 {
295   SALOMEDSImpl_SObject so;
296   if(!_root) return so;
297
298   SALOMEDSImpl_AttributeReference* aRef = NULL;
299   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
300     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
301   }  
302   
303   DF_Label aCurrent = aRef->Get();  
304   if(aCurrent.IsNull()) return so;
305
306   return SALOMEDSImpl_Study::SObject(aCurrent);
307 }
308
309 //============================================================================
310 /*! Function : GetName
311  *  Purpose  :
312  */
313 //============================================================================
314 string SALOMEDSImpl_UseCaseBuilder::GetName() 
315 {
316   string aString;
317   if(!_root) return aString;
318   
319   SALOMEDSImpl_AttributeName* aName = NULL;
320   if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
321   return aName->Value();
322 }
323
324 //============================================================================ 
325 /*! Function :  IsUseCase
326  *  Purpose  :  
327  */ 
328 //============================================================================ 
329 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
330 {
331   if(!theObject) return false;
332   DF_Label aFather, aLabel = theObject.GetLabel(); 
333   aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
334   if(aLabel.Father() == aFather) return true;
335   return false;
336 }
337
338 //============================================================================ 
339 /*! Function : NewUseCase 
340  *  Purpose  :  
341  */ 
342 //============================================================================ 
343 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const string& theName)
344 {
345   string aBasicGUID(USE_CASE_GUID);
346
347   //Create a use cases structure if it not exists 
348
349   SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
350   SALOMEDSImpl_AttributeInteger* anInteger = NULL;
351   SALOMEDSImpl_AttributeReference* aRef = NULL;
352
353   DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
354
355   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
356     aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
357   }
358  
359   if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
360     aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
361   }
362
363   if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
364     anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
365   }    
366
367   //Create a new use case
368   anInteger->SetValue(anInteger->Value()+1);
369   DF_Label aChild = aLabel.FindChild(anInteger->Value());
370   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
371   aNode->Remove();
372   aFatherNode->Append(aNode);
373   SALOMEDSImpl_AttributeName::Set(aChild, theName);
374
375   return SALOMEDSImpl_Study::SObject(aChild);
376 }
377
378 //============================================================================
379 /*! Function : GetUseCaseIterator
380  *  Purpose  : Creates a new UseCase iterator, if anObject is null all use cases are iterated 
381  */
382 //============================================================================
383 SALOMEDSImpl_UseCaseIterator
384 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject) 
385 {
386   DF_Label aLabel;
387
388   if(theObject) {
389     aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
390   }
391   else {
392     aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
393   }
394
395   return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false); 
396 }
397
398
399 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const string& theEntry)
400 {
401   DF_Label L = DF_Label::Label(_root->Label(), theEntry);
402   return SALOMEDSImpl_Study::SObject(L);    
403 }