]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_StudyBuilder.cxx
Salome HOME
Issue 0020472: An error in SALOMEDSImpl_Tool::Exists
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_StudyBuilder.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_StudyBuilder.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "SALOMEDSImpl_Attributes.hxx"
27 #include "SALOMEDSImpl_Study.hxx"
28 #include "SALOMEDSImpl_StudyBuilder.hxx"
29 #include "SALOMEDSImpl_SObject.hxx"
30 #include "SALOMEDSImpl_SComponent.hxx"
31 #include "SALOMEDSImpl_Tool.hxx"
32 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
33
34 #include "DF_ChildIterator.hxx"
35 #include "DF_Label.hxx"
36
37 #include <HDFOI.hxx>
38 #include <stdlib.h> 
39 #include <string.h> 
40
41 using namespace std;
42
43 #define USE_CASE_LABEL_TAG            2
44 #define DIRECTORYID                   16661
45 #define FILELOCALID                   26662 
46
47 static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII);
48
49 //============================================================================
50 /*! Function : constructor
51  *  Purpose  :
52  */
53 //============================================================================
54 SALOMEDSImpl_StudyBuilder::SALOMEDSImpl_StudyBuilder(const SALOMEDSImpl_Study* theOwner)
55 {
56    _errorCode = "";
57   _callbackOnAdd=NULL;
58   _callbackOnRemove = NULL;
59    _study = (SALOMEDSImpl_Study*)theOwner;
60    _doc = _study->GetDocument();
61 }
62
63 //============================================================================
64 /*! Function : destructor
65  *  Purpose  :
66  */
67 //============================================================================
68 SALOMEDSImpl_StudyBuilder::~SALOMEDSImpl_StudyBuilder()
69 {}
70
71 //============================================================================
72 /*! Function : NewComponent
73  *  Purpose  : Create a new component (Scomponent)
74  */
75 //============================================================================
76 SALOMEDSImpl_SComponent SALOMEDSImpl_StudyBuilder::NewComponent(const string& DataType)
77 {
78   _errorCode = "";
79   CheckLocked();
80
81   SALOMEDSImpl_SComponent sco;
82
83   if(DataType.size() == 0) return sco;
84
85   //Always create component under main label.
86   DF_Label L  = _doc->Main();
87
88   DF_Label NL = L.NewChild();
89
90   SALOMEDSImpl_AttributeComment::Set(NL, DataType);
91
92   SALOMEDSImpl_SComponent so =  _study->GetSComponent (NL);
93
94   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
95
96   _doc->SetModified(true);
97
98   return so;
99 }
100
101 //============================================================================
102 /*! Function : DefineComponentInstance
103  *  Purpose  : Add IOR attribute of a Scomponent
104  */
105 //============================================================================
106 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const SALOMEDSImpl_SComponent& aComponent,
107                                                         const string& IOR)
108 {
109    _errorCode = "";
110
111   CheckLocked();
112   if(!aComponent || IOR.empty()) {
113     _errorCode = "Invalid arguments";
114     return false;
115   }
116   //add IOR definition 
117   SALOMEDSImpl_AttributeIOR::Set(aComponent.GetLabel(), IOR);  
118
119   return true;
120 }
121
122 //============================================================================
123 /*! Function : RemoveComponent
124  *  Purpose  : Delete a Scomponent
125  */
126 //============================================================================
127 bool SALOMEDSImpl_StudyBuilder::RemoveComponent(const SALOMEDSImpl_SComponent& aComponent)
128 {
129    _errorCode = "";
130   CheckLocked();
131   return RemoveObject(aComponent);
132 }
133
134 //============================================================================
135 /*! Function : NewObject
136  *  Purpose  : Create a new SObject
137  */
138 //============================================================================
139 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObject(const SALOMEDSImpl_SObject& theFatherObject)
140 {
141   _errorCode = "";
142   CheckLocked();
143
144   //Find label of father
145   DF_Label Lab = theFatherObject.GetLabel();
146   
147   //Create a new label
148   DF_Label NewLab = Lab.NewChild();
149   
150   SALOMEDSImpl_SObject so = _study->GetSObject(NewLab);
151   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
152
153   _doc->SetModified(true);  
154   return so;
155 }
156
157 //============================================================================
158 /*! Function : NewObjectToTag
159  *  Purpose  :
160  */
161 //============================================================================
162 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObjectToTag(const SALOMEDSImpl_SObject& theFatherObject,
163                                                         const int theTag)
164 {
165   _errorCode = "";
166   CheckLocked();
167   //Find label of father
168   DF_Label Lab = theFatherObject.GetLabel();
169
170   //Create or find label
171   DF_Label NewLab = Lab.FindChild(theTag, 1);
172
173   SALOMEDSImpl_SObject so = _study->GetSObject(NewLab);
174
175   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
176
177   _doc->SetModified(true);  
178   return so;
179 }
180
181 //============================================================================
182 /*! Function : RemoveObject
183  *  Purpose  :
184  */
185 //============================================================================
186 bool SALOMEDSImpl_StudyBuilder::RemoveObject(const SALOMEDSImpl_SObject& anObject)
187 {
188    _errorCode = "";
189   CheckLocked();
190   if(!anObject) {
191     _errorCode = "Null object";
192     return false;
193   }
194
195   if(_callbackOnRemove) _callbackOnRemove->OnRemoveSObject(anObject);
196
197   DF_Label Lab = anObject.GetLabel();
198
199   SALOMEDSImpl_AttributeReference* aReference = NULL;
200   if ((aReference=(SALOMEDSImpl_AttributeReference*)Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
201     SALOMEDSImpl_AttributeTarget* aTarget = NULL;
202     if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
203       aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
204   }
205
206   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
207   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
208   }
209
210   Lab.ForgetAllAttributes();
211  
212   _doc->SetModified(true);  
213     
214   return true;
215 }
216
217 //============================================================================
218 /*! Function : RemoveObjectWithChildren
219  *  Purpose  :
220  */
221 //============================================================================
222 bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObject& anObject)
223 {
224    _errorCode = "";
225   CheckLocked();
226   if(!anObject) {
227     _errorCode = "Null object";
228     return false;
229   }
230
231   if(_callbackOnRemove) _callbackOnRemove->OnRemoveSObject(anObject);
232
233   DF_Label Lab = anObject.GetLabel();
234
235   SALOMEDSImpl_AttributeReference* aReference = NULL;
236   if ((aReference=(SALOMEDSImpl_AttributeReference*)Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
237     SALOMEDSImpl_AttributeTarget* aTarget = NULL;
238     if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
239       aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
240   }
241   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
242   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
243   }
244
245   DF_ChildIterator it(Lab, true);
246   for(;it.More();it.Next()) {
247     DF_Label aLabel = it.Value();
248     if ((aReference=(SALOMEDSImpl_AttributeReference*)aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
249       SALOMEDSImpl_AttributeTarget* aTarget = NULL;
250       if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
251         aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
252     }
253     SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
254     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
255     }
256   }
257
258   Lab.ForgetAllAttributes(true);
259
260   _doc->SetModified(true);  
261   
262   return true;
263 }
264
265 //============================================================================
266 /*! Function : LoadWith
267  *  Purpose  : 
268  */
269 //============================================================================
270 bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
271                                          SALOMEDSImpl_Driver* aDriver) 
272 {
273   _errorCode = "";
274
275   DF_Label Lab = anSCO.GetLabel();
276   SALOMEDSImpl_AttributePersistentRef* Att = NULL;
277
278   //Find the current Url of the study  
279   if ((Att=(SALOMEDSImpl_AttributePersistentRef*)_doc->Main().FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
280     int aLocked = _study->GetProperties()->IsLocked();
281     if (aLocked) _study->GetProperties()->SetLocked(false);
282
283     std::string Res(Att->Value());
284     string aHDFPath(Res);
285
286     SALOMEDSImpl_AttributeComment* type = NULL;
287     std::string DataType;
288     if ((type=(SALOMEDSImpl_AttributeComment*)Lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID())))
289       DataType = type->Value();
290
291     // associate the driver to the SComponent
292     if(aDriver == NULL) {
293       _errorCode = "Driver is null";
294       return false;
295     }
296
297     // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
298     SALOMEDSImpl_AttributeIOR* attrIOR = NULL;
299     if ((attrIOR=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
300       if (aLocked) _study->GetProperties()->SetLocked(true);
301       return true;
302     }
303
304     DefineComponentInstance (anSCO, aDriver->GetIOR());
305
306     string aHDFUrl;
307     bool isASCII = false;
308     if (HDFascii::isASCII(aHDFPath.c_str())) {
309       isASCII = true;
310       aHDFUrl = HDFascii::ConvertFromASCIIToHDF(aHDFPath.c_str());
311       aHDFUrl += "hdf_from_ascii.hdf";
312     } else {
313       aHDFUrl = aHDFPath;
314     }
315
316     //Open the Study HDF file 
317     HDFfile *hdf_file = new HDFfile((char*)aHDFUrl.c_str()); 
318
319     char aMultifileState[2];
320     char ASCIIfileState[2];
321     try {
322       string scoid = anSCO.GetID();
323       hdf_file->OpenOnDisk(HDF_RDONLY);
324       HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
325       hdf_group->OpenOnDisk();
326       HDFgroup *hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group);
327       hdf_sco_group->OpenOnDisk();
328
329       unsigned char* aStreamFile = NULL;
330       int aStreamSize = 0;
331
332       if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
333         HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
334         hdf_dataset->OpenOnDisk();
335         aStreamSize = hdf_dataset->GetSize();
336         aStreamFile  = new unsigned char[aStreamSize];
337         if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
338         hdf_dataset->ReadFromDisk(aStreamFile);
339         hdf_dataset->CloseOnDisk();
340         hdf_dataset = 0;
341       } else
342         aStreamFile = NULL;
343
344       HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
345       multifile_hdf_dataset->OpenOnDisk();
346       multifile_hdf_dataset->ReadFromDisk(aMultifileState);
347
348       HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
349       ascii_hdf_dataset->OpenOnDisk();
350       ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
351
352       string aDir = SALOMEDSImpl_Tool::GetDirFromPath(Res);
353
354       bool aResult = (ASCIIfileState[0]=='A')?
355         aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M'):
356         aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M');
357
358       if(aStreamFile != NULL) delete []aStreamFile; 
359
360       if(!aResult) {
361         RemoveAttribute( anSCO, "AttributeIOR" );
362
363         _errorCode = "Can't load component";
364         throw HDFexception("Unable to load component");
365       }
366
367       //if(aDir != NULL) delete []aDir;
368
369       multifile_hdf_dataset->CloseOnDisk();
370       multifile_hdf_dataset = 0;
371       ascii_hdf_dataset->CloseOnDisk();
372       ascii_hdf_dataset = 0;
373
374       hdf_sco_group->CloseOnDisk();
375       hdf_sco_group = 0;
376       hdf_group->CloseOnDisk();
377       hdf_group = 0;
378       hdf_file->CloseOnDisk();
379       delete hdf_file;
380
381       if (isASCII) {
382         vector<string> aFilesToRemove;
383         aFilesToRemove.push_back("hdf_from_ascii.hdf");
384         SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
385                                                 aFilesToRemove, true);
386       }      
387     }
388     catch (HDFexception) {
389       delete hdf_file;
390
391       if (isASCII) {
392         vector<string> aFilesToRemove;
393         aFilesToRemove.push_back(aHDFUrl);
394         SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
395       }
396
397       if (aLocked) _study->GetProperties()->SetLocked(true);
398       _errorCode = "No persistent file";   
399       return false;
400     }
401
402     try {
403       Translate_persistentID_to_IOR (Lab, aDriver, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
404     } catch(...) {
405       _errorCode = "Can not convert persistent IDs to IORs";
406       return false;
407     }
408
409     if (aLocked) _study->GetProperties()->SetLocked(true);
410   } else {
411     _errorCode = "No persistent file";   
412   }
413
414   return true;
415 }
416
417
418 //============================================================================
419 /*! Function : Load
420  *  Purpose  : 
421  */
422 //============================================================================
423 bool SALOMEDSImpl_StudyBuilder::Load(const SALOMEDSImpl_SObject& sco)
424 {
425   _errorCode = "Not implemented";
426   return false;
427 }
428
429 //============================================================================
430 /*! Function : FindOrCreateAttribute
431  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
432  *  existing one
433  */
434 //============================================================================
435 DF_Attribute* SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const SALOMEDSImpl_SObject& anObject, 
436                                                                const string& aTypeOfAttribute)
437 {
438   _errorCode = "";
439   if(!anObject) {
440     _errorCode = "Invalid arguments";
441     return NULL;
442   }
443
444   DF_Label Lab = anObject.GetLabel();
445   if(Lab.IsNull()) {
446     _errorCode = "Null label";
447     return NULL;
448   }
449
450   _doc->SetModified(true);  
451
452   //The macro adds all necessary checks for standardly behaiving attributes
453   __FindOrCreateAttributeForBuilder
454
455  
456   //Add checks for TreeNode and UserID attributes  
457   if (strncmp(aTypeOfAttribute.c_str(), "AttributeTreeNode",17) == 0 ) {
458     
459     string aTreeNodeGUID;
460     if (strcmp(aTypeOfAttribute.c_str(), "AttributeTreeNode") == 0) {
461       aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
462     } else {
463       aTreeNodeGUID = aTypeOfAttribute.substr(21, aTypeOfAttribute.size()); // create tree node GUID by name
464     }
465     SALOMEDSImpl_AttributeTreeNode* anAttr = NULL;
466     if (!(anAttr=(SALOMEDSImpl_AttributeTreeNode*)Lab.FindAttribute(aTreeNodeGUID))) {
467       CheckLocked();
468       anAttr = SALOMEDSImpl_AttributeTreeNode::Set(Lab, aTreeNodeGUID);
469     }
470     return anAttr;
471   }
472
473   if (strncmp(aTypeOfAttribute.c_str(), "AttributeUserID",15) == 0 ) {
474     std::string aUserGUID;
475     if (strcmp(aTypeOfAttribute.c_str(), "AttributeUserID") == 0) {
476       aUserGUID = SALOMEDSImpl_AttributeUserID::DefaultID();
477     } else {
478       aUserGUID = aTypeOfAttribute.substr(15, aTypeOfAttribute.size()); // create tree node GUID by name
479     }
480     SALOMEDSImpl_AttributeUserID* anAttr = NULL;
481     if (!(anAttr=(SALOMEDSImpl_AttributeUserID*)Lab.FindAttribute(aUserGUID))) {
482       CheckLocked();
483       anAttr = SALOMEDSImpl_AttributeUserID::Set(Lab, aUserGUID);
484     }
485     return anAttr;
486   }
487   _errorCode = "Can not create an attribute";
488
489   return NULL;
490 }
491
492 //============================================================================
493 /*! Function : FindAttribute
494  *  Purpose  : Find attribute of given type assigned SObject, returns Standard_True if it is found
495  */
496 //============================================================================
497
498 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const SALOMEDSImpl_SObject& anObject, 
499                                               DF_Attribute*& anAttribute, 
500                                               const string& aTypeOfAttribute)
501 {
502   _errorCode = "";
503   if(!anObject) {
504     _errorCode = "Invalid arguments";
505     return false;
506   }
507   DF_Label Lab = anObject.GetLabel();
508   if ((anAttribute=Lab.FindAttribute(SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute)))) {
509     // commented out because NO MODIFICATION is done to attributes when calling FindAttribute()
510     // _doc->Modify();  
511     return true;
512   }
513   return false;
514 }
515
516 //============================================================================
517 /*! Function : RemoveAttribute
518  *  Purpose  : Remove attribute of given type assigned SObject
519  */
520 //============================================================================
521
522 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anObject, 
523                                                 const string& aTypeOfAttribute)
524 {
525   _errorCode = "";
526   CheckLocked();
527   if(!anObject) {
528     _errorCode = "Invalid arguments";
529     return false;
530   }
531   DF_Label Lab = anObject.GetLabel();
532   
533   if (aTypeOfAttribute == string("AttributeIOR")) { // Remove from IORLabel map
534     SALOMEDSImpl_AttributeIOR* anAttr = NULL;
535     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
536     }
537   }
538
539   Lab.ForgetAttribute (SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute));
540     
541   _doc->SetModified(true);  
542     
543   return true;
544 }
545
546 //============================================================================
547 /*! Function : Addreference
548  *  Purpose  : 
549  */
550 //============================================================================
551 bool SALOMEDSImpl_StudyBuilder::Addreference(const SALOMEDSImpl_SObject& me, 
552                                              const SALOMEDSImpl_SObject& theReferencedObject)
553 {
554   _errorCode = "";
555   if(!me || !theReferencedObject) {
556    _errorCode = "Invalid arguments";
557    return false;
558   }
559   CheckLocked();
560   DF_Label Lab = me.GetLabel();
561   DF_Label RefLab = theReferencedObject.GetLabel();
562   SALOMEDSImpl_AttributeReference::Set(Lab,RefLab);
563
564   SALOMEDSImpl_AttributeTarget::Set(RefLab)->Add(SALOMEDSImpl_Study::SObject(Lab));
565
566   if(_callbackOnRemove && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
567   
568   return true;
569 }
570
571 //============================================================================
572 /*! Function : RemoveReference
573  *  Purpose  : 
574  */
575 //============================================================================
576 bool SALOMEDSImpl_StudyBuilder::RemoveReference(const SALOMEDSImpl_SObject& me)
577 {
578   _errorCode = "";
579   SALOMEDSImpl_SObject theReferencedObject;
580   
581   if(!me.ReferencedObject(theReferencedObject)) return false;  //No reference is found
582   
583   CheckLocked();
584   DF_Label Lab = me.GetLabel();
585
586   //SRN: 30 Aug, 2004 : fix from Ecole l'ete version 
587
588   DF_Label RefLab = theReferencedObject.GetLabel();
589        
590   SALOMEDSImpl_AttributeTarget* aTarget = NULL;
591   if((aTarget=(SALOMEDSImpl_AttributeTarget*)RefLab.FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
592     aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
593   }
594   
595   Lab.ForgetAttribute(SALOMEDSImpl_AttributeReference::GetID());  
596   
597   _doc->SetModified(true);  
598   
599   return true;
600 }
601
602
603
604 //============================================================================
605 /*! Function : AddDirectory
606  *  Purpose  : adds a new directory with a path = thePath
607  */
608 //============================================================================
609 bool SALOMEDSImpl_StudyBuilder::AddDirectory(const string& thePath) 
610 {
611   _errorCode = "";
612   CheckLocked();
613   if(thePath.empty()) {
614     _errorCode = "Invalid path";
615     return false;
616   }
617
618   string aPath(thePath), aContext(""), aFatherPath;
619   DF_Label aLabel;
620   SALOMEDSImpl_SObject anObject;
621
622   try { 
623     anObject = _study->FindObjectByPath(thePath); //Check if the directory already exists
624   }
625   catch(...) { }
626
627   if(anObject) {
628     _errorCode = "StudyNameAlreadyUsed";
629     return false; 
630   }
631
632   if(aPath[0] != '/') { //Relative path 
633     aPath.insert(aPath.begin(), '/');
634     aPath = _study->GetContext() + aPath;
635   }
636
637   vector<string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
638   if(vs.size() == 1) 
639     aFatherPath = "/";
640   else {
641     for(int i = 0, len = vs.size()-1; i<len; i++) { 
642       aFatherPath += "/";
643       aFatherPath += vs[i];
644     }
645   }
646
647   try { 
648     anObject = _study->FindObjectByPath(aFatherPath); //Check if the father directory exists
649   }
650   catch(...) { ; }
651   if(!anObject) {
652     _errorCode = "StudyInvalidDirectory";
653     return false; 
654   }
655
656   SALOMEDSImpl_SObject aNewObject = NewObject(anObject);
657   aLabel = aNewObject.GetLabel();
658   if(aLabel.IsNull()) {
659     _errorCode = "StudyInvalidComponent";
660     return false;
661   }
662
663   SALOMEDSImpl_AttributeName::Set(aLabel, vs.back());
664
665   //Set LocalID attribute to identify the directory object
666   SALOMEDSImpl_AttributeLocalID::Set(aLabel, DIRECTORYID);
667   
668   _doc->SetModified(true); 
669   
670   return true;
671 }
672
673
674 //============================================================================
675 /*! Function : SetGUID
676  *  Purpose  : 
677  */
678 //============================================================================
679 bool SALOMEDSImpl_StudyBuilder::SetGUID(const SALOMEDSImpl_SObject& anObject, 
680                                         const string& theGUID)
681 {
682   _errorCode = "";
683   CheckLocked();
684   if(!anObject) {
685     _errorCode = "Invalid arguments";
686     return false;
687   }
688
689   DF_Label aLabel = anObject.GetLabel();
690   SALOMEDSImpl_AttributeUserID::Set(aLabel, theGUID);
691
692   _doc->SetModified(true);  
693
694   return true;
695 }
696
697 //============================================================================
698 /*! Function : IsGUID
699  *  Purpose  : 
700  */
701 //============================================================================
702 bool SALOMEDSImpl_StudyBuilder::IsGUID(const SALOMEDSImpl_SObject& anObject, 
703                                        const string& theGUID)
704 {
705   _errorCode = "";
706   if(!anObject) {
707     _errorCode = "Invalid arguments";
708     return false;
709   }
710   DF_Label aLabel = anObject.GetLabel();
711   return aLabel.IsAttribute(theGUID);
712 }
713
714
715 //============================================================================
716 /*! Function : NewCommand
717  *  Purpose  : 
718  */
719 //============================================================================
720 void SALOMEDSImpl_StudyBuilder::NewCommand()
721 {
722   _errorCode = "";
723
724   // mpv: for SAL2114 - unset "lock changed" flag at the operation start
725   _study->GetProperties()->IsLockChanged(true);
726
727   //Not implemented
728 }
729
730 //============================================================================
731 /*! Function : CommitCommand
732  *  Purpose  : 
733  */
734 //============================================================================
735 void SALOMEDSImpl_StudyBuilder::CommitCommand()
736 {
737   _errorCode = "";
738   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
739   if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
740     _errorCode = "LockProtection";
741     throw LockProtection("LockProtection");
742   } else {
743     int aModif = anAttr->GetModified();
744     if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
745     anAttr->SetModified(aModif+1);
746   }
747   
748
749   //Not implemented
750   _doc->SetModified(true);  
751 }
752
753 //============================================================================
754 /*! Function : HasOpenCommand
755  *  Purpose  : 
756  */
757 //============================================================================
758 bool SALOMEDSImpl_StudyBuilder::HasOpenCommand()
759 {
760   _errorCode = "";
761
762   //Not implememnted
763   return false;
764 }
765
766 //============================================================================
767 /*! Function : AbortCommand
768  *  Purpose  : 
769  */
770 //============================================================================
771 void SALOMEDSImpl_StudyBuilder::AbortCommand()
772 {
773   _errorCode = "";
774   //Not implemented    
775 }
776
777 //============================================================================
778 /*! Function : Undo
779  *  Purpose  : 
780  */
781 //============================================================================
782 void SALOMEDSImpl_StudyBuilder::Undo()
783 {
784   //Not implemented
785   _errorCode = "";
786   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
787   if (anAttr->IsLocked()) {
788     _errorCode = "LockProtection";
789     throw LockProtection("LockProtection");
790   } else {
791     anAttr->SetModified(anAttr->GetModified()-1);
792   }
793
794   _doc->SetModified(true);  
795 }
796
797 //============================================================================
798 /*! Function : Redo
799  *  Purpose  : 
800  */
801 //============================================================================
802 void SALOMEDSImpl_StudyBuilder::Redo() 
803 {
804   _errorCode = "";
805   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
806   if (anAttr->IsLocked()) {
807     _errorCode = "LockProtection";
808     throw LockProtection("LockProtection");
809   } else {
810     anAttr->SetModified(anAttr->GetModified()+1);
811   }
812
813   //Not implemented
814
815   _doc->SetModified(true);  
816 }
817
818 //============================================================================
819 /*! Function : GetAvailableUndos
820  *  Purpose  : 
821  */
822 //============================================================================
823 bool SALOMEDSImpl_StudyBuilder::GetAvailableUndos()
824 {
825   _errorCode = "";
826   return false;
827 }
828
829 //============================================================================
830 /*! Function : GetAvailableRedos
831  *  Purpose  : 
832  */
833 //============================================================================
834 bool  SALOMEDSImpl_StudyBuilder::GetAvailableRedos()
835 {
836   _errorCode = "";
837   return false;
838 }
839
840 //============================================================================
841 /*! Function : UndoLimit
842  *  Purpose  : 
843  */
844 //============================================================================
845 int  SALOMEDSImpl_StudyBuilder::UndoLimit()
846 {
847   _errorCode = "";
848   return 1;
849 }
850
851 //============================================================================
852 /*! Function : UndoLimit
853  *  Purpose  : 
854  */
855 //============================================================================
856 void SALOMEDSImpl_StudyBuilder::UndoLimit(int n)
857 {
858   _errorCode = "";
859   CheckLocked();
860   //Not implemented
861 }
862
863 //============================================================================
864 /*! Function : SetOnAddSObject
865  *  Purpose  : 
866  */
867 //============================================================================
868 SALOMEDSImpl_Callback*
869 SALOMEDSImpl_StudyBuilder::SetOnAddSObject(const SALOMEDSImpl_Callback* theCallback)
870 {
871   _errorCode = "";
872   SALOMEDSImpl_Callback* aRet = _callbackOnAdd;
873   _callbackOnAdd = (SALOMEDSImpl_Callback*)theCallback;
874   return aRet;
875 }
876
877 //============================================================================
878 /*! Function : SetOnNewSObject
879  *  Purpose  : 
880  */
881 //============================================================================
882 SALOMEDSImpl_Callback* 
883 SALOMEDSImpl_StudyBuilder::SetOnRemoveSObject(const SALOMEDSImpl_Callback* theCallback)
884 {
885   _errorCode = "";
886   SALOMEDSImpl_Callback* aRet = _callbackOnRemove;
887   _callbackOnRemove = (SALOMEDSImpl_Callback*)theCallback;
888   return aRet;
889 }
890
891 //============================================================================
892 /*! Function : CheckLocked
893  *  Purpose  : 
894  */
895 //============================================================================
896 void SALOMEDSImpl_StudyBuilder::CheckLocked()
897 {
898   _errorCode = "";
899   if (HasOpenCommand()) return;
900   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
901   if (anAttr->IsLocked()) {
902     _errorCode = "LockProtection";
903     throw LockProtection("LockProtection");
904   }
905 }
906
907 //============================================================================
908 /*! Function : SetName
909  *  Purpose  : 
910  */
911 //============================================================================
912 bool SALOMEDSImpl_StudyBuilder::SetName(const SALOMEDSImpl_SObject& theSO, 
913                                         const string& theValue)
914 {
915   _errorCode = "";
916   CheckLocked();
917   if(!theSO) {
918     _errorCode = "Invalid arguments";
919     return false;
920   }
921   SALOMEDSImpl_AttributeName::Set(theSO.GetLabel(), theValue);
922
923   _doc->SetModified(true);  
924
925   return true;
926 }
927
928 //============================================================================
929 /*! Function : SetComment
930  *  Purpose  : 
931  */
932 //============================================================================
933 bool SALOMEDSImpl_StudyBuilder::SetComment(const SALOMEDSImpl_SObject& theSO, 
934                                            const string& theValue)
935 {
936   _errorCode = "";
937   CheckLocked();
938   if(!theSO) {
939     _errorCode = "Invalid arguments";
940     return false;
941   }
942   SALOMEDSImpl_AttributeComment::Set(theSO.GetLabel(), theValue);
943
944   _doc->SetModified(true);  
945
946   return true;
947 }
948
949 //============================================================================
950 /*! Function : SetIOR
951  *  Purpose  : 
952  */
953 //============================================================================
954 bool SALOMEDSImpl_StudyBuilder::SetIOR(const SALOMEDSImpl_SObject& theSO, 
955                                        const string& theValue)
956 {
957   _errorCode = "";
958   CheckLocked();
959   if(!theSO) {
960     _errorCode = "Invalid arguments";
961     return false;
962   }
963   SALOMEDSImpl_AttributeIOR::Set(theSO.GetLabel(), theValue);
964
965   _doc->SetModified(true);  
966
967   return true;
968 }
969
970
971 //============================================================================
972 /*! Function : Translate_persistentID_to_IOR
973  *  Purpose  :
974  */
975 //============================================================================
976 static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII)
977 {
978   if(driver == NULL) return;
979   DF_ChildIterator  itchild (Lab);
980   
981   for (; itchild.More(); itchild.Next()) {
982     DF_Label current = itchild.Value();
983     SALOMEDSImpl_AttributePersistentRef* Att = NULL;
984     if ((Att=(SALOMEDSImpl_AttributePersistentRef*)current.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {  
985
986       SALOMEDSImpl_AttributeLocalID* anID = NULL;
987       if ((anID=(SALOMEDSImpl_AttributeLocalID*)current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) 
988         if (anID->Value() == FILELOCALID) continue;   //SRN: This attribute store a file name, skip it 
989
990       string persist_ref = Att->Value();
991       SALOMEDSImpl_SObject so = SALOMEDSImpl_Study::SObject(current);
992       string ior_string = driver->LocalPersistentIDToIOR(so, 
993                                                          persist_ref, 
994                                                          isMultiFile, 
995                                                          isASCII);
996       SALOMEDSImpl_AttributeIOR::Set (current, ior_string); 
997      
998     }
999     Translate_persistentID_to_IOR (current, driver, isMultiFile, isASCII);
1000   }
1001 }
1002