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