Back to index

salome-kernel  6.5.0
SALOMEDSImpl_UseCaseBuilder.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOMEDSImpl_UseCaseBuilder.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 
00027 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
00028 #include "SALOMEDSImpl_SObject.hxx"
00029 #include "SALOMEDSImpl_SComponent.hxx"
00030 #include "SALOMEDSImpl_Study.hxx"
00031 #include "SALOMEDSImpl_Attributes.hxx"
00032 
00033 #include "DF_ChildIterator.hxx"
00034 
00035 #define USE_CASE_LABEL_TAG           2
00036 #define USE_CASE_GUID                "AA43BB12-D9CD-11d6-945D-0050DA506788"
00037 
00038 
00039 //============================================================================
00043 //============================================================================
00044 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(DF_Document* theDocument)
00045 :_doc(theDocument)
00046 {
00047   if(!_doc) return;
00048   
00049   DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
00050   if(!(_root = (SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(std::string(USE_CASE_GUID)))) {
00051     _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, std::string(USE_CASE_GUID));
00052   }
00053  
00054   SALOMEDSImpl_AttributeReference* aRef = NULL;
00055   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00056     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
00057   }  
00058 
00059   if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())) { 
00060     SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases"); 
00061   }  
00062 }
00063 
00064 //============================================================================
00068 //============================================================================
00069 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
00070 {
00071 }
00072 
00073 
00074 //============================================================================
00078 //============================================================================
00079 bool SALOMEDSImpl_UseCaseBuilder::Append(const SALOMEDSImpl_SObject& theObject)
00080 {
00081   if(!_root || !theObject) return false;
00082 
00083   DF_Label aLabel = theObject.GetLabel();
00084   if(aLabel.IsNull()) return false;
00085 
00086   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
00087   SALOMEDSImpl_AttributeTreeNode* aCurrentNode = NULL;
00088   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
00089   aNode->Remove();
00090 
00091   SALOMEDSImpl_AttributeReference* aRef;
00092   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00093     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
00094   }  
00095 
00096   DF_Label aCurrent = aRef->Get();
00097   if(aCurrent.IsNull() || !(aCurrentNode=(SALOMEDSImpl_AttributeTreeNode*)aCurrent.FindAttribute(_root->ID()))) 
00098     aCurrentNode = _root;
00099 
00100   aCurrentNode->Append(aNode);
00101 
00102   // Mantis issue 0020136: Drag&Drop in OB
00103   theObject.GetStudy()->addSO_Notification(theObject);
00104 
00105   return true;
00106 }
00107 
00108  //============================================================================
00112 //============================================================================
00113 bool SALOMEDSImpl_UseCaseBuilder::Remove(const SALOMEDSImpl_SObject& theObject)
00114 {
00115   if(!_root || !theObject) return false;
00116 
00117   DF_Label aLabel = theObject.GetLabel();   
00118   if(aLabel.IsNull()) return false;
00119 
00120   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
00121   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
00122 
00123   aNode->Remove();
00124 
00125   std::vector<DF_Attribute*> aList;
00126   aList.push_back(aNode);
00127 
00128   SALOMEDSImpl_AttributeReference* aRef = NULL;
00129   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00130     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
00131   }  
00132 
00133   DF_Label aCurrent = aRef->Get();
00134 
00135   SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, true);
00136   for(; aChildItr.More(); aChildItr.Next()) 
00137     aList.push_back(aChildItr.Value());
00138 
00139   for(int i = 0, len = aList.size(); i<len; i++) {
00140     if(aList[i]->Label() ==  aCurrent) { //The current node is removed
00141       aRef->Set(_root->Label()); //Reset the current node to the root
00142     }
00143     aList[i]->Label().ForgetAttribute(_root->ID());
00144   }
00145 
00146   return true;
00147 }
00148 
00149 
00150 //============================================================================
00154 //============================================================================
00155 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const SALOMEDSImpl_SObject& theFather, 
00156                                            const SALOMEDSImpl_SObject& theObject)
00157 {
00158   if(!_root || !theFather || !theObject) return false;
00159 
00160   DF_Label aFatherLabel = theFather.GetLabel(), aLabel = theObject.GetLabel();
00161   if(aFatherLabel == aLabel) return false;
00162 
00163   SALOMEDSImpl_AttributeTreeNode *aFather = false, *aNode = false;
00164   
00165   if(aFatherLabel.IsNull()) return false;
00166   if(!(aFather=(SALOMEDSImpl_AttributeTreeNode*)aFatherLabel.FindAttribute(_root->ID()))) return false;
00167 
00168   if(aLabel.IsNull()) return false;
00169   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) {
00170     aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
00171   }
00172 
00173   aNode->Remove();
00174 
00175   bool ret = aFather->Append(aNode);
00176 
00177   // Mantis issue 0020136: Drag&Drop in OB
00178   theObject.GetStudy()->addSO_Notification(theObject);
00179 
00180   return ret;
00181 }
00182 
00183 //============================================================================
00187 //============================================================================
00188 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const SALOMEDSImpl_SObject& theFirst, 
00189                                                const SALOMEDSImpl_SObject& theNext)
00190 {
00191   if(!_root || !theFirst || !theNext) return false;
00192 
00193   DF_Label aFirstLabel = theFirst.GetLabel(), aLabel= theNext.GetLabel();
00194   if(aFirstLabel == aLabel) return false;
00195 
00196   SALOMEDSImpl_AttributeTreeNode *aFirstNode = NULL, *aNode = NULL;
00197   
00198   if(aFirstLabel.IsNull()) return false;
00199   if((aFirstNode=(SALOMEDSImpl_AttributeTreeNode*)aFirstLabel.FindAttribute(_root->ID()))) {
00200     aFirstNode->Remove();
00201     aFirstLabel.ForgetAttribute(aFirstNode->ID());
00202   }
00203 
00204   aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
00205   
00206   if(aLabel.IsNull()) return false;
00207   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;    
00208 
00209   aFirstNode->Remove();
00210 
00211   bool ret = aNode->InsertBefore(aFirstNode);
00212 
00213   // Mantis issue 0020136: Drag&Drop in OB
00214   theFirst.GetStudy()->addSO_Notification(theFirst);
00215 
00216   return ret;
00217 }
00218 
00219 
00220 //============================================================================
00224 //============================================================================
00225 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const SALOMEDSImpl_SObject& theObject)
00226 {
00227   if(!_root || !theObject) return false;
00228 
00229   DF_Label aLabel = theObject.GetLabel();
00230   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
00231   if(aLabel.IsNull()) return false;
00232   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
00233 
00234   SALOMEDSImpl_AttributeReference* aRef = NULL;
00235   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00236     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());  
00237   }
00238   
00239   aRef->Set(aNode->Label());
00240 
00241   return true;
00242 }
00243 
00244 //============================================================================
00248 //============================================================================
00249 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
00250 {
00251   if(!_root) return false;
00252    
00253   SALOMEDSImpl_AttributeReference* aRef = NULL;
00254   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) 
00255     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
00256 
00257   aRef->Set(_root->Label());
00258   return true;
00259 }
00260 
00261 //============================================================================
00265 //============================================================================
00266 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObject)
00267 {
00268   if(!_root) return false;
00269 
00270   DF_Label aLabel;
00271   if (!theObject) aLabel = _root->Label();
00272   else 
00273     aLabel = theObject.GetLabel(); 
00274   if(aLabel.IsNull()) return false;
00275 
00276   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
00277   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
00278   
00279   return (aNode->GetFirst());
00280 }
00281 
00282 //============================================================================
00286 //============================================================================
00287 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetFather(const SALOMEDSImpl_SObject& theObject)
00288 {
00289   SALOMEDSImpl_SObject so;
00290   if (!_root || !theObject) return so;
00291 
00292   DF_Label aLabel = theObject.GetLabel(); 
00293   if (aLabel.IsNull()) return so;
00294 
00295   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
00296   if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return so; 
00297 
00298   SALOMEDSImpl_AttributeTreeNode* aFatherNode = aNode->GetFather();
00299   if (!aFatherNode) return so;
00300 
00301   return aFatherNode->GetSObject();
00302 }
00303 
00304 //============================================================================
00308 //============================================================================
00309 bool SALOMEDSImpl_UseCaseBuilder::SetName(const std::string& theName) {
00310   if(!_root) return false;
00311 
00312   SALOMEDSImpl_AttributeName* aNameAttrib = NULL;
00313 
00314   if (!(aNameAttrib=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
00315     aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
00316      
00317   aNameAttrib->SetValue(theName);
00318     
00319   return true;
00320 }
00321 
00322 
00323 //============================================================================
00327 //============================================================================
00328 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
00329 {
00330   SALOMEDSImpl_SObject so;
00331   if(!_root) return so;
00332 
00333   SALOMEDSImpl_AttributeReference* aRef = NULL;
00334   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00335     aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());  
00336   }  
00337   
00338   DF_Label aCurrent = aRef->Get();  
00339   if(aCurrent.IsNull()) return so;
00340 
00341   return SALOMEDSImpl_Study::SObject(aCurrent);
00342 }
00343 
00344 //============================================================================
00348 //============================================================================
00349 std::string SALOMEDSImpl_UseCaseBuilder::GetName() 
00350 {
00351   std::string aString;
00352   if(!_root) return aString;
00353   
00354   SALOMEDSImpl_AttributeName* aName = NULL;
00355   if (!(aName=(SALOMEDSImpl_AttributeName*)_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) return aString;
00356   return aName->Value();
00357 }
00358 
00359 //============================================================================ 
00363 //============================================================================ 
00364 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const SALOMEDSImpl_SObject& theObject)
00365 {
00366   if(!theObject) return false;
00367   DF_Label aFather, aLabel = theObject.GetLabel(); 
00368   aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
00369   if(aLabel.Father() == aFather) return true;
00370   return false;
00371 }
00372 
00373 //============================================================================ 
00377 //============================================================================ 
00378 bool SALOMEDSImpl_UseCaseBuilder::IsUseCaseNode(const SALOMEDSImpl_SObject& theObject)
00379 {
00380   if(!_root) return false;
00381 
00382   DF_Label aLabel;
00383   if (!theObject) aLabel = _root->Label();
00384   else 
00385     aLabel = theObject.GetLabel(); 
00386   if(aLabel.IsNull()) return false;
00387 
00388   SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
00389   if(!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false; 
00390   
00391   return true;
00392 }
00393 
00394 //============================================================================ 
00398 //============================================================================ 
00399 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::AddUseCase(const std::string& theName)
00400 {
00401   std::string aBasicGUID(USE_CASE_GUID);
00402 
00403   //Create a use cases structure if it not exists 
00404 
00405   SALOMEDSImpl_AttributeTreeNode *aFatherNode = NULL, *aNode = NULL;
00406   SALOMEDSImpl_AttributeInteger* anInteger = NULL;
00407   SALOMEDSImpl_AttributeReference* aRef = NULL;
00408 
00409   DF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
00410 
00411   if(!(aRef=(SALOMEDSImpl_AttributeReference*)_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00412     aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
00413   }
00414  
00415   if(!(aFatherNode=(SALOMEDSImpl_AttributeTreeNode*)aRef->Get().FindAttribute(aBasicGUID))) {
00416     aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
00417   }
00418 
00419   if(!(anInteger=(SALOMEDSImpl_AttributeInteger*)_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
00420     anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
00421   }    
00422 
00423   //Create a new use case
00424   anInteger->SetValue(anInteger->Value()+1);
00425   DF_Label aChild = aLabel.FindChild(anInteger->Value());
00426   aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
00427   aNode->Remove();
00428   aFatherNode->Append(aNode);
00429   SALOMEDSImpl_AttributeName::Set(aChild, theName);
00430 
00431   return SALOMEDSImpl_Study::SObject(aChild);
00432 }
00433 
00434 //============================================================================
00438 //============================================================================
00439 SALOMEDSImpl_UseCaseIterator
00440 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const SALOMEDSImpl_SObject& theObject) 
00441 {
00442   DF_Label aLabel;
00443 
00444   if(theObject) {
00445     aLabel = theObject.GetLabel(); //Iterate only sub tree in the use case
00446   }
00447   else {
00448     aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
00449   }
00450 
00451   return SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false); 
00452 }
00453 
00454 
00455 SALOMEDSImpl_SObject SALOMEDSImpl_UseCaseBuilder::GetSObject(const std::string& theEntry)
00456 {
00457   DF_Label L = DF_Label::Label(_root->Label(), theEntry);
00458   return SALOMEDSImpl_Study::SObject(L);    
00459 }