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