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 #include <CDM_Application.hxx>
20 #include <CDM_DataMapIteratorOfMetaDataLookUpTable.hxx>
21 #include <CDM_Document.hxx>
22 #include <CDM_ListIteratorOfListOfDocument.hxx>
23 #include <CDM_ListOfDocument.hxx>
24 #include <CDM_MetaData.hxx>
25 #include <CDM_NamesDirectory.hxx>
26 #include <CDM_Reference.hxx>
27 #include <CDM_ReferenceIterator.hxx>
28 #include <Resource_Manager.hxx>
29 #include <Standard_DomainError.hxx>
30 #include <Standard_Dump.hxx>
31 #include <Standard_Failure.hxx>
32 #include <Standard_GUID.hxx>
33 #include <Standard_NoSuchObject.hxx>
34 #include <Standard_NullObject.hxx>
35 #include <Standard_ProgramError.hxx>
36 #include <Standard_Type.hxx>
37 #include <TCollection_ExtendedString.hxx>
38 #include <UTL.hxx>
39 
IMPLEMENT_STANDARD_RTTIEXT(CDM_Document,Standard_Transient)40 IMPLEMENT_STANDARD_RTTIEXT(CDM_Document,Standard_Transient)
41 
42 //=======================================================================
43 //function : CDM_Document
44 //purpose  :
45 //=======================================================================
46 
47 CDM_Document::CDM_Document():
48   myResourcesAreLoaded          (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   myDescriptionWasFound         (Standard_False)
58 {}
59 
60 
61 //=======================================================================
62 //function : ~CDM_Document
63 //purpose  :
64 //=======================================================================
65 
~CDM_Document()66 CDM_Document::~CDM_Document()
67 {
68   if(!myMetaData.IsNull()) myMetaData->UnsetDocument();
69 }
70 
71 //=======================================================================
72 //function : Update
73 //purpose  :
74 //=======================================================================
75 
Update(const Handle (CDM_Document)&,const Standard_Integer,const Standard_Address)76 void CDM_Document::Update (const Handle(CDM_Document)& /*aToDocument*/,
77                            const Standard_Integer /*aReferenceIdentifier*/,
78                            const Standard_Address /*aModifContext*/)
79 {
80 }
81 
82 //=======================================================================
83 //function : Update
84 //purpose  :
85 //=======================================================================
86 
Update()87 void CDM_Document::Update()
88 {
89 }
90 
91 //=======================================================================
92 //function : Update
93 //purpose  :
94 //=======================================================================
95 
Update(TCollection_ExtendedString & ErrorString)96 Standard_Boolean CDM_Document::Update(TCollection_ExtendedString& ErrorString)
97 {
98   ErrorString.Clear();
99   Update();
100   return Standard_True;
101 }
102 
103 //=======================================================================
104 //function : GetAlternativeDocument
105 //purpose  :
106 //=======================================================================
107 
GetAlternativeDocument(const TCollection_ExtendedString & aFormat,Handle (CDM_Document)& anAlternativeDocument)108 Standard_Boolean CDM_Document::GetAlternativeDocument
109                                 (const TCollection_ExtendedString& aFormat,
110                                  Handle(CDM_Document)& anAlternativeDocument)
111 {
112   anAlternativeDocument = this;
113   return aFormat == StorageFormat();
114 }
115 
116 //=======================================================================
117 //function : Extensions
118 //purpose  :
119 //=======================================================================
120 
Extensions(TColStd_SequenceOfExtendedString & Extensions) const121 void CDM_Document::Extensions (TColStd_SequenceOfExtendedString& Extensions) const
122 {
123   Extensions.Clear();
124 }
125 
126 //=======================================================================
127 //function : CreateReference
128 //purpose  :
129 //=======================================================================
130 
CreateReference(const Handle (CDM_Document)& anOtherDocument)131 Standard_Integer CDM_Document::CreateReference
132                                 (const Handle(CDM_Document)& anOtherDocument)
133 {
134   CDM_ListIteratorOfListOfReferences it(myToReferences);
135 
136   for(; it.More(); it.Next()) {
137     if(anOtherDocument == it.Value()->Document())
138       return it.Value()->ReferenceIdentifier();
139   }
140 
141   Handle(CDM_Reference) r = new CDM_Reference (this,
142                                                anOtherDocument,
143                                                ++myActualReferenceIdentifier,
144                                                anOtherDocument->Modifications());
145   AddToReference(r);
146   anOtherDocument->AddFromReference(r);
147   return  r->ReferenceIdentifier();
148 }
149 
150 //=======================================================================
151 //function : RemoveAllReferences
152 //purpose  :
153 //=======================================================================
154 
RemoveAllReferences()155 void CDM_Document::RemoveAllReferences()
156 {
157   CDM_ListIteratorOfListOfReferences it(myToReferences);
158 
159   for(; it.More(); it.Next()) {
160     it.Value()->ToDocument()->RemoveFromReference(it.Value()->ReferenceIdentifier());
161   }
162   myToReferences.Clear();
163 }
164 
165 //=======================================================================
166 //function : RemoveReference
167 //purpose  :
168 //=======================================================================
169 
RemoveReference(const Standard_Integer aReferenceIdentifier)170 void CDM_Document::RemoveReference(const Standard_Integer aReferenceIdentifier)
171 {
172   CDM_ListIteratorOfListOfReferences it(myToReferences);
173 
174   for(; it.More(); it.Next()) {
175     if(aReferenceIdentifier == it.Value()->ReferenceIdentifier()) {
176       it.Value()->ToDocument()->RemoveFromReference(aReferenceIdentifier);
177       myToReferences.Remove(it);
178       return;
179     }
180   }
181 }
182 
183 //=======================================================================
184 //function : Document
185 //purpose  :
186 //=======================================================================
187 
Handle(CDM_Document)188 Handle(CDM_Document) CDM_Document::Document
189                                 (const Standard_Integer aReferenceIdentifier) const
190 {
191   Handle(CDM_Document) theDocument;
192 
193   if(aReferenceIdentifier == 0)
194     theDocument = this;
195 
196   else {
197     Handle(CDM_Reference) theReference = Reference(aReferenceIdentifier);
198     if(!theReference.IsNull()) theDocument = theReference->ToDocument();
199   }
200   return theDocument;
201 }
202 
203 //=======================================================================
204 //function : Reference
205 //purpose  :
206 //=======================================================================
207 
Handle(CDM_Reference)208 Handle(CDM_Reference) CDM_Document::Reference
209                                 (const Standard_Integer aReferenceIdentifier) const
210 {
211   Handle(CDM_Reference) theReference;
212 
213   CDM_ListIteratorOfListOfReferences it(myToReferences);
214 
215   Standard_Boolean found = Standard_False;
216 
217   for(; it.More() && !found; it.Next()) {
218     found = aReferenceIdentifier == it.Value()->ReferenceIdentifier();
219     if(found) theReference = it.Value();
220   }
221   return theReference;
222 }
223 
224 //=======================================================================
225 //function : IsInSession
226 //purpose  :
227 //=======================================================================
228 
IsInSession(const Standard_Integer aReferenceIdentifier) const229 Standard_Boolean CDM_Document::IsInSession
230                                 (const Standard_Integer aReferenceIdentifier) const
231 {
232   if(aReferenceIdentifier == 0) return Standard_True;
233   Handle(CDM_Reference) theReference=Reference(aReferenceIdentifier);
234   if(theReference.IsNull())
235     throw Standard_NoSuchObject("CDM_Document::IsInSession: "
236                                  "invalid reference identifier");
237   return theReference->IsInSession();
238 }
239 
240 //=======================================================================
241 //function : IsStored
242 //purpose  :
243 //=======================================================================
244 
IsStored(const Standard_Integer aReferenceIdentifier) const245 Standard_Boolean CDM_Document::IsStored
246                                 (const Standard_Integer aReferenceIdentifier) const
247 {
248   if(aReferenceIdentifier == 0) return IsStored();
249   Handle(CDM_Reference) theReference=Reference(aReferenceIdentifier);
250   if(theReference.IsNull())
251     throw Standard_NoSuchObject("CDM_Document::IsInSession: "
252                                  "invalid reference identifier");
253   return theReference->IsStored();
254 }
255 
256 //=======================================================================
257 //function : Name
258 //purpose  :
259 //=======================================================================
260 
Name(const Standard_Integer aReferenceIdentifier) const261 TCollection_ExtendedString CDM_Document::Name
262                                 (const Standard_Integer aReferenceIdentifier) const
263 {
264   if(!IsStored(aReferenceIdentifier))
265     throw Standard_DomainError("CDM_Document::Name: document is not stored");
266 
267   if(aReferenceIdentifier == 0) return myMetaData->Name();
268 
269   return Reference(aReferenceIdentifier)->MetaData()->Name();
270 }
271 
272 //=======================================================================
273 //function : UpdateFromDocuments
274 //purpose  :
275 //=======================================================================
UpdateFromDocuments(const Standard_Address aModifContext) const276 void CDM_Document::UpdateFromDocuments(const Standard_Address aModifContext) const
277 {
278   CDM_ListOfDocument aListOfDocumentsToUpdate;
279   Standard_Boolean StartUpdateCycle = aListOfDocumentsToUpdate.IsEmpty();
280   CDM_ListIteratorOfListOfReferences it(myFromReferences);
281   for(; it.More() ; it.Next()) {
282     Handle(CDM_Document) theFromDocument=it.Value()->FromDocument();
283     CDM_ListIteratorOfListOfDocument itUpdate;
284     for(; itUpdate.More(); itUpdate.Next()) {
285       if(itUpdate.Value() == theFromDocument) break;
286 
287       if(itUpdate.Value()->ShallowReferences(theFromDocument)) {
288         aListOfDocumentsToUpdate.InsertBefore(theFromDocument,itUpdate);
289 	break;
290       }
291     }
292     if(!itUpdate.More()) aListOfDocumentsToUpdate.Append(theFromDocument);
293     theFromDocument->Update(this,it.Value()->ReferenceIdentifier(),aModifContext);
294   }
295 
296   if(StartUpdateCycle) {
297 
298     Handle(CDM_Document) theDocumentToUpdate;
299     Handle(CDM_Application) theApplication;
300     TCollection_ExtendedString ErrorString;
301 
302     while(!aListOfDocumentsToUpdate.IsEmpty()) {
303       theDocumentToUpdate = aListOfDocumentsToUpdate.First();
304       theApplication=theDocumentToUpdate->Application();
305       ErrorString.Clear();
306       theApplication->BeginOfUpdate(theDocumentToUpdate);
307       theApplication->EndOfUpdate (theDocumentToUpdate,
308                                    theDocumentToUpdate->Update(ErrorString),
309                                    ErrorString);
310       aListOfDocumentsToUpdate.RemoveFirst();
311     }
312   }
313 }
314 
315 //=======================================================================
316 //function : ToReferencesNumber
317 //purpose  :
318 //=======================================================================
319 
ToReferencesNumber() const320 Standard_Integer CDM_Document::ToReferencesNumber() const
321 {
322   return myToReferences.Extent();
323 }
324 
325 //=======================================================================
326 //function : FromReferencesNumber
327 //purpose  :
328 //=======================================================================
329 
FromReferencesNumber() const330 Standard_Integer CDM_Document::FromReferencesNumber() const
331 {
332   return myFromReferences.Extent();
333 }
334 
335 //=======================================================================
336 //function : ShallowReferences
337 //purpose  :
338 //=======================================================================
339 
ShallowReferences(const Handle (CDM_Document)& aDocument) const340 Standard_Boolean CDM_Document::ShallowReferences
341                                 (const Handle(CDM_Document)& aDocument) const
342 {
343   CDM_ListIteratorOfListOfReferences it(myFromReferences);
344   for(; it.More() ; it.Next()) {
345     if(it.Value()->Document() == aDocument) return Standard_True;
346   }
347   return Standard_False;
348 }
349 
350 //=======================================================================
351 //function : DeepReferences
352 //purpose  :
353 //=======================================================================
354 
DeepReferences(const Handle (CDM_Document)& aDocument) const355 Standard_Boolean CDM_Document::DeepReferences
356                                 (const Handle(CDM_Document)& aDocument) const
357 {
358   CDM_ListIteratorOfListOfReferences it(myFromReferences);
359   for(; it.More() ; it.Next()) {
360     Handle(CDM_Document) theToDocument=it.Value()->Document();
361     if(!theToDocument.IsNull()) {
362       if(theToDocument == aDocument) return Standard_True;
363       if(theToDocument->DeepReferences(aDocument)) return Standard_True;
364     }
365   }
366   return Standard_False;
367 }
368 
369 //=======================================================================
370 //function : CopyReference
371 //purpose  :
372 //=======================================================================
373 
CopyReference(const Handle (CDM_Document)&,const Standard_Integer aReferenceIdentifier)374 Standard_Integer CDM_Document::CopyReference
375                                 (const Handle(CDM_Document)& /*aFromDocument*/,
376                                  const Standard_Integer aReferenceIdentifier)
377 {
378   Handle(CDM_Reference) theReference = Reference(aReferenceIdentifier);
379   if(!theReference.IsNull()) {
380     Handle(CDM_Document) theDocument = theReference->Document();
381     if(!theDocument.IsNull()) {
382       return CreateReference(theDocument);
383     }
384     else
385       return CreateReference(theReference->MetaData(),
386                              theReference->Application(),
387                              theReference->DocumentVersion(),
388                              theReference->UseStorageConfiguration());
389   }
390   return 0; //for NT ...
391 }
392 
393 //=======================================================================
394 //function : Modify
395 //purpose  :
396 //=======================================================================
397 
Modify()398 void CDM_Document::Modify()
399 {
400   myVersion++;
401 }
402 
403 //=======================================================================
404 //function : UnModify
405 //purpose  :
406 //=======================================================================
407 
UnModify()408 void CDM_Document::UnModify()
409 {
410   myVersion = myStorageVersion;
411 }
412 
413 //=======================================================================
414 //function : Modifications
415 //purpose  :
416 //=======================================================================
417 
Modifications() const418 Standard_Integer CDM_Document::Modifications() const
419 {
420   return myVersion;
421 }
422 
423 //=======================================================================
424 //function : SetModifications
425 //purpose  :
426 //=======================================================================
427 
SetModifications(const Standard_Integer Modifications)428 void CDM_Document::SetModifications(const Standard_Integer Modifications)
429 {
430   myVersion = Modifications;
431 }
432 
433 //=======================================================================
434 //function : IsUpToDate
435 //purpose  :
436 //=======================================================================
437 
IsUpToDate(const Standard_Integer aReferenceIdentifier) const438 Standard_Boolean CDM_Document::IsUpToDate
439                                 (const Standard_Integer aReferenceIdentifier) const
440 {
441   return  Reference(aReferenceIdentifier)->IsUpToDate();
442 }
443 
444 //=======================================================================
445 //function : SetIsUpToDate
446 //purpose  :
447 //=======================================================================
448 
SetIsUpToDate(const Standard_Integer aReferenceIdentifier)449 void CDM_Document::SetIsUpToDate (const Standard_Integer aReferenceIdentifier)
450 {
451   Reference(aReferenceIdentifier)->SetIsUpToDate();
452 }
453 
454 //=======================================================================
455 //function : SetComment
456 //purpose  :
457 //=======================================================================
458 
SetComment(const TCollection_ExtendedString & aComment)459 void CDM_Document::SetComment(const TCollection_ExtendedString& aComment)
460 {
461   myComments.Clear();
462   myComments.Append(aComment);
463 }
464 
465 //=======================================================================
466 //function : AddComment
467 //purpose  :
468 //=======================================================================
469 
AddComment(const TCollection_ExtendedString & aComment)470 void CDM_Document::AddComment(const TCollection_ExtendedString& aComment)
471 {
472   myComments.Append(aComment);
473 }
474 
475 //=======================================================================
476 //function : SetComments
477 //purpose  :
478 //=======================================================================
479 
SetComments(const TColStd_SequenceOfExtendedString & aComments)480 void CDM_Document::SetComments(const TColStd_SequenceOfExtendedString& aComments)
481 {
482   myComments = aComments;
483 }
484 
485 //=======================================================================
486 //function : Comments
487 //purpose  :
488 //=======================================================================
489 
Comments(TColStd_SequenceOfExtendedString & aComments) const490 void CDM_Document::Comments(TColStd_SequenceOfExtendedString& aComments) const
491 {
492   aComments = myComments;
493 }
494 
495 //=======================================================================
496 //function : Comment
497 //purpose  :
498 //=======================================================================
499 
Comment() const500 Standard_ExtString CDM_Document::Comment() const
501 {
502   if (myComments.Length() < 1)
503     return 0;
504   return myComments(1).ToExtString();
505 }
506 
507 //=======================================================================
508 //function : IsStored
509 //purpose  :
510 //=======================================================================
511 
IsStored() const512 Standard_Boolean CDM_Document::IsStored() const
513 {
514   return !myMetaData.IsNull();
515 }
516 
517 //=======================================================================
518 //function : StorageVersion
519 //purpose  :
520 //=======================================================================
521 
StorageVersion() const522 Standard_Integer CDM_Document::StorageVersion() const
523 {
524   return myStorageVersion;
525 }
526 
527 //=======================================================================
528 //function : SetMetaData
529 //purpose  :
530 //=======================================================================
531 
SetMetaData(const Handle (CDM_MetaData)& aMetaData)532 void CDM_Document::SetMetaData(const Handle(CDM_MetaData)& aMetaData)
533 {
534   if(!aMetaData->IsRetrieved() || aMetaData->Document() != This()) {
535 
536     aMetaData->SetDocument(this);
537 
538     // Update the document refencing this MetaData:
539     CDM_DataMapIteratorOfMetaDataLookUpTable it(Application()->MetaDataLookUpTable());
540     for(;it.More();it.Next()) {
541       const Handle(CDM_MetaData)& theMetaData=it.Value();
542       if(theMetaData != aMetaData && theMetaData->IsRetrieved()) {
543         CDM_ListIteratorOfListOfReferences rit(theMetaData->Document()->myToReferences);
544         for(;rit.More();rit.Next()) {
545 	  rit.Value()->Update(aMetaData);
546         }
547       }
548     }
549     if(!myMetaData.IsNull()) {
550       myMetaData->UnsetDocument();
551     }
552   }
553 
554   myStorageVersion = Modifications();
555 
556   myMetaData = aMetaData;
557 
558   SetRequestedFolder(aMetaData->Folder());
559   if(aMetaData->HasVersion()) SetRequestedPreviousVersion(aMetaData->Version());
560 }
561 
562 //=======================================================================
563 //function : UnsetIsStored
564 //purpose  :
565 //=======================================================================
566 
UnsetIsStored()567 void CDM_Document::UnsetIsStored()
568 {
569   if(!myMetaData.IsNull()) {
570     myMetaData->UnsetDocument();
571   }
572 }
573 
574 //=======================================================================
575 //function : MetaData
576 //purpose  :
577 //=======================================================================
578 
Handle(CDM_MetaData)579 Handle(CDM_MetaData) CDM_Document::MetaData() const
580 {
581   if(myMetaData.IsNull())
582     throw Standard_NoSuchObject("cannot furnish the MetaData of an object "
583                                  "which is not stored");
584   return myMetaData;
585 }
586 
587 //=======================================================================
588 //function : SetRequestedComment
589 //purpose  :
590 //=======================================================================
591 
SetRequestedComment(const TCollection_ExtendedString & aComment)592 void CDM_Document::SetRequestedComment(const TCollection_ExtendedString& aComment)
593 {
594   myRequestedComment=aComment;
595 }
596 
597 //=======================================================================
598 //function : RequestedComment
599 //purpose  :
600 //=======================================================================
601 
RequestedComment() const602 TCollection_ExtendedString CDM_Document::RequestedComment() const
603 {
604   return myRequestedComment.ToExtString();
605 }
606 
607 //=======================================================================
608 //function : Folder
609 //purpose  :
610 //=======================================================================
611 
Folder() const612 TCollection_ExtendedString CDM_Document::Folder() const {
613   if(myMetaData.IsNull())
614     throw Standard_NoSuchObject("cannot furnish the folder of an object "
615                                  "which is not stored");
616   return myMetaData->Folder();
617 }
618 
619 //=======================================================================
620 //function : SetRequestedFolder
621 //purpose  :
622 //=======================================================================
623 
SetRequestedFolder(const TCollection_ExtendedString & aFolder)624 void CDM_Document::SetRequestedFolder(const TCollection_ExtendedString& aFolder)
625 {
626   TCollection_ExtendedString f(aFolder);
627   if(f.Length() != 0) {
628     myRequestedFolderIsDefined=Standard_True;
629     myRequestedFolder=aFolder;
630   }
631 }
632 
633 //=======================================================================
634 //function : RequestedFolder
635 //purpose  :
636 //=======================================================================
637 
RequestedFolder() const638 TCollection_ExtendedString CDM_Document::RequestedFolder() const
639 {
640   Standard_NoSuchObject_Raise_if (!myRequestedFolderIsDefined,
641                                   "storage folder is undefined for this document");
642   return myRequestedFolder;
643 }
644 
645 //=======================================================================
646 //function : HasRequestedFolder
647 //purpose  :
648 //=======================================================================
649 
HasRequestedFolder() const650 Standard_Boolean CDM_Document::HasRequestedFolder() const
651 {
652   return myRequestedFolderIsDefined;
653 }
654 
655 //=======================================================================
656 //function : SetRequestedName
657 //purpose  :
658 //=======================================================================
659 
SetRequestedName(const TCollection_ExtendedString & aName)660 void CDM_Document::SetRequestedName(const TCollection_ExtendedString& aName)
661 {
662   myRequestedName=aName;
663   myRequestedNameIsDefined=Standard_True;
664 }
665 
666 //=======================================================================
667 //function : RequestedName
668 //purpose  :
669 //=======================================================================
670 
RequestedName()671 TCollection_ExtendedString CDM_Document::RequestedName()
672 {
673   if(!myRequestedNameIsDefined) {
674     if(!myMetaData.IsNull())
675       myRequestedName=myMetaData->Name();
676     else
677       myRequestedName="Document_";
678   }
679   myRequestedNameIsDefined=Standard_True;
680   return myRequestedName;
681 }
682 
683 //=======================================================================
684 //function : SetRequestedPreviousVersion
685 //purpose  :
686 //=======================================================================
687 
SetRequestedPreviousVersion(const TCollection_ExtendedString & aPreviousVersion)688 void CDM_Document::SetRequestedPreviousVersion
689                              (const TCollection_ExtendedString& aPreviousVersion)
690 {
691   myRequestedPreviousVersionIsDefined=Standard_True;
692   myRequestedPreviousVersion=aPreviousVersion;
693 }
694 
695 //=======================================================================
696 //function : RequestedPreviousVersion
697 //purpose  :
698 //=======================================================================
699 
RequestedPreviousVersion() const700 TCollection_ExtendedString CDM_Document::RequestedPreviousVersion() const
701 {
702   Standard_NoSuchObject_Raise_if (!myRequestedPreviousVersionIsDefined,
703                                   "PreviousVersion is undefined fro this document");
704   return myRequestedPreviousVersion;
705 }
706 
707 //=======================================================================
708 //function : HasRequestedPreviousVersion
709 //purpose  :
710 //=======================================================================
711 
HasRequestedPreviousVersion() const712 Standard_Boolean CDM_Document::HasRequestedPreviousVersion() const
713 {
714   return myRequestedPreviousVersionIsDefined;
715 }
716 
717 //=======================================================================
718 //function : UnsetRequestedPreviousVersion
719 //purpose  :
720 //=======================================================================
721 
UnsetRequestedPreviousVersion()722 void CDM_Document::UnsetRequestedPreviousVersion()
723 {
724   myRequestedPreviousVersionIsDefined=Standard_False;
725 }
726 
727 //=======================================================================
728 //function : IsOpened
729 //purpose  :
730 //=======================================================================
731 
IsOpened() const732 Standard_Boolean CDM_Document::IsOpened() const
733 {
734   return !myApplication.IsNull();
735 }
736 
737 //=======================================================================
738 //function : IsOpened
739 //purpose  :
740 //=======================================================================
741 
IsOpened(const Standard_Integer aReferenceIdentifier) const742 Standard_Boolean CDM_Document::IsOpened
743                                 (const Standard_Integer aReferenceIdentifier) const
744 {
745   CDM_ListIteratorOfListOfReferences it(myToReferences);
746 
747   for (; it.More(); it.Next()) {
748     if (aReferenceIdentifier == it.Value()->ReferenceIdentifier())
749       return it.Value()->IsOpened();
750   }
751   return Standard_False;
752 }
753 
754 //=======================================================================
755 //function : Open
756 //purpose  :
757 //=======================================================================
758 
Open(const Handle (CDM_Application)& anApplication)759 void CDM_Document::Open(const Handle(CDM_Application)& anApplication)
760 {
761   myApplication=anApplication;
762 }
763 
764 //=======================================================================
765 //function : Close
766 //purpose  :
767 //=======================================================================
768 
Close()769 void CDM_Document::Close()
770 {
771   switch (CanClose()) {
772   case CDM_CCS_NotOpen:
773     throw Standard_Failure("cannot close a document that has not been opened");
774     break;
775   case CDM_CCS_UnstoredReferenced:
776      throw Standard_Failure("cannot close an unstored document which is referenced");
777     break;
778   case CDM_CCS_ModifiedReferenced:
779     throw Standard_Failure("cannot close a document which is referenced when "
780                             "the document has been modified since it was stored.");
781     break;
782   case CDM_CCS_ReferenceRejection:
783     throw Standard_Failure("cannot close this document because a document "
784                             "referencing it refuses");
785     break;
786   default:
787     break;
788   }
789   if(FromReferencesNumber() != 0) {
790     CDM_ListIteratorOfListOfReferences it(myFromReferences);
791     for(; it.More(); it.Next()) {
792       it.Value()->UnsetToDocument(MetaData(),myApplication);
793     }
794   }
795   RemoveAllReferences();
796   UnsetIsStored();
797   myApplication.Nullify();
798 
799 }
800 
801 //=======================================================================
802 //function : CanClose
803 //purpose  :
804 //=======================================================================
805 
CanClose() const806 CDM_CanCloseStatus CDM_Document::CanClose() const
807 {
808   if(!IsOpened()) return CDM_CCS_NotOpen;
809 
810   if(FromReferencesNumber() != 0) {
811     if(!IsStored()) return CDM_CCS_UnstoredReferenced;
812     if(IsModified()) return CDM_CCS_ModifiedReferenced;
813 
814 
815     CDM_ListIteratorOfListOfReferences it(myFromReferences);
816     for(; it.More(); it.Next()) {
817       if(!it.Value()->FromDocument()->CanCloseReference(this, it.Value()->ReferenceIdentifier()))
818 	return CDM_CCS_ReferenceRejection;
819     }
820   }
821   return CDM_CCS_OK;
822 }
823 
824 //=======================================================================
825 //function : CanCloseReference
826 //purpose  :
827 //=======================================================================
828 
CanCloseReference(const Handle (CDM_Document)&,const Standard_Integer) const829 Standard_Boolean CDM_Document::CanCloseReference
830                                 (const Handle(CDM_Document)& /*aDocument*/,
831                                  const Standard_Integer /*(aReferenceIdent*/) const
832 {
833   return Standard_True;
834 }
835 
836 //=======================================================================
837 //function : CloseReference
838 //purpose  :
839 //=======================================================================
840 
CloseReference(const Handle (CDM_Document)&,const Standard_Integer)841 void CDM_Document::CloseReference (const Handle(CDM_Document)& /*aDocument*/,
842                                    const Standard_Integer /*aReferenceIdentifier*/)
843 {
844 }
845 
846 //=======================================================================
847 //function : Application
848 //purpose  :
849 //=======================================================================
850 
Handle(CDM_Application)851 const Handle(CDM_Application)& CDM_Document::Application() const
852 {
853   if(!IsOpened())
854     throw Standard_Failure("this document has not yet been opened "
855                             "by any application");
856   return myApplication;
857 }
858 
859 //=======================================================================
860 //function : IsModified
861 //purpose  :
862 //=======================================================================
863 
IsModified() const864 Standard_Boolean CDM_Document::IsModified() const
865 {
866   return Modifications() > StorageVersion();
867 }
868 
869 //=======================================================================
870 //function : Print
871 //purpose  :
872 //=======================================================================
873 
Print(Standard_OStream & anOStream) const874 Standard_OStream& CDM_Document::Print(Standard_OStream& anOStream) const
875 {
876   return anOStream;
877 }
878 
879 //=======================================================================
880 //function : CreateReference
881 //purpose  :
882 //=======================================================================
883 
CreateReference(const Handle (CDM_MetaData)& aMetaData,const Standard_Integer aReferenceIdentifier,const Handle (CDM_Application)& anApplication,const Standard_Integer aToDocumentVersion,const Standard_Boolean UseStorageConfiguration)884 void CDM_Document::CreateReference(const Handle(CDM_MetaData)& aMetaData,
885                                    const Standard_Integer aReferenceIdentifier,
886                                    const Handle(CDM_Application)& anApplication,
887                                    const Standard_Integer aToDocumentVersion,
888                                    const Standard_Boolean UseStorageConfiguration)
889 {
890   myActualReferenceIdentifier=Max(myActualReferenceIdentifier,aReferenceIdentifier);
891 
892   if(aMetaData->IsRetrieved()) {
893     Handle(CDM_Reference) r = new CDM_Reference (this,
894                                                  aMetaData->Document(),
895                                                  aReferenceIdentifier,
896                                                  aToDocumentVersion);
897     AddToReference(r);
898     aMetaData->Document()->AddFromReference(r);
899 
900   }
901   else  {
902       Handle(CDM_Reference) r = new CDM_Reference (this,
903                                                    aMetaData,
904                                                    aReferenceIdentifier,
905                                                    anApplication,
906                                                    aToDocumentVersion,
907                                                    UseStorageConfiguration);
908       AddToReference(r);
909     }
910 }
911 
912 //=======================================================================
913 //function : CreateReference
914 //purpose  :
915 //=======================================================================
916 
CreateReference(const Handle (CDM_MetaData)& aMetaData,const Handle (CDM_Application)& anApplication,const Standard_Integer aDocumentVersion,const Standard_Boolean UseStorageConfiguration)917 Standard_Integer CDM_Document::CreateReference
918                                 (const Handle(CDM_MetaData)& aMetaData,
919                                  const Handle(CDM_Application)& anApplication,
920                                  const Standard_Integer aDocumentVersion,
921                                  const Standard_Boolean UseStorageConfiguration)
922 {
923   CDM_ListIteratorOfListOfReferences it(myToReferences);
924 
925   for(; it.More(); it.Next()) {
926     if(aMetaData == it.Value()->MetaData())
927       return it.Value()->ReferenceIdentifier();
928   }
929   Handle(CDM_Reference) r = new CDM_Reference (this,
930                                                aMetaData,
931                                                ++myActualReferenceIdentifier,
932                                                anApplication,
933                                                aDocumentVersion,
934                                                UseStorageConfiguration);
935   AddToReference(r);
936   return  r->ReferenceIdentifier();
937 }
938 
939 //=======================================================================
940 //function : AddToReference
941 //purpose  :
942 //=======================================================================
943 
AddToReference(const Handle (CDM_Reference)& aReference)944 void CDM_Document::AddToReference(const Handle(CDM_Reference)& aReference)
945 {
946   myToReferences.Append(aReference);
947 }
948 
949 //=======================================================================
950 //function : AddFromReference
951 //purpose  :
952 //=======================================================================
953 
AddFromReference(const Handle (CDM_Reference)& aReference)954 void CDM_Document::AddFromReference(const Handle(CDM_Reference)& aReference)
955 {
956   myFromReferences.Append(aReference);
957 }
958 
959 //=======================================================================
960 //function : RemoveFromReference
961 //purpose  :
962 //=======================================================================
963 
RemoveFromReference(const Standard_Integer aReferenceIdentifier)964 void CDM_Document::RemoveFromReference(const Standard_Integer aReferenceIdentifier)
965 {
966   CDM_ListIteratorOfListOfReferences it(myFromReferences);
967 
968   for(; it.More(); it.Next()) {
969     if(aReferenceIdentifier == it.Value()->ReferenceIdentifier()) {
970       myFromReferences.Remove(it);
971       return;
972     }
973   }
974 }
975 
976 //=======================================================================
977 //function : GetResource
978 //purpose  :
979 //=======================================================================
980 
GetResource(const TCollection_ExtendedString aFormat,const TCollection_ExtendedString anItem)981 TCollection_ExtendedString GetResource (const TCollection_ExtendedString aFormat,
982                                         const TCollection_ExtendedString anItem)
983 {
984   TCollection_ExtendedString theResource;
985   theResource+= aFormat;
986   theResource+= ".";
987   theResource+= anItem;
988   return theResource;
989 }
990 
FIND(const Handle (Resource_Manager)& theDocumentResource,const TCollection_ExtendedString & theResourceName,Standard_Boolean & IsDef,TCollection_ExtendedString & theValue)991 static void FIND (const Handle(Resource_Manager)& theDocumentResource,
992                   const TCollection_ExtendedString& theResourceName,
993                   Standard_Boolean& IsDef,
994                   TCollection_ExtendedString& theValue)
995 {
996   IsDef=UTL::Find(theDocumentResource,theResourceName);
997   if(IsDef) theValue=UTL::Value(theDocumentResource,theResourceName);
998 
999 }
1000 
1001 //=======================================================================
1002 //function : StorageResource
1003 //purpose  :
1004 //=======================================================================
1005 
Handle(Resource_Manager)1006 Handle(Resource_Manager) CDM_Document::StorageResource()
1007 {
1008   if(myApplication.IsNull()) {
1009     Standard_SStream aMsg;
1010     aMsg << "this document of format "<< StorageFormat()
1011          << " has not yet been opened by any application. "<< std::endl;
1012     throw Standard_Failure(aMsg.str().c_str());
1013   }
1014   return myApplication->Resources();
1015 }
1016 
1017 //=======================================================================
1018 //function : LoadResources
1019 //purpose  :
1020 //=======================================================================
1021 
LoadResources()1022 void CDM_Document::LoadResources()
1023 {
1024   if(!myResourcesAreLoaded) {
1025     Handle(Resource_Manager) theDocumentResource = StorageResource();
1026 
1027     TCollection_ExtendedString theFormat=StorageFormat(); theFormat+=".";
1028     TCollection_ExtendedString theResourceName;
1029 
1030     theResourceName=theFormat;
1031     theResourceName+="FileExtension";
1032     FIND(theDocumentResource,
1033          theResourceName,myFileExtensionWasFound,myFileExtension);
1034 
1035     theResourceName=theFormat;
1036     theResourceName+="Description";
1037     FIND(theDocumentResource,theResourceName,myDescriptionWasFound,myDescription);
1038 
1039     myResourcesAreLoaded=Standard_True;
1040 
1041 //    std::cout << "resource Loaded: Format: " << theFormat << ", FileExtension:" << myFileExtension << ", Description:" << myDescription << std::endl;
1042   }
1043   return;
1044 }
1045 
1046 //=======================================================================
1047 //function : FindFileExtension
1048 //purpose  :
1049 //=======================================================================
1050 
FindFileExtension()1051 Standard_Boolean CDM_Document::FindFileExtension ()
1052 {
1053   LoadResources();
1054   return myFileExtensionWasFound;
1055 }
1056 
1057 //=======================================================================
1058 //function : FileExtension
1059 //purpose  :
1060 //=======================================================================
1061 
FileExtension()1062 TCollection_ExtendedString CDM_Document::FileExtension()
1063 {
1064   LoadResources();
1065   return  myFileExtension;
1066 }
1067 
1068 //=======================================================================
1069 //function : FindDescription
1070 //purpose  :
1071 //=======================================================================
1072 
FindDescription()1073 Standard_Boolean CDM_Document::FindDescription ()
1074 {
1075   LoadResources();
1076   return myDescriptionWasFound;
1077 }
1078 
1079 //=======================================================================
1080 //function : Description
1081 //purpose  :
1082 //=======================================================================
1083 
Description()1084 TCollection_ExtendedString CDM_Document::Description()
1085 {
1086   LoadResources();
1087   return myDescription;
1088 }
1089 
1090 //=======================================================================
1091 //function : IsReadOnly
1092 //purpose  :
1093 //=======================================================================
1094 
IsReadOnly() const1095 Standard_Boolean CDM_Document::IsReadOnly() const
1096 {
1097   if(IsStored()) return myMetaData->IsReadOnly();
1098   return Standard_False;
1099 }
1100 
1101 //=======================================================================
1102 //function : IsReadOnly
1103 //purpose  :
1104 //=======================================================================
1105 
IsReadOnly(const Standard_Integer aReferenceIdentifier) const1106 Standard_Boolean CDM_Document::IsReadOnly
1107                                 (const Standard_Integer aReferenceIdentifier) const
1108 {
1109   return Reference(aReferenceIdentifier)->IsReadOnly();
1110 }
1111 
1112 //=======================================================================
1113 //function : SetIsReadOnly
1114 //purpose  :
1115 //=======================================================================
1116 
SetIsReadOnly()1117 void CDM_Document::SetIsReadOnly()
1118 {
1119   if(IsStored()) myMetaData->SetIsReadOnly();
1120 }
1121 
1122 //=======================================================================
1123 //function : UnsetIsReadOnly
1124 //purpose  :
1125 //=======================================================================
1126 
UnsetIsReadOnly()1127 void CDM_Document::UnsetIsReadOnly()
1128 {
1129   if(IsStored()) myMetaData->UnsetIsReadOnly();
1130 }
1131 
1132 //=======================================================================
1133 //function : ReferenceCounter
1134 //purpose  :
1135 //=======================================================================
1136 
ReferenceCounter() const1137 Standard_Integer CDM_Document::ReferenceCounter() const
1138 {
1139   return myActualReferenceIdentifier;
1140 }
1141 
1142 //=======================================================================
1143 //function : SetReferenceCounter
1144 //purpose  :
1145 //=======================================================================
1146 
SetReferenceCounter(const Standard_Integer aReferenceCounter)1147 void CDM_Document::SetReferenceCounter (const Standard_Integer aReferenceCounter)
1148 {
1149   myActualReferenceIdentifier=aReferenceCounter;
1150 }
1151 
1152 //=======================================================================
1153 //function : DumpJson
1154 //purpose  :
1155 //=======================================================================
DumpJson(Standard_OStream & theOStream,Standard_Integer theDepth) const1156 void CDM_Document::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
1157 {
1158   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
1159 
1160   for (TColStd_SequenceOfExtendedString::Iterator aCommentIt (myComments); aCommentIt.More(); aCommentIt.Next())
1161   {
1162     const TCollection_ExtendedString& aComment = aCommentIt.Value();
1163     OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aComment)
1164   }
1165 
1166   for (CDM_ListOfReferences::Iterator aFromReferenceIt (myFromReferences); aFromReferenceIt.More(); aFromReferenceIt.Next())
1167   {
1168     const Handle(CDM_Reference)& aFromReference = aFromReferenceIt.Value().get();
1169     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aFromReference.get())
1170   }
1171 
1172   for (CDM_ListOfReferences::Iterator aToReferenceIt (myToReferences); aToReferenceIt.More(); aToReferenceIt.Next())
1173   {
1174     const Handle(CDM_Reference)& aToReference = aToReferenceIt.Value().get();
1175     OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, aToReference.get())
1176   }
1177 
1178   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myVersion)
1179   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myActualReferenceIdentifier)
1180   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myStorageVersion)
1181 
1182   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myMetaData.get())
1183 
1184   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myRequestedComment)
1185   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myRequestedFolder)
1186   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRequestedFolderIsDefined)
1187   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myRequestedName)
1188   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRequestedNameIsDefined)
1189   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRequestedPreviousVersionIsDefined)
1190   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myRequestedPreviousVersion)
1191   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myFileExtension)
1192   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myDescription)
1193   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myFileExtensionWasFound)
1194   OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDescriptionWasFound)
1195 
1196   OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, myApplication.get())
1197 }
1198