6c326d9976671ee9f5c2e49eacb70aa7f366b45c
[occt.git] / src / CDM / CDM_Document.cxx
1 // Created on: 1997-07-30
2 // Created by: Jean-Louis Frenkel
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Modified by rmi, Tue Nov 18 08:17:41 1997
18
19
20 #include <CDM_Document.ixx>
21
22 #include <Standard_NullObject.hxx>
23
24 #include <TCollection_ExtendedString.hxx>
25 #include <Standard_ProgramError.hxx>
26
27 #include <CDM_PresentationDirectory.hxx>
28 #include <CDM_NamesDirectory.hxx>
29
30 #include <CDM_ListOfDocument.hxx>
31 #include <CDM_ListIteratorOfListOfDocument.hxx>
32
33 #include <UTL.hxx>
34 #include <CDM_DataMapIteratorOfMetaDataLookUpTable.hxx>
35
36 static CDM_PresentationDirectory& getPresentations() {
37   static CDM_PresentationDirectory thePresentations;
38   return thePresentations;
39 }
40
41 //=======================================================================
42 //function : CDM_Document
43 //purpose  : 
44 //=======================================================================
45
46 CDM_Document::CDM_Document():
47   myResourcesAreLoaded          (Standard_False),
48   myValidPresentation           (Standard_False),
49   myVersion                     (1),
50   myActualReferenceIdentifier   (0),
51   myStorageVersion              (0),
52   myRequestedComment            (""),
53   myRequestedFolderIsDefined    (Standard_False),
54   myRequestedNameIsDefined      (Standard_False),
55   myRequestedPreviousVersionIsDefined(Standard_False),
56   myFileExtensionWasFound       (Standard_False),
57   myDataTypeWasFound            (Standard_False),
58   myVersionDataTypeWasFound     (Standard_False),
59   myDescriptionWasFound         (Standard_False),
60   myDomainWasFound              (Standard_False),
61   myStoragePluginWasFound       (Standard_False),
62   myDefaultPresentationWasFound (Standard_False)
63 {}
64
65
66 //=======================================================================
67 //function : Destroy
68 //purpose  : 
69 //=======================================================================
70
71 void CDM_Document::Destroy() {
72   if(!myMetaData.IsNull()) myMetaData->UnsetDocument();
73 }
74
75 //=======================================================================
76 //function : Update
77 //purpose  : 
78 //=======================================================================
79
80 void CDM_Document::Update (const Handle(CDM_Document)& /*aToDocument*/,
81                            const Standard_Integer /*aReferenceIdentifier*/,
82                            const Standard_Address /*aModifContext*/)
83 {
84 }
85
86 //=======================================================================
87 //function : Update
88 //purpose  : 
89 //=======================================================================
90
91 void CDM_Document::Update()
92 {
93 }
94
95 //=======================================================================
96 //function : Update
97 //purpose  : 
98 //=======================================================================
99
100 Standard_Boolean CDM_Document::Update(TCollection_ExtendedString& ErrorString)
101 {
102   ErrorString.Clear();
103   Update();
104   return Standard_True;
105 }
106
107 //=======================================================================
108 //function : GetAlternativeDocument
109 //purpose  : 
110 //=======================================================================
111
112 Standard_Boolean CDM_Document::GetAlternativeDocument
113                                 (const TCollection_ExtendedString& aFormat,
114                                  Handle(CDM_Document)& anAlternativeDocument)
115 {
116   anAlternativeDocument = this;
117   return aFormat == StorageFormat();
118 }
119
120 //=======================================================================
121 //function : Extensions
122 //purpose  : 
123 //=======================================================================
124
125 void CDM_Document::Extensions (TColStd_SequenceOfExtendedString& Extensions) const
126 {
127   Extensions.Clear();
128 }
129
130 //=======================================================================
131 //function : CreateReference
132 //purpose  : 
133 //=======================================================================
134
135 Standard_Integer CDM_Document::CreateReference
136                                 (const Handle(CDM_Document)& anOtherDocument)
137 {
138   CDM_ListIteratorOfListOfReferences it(myToReferences);
139   
140   for(; it.More(); it.Next()) {
141     if(anOtherDocument == it.Value()->Document())
142       return it.Value()->ReferenceIdentifier();
143   }
144
145   Handle(CDM_Reference) r = new CDM_Reference (this,
146                                                anOtherDocument,
147                                                ++myActualReferenceIdentifier,
148                                                anOtherDocument->Modifications());
149   AddToReference(r);
150   anOtherDocument->AddFromReference(r);
151   return  r->ReferenceIdentifier();
152 }
153
154 //=======================================================================
155 //function : RemoveAllReferences
156 //purpose  : 
157 //=======================================================================
158
159 void CDM_Document::RemoveAllReferences()
160 {
161   CDM_ListIteratorOfListOfReferences it(myToReferences);
162
163   for(; it.More(); it.Next()) {
164     it.Value()->ToDocument()->RemoveFromReference(it.Value()->ReferenceIdentifier());
165   }
166   myToReferences.Clear();
167 }
168
169 //=======================================================================
170 //function : RemoveReference
171 //purpose  : 
172 //=======================================================================
173
174 void CDM_Document::RemoveReference(const Standard_Integer aReferenceIdentifier)
175 {
176   CDM_ListIteratorOfListOfReferences it(myToReferences);
177   
178   for(; it.More(); it.Next()) {
179     if(aReferenceIdentifier == it.Value()->ReferenceIdentifier()) {
180       it.Value()->ToDocument()->RemoveFromReference(aReferenceIdentifier);
181       myToReferences.Remove(it);
182       return;
183     }
184   }
185 }
186
187 //=======================================================================
188 //function : Document
189 //purpose  : 
190 //=======================================================================
191
192 Handle(CDM_Document) CDM_Document::Document
193                                 (const Standard_Integer aReferenceIdentifier) const
194 {
195   Handle(CDM_Document) theDocument;
196
197   if(aReferenceIdentifier == 0) 
198     theDocument = this;
199   
200   else {
201     Handle(CDM_Reference) theReference = Reference(aReferenceIdentifier);
202     if(!theReference.IsNull()) theDocument = theReference->ToDocument();
203   }
204   return theDocument;
205 }
206
207 //=======================================================================
208 //function : Reference
209 //purpose  : 
210 //=======================================================================
211
212 Handle(CDM_Reference) CDM_Document::Reference
213                                 (const Standard_Integer aReferenceIdentifier) const
214 {
215   Handle(CDM_Reference) theReference;
216
217   CDM_ListIteratorOfListOfReferences it(myToReferences);
218     
219   Standard_Boolean found = Standard_False;
220     
221   for(; it.More() && !found; it.Next()) {
222     found = aReferenceIdentifier == it.Value()->ReferenceIdentifier();
223     if(found) theReference = it.Value();
224   }
225   return theReference;
226 }
227
228 static CDM_ListOfDocument& getListOfDocumentToUpdate() {
229   static CDM_ListOfDocument theListOfDocumentToUpdate;
230   return theListOfDocumentToUpdate;
231 }
232
233 //=======================================================================
234 //function : IsInSession
235 //purpose  : 
236 //=======================================================================
237
238 Standard_Boolean CDM_Document::IsInSession
239                                 (const Standard_Integer aReferenceIdentifier) const
240 {
241   if(aReferenceIdentifier == 0) return Standard_True;
242   Handle(CDM_Reference) theReference=Reference(aReferenceIdentifier);
243   if(theReference.IsNull())
244     Standard_NoSuchObject::Raise("CDM_Document::IsInSession: "
245                                  "invalid reference identifier");
246   return theReference->IsInSession();
247 }
248
249 //=======================================================================
250 //function : IsStored
251 //purpose  : 
252 //=======================================================================
253
254 Standard_Boolean CDM_Document::IsStored
255                                 (const Standard_Integer aReferenceIdentifier) const
256 {
257   if(aReferenceIdentifier == 0) return IsStored();
258   Handle(CDM_Reference) theReference=Reference(aReferenceIdentifier);
259   if(theReference.IsNull())
260     Standard_NoSuchObject::Raise("CDM_Document::IsInSession: "
261                                  "invalid reference identifier");
262   return theReference->IsStored();
263 }
264
265 //=======================================================================
266 //function : Name
267 //purpose  : 
268 //=======================================================================
269
270 TCollection_ExtendedString CDM_Document::Name
271                                 (const Standard_Integer aReferenceIdentifier) const
272 {
273   if(!IsStored(aReferenceIdentifier))
274     Standard_DomainError::Raise("CDM_Document::Name: document is not stored");
275
276   if(aReferenceIdentifier == 0) return myMetaData->Name();
277
278   return Reference(aReferenceIdentifier)->MetaData()->Name();
279 }
280
281 //=======================================================================
282 //function : UpdateFromDocuments
283 //purpose  : 
284 //=======================================================================
285
286 void CDM_Document::UpdateFromDocuments(const Standard_Address aModifContext) const
287 {
288   Standard_Boolean StartUpdateCycle=getListOfDocumentToUpdate().IsEmpty();
289   
290   CDM_ListIteratorOfListOfReferences it(myFromReferences);
291   for(; it.More() ; it.Next()) {
292     Handle(CDM_Document) theFromDocument=it.Value()->FromDocument();
293     CDM_ListIteratorOfListOfDocument itUpdate;
294
295     for(; itUpdate.More(); itUpdate.Next()) {
296       if(itUpdate.Value() == theFromDocument) break;
297       
298       if(itUpdate.Value()->ShallowReferences(theFromDocument)) {
299         getListOfDocumentToUpdate().InsertBefore(theFromDocument,itUpdate);
300         break;
301       }
302     }
303     if(!itUpdate.More()) getListOfDocumentToUpdate().Append(theFromDocument);
304     theFromDocument->Update(this,it.Value()->ReferenceIdentifier(),aModifContext);
305   }
306   
307   if(StartUpdateCycle) {
308
309     Handle(CDM_Document) theDocumentToUpdate;
310     Handle(CDM_Application) theApplication;
311     TCollection_ExtendedString ErrorString;
312
313     while(!getListOfDocumentToUpdate().IsEmpty()) {
314       theDocumentToUpdate=getListOfDocumentToUpdate().First();
315       theApplication=theDocumentToUpdate->Application();
316       ErrorString.Clear();
317       theApplication->BeginOfUpdate(theDocumentToUpdate);
318       theApplication->EndOfUpdate (theDocumentToUpdate,
319                                    theDocumentToUpdate->Update(ErrorString),
320                                    ErrorString);
321       getListOfDocumentToUpdate().RemoveFirst();
322     }
323   }
324 }
325
326 //=======================================================================
327 //function : ToReferencesNumber
328 //purpose  : 
329 //=======================================================================
330
331 Standard_Integer CDM_Document::ToReferencesNumber() const
332 {
333   return myToReferences.Extent();
334 }
335
336 //=======================================================================
337 //function : FromReferencesNumber
338 //purpose  : 
339 //=======================================================================
340
341 Standard_Integer CDM_Document::FromReferencesNumber() const
342 {
343   return myFromReferences.Extent();
344 }
345
346 //=======================================================================
347 //function : ShallowReferences
348 //purpose  : 
349 //=======================================================================
350
351 Standard_Boolean CDM_Document::ShallowReferences
352                                 (const Handle(CDM_Document)& aDocument) const
353 {
354   CDM_ListIteratorOfListOfReferences it(myFromReferences);
355   for(; it.More() ; it.Next()) {
356     if(it.Value()->Document() == aDocument) return Standard_True;
357   }
358   return Standard_False;
359 }
360
361 //=======================================================================
362 //function : DeepReferences
363 //purpose  : 
364 //=======================================================================
365
366 Standard_Boolean CDM_Document::DeepReferences
367                                 (const Handle(CDM_Document)& aDocument) const
368 {
369   CDM_ListIteratorOfListOfReferences it(myFromReferences);
370   for(; it.More() ; it.Next()) {
371     Handle(CDM_Document) theToDocument=it.Value()->Document();
372     if(!theToDocument.IsNull()) {
373       if(theToDocument == aDocument) return Standard_True;
374       if(theToDocument->DeepReferences(aDocument)) return Standard_True;
375     }
376   }
377   return Standard_False;
378 }
379
380 //=======================================================================
381 //function : CopyReference
382 //purpose  : 
383 //=======================================================================
384
385 Standard_Integer CDM_Document::CopyReference
386                                 (const Handle(CDM_Document)& /*aFromDocument*/,
387                                  const Standard_Integer aReferenceIdentifier)
388 {
389   Handle(CDM_Reference) theReference = Reference(aReferenceIdentifier);
390   if(!theReference.IsNull()) {
391     Handle(CDM_Document) theDocument = theReference->Document();
392     if(!theDocument.IsNull()) {
393       return CreateReference(theDocument);
394     }
395     else
396       return CreateReference(theReference->MetaData(),
397                              theReference->Application(),
398                              theReference->DocumentVersion(),
399                              theReference->UseStorageConfiguration());
400   }
401   return 0; //for NT ...
402 }
403
404 //=======================================================================
405 //function : Modify
406 //purpose  : 
407 //=======================================================================
408
409 void CDM_Document::Modify()
410 {
411   myVersion++;
412 }
413
414 //=======================================================================
415 //function : UnModify
416 //purpose  : 
417 //=======================================================================
418
419 void CDM_Document::UnModify()
420 {
421   myVersion = myStorageVersion;
422 }
423
424 //=======================================================================
425 //function : Modifications
426 //purpose  : 
427 //=======================================================================
428
429 Standard_Integer CDM_Document::Modifications() const
430 {
431   return myVersion;
432 }
433
434 //=======================================================================
435 //function : SetModifications
436 //purpose  : 
437 //=======================================================================
438
439 void CDM_Document::SetModifications(const Standard_Integer Modifications)
440 {
441   myVersion = Modifications;
442 }
443
444 //=======================================================================
445 //function : IsUpToDate
446 //purpose  : 
447 //=======================================================================
448
449 Standard_Boolean CDM_Document::IsUpToDate
450                                 (const Standard_Integer aReferenceIdentifier) const
451 {
452   return  Reference(aReferenceIdentifier)->IsUpToDate();
453 }
454
455 //=======================================================================
456 //function : SetIsUpToDate
457 //purpose  : 
458 //=======================================================================
459
460 void CDM_Document::SetIsUpToDate (const Standard_Integer aReferenceIdentifier)
461 {
462   Reference(aReferenceIdentifier)->SetIsUpToDate();
463 }  
464
465 //=======================================================================
466 //function : SetComment
467 //purpose  : 
468 //=======================================================================
469
470 void CDM_Document::SetComment(const TCollection_ExtendedString& aComment)
471 {
472   myComments.Clear();
473   myComments.Append(aComment);
474 }
475
476 //=======================================================================
477 //function : AddComment
478 //purpose  : 
479 //=======================================================================
480
481 void CDM_Document::AddComment(const TCollection_ExtendedString& aComment)
482 {
483   myComments.Append(aComment);
484 }
485
486 //=======================================================================
487 //function : SetComments
488 //purpose  : 
489 //=======================================================================
490
491 void CDM_Document::SetComments(const TColStd_SequenceOfExtendedString& aComments)
492 {
493   myComments = aComments;
494 }
495
496 //=======================================================================
497 //function : Comments
498 //purpose  : 
499 //=======================================================================
500
501 void CDM_Document::Comments(TColStd_SequenceOfExtendedString& aComments) const
502 {
503   aComments = myComments;
504 }
505
506 //=======================================================================
507 //function : Comment
508 //purpose  : 
509 //=======================================================================
510
511 Standard_ExtString CDM_Document::Comment() const
512 {
513   if (myComments.Length() < 1)
514     return TCollection_ExtendedString().ToExtString();
515   return myComments(1).ToExtString();
516 }
517
518 //=======================================================================
519 //function : Presentation
520 //purpose  : 
521 //=======================================================================
522
523 Standard_ExtString CDM_Document::Presentation()
524 {
525   if(!myValidPresentation) ComputePresentation();
526   return myPresentation.ToExtString();
527 }
528
529 //=======================================================================
530 //function : UnvalidPresentation
531 //purpose  : 
532 //=======================================================================
533
534 void CDM_Document::UnvalidPresentation()
535 {
536   if(myValidPresentation) {
537     getPresentations().UnBind(myPresentation);
538     myValidPresentation=Standard_False;
539   }
540 }
541
542 //=======================================================================
543 //function : ComputePresentation
544 //purpose  : 
545 //=======================================================================
546
547 void CDM_Document::ComputePresentation()
548 {
549   TCollection_ExtendedString presentation("");
550   static Standard_Integer theUnnamedDocuments(0);
551   static CDM_NamesDirectory theNames;
552   static CDM_NamesDirectory theTypes;
553
554   if(!myMetaData.IsNull()) {
555     presentation += myMetaData->Name();
556     if(!theNames.IsBound(presentation)) theNames.Bind(presentation,0);
557     Standard_Integer range = theNames(presentation);
558     range += 1;
559     theNames(presentation) = range;
560     if(range != 1) {
561       presentation += "<";
562       presentation += range;
563       presentation += ">";
564     }
565   }
566   else {
567     LoadResources();
568     if(myDefaultPresentationWasFound) {
569 //      presentation += "$";
570       presentation += myDefaultPresentation;
571       if(!theTypes.IsBound(presentation)) theTypes.Bind(presentation,0);
572       Standard_Integer range = theTypes(presentation);
573       range +=1;
574       theTypes(presentation) = range;
575       presentation += "_";
576       presentation += range;
577     }
578     else {
579       presentation += TCollection_ExtendedString("Document_");
580       presentation += ++theUnnamedDocuments;
581     }
582   }
583   
584   if(getPresentations().IsBound(presentation)) {
585     TCollection_ExtendedString Test = presentation;
586     Test += "!";
587     Standard_Integer Count=0;
588     while (getPresentations().IsBound(Test)) {
589       Count++;
590       Test = presentation; Test+= "!"; Test+= Count;
591     }
592     presentation = Test;
593   }
594   
595   
596   myPresentation = TCollection_ExtendedString(presentation);
597   myValidPresentation = Standard_True;
598   getPresentations().Bind(presentation,this);
599 }
600
601 //=======================================================================
602 //function : FindFromPresentation
603 //purpose  : 
604 //=======================================================================
605
606 Handle(CDM_Document) CDM_Document::FindFromPresentation
607                                 (const TCollection_ExtendedString& aPresentation)
608 {
609   TCollection_ExtendedString x(aPresentation);
610   if(!getPresentations().IsBound(x)) {
611     Standard_SStream aMsg;
612     aMsg <<"No document having this presentation: " << x << " does exist."
613          << endl << (char)0;
614     Standard_NoSuchObject::Raise(aMsg);
615   }
616   return getPresentations()(x);
617 }
618
619 //=======================================================================
620 //function : FindPresentation
621 //purpose  : 
622 //=======================================================================
623
624 Standard_Boolean CDM_Document::FindPresentation
625                                 (const TCollection_ExtendedString& aPresentation)
626 {
627   return getPresentations().IsBound(aPresentation);
628 }
629
630 //=======================================================================
631 //function : IsStored
632 //purpose  : 
633 //=======================================================================
634
635 Standard_Boolean CDM_Document::IsStored() const
636 {
637   return !myMetaData.IsNull();
638 }
639
640 //=======================================================================
641 //function : StorageVersion
642 //purpose  : 
643 //=======================================================================
644
645 Standard_Integer CDM_Document::StorageVersion() const
646 {
647   return myStorageVersion;
648 }
649
650 //=======================================================================
651 //function : SetMetaData
652 //purpose  : 
653 //=======================================================================
654
655 void CDM_Document::SetMetaData(const Handle(CDM_MetaData)& aMetaData)
656 {
657   if(!aMetaData->IsRetrieved() || aMetaData->Document() != This()) {
658
659     aMetaData->SetDocument(this);
660
661 // Update the document refencing this MetaData:
662     CDM_DataMapIteratorOfMetaDataLookUpTable it(CDM_MetaData::LookUpTable());
663     for(;it.More();it.Next()) {
664       const Handle(CDM_MetaData)& theMetaData=it.Value();
665       if(theMetaData != aMetaData && theMetaData->IsRetrieved()) {
666         CDM_ListIteratorOfListOfReferences rit(theMetaData->Document()->myToReferences);
667         for(;rit.More();rit.Next()) {
668           rit.Value()->Update(aMetaData);
669         }
670       }
671     }
672     if(!myMetaData.IsNull()) {
673       if(myMetaData->Name() != aMetaData->Name()) UnvalidPresentation();
674       myMetaData->UnsetDocument();
675     }
676     else
677       UnvalidPresentation();
678   }
679
680   myStorageVersion = Modifications();
681   
682   myMetaData = aMetaData;
683   
684   SetRequestedFolder(aMetaData->Folder());
685   if(aMetaData->HasVersion()) SetRequestedPreviousVersion(aMetaData->Version());
686 }
687
688 //=======================================================================
689 //function : UnsetIsStored
690 //purpose  : 
691 //=======================================================================
692
693 void CDM_Document::UnsetIsStored()
694 {
695   if(!myMetaData.IsNull()) { 
696     myMetaData->UnsetDocument();
697 //    myMetaData.Nullify();
698   }
699 }
700
701 //=======================================================================
702 //function : MetaData
703 //purpose  : 
704 //=======================================================================
705
706 Handle(CDM_MetaData) CDM_Document::MetaData() const
707 {
708   if(myMetaData.IsNull())
709     Standard_NoSuchObject::Raise("cannot furnish the MetaData of an object "
710                                  "which is not stored");
711   return myMetaData;
712 }
713
714 //=======================================================================
715 //function : SetRequestedComment
716 //purpose  : 
717 //=======================================================================
718
719 void CDM_Document::SetRequestedComment(const TCollection_ExtendedString& aComment)
720 {
721   myRequestedComment=aComment;
722 }
723
724 //=======================================================================
725 //function : RequestedComment
726 //purpose  : 
727 //=======================================================================
728
729 TCollection_ExtendedString CDM_Document::RequestedComment() const
730 {
731   return myRequestedComment.ToExtString();
732 }
733
734 //=======================================================================
735 //function : Folder
736 //purpose  : 
737 //=======================================================================
738
739 TCollection_ExtendedString CDM_Document::Folder() const {
740   if(myMetaData.IsNull())
741     Standard_NoSuchObject::Raise("cannot furnish the folder of an object "
742                                  "which is not stored");
743   return myMetaData->Folder();
744 }
745
746 //=======================================================================
747 //function : SetRequestedFolder
748 //purpose  : 
749 //=======================================================================
750
751 void CDM_Document::SetRequestedFolder(const TCollection_ExtendedString& aFolder)
752 {
753   TCollection_ExtendedString f(aFolder);
754   if(f.Length() != 0) {
755     myRequestedFolderIsDefined=Standard_True;
756     myRequestedFolder=aFolder;
757   }
758 }
759
760 //=======================================================================
761 //function : RequestedFolder
762 //purpose  : 
763 //=======================================================================
764
765 TCollection_ExtendedString CDM_Document::RequestedFolder() const
766 {
767   Standard_NoSuchObject_Raise_if (!myRequestedFolderIsDefined,
768                                   "storage folder is undefined for this document");
769   return myRequestedFolder;
770 }
771
772 //=======================================================================
773 //function : HasRequestedFolder
774 //purpose  : 
775 //=======================================================================
776
777 Standard_Boolean CDM_Document::HasRequestedFolder() const
778 {
779   return myRequestedFolderIsDefined;
780 }
781
782 //=======================================================================
783 //function : SetRequestedName
784 //purpose  : 
785 //=======================================================================
786
787 void CDM_Document::SetRequestedName(const TCollection_ExtendedString& aName)
788 {
789   myRequestedName=aName;
790   myRequestedNameIsDefined=Standard_True;
791 }
792
793 //=======================================================================
794 //function : RequestedName
795 //purpose  : 
796 //=======================================================================
797
798 TCollection_ExtendedString CDM_Document::RequestedName()
799 {
800   if(!myRequestedNameIsDefined) {
801     if(!myMetaData.IsNull())
802       myRequestedName=myMetaData->Name();
803     else
804       myRequestedName=Presentation();
805   }
806   myRequestedNameIsDefined=Standard_True;
807   return myRequestedName;
808 }
809
810 //=======================================================================
811 //function : SetRequestedPreviousVersion
812 //purpose  : 
813 //=======================================================================
814
815 void CDM_Document::SetRequestedPreviousVersion
816                              (const TCollection_ExtendedString& aPreviousVersion)
817 {
818   myRequestedPreviousVersionIsDefined=Standard_True;
819   myRequestedPreviousVersion=aPreviousVersion;
820 }
821
822 //=======================================================================
823 //function : RequestedPreviousVersion
824 //purpose  : 
825 //=======================================================================
826
827 TCollection_ExtendedString CDM_Document::RequestedPreviousVersion() const
828 {
829   Standard_NoSuchObject_Raise_if (!myRequestedPreviousVersionIsDefined,
830                                   "PreviousVersion is undefined fro this document");
831   return myRequestedPreviousVersion;
832 }
833
834 //=======================================================================
835 //function : HasRequestedPreviousVersion
836 //purpose  : 
837 //=======================================================================
838
839 Standard_Boolean CDM_Document::HasRequestedPreviousVersion() const
840 {
841   return myRequestedPreviousVersionIsDefined;
842 }
843
844 //=======================================================================
845 //function : UnsetRequestedPreviousVersion
846 //purpose  : 
847 //=======================================================================
848
849 void CDM_Document::UnsetRequestedPreviousVersion()
850 {
851   myRequestedPreviousVersionIsDefined=Standard_False;
852 }
853
854 //=======================================================================
855 //function : IsOpened
856 //purpose  : 
857 //=======================================================================
858
859 Standard_Boolean CDM_Document::IsOpened() const
860 {
861   return !myApplication.IsNull();
862 }
863
864 //=======================================================================
865 //function : IsOpened
866 //purpose  : 
867 //=======================================================================
868
869 Standard_Boolean CDM_Document::IsOpened
870                                 (const Standard_Integer aReferenceIdentifier) const
871 {
872   CDM_ListIteratorOfListOfReferences it(myToReferences);
873   
874   for (; it.More(); it.Next()) {
875     if (aReferenceIdentifier == it.Value()->ReferenceIdentifier())
876       return it.Value()->IsOpened();
877   }
878   return Standard_False;
879 }
880
881 //=======================================================================
882 //function : Open
883 //purpose  : 
884 //=======================================================================
885
886 void CDM_Document::Open(const Handle(CDM_Application)& anApplication)
887 {
888   myApplication=anApplication;
889 }
890
891 //=======================================================================
892 //function : Close
893 //purpose  : 
894 //=======================================================================
895
896 void CDM_Document::Close()
897 {
898   switch (CanClose()) {
899   case CDM_CCS_NotOpen: 
900     Standard_Failure::Raise("cannot close a document that has not been opened");
901     break;
902   case CDM_CCS_UnstoredReferenced:
903      Standard_Failure::Raise("cannot close an unstored document which is referenced");
904     break;
905   case CDM_CCS_ModifiedReferenced:
906     Standard_Failure::Raise("cannot close a document which is referenced when "
907                             "the document has been modified since it was stored.");
908     break;
909   case CDM_CCS_ReferenceRejection:
910     Standard_Failure::Raise("cannot close this document because a document "
911                             "referencing it refuses");
912     break;
913   default:
914     break;
915   }
916   if(FromReferencesNumber() != 0) {
917     CDM_ListIteratorOfListOfReferences it(myFromReferences);
918     for(; it.More(); it.Next()) {
919       it.Value()->UnsetToDocument(MetaData(),myApplication);
920     }
921   }
922   RemoveAllReferences();
923   UnsetIsStored();
924   myApplication.Nullify();
925   UnvalidPresentation();
926
927 }
928
929 //=======================================================================
930 //function : CanClose
931 //purpose  : 
932 //=======================================================================
933
934 CDM_CanCloseStatus CDM_Document::CanClose() const
935 {
936   if(!IsOpened()) return CDM_CCS_NotOpen;
937
938   if(FromReferencesNumber() != 0) {
939     if(!IsStored()) return CDM_CCS_UnstoredReferenced;
940     if(IsModified()) return CDM_CCS_ModifiedReferenced;
941
942
943     CDM_ListIteratorOfListOfReferences it(myFromReferences);
944     for(; it.More(); it.Next()) {
945       if(!it.Value()->FromDocument()->CanCloseReference(this, it.Value()->ReferenceIdentifier()))
946         return CDM_CCS_ReferenceRejection;
947     }
948   } 
949   return CDM_CCS_OK;
950 }
951
952 //=======================================================================
953 //function : CanCloseReference
954 //purpose  : 
955 //=======================================================================
956
957 Standard_Boolean CDM_Document::CanCloseReference
958                                 (const Handle(CDM_Document)& /*aDocument*/,
959                                  const Standard_Integer /*(aReferenceIdent*/) const
960 {
961   return Standard_True;
962 }
963
964 //=======================================================================
965 //function : CloseReference
966 //purpose  : 
967 //=======================================================================
968
969 void CDM_Document::CloseReference (const Handle(CDM_Document)& /*aDocument*/,
970                                    const Standard_Integer /*aReferenceIdentifier*/)
971 {
972 }
973
974 //=======================================================================
975 //function : Application
976 //purpose  : 
977 //=======================================================================
978
979 const Handle(CDM_Application)& CDM_Document::Application() const
980 {
981   if(!IsOpened())
982     Standard_Failure::Raise("this document has not yet been opened "
983                             "by any application");
984   return myApplication;
985 }
986
987 //=======================================================================
988 //function : IsModified
989 //purpose  : 
990 //=======================================================================
991
992 Standard_Boolean CDM_Document::IsModified() const
993 {
994   return Modifications() > StorageVersion();
995 }
996
997 //=======================================================================
998 //function : Print
999 //purpose  : 
1000 //=======================================================================
1001
1002 Standard_OStream& CDM_Document::Print(Standard_OStream& anOStream) const
1003 {
1004   return anOStream;
1005 }
1006
1007 //=======================================================================
1008 //function : CreateReference
1009 //purpose  : 
1010 //=======================================================================
1011
1012 void CDM_Document::CreateReference(const Handle(CDM_MetaData)& aMetaData,
1013                                    const Standard_Integer aReferenceIdentifier,
1014                                    const Handle(CDM_Application)& anApplication,
1015                                    const Standard_Integer aToDocumentVersion,
1016                                    const Standard_Boolean UseStorageConfiguration)
1017 {
1018   myActualReferenceIdentifier=Max(myActualReferenceIdentifier,aReferenceIdentifier);
1019
1020   if(aMetaData->IsRetrieved()) {
1021     Handle(CDM_Reference) r = new CDM_Reference (this,
1022                                                  aMetaData->Document(),
1023                                                  aReferenceIdentifier,
1024                                                  aToDocumentVersion);
1025     AddToReference(r);
1026     aMetaData->Document()->AddFromReference(r);
1027     
1028   }
1029   else  {
1030       Handle(CDM_Reference) r = new CDM_Reference (this,
1031                                                    aMetaData,
1032                                                    aReferenceIdentifier,
1033                                                    anApplication,
1034                                                    aToDocumentVersion,
1035                                                    UseStorageConfiguration);
1036       AddToReference(r);
1037     }
1038 }
1039
1040 //=======================================================================
1041 //function : CreateReference
1042 //purpose  : 
1043 //=======================================================================
1044
1045 Standard_Integer CDM_Document::CreateReference
1046                                 (const Handle(CDM_MetaData)& aMetaData,
1047                                  const Handle(CDM_Application)& anApplication,
1048                                  const Standard_Integer aDocumentVersion,
1049                                  const Standard_Boolean UseStorageConfiguration)
1050 {
1051   CDM_ListIteratorOfListOfReferences it(myToReferences);
1052
1053   for(; it.More(); it.Next()) {
1054     if(aMetaData == it.Value()->MetaData())
1055       return it.Value()->ReferenceIdentifier();
1056   }
1057   Handle(CDM_Reference) r = new CDM_Reference (this,
1058                                                aMetaData,
1059                                                ++myActualReferenceIdentifier,
1060                                                anApplication,
1061                                                aDocumentVersion,
1062                                                UseStorageConfiguration);
1063   AddToReference(r);
1064   return  r->ReferenceIdentifier();
1065 }
1066
1067 //=======================================================================
1068 //function : AddToReference
1069 //purpose  : 
1070 //=======================================================================
1071
1072 void CDM_Document::AddToReference(const Handle(CDM_Reference)& aReference)
1073 {
1074   myToReferences.Append(aReference);
1075 }
1076
1077 //=======================================================================
1078 //function : AddFromReference
1079 //purpose  : 
1080 //=======================================================================
1081
1082 void CDM_Document::AddFromReference(const Handle(CDM_Reference)& aReference)
1083 {
1084   myFromReferences.Append(aReference);
1085 }
1086
1087 //=======================================================================
1088 //function : RemoveFromReference
1089 //purpose  : 
1090 //=======================================================================
1091
1092 void CDM_Document::RemoveFromReference(const Standard_Integer aReferenceIdentifier)
1093 {
1094   CDM_ListIteratorOfListOfReferences it(myFromReferences);
1095   
1096   for(; it.More(); it.Next()) {
1097     if(aReferenceIdentifier == it.Value()->ReferenceIdentifier()) {
1098       myFromReferences.Remove(it);
1099       return;
1100     }
1101   }
1102 }
1103
1104 //=======================================================================
1105 //function : GetResource
1106 //purpose  : 
1107 //=======================================================================
1108
1109 TCollection_ExtendedString GetResource (const TCollection_ExtendedString aFormat,
1110                                         const TCollection_ExtendedString anItem)
1111 {
1112   TCollection_ExtendedString theResource;
1113   theResource+= aFormat;
1114   theResource+= ".";
1115   theResource+= anItem;
1116   return theResource;
1117 }
1118
1119 static void FIND (const Handle(Resource_Manager)& theDocumentResource,
1120                   const TCollection_ExtendedString& theResourceName,
1121                   Standard_Boolean& IsDef,
1122                   TCollection_ExtendedString& theValue)
1123 {
1124   IsDef=UTL::Find(theDocumentResource,theResourceName);
1125   if(IsDef) theValue=UTL::Value(theDocumentResource,theResourceName);
1126   
1127 }
1128
1129 //=======================================================================
1130 //function : StorageResource
1131 //purpose  : 
1132 //=======================================================================
1133
1134 Handle(Resource_Manager) CDM_Document::StorageResource()
1135 {
1136   if(myApplication.IsNull()) {
1137     Standard_SStream aMsg;
1138     aMsg << "this document of format "<< StorageFormat()
1139          << " has not yet been opened by any application. "<< endl;
1140     Standard_Failure::Raise(aMsg);
1141   }
1142   return myApplication->Resources();
1143 }  
1144
1145 //=======================================================================
1146 //function : LoadResources
1147 //purpose  : 
1148 //=======================================================================
1149
1150 void CDM_Document::LoadResources()
1151 {
1152   if(!myResourcesAreLoaded) {
1153     Handle(Resource_Manager) theDocumentResource = StorageResource();
1154  
1155     TCollection_ExtendedString theFormat=StorageFormat(); theFormat+=".";
1156     TCollection_ExtendedString theResourceName;
1157     
1158     theResourceName=theFormat;
1159     theResourceName+="FileExtension";
1160     FIND(theDocumentResource,
1161          theResourceName,myFileExtensionWasFound,myFileExtension);
1162     
1163     theResourceName=theFormat;
1164     theResourceName+="DataType";
1165     FIND(theDocumentResource,theResourceName,myDataTypeWasFound,myDataType);
1166     
1167     
1168     theResourceName=theFormat;
1169     theResourceName+="VersionDataType";
1170     FIND(theDocumentResource,
1171          theResourceName,myVersionDataTypeWasFound,myVersionDataType);
1172     
1173     theResourceName=theFormat;
1174     theResourceName+="Description";
1175     FIND(theDocumentResource,theResourceName,myDescriptionWasFound,myDescription);
1176     
1177     theResourceName=theFormat;
1178     theResourceName+="Domain";
1179     FIND(theDocumentResource,theResourceName,myDomainWasFound,myDomain);
1180     
1181     theResourceName=theFormat;
1182     theResourceName+="Presentation";
1183     FIND(theDocumentResource,
1184          theResourceName,myDefaultPresentationWasFound,myDefaultPresentation);
1185     
1186     theResourceName=theFormat;
1187     theResourceName+="StoragePlugin";
1188     TCollection_ExtendedString thePluginId;
1189     FIND(theDocumentResource,theResourceName,myStoragePluginWasFound,thePluginId);
1190     if(myStoragePluginWasFound) myStoragePlugin=UTL::GUID(thePluginId);
1191     myResourcesAreLoaded=Standard_True;
1192     
1193 //    cout << "resource Loaded: " << "Format: " << theFormat << ", FileExtension:" << myFileExtension << ", DataType:" <<  myDataType << ", VersionDataType:" << myVersionDataType << ", Description:" << myDescription << ", Domain:" << myDomain << endl;
1194   }
1195   return;
1196 }
1197
1198 //=======================================================================
1199 //function : FindFileExtension
1200 //purpose  : 
1201 //=======================================================================
1202
1203 Standard_Boolean CDM_Document::FindFileExtension ()
1204 {
1205   LoadResources();
1206   return myFileExtensionWasFound;
1207 }
1208
1209 //=======================================================================
1210 //function : FileExtension
1211 //purpose  : 
1212 //=======================================================================
1213
1214 TCollection_ExtendedString CDM_Document::FileExtension()
1215 {
1216   LoadResources();
1217   return  myFileExtension;
1218 }
1219
1220 //=======================================================================
1221 //function : FindDataType
1222 //purpose  : 
1223 //=======================================================================
1224
1225 Standard_Boolean CDM_Document::FindDataType ()
1226 {
1227   LoadResources();
1228   return myDataTypeWasFound;
1229 }
1230
1231 //=======================================================================
1232 //function : DataType
1233 //purpose  : 
1234 //=======================================================================
1235
1236 TCollection_ExtendedString CDM_Document::DataType()
1237 {
1238   LoadResources();
1239   return myDataType;
1240 }
1241
1242 //=======================================================================
1243 //function : FindVersionDataType
1244 //purpose  : 
1245 //=======================================================================
1246
1247 Standard_Boolean CDM_Document::FindVersionDataType ()
1248 {
1249   LoadResources();
1250   return myVersionDataTypeWasFound;
1251 }
1252
1253 //=======================================================================
1254 //function : VersionDataType
1255 //purpose  : 
1256 //=======================================================================
1257
1258 TCollection_ExtendedString CDM_Document::VersionDataType()
1259 {
1260   LoadResources();
1261   return myVersionDataType;
1262 }
1263
1264 //=======================================================================
1265 //function : FindDescription
1266 //purpose  : 
1267 //=======================================================================
1268
1269 Standard_Boolean CDM_Document::FindDescription ()
1270 {
1271   LoadResources();
1272   return myDescriptionWasFound;
1273 }
1274
1275 //=======================================================================
1276 //function : Description
1277 //purpose  : 
1278 //=======================================================================
1279
1280 TCollection_ExtendedString CDM_Document::Description()
1281 {
1282   LoadResources();
1283   return myDescription;
1284 }
1285
1286 //=======================================================================
1287 //function : FindDomain
1288 //purpose  : 
1289 //=======================================================================
1290
1291 Standard_Boolean CDM_Document::FindDomain ()
1292 {
1293   LoadResources();
1294   return myDomainWasFound;
1295 }
1296
1297 //=======================================================================
1298 //function : Domain
1299 //purpose  : 
1300 //=======================================================================
1301
1302 TCollection_ExtendedString CDM_Document::Domain()
1303 {
1304   LoadResources();
1305   return myDomain;
1306 }
1307
1308 //=======================================================================
1309 //function : FindStoragePlugin
1310 //purpose  : 
1311 //=======================================================================
1312
1313 Standard_Boolean CDM_Document::FindStoragePlugin()
1314 {
1315   LoadResources();
1316   return myStoragePluginWasFound;
1317 }
1318
1319 //=======================================================================
1320 //function : StoragePlugin
1321 //purpose  : 
1322 //=======================================================================
1323
1324 Standard_GUID CDM_Document::StoragePlugin()
1325 {
1326   LoadResources();
1327   return myStoragePlugin;
1328 }
1329
1330 //=======================================================================
1331 //function : IsReadOnly
1332 //purpose  : 
1333 //=======================================================================
1334
1335 Standard_Boolean CDM_Document::IsReadOnly() const
1336 {
1337   if(IsStored()) return myMetaData->IsReadOnly();
1338   return Standard_False;
1339 }
1340
1341 //=======================================================================
1342 //function : IsReadOnly
1343 //purpose  : 
1344 //=======================================================================
1345
1346 Standard_Boolean CDM_Document::IsReadOnly
1347                                 (const Standard_Integer aReferenceIdentifier) const
1348 {
1349   return Reference(aReferenceIdentifier)->IsReadOnly();
1350 }
1351
1352 //=======================================================================
1353 //function : SetIsReadOnly
1354 //purpose  : 
1355 //=======================================================================
1356
1357 void CDM_Document::SetIsReadOnly()
1358 {
1359   if(IsStored()) myMetaData->SetIsReadOnly();
1360 }
1361     
1362 //=======================================================================
1363 //function : UnsetIsReadOnly
1364 //purpose  : 
1365 //=======================================================================
1366
1367 void CDM_Document::UnsetIsReadOnly()
1368 {
1369   if(IsStored()) myMetaData->UnsetIsReadOnly();
1370 }
1371
1372 //=======================================================================
1373 //function : ReferenceCounter
1374 //purpose  : 
1375 //=======================================================================
1376
1377 Standard_Integer CDM_Document::ReferenceCounter() const
1378 {
1379   return myActualReferenceIdentifier;
1380 }
1381
1382 //=======================================================================
1383 //function : SetReferenceCounter
1384 //purpose  : 
1385 //=======================================================================
1386
1387 void CDM_Document::SetReferenceCounter (const Standard_Integer aReferenceCounter)
1388 {
1389   myActualReferenceIdentifier=aReferenceCounter;
1390 }