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