Back to index

salome-kernel  6.5.0
SALOMEDSImpl_StudyBuilder.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_StudyBuilder.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 //
00027 #include "SALOMEDSImpl_Attributes.hxx"
00028 #include "SALOMEDSImpl_Study.hxx"
00029 #include "SALOMEDSImpl_StudyBuilder.hxx"
00030 #include "SALOMEDSImpl_SObject.hxx"
00031 #include "SALOMEDSImpl_SComponent.hxx"
00032 #include "SALOMEDSImpl_Tool.hxx"
00033 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
00034 
00035 #include "DF_ChildIterator.hxx"
00036 #include "DF_Label.hxx"
00037 
00038 #include <HDFOI.hxx>
00039 #include <stdlib.h> 
00040 #include <string.h> 
00041 
00042 #define USE_CASE_LABEL_TAG            2
00043 #define DIRECTORYID                   16661
00044 #define FILELOCALID                   26662 
00045 
00046 static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII);
00047 
00048 //============================================================================
00052 //============================================================================
00053 SALOMEDSImpl_StudyBuilder::SALOMEDSImpl_StudyBuilder(const SALOMEDSImpl_Study* theOwner)
00054 {
00055    _errorCode = "";
00056   _callbackOnAdd=NULL;
00057   _callbackOnRemove = NULL;
00058    _study = (SALOMEDSImpl_Study*)theOwner;
00059    _doc = _study->GetDocument();
00060 }
00061 
00062 //============================================================================
00066 //============================================================================
00067 SALOMEDSImpl_StudyBuilder::~SALOMEDSImpl_StudyBuilder()
00068 {}
00069 
00070 //============================================================================
00074 //============================================================================
00075 SALOMEDSImpl_SComponent SALOMEDSImpl_StudyBuilder::NewComponent(const std::string& DataType)
00076 {
00077   _errorCode = "";
00078   CheckLocked();
00079 
00080   SALOMEDSImpl_SComponent sco;
00081 
00082   if(DataType.size() == 0) return sco;
00083 
00084   //Always create component under main label.
00085   DF_Label L  = _doc->Main();
00086 
00087   DF_Label NL = L.NewChild();
00088 
00089   SALOMEDSImpl_AttributeComment::Set(NL, DataType);
00090 
00091   SALOMEDSImpl_SComponent so =  _study->GetSComponent (NL);
00092 
00093   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
00094   _study->addSO_Notification(so);
00095 
00096   _doc->SetModified(true);
00097 
00098   return so;
00099 }
00100 
00101 //============================================================================
00105 //============================================================================
00106 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const SALOMEDSImpl_SComponent& aComponent,
00107                                                         const std::string& IOR)
00108 {
00109    _errorCode = "";
00110 
00111   CheckLocked();
00112   if(!aComponent || IOR.empty()) {
00113     _errorCode = "Invalid arguments";
00114     return false;
00115   }
00116   //add IOR definition 
00117   SALOMEDSImpl_AttributeIOR::Set(aComponent.GetLabel(), IOR);  
00118 
00119   return true;
00120 }
00121 
00122 //============================================================================
00126 //============================================================================
00127 bool SALOMEDSImpl_StudyBuilder::RemoveComponent(const SALOMEDSImpl_SComponent& aComponent)
00128 {
00129    _errorCode = "";
00130   CheckLocked();
00131   return RemoveObject(aComponent);
00132 }
00133 
00134 //============================================================================
00138 //============================================================================
00139 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObject(const SALOMEDSImpl_SObject& theFatherObject)
00140 {
00141   _errorCode = "";
00142   CheckLocked();
00143 
00144   //Find label of father
00145   DF_Label Lab = theFatherObject.GetLabel();
00146   
00147   //Create a new label
00148   DF_Label NewLab = Lab.NewChild();
00149   
00150   SALOMEDSImpl_SObject so = _study->GetSObject(NewLab);
00151   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
00152   _study->addSO_Notification(so);
00153 
00154   _doc->SetModified(true);  
00155   return so;
00156 }
00157 
00158 //============================================================================
00162 //============================================================================
00163 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObjectToTag(const SALOMEDSImpl_SObject& theFatherObject,
00164                                                         const int theTag)
00165 {
00166   _errorCode = "";
00167   CheckLocked();
00168   //Find label of father
00169   DF_Label Lab = theFatherObject.GetLabel();
00170 
00171   //Create or find label
00172   DF_Label NewLab = Lab.FindChild(theTag, 1);
00173 
00174   SALOMEDSImpl_SObject so = _study->GetSObject(NewLab);
00175 
00176   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
00177   _study->addSO_Notification(so);
00178 
00179   _doc->SetModified(true);  
00180   return so;
00181 }
00182 
00183 //============================================================================
00187 //============================================================================
00188 bool SALOMEDSImpl_StudyBuilder::RemoveObject(const SALOMEDSImpl_SObject& anObject)
00189 {
00190    _errorCode = "";
00191   CheckLocked();
00192   if(!anObject) {
00193     _errorCode = "Null object";
00194     return false;
00195   }
00196 
00197   if(_callbackOnRemove) _callbackOnRemove->OnRemoveSObject(anObject);
00198 
00199   DF_Label Lab = anObject.GetLabel();
00200 
00201   SALOMEDSImpl_AttributeReference* aReference = NULL;
00202   if ((aReference=(SALOMEDSImpl_AttributeReference*)Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00203     SALOMEDSImpl_AttributeTarget* aTarget = NULL;
00204     if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
00205       aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
00206   }
00207 
00208   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
00209   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
00210     _study->DeleteIORLabelMapItem(anAttr->Value());
00211   }
00212 
00213   Lab.ForgetAllAttributes();
00214  
00215   _doc->SetModified(true);  
00216   _study->removeSO_Notification(anObject);
00217     
00218   return true;
00219 }
00220 
00221 //============================================================================
00225 //============================================================================
00226 bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObject& anObject)
00227 {
00228    _errorCode = "";
00229   CheckLocked();
00230   if(!anObject) {
00231     _errorCode = "Null object";
00232     return false;
00233   }
00234 
00235   if(_callbackOnRemove) _callbackOnRemove->OnRemoveSObject(anObject);
00236 
00237   DF_Label Lab = anObject.GetLabel();
00238 
00239   SALOMEDSImpl_AttributeReference* aReference = NULL;
00240   if ((aReference=(SALOMEDSImpl_AttributeReference*)Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00241     SALOMEDSImpl_AttributeTarget* aTarget = NULL;
00242     if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
00243       aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
00244   }
00245   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
00246   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
00247     _study->DeleteIORLabelMapItem(anAttr->Value());
00248   }
00249 
00250   DF_ChildIterator it(Lab, true);
00251   for(;it.More();it.Next()) {
00252     DF_Label aLabel = it.Value();
00253     if ((aReference=(SALOMEDSImpl_AttributeReference*)aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
00254       SALOMEDSImpl_AttributeTarget* aTarget = NULL;
00255       if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
00256         aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
00257     }
00258     SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
00259     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
00260       _study->DeleteIORLabelMapItem(anAttr->Value());
00261     }
00262   }
00263 
00264   Lab.ForgetAllAttributes(true);
00265 
00266   _doc->SetModified(true);  
00267   _study->removeSO_Notification(anObject);
00268   
00269   return true;
00270 }
00271 
00272 //============================================================================
00276 //============================================================================
00277 bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
00278                                          SALOMEDSImpl_Driver* aDriver) 
00279 {
00280   _errorCode = "";
00281 
00282   DF_Label Lab = anSCO.GetLabel();
00283   SALOMEDSImpl_AttributePersistentRef* Att = NULL;
00284 
00285   //Find the current Url of the study  
00286   if ((Att=(SALOMEDSImpl_AttributePersistentRef*)_doc->Main().FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
00287     int aLocked = _study->GetProperties()->IsLocked();
00288     if (aLocked) _study->GetProperties()->SetLocked(false);
00289 
00290     std::string Res(Att->Value());
00291     std::string aHDFPath(Res);
00292 
00293     SALOMEDSImpl_AttributeComment* type = NULL;
00294     std::string DataType;
00295     if ((type=(SALOMEDSImpl_AttributeComment*)Lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID())))
00296       DataType = type->Value();
00297 
00298     // associate the driver to the SComponent
00299     if(aDriver == NULL) {
00300       _errorCode = "Driver is null";
00301       return false;
00302     }
00303 
00304     // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
00305     SALOMEDSImpl_AttributeIOR* attrIOR = NULL;
00306     if ((attrIOR=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
00307       if (aLocked) _study->GetProperties()->SetLocked(true);
00308       return true;
00309     }
00310 
00311     DefineComponentInstance (anSCO, aDriver->GetIOR());
00312 
00313     std::string aHDFUrl;
00314     bool isASCII = false;
00315     if (HDFascii::isASCII(aHDFPath.c_str())) {
00316       isASCII = true;
00317       aHDFUrl = HDFascii::ConvertFromASCIIToHDF(aHDFPath.c_str());
00318       aHDFUrl += "hdf_from_ascii.hdf";
00319     } else {
00320       aHDFUrl = aHDFPath;
00321     }
00322 
00323     //Open the Study HDF file 
00324     HDFfile *hdf_file = new HDFfile((char*)aHDFUrl.c_str()); 
00325 
00326     char aMultifileState[2];
00327     char ASCIIfileState[2];
00328     bool hasModuleData = false;
00329     try {
00330       std::string scoid = anSCO.GetID();
00331       hdf_file->OpenOnDisk(HDF_RDONLY);
00332       HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
00333       hdf_group->OpenOnDisk();
00334       HDFgroup *hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group);
00335       hdf_sco_group->OpenOnDisk();
00336       hasModuleData = true;
00337 
00338       unsigned char* aStreamFile = NULL;
00339       int aStreamSize = 0;
00340 
00341       if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
00342         HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
00343         hdf_dataset->OpenOnDisk();
00344         aStreamSize = hdf_dataset->GetSize();
00345         aStreamFile  = new unsigned char[aStreamSize];
00346         if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
00347         hdf_dataset->ReadFromDisk(aStreamFile);
00348         hdf_dataset->CloseOnDisk();
00349         hdf_dataset = 0;
00350       } else
00351         aStreamFile = NULL;
00352 
00353       HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
00354       multifile_hdf_dataset->OpenOnDisk();
00355       multifile_hdf_dataset->ReadFromDisk(aMultifileState);
00356 
00357       HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
00358       ascii_hdf_dataset->OpenOnDisk();
00359       ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
00360 
00361       std::string aDir = SALOMEDSImpl_Tool::GetDirFromPath(Res);
00362 
00363       bool aResult = true;
00364       if(aStreamFile && aStreamSize > 0 ) {
00365         aResult = (ASCIIfileState[0]=='A')?
00366         aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M'):
00367         aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M');
00368       }
00369 
00370       if(aStreamFile != NULL) delete []aStreamFile; 
00371 
00372       if(!aResult) {
00373         RemoveAttribute( anSCO, "AttributeIOR" );
00374 
00375         _errorCode = "Can't load component";
00376         throw HDFexception("Unable to load component");
00377       }
00378 
00379       //if(aDir != NULL) delete []aDir;
00380 
00381       multifile_hdf_dataset->CloseOnDisk();
00382       multifile_hdf_dataset = 0;
00383       ascii_hdf_dataset->CloseOnDisk();
00384       ascii_hdf_dataset = 0;
00385 
00386       hdf_sco_group->CloseOnDisk();
00387       hdf_sco_group = 0;
00388       hdf_group->CloseOnDisk();
00389       hdf_group = 0;
00390       hdf_file->CloseOnDisk();
00391       delete hdf_file;
00392 
00393       if (isASCII) {
00394         std::vector<std::string> aFilesToRemove;
00395         aFilesToRemove.push_back("hdf_from_ascii.hdf");
00396         SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
00397                                                 aFilesToRemove, true);
00398       }      
00399     }
00400     catch (HDFexception) {
00401       delete hdf_file;
00402 
00403       if (isASCII) {
00404         std::vector<std::string> aFilesToRemove;
00405         aFilesToRemove.push_back(aHDFUrl);
00406         SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
00407       }
00408 
00409       if (aLocked) _study->GetProperties()->SetLocked(true);
00410 
00411       if (!hasModuleData)
00412        return true;
00413 
00414       _errorCode = "No persistent file";   
00415       return false;
00416     }
00417 
00418     try {
00419       Translate_persistentID_to_IOR (Lab, aDriver, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
00420     } catch(...) {
00421       _errorCode = "Can not convert persistent IDs to IORs";
00422       return false;
00423     }
00424 
00425     if (aLocked) _study->GetProperties()->SetLocked(true);
00426   } else {
00427     _errorCode = "No persistent file";   
00428   }
00429 
00430   return true;
00431 }
00432 
00433 
00434 //============================================================================
00438 //============================================================================
00439 bool SALOMEDSImpl_StudyBuilder::Load(const SALOMEDSImpl_SObject& sco)
00440 {
00441   _errorCode = "Not implemented";
00442   return false;
00443 }
00444 
00445 //============================================================================
00450 //============================================================================
00451 DF_Attribute* SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const SALOMEDSImpl_SObject& anObject, 
00452                                                                const std::string& aTypeOfAttribute)
00453 {
00454   _errorCode = "";
00455   if(!anObject) {
00456     _errorCode = "Invalid arguments";
00457     return NULL;
00458   }
00459 
00460   DF_Label Lab = anObject.GetLabel();
00461   if(Lab.IsNull()) {
00462     _errorCode = "Null label";
00463     return NULL;
00464   }
00465 
00466   _doc->SetModified(true);  
00467 
00468   //The macro adds all necessary checks for standardly behaiving attributes
00469   __FindOrCreateAttributeForBuilder
00470 
00471  
00472   //Add checks for TreeNode and UserID attributes  
00473   if (strncmp(aTypeOfAttribute.c_str(), "AttributeTreeNode",17) == 0 ) {
00474     
00475     std::string aTreeNodeGUID;
00476     if (strcmp(aTypeOfAttribute.c_str(), "AttributeTreeNode") == 0) {
00477       aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
00478     } else {
00479       aTreeNodeGUID = aTypeOfAttribute.substr(21, aTypeOfAttribute.size()); // create tree node GUID by name
00480     }
00481     SALOMEDSImpl_AttributeTreeNode* anAttr = NULL;
00482     if (!(anAttr=(SALOMEDSImpl_AttributeTreeNode*)Lab.FindAttribute(aTreeNodeGUID))) {
00483       CheckLocked();
00484       anAttr = SALOMEDSImpl_AttributeTreeNode::Set(Lab, aTreeNodeGUID);
00485     }
00486     return anAttr;
00487   }
00488 
00489   if (strncmp(aTypeOfAttribute.c_str(), "AttributeUserID",15) == 0 ) {
00490     std::string aUserGUID;
00491     if (strcmp(aTypeOfAttribute.c_str(), "AttributeUserID") == 0) {
00492       aUserGUID = SALOMEDSImpl_AttributeUserID::DefaultID();
00493     } else {
00494       aUserGUID = aTypeOfAttribute.substr(15, aTypeOfAttribute.size()); // create tree node GUID by name
00495     }
00496     SALOMEDSImpl_AttributeUserID* anAttr = NULL;
00497     if (!(anAttr=(SALOMEDSImpl_AttributeUserID*)Lab.FindAttribute(aUserGUID))) {
00498       CheckLocked();
00499       anAttr = SALOMEDSImpl_AttributeUserID::Set(Lab, aUserGUID);
00500     }
00501     return anAttr;
00502   }
00503   _errorCode = "Can not create an attribute";
00504 
00505   return NULL;
00506 }
00507 
00508 //============================================================================
00512 //============================================================================
00513 
00514 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const SALOMEDSImpl_SObject& anObject, 
00515                                               DF_Attribute*& anAttribute, 
00516                                               const std::string& aTypeOfAttribute)
00517 {
00518   _errorCode = "";
00519   if(!anObject) {
00520     _errorCode = "Invalid arguments";
00521     return false;
00522   }
00523   DF_Label Lab = anObject.GetLabel();
00524   if ((anAttribute=Lab.FindAttribute(SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute)))) {
00525     // commented out because NO MODIFICATION is done to attributes when calling FindAttribute()
00526     // _doc->Modify();  
00527     return true;
00528   }
00529   return false;
00530 }
00531 
00532 //============================================================================
00536 //============================================================================
00537 
00538 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anObject, 
00539                                                 const std::string& aTypeOfAttribute)
00540 {
00541   _errorCode = "";
00542   CheckLocked();
00543   if(!anObject) {
00544     _errorCode = "Invalid arguments";
00545     return false;
00546   }
00547   DF_Label Lab = anObject.GetLabel();
00548   
00549   if (aTypeOfAttribute == std::string("AttributeIOR")) { // Remove from IORLabel map
00550     SALOMEDSImpl_AttributeIOR* anAttr = NULL;
00551     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
00552       _study->DeleteIORLabelMapItem(anAttr->Value());
00553     }
00554   }
00555 
00556   Lab.ForgetAttribute (SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute));
00557     
00558   _doc->SetModified(true);  
00559   _study->modifySO_Notification(anObject,0);
00560     
00561   return true;
00562 }
00563 
00564 //============================================================================
00568 //============================================================================
00569 bool SALOMEDSImpl_StudyBuilder::Addreference(const SALOMEDSImpl_SObject& me, 
00570                                              const SALOMEDSImpl_SObject& theReferencedObject)
00571 {
00572   _errorCode = "";
00573   if(!me || !theReferencedObject) {
00574    _errorCode = "Invalid arguments";
00575    return false;
00576   }
00577   CheckLocked();
00578   DF_Label Lab = me.GetLabel();
00579   DF_Label RefLab = theReferencedObject.GetLabel();
00580   SALOMEDSImpl_AttributeReference::Set(Lab,RefLab);
00581 
00582   SALOMEDSImpl_AttributeTarget::Set(RefLab)->Add(SALOMEDSImpl_Study::SObject(Lab));
00583 
00584   if(_callbackOnRemove && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
00585   
00586   return true;
00587 }
00588 
00589 //============================================================================
00593 //============================================================================
00594 bool SALOMEDSImpl_StudyBuilder::RemoveReference(const SALOMEDSImpl_SObject& me)
00595 {
00596   _errorCode = "";
00597   SALOMEDSImpl_SObject theReferencedObject;
00598   
00599   if(!me.ReferencedObject(theReferencedObject)) return false;  //No reference is found
00600   
00601   CheckLocked();
00602   DF_Label Lab = me.GetLabel();
00603 
00604   //SRN: 30 Aug, 2004 : fix from Ecole l'ete version 
00605 
00606   DF_Label RefLab = theReferencedObject.GetLabel();
00607        
00608   SALOMEDSImpl_AttributeTarget* aTarget = NULL;
00609   if((aTarget=(SALOMEDSImpl_AttributeTarget*)RefLab.FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
00610     aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
00611   }
00612   
00613   Lab.ForgetAttribute(SALOMEDSImpl_AttributeReference::GetID());  
00614   
00615   _doc->SetModified(true);  
00616   
00617   return true;
00618 }
00619 
00620 
00621 
00622 //============================================================================
00626 //============================================================================
00627 bool SALOMEDSImpl_StudyBuilder::AddDirectory(const std::string& thePath) 
00628 {
00629   _errorCode = "";
00630   CheckLocked();
00631   if(thePath.empty()) {
00632     _errorCode = "Invalid path";
00633     return false;
00634   }
00635 
00636   std::string aPath(thePath), aContext(""), aFatherPath;
00637   DF_Label aLabel;
00638   SALOMEDSImpl_SObject anObject;
00639 
00640   try { 
00641     anObject = _study->FindObjectByPath(thePath); //Check if the directory already exists
00642   }
00643   catch(...) { }
00644 
00645   if(anObject) {
00646     _errorCode = "StudyNameAlreadyUsed";
00647     return false; 
00648   }
00649 
00650   if(aPath[0] != '/') { //Relative path 
00651     aPath.insert(aPath.begin(), '/');
00652     aPath = _study->GetContext() + aPath;
00653   }
00654 
00655   std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
00656   if(vs.size() == 1) 
00657     aFatherPath = "/";
00658   else {
00659     for(int i = 0, len = vs.size()-1; i<len; i++) { 
00660       aFatherPath += "/";
00661       aFatherPath += vs[i];
00662     }
00663   }
00664 
00665   try { 
00666     anObject = _study->FindObjectByPath(aFatherPath); //Check if the father directory exists
00667   }
00668   catch(...) { ; }
00669   if(!anObject) {
00670     _errorCode = "StudyInvalidDirectory";
00671     return false; 
00672   }
00673 
00674   SALOMEDSImpl_SObject aNewObject = NewObject(anObject);
00675   aLabel = aNewObject.GetLabel();
00676   if(aLabel.IsNull()) {
00677     _errorCode = "StudyInvalidComponent";
00678     return false;
00679   }
00680 
00681   SALOMEDSImpl_AttributeName::Set(aLabel, vs.back());
00682 
00683   //Set LocalID attribute to identify the directory object
00684   SALOMEDSImpl_AttributeLocalID::Set(aLabel, DIRECTORYID);
00685   
00686   _doc->SetModified(true); 
00687   
00688   return true;
00689 }
00690 
00691 
00692 //============================================================================
00696 //============================================================================
00697 bool SALOMEDSImpl_StudyBuilder::SetGUID(const SALOMEDSImpl_SObject& anObject, 
00698                                         const std::string& theGUID)
00699 {
00700   _errorCode = "";
00701   CheckLocked();
00702   if(!anObject) {
00703     _errorCode = "Invalid arguments";
00704     return false;
00705   }
00706 
00707   DF_Label aLabel = anObject.GetLabel();
00708   SALOMEDSImpl_AttributeUserID::Set(aLabel, theGUID);
00709 
00710   _doc->SetModified(true);  
00711 
00712   return true;
00713 }
00714 
00715 //============================================================================
00719 //============================================================================
00720 bool SALOMEDSImpl_StudyBuilder::IsGUID(const SALOMEDSImpl_SObject& anObject, 
00721                                        const std::string& theGUID)
00722 {
00723   _errorCode = "";
00724   if(!anObject) {
00725     _errorCode = "Invalid arguments";
00726     return false;
00727   }
00728   DF_Label aLabel = anObject.GetLabel();
00729   return aLabel.IsAttribute(theGUID);
00730 }
00731 
00732 
00733 //============================================================================
00737 //============================================================================
00738 void SALOMEDSImpl_StudyBuilder::NewCommand()
00739 {
00740   _errorCode = "";
00741 
00742   // mpv: for SAL2114 - unset "lock changed" flag at the operation start
00743   _study->GetProperties()->IsLockChanged(true);
00744 
00745   //Not implemented
00746 }
00747 
00748 //============================================================================
00752 //============================================================================
00753 void SALOMEDSImpl_StudyBuilder::CommitCommand()
00754 {
00755   _errorCode = "";
00756   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
00757   if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
00758     _errorCode = "LockProtection";
00759     throw LockProtection("LockProtection");
00760   } else {
00761     int aModif = anAttr->GetModified();
00762     if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
00763     anAttr->SetModified(aModif+1);
00764   }
00765   
00766 
00767   //Not implemented
00768   _doc->SetModified(true);  
00769 }
00770 
00771 //============================================================================
00775 //============================================================================
00776 bool SALOMEDSImpl_StudyBuilder::HasOpenCommand()
00777 {
00778   _errorCode = "";
00779 
00780   //Not implememnted
00781   return false;
00782 }
00783 
00784 //============================================================================
00788 //============================================================================
00789 void SALOMEDSImpl_StudyBuilder::AbortCommand()
00790 {
00791   _errorCode = "";
00792   //Not implemented    
00793 }
00794 
00795 //============================================================================
00799 //============================================================================
00800 void SALOMEDSImpl_StudyBuilder::Undo()
00801 {
00802   //Not implemented
00803   _errorCode = "";
00804   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
00805   if (anAttr->IsLocked()) {
00806     _errorCode = "LockProtection";
00807     throw LockProtection("LockProtection");
00808   } else {
00809     anAttr->SetModified(anAttr->GetModified()-1);
00810   }
00811 
00812   _doc->SetModified(true);  
00813 }
00814 
00815 //============================================================================
00819 //============================================================================
00820 void SALOMEDSImpl_StudyBuilder::Redo() 
00821 {
00822   _errorCode = "";
00823   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
00824   if (anAttr->IsLocked()) {
00825     _errorCode = "LockProtection";
00826     throw LockProtection("LockProtection");
00827   } else {
00828     anAttr->SetModified(anAttr->GetModified()+1);
00829   }
00830 
00831   //Not implemented
00832 
00833   _doc->SetModified(true);  
00834 }
00835 
00836 //============================================================================
00840 //============================================================================
00841 bool SALOMEDSImpl_StudyBuilder::GetAvailableUndos()
00842 {
00843   _errorCode = "";
00844   return false;
00845 }
00846 
00847 //============================================================================
00851 //============================================================================
00852 bool  SALOMEDSImpl_StudyBuilder::GetAvailableRedos()
00853 {
00854   _errorCode = "";
00855   return false;
00856 }
00857 
00858 //============================================================================
00862 //============================================================================
00863 int  SALOMEDSImpl_StudyBuilder::UndoLimit()
00864 {
00865   _errorCode = "";
00866   return 1;
00867 }
00868 
00869 //============================================================================
00873 //============================================================================
00874 void SALOMEDSImpl_StudyBuilder::UndoLimit(int n)
00875 {
00876   _errorCode = "";
00877   CheckLocked();
00878   //Not implemented
00879 }
00880 
00881 //============================================================================
00885 //============================================================================
00886 SALOMEDSImpl_Callback*
00887 SALOMEDSImpl_StudyBuilder::SetOnAddSObject(const SALOMEDSImpl_Callback* theCallback)
00888 {
00889   _errorCode = "";
00890   SALOMEDSImpl_Callback* aRet = _callbackOnAdd;
00891   _callbackOnAdd = (SALOMEDSImpl_Callback*)theCallback;
00892   return aRet;
00893 }
00894 
00895 //============================================================================
00899 //============================================================================
00900 SALOMEDSImpl_Callback* 
00901 SALOMEDSImpl_StudyBuilder::SetOnRemoveSObject(const SALOMEDSImpl_Callback* theCallback)
00902 {
00903   _errorCode = "";
00904   SALOMEDSImpl_Callback* aRet = _callbackOnRemove;
00905   _callbackOnRemove = (SALOMEDSImpl_Callback*)theCallback;
00906   return aRet;
00907 }
00908 
00909 //============================================================================
00913 //============================================================================
00914 void SALOMEDSImpl_StudyBuilder::CheckLocked()
00915 {
00916   _errorCode = "";
00917   if (HasOpenCommand()) return;
00918   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
00919   if (anAttr->IsLocked()) {
00920     _errorCode = "LockProtection";
00921     throw LockProtection("LockProtection");
00922   }
00923 }
00924 
00925 //============================================================================
00929 //============================================================================
00930 bool SALOMEDSImpl_StudyBuilder::SetName(const SALOMEDSImpl_SObject& theSO, 
00931                                         const std::string& theValue)
00932 {
00933   _errorCode = "";
00934   CheckLocked();
00935   if(!theSO) {
00936     _errorCode = "Invalid arguments";
00937     return false;
00938   }
00939   SALOMEDSImpl_AttributeName::Set(theSO.GetLabel(), theValue);
00940 
00941   _doc->SetModified(true);  
00942 
00943   return true;
00944 }
00945 
00946 //============================================================================
00950 //============================================================================
00951 bool SALOMEDSImpl_StudyBuilder::SetComment(const SALOMEDSImpl_SObject& theSO, 
00952                                            const std::string& theValue)
00953 {
00954   _errorCode = "";
00955   CheckLocked();
00956   if(!theSO) {
00957     _errorCode = "Invalid arguments";
00958     return false;
00959   }
00960   SALOMEDSImpl_AttributeComment::Set(theSO.GetLabel(), theValue);
00961 
00962   _doc->SetModified(true);  
00963 
00964   return true;
00965 }
00966 
00967 //============================================================================
00971 //============================================================================
00972 bool SALOMEDSImpl_StudyBuilder::SetIOR(const SALOMEDSImpl_SObject& theSO, 
00973                                        const std::string& theValue)
00974 {
00975   _errorCode = "";
00976   CheckLocked();
00977   if(!theSO) {
00978     _errorCode = "Invalid arguments";
00979     return false;
00980   }
00981   SALOMEDSImpl_AttributeIOR::Set(theSO.GetLabel(), theValue);
00982 
00983   _doc->SetModified(true);  
00984 
00985   return true;
00986 }
00987 
00988 
00989 //============================================================================
00993 //============================================================================
00994 static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII)
00995 {
00996   if(driver == NULL) return;
00997   DF_ChildIterator  itchild (Lab);
00998   
00999   for (; itchild.More(); itchild.Next()) {
01000     DF_Label current = itchild.Value();
01001     SALOMEDSImpl_AttributePersistentRef* Att = NULL;
01002     if ((Att=(SALOMEDSImpl_AttributePersistentRef*)current.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {  
01003 
01004       SALOMEDSImpl_AttributeLocalID* anID = NULL;
01005       if ((anID=(SALOMEDSImpl_AttributeLocalID*)current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) 
01006         if (anID->Value() == FILELOCALID) continue;   //SRN: This attribute store a file name, skip it 
01007 
01008       std::string persist_ref = Att->Value();
01009       SALOMEDSImpl_SObject so = SALOMEDSImpl_Study::SObject(current);
01010       std::string ior_string = driver->LocalPersistentIDToIOR(so, 
01011                                                          persist_ref, 
01012                                                          isMultiFile, 
01013                                                          isASCII);
01014       SALOMEDSImpl_AttributeIOR::Set (current, ior_string); 
01015      
01016     }
01017     Translate_persistentID_to_IOR (current, driver, isMultiFile, isASCII);
01018   }
01019 }
01020