0023024: Update headers of OCCT files
[occt.git] / src / CDF / CDF_StoreList.cxx
1 // Created on: 1997-08-08
2 // Created by: Jean-Louis Frenkel
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <CDF_StoreList.ixx>
24
25 #include <Standard_ErrorHandler.hxx>
26 #include <Standard_Macro.hxx>
27
28 #include <CDM_ReferenceIterator.hxx>
29
30 #include <PCDM.hxx>
31 #include <PCDM_Document.hxx>
32 #include <PCDM_StorageDriver.hxx>
33
34 #include <CDF_MetaDataDriverError.hxx>
35 #include <CDF_MetaDataDriver.hxx>
36
37 #include <CDF_Session.hxx>
38 #include <CDF_Application.hxx>
39 #include <CDF_Timer.hxx>
40
41 static void CAUGHT(TCollection_ExtendedString& status,const TCollection_ExtendedString& what) {
42   Handle(Standard_Failure) F = Standard_Failure::Caught();
43   status += what;
44   status += F->GetMessageString();
45 }
46
47 CDF_StoreList::CDF_StoreList(const Handle(CDM_Document)& aDocument) {
48   myMainDocument = aDocument;
49   Add(aDocument);
50 }
51
52 void CDF_StoreList::Add(const Handle(CDM_Document)& aDocument) {
53
54   if(!myItems.Contains(aDocument) && aDocument != myMainDocument) myItems.Add(aDocument);
55   myStack.Push(aDocument);
56   
57   CDM_ReferenceIterator it(aDocument);
58   for (;it.More();it.Next()) {
59     if(it.Document()->IsModified())  Add(it.Document());
60   }
61 }
62 Standard_Boolean CDF_StoreList::IsConsistent () const {
63   Standard_Boolean yes = Standard_True;
64   CDM_MapIteratorOfMapOfDocument it (myItems); 
65   for ( ; it.More() && yes ; it.Next()) {
66     yes = it.Key()->HasRequestedFolder();
67   }
68   return yes && myMainDocument->HasRequestedFolder();
69 }
70 void CDF_StoreList::Init() {
71   myIterator = CDM_MapIteratorOfMapOfDocument(myItems);
72 }
73 Standard_Boolean CDF_StoreList::More() const {
74   return myIterator.More();
75 }
76
77 void CDF_StoreList::Next() {
78   myIterator.Next();
79 }
80
81 Handle(CDM_Document) CDF_StoreList::Value() const {
82   return myIterator.Key();
83 }
84 PCDM_StoreStatus CDF_StoreList::Store (Handle(CDM_MetaData)& aMetaData, TCollection_ExtendedString& aStatusAssociatedText) {
85
86   Handle(CDF_MetaDataDriver) theMetaDataDriver = CDF_Session::CurrentSession()->MetaDataDriver();
87
88   PCDM_StoreStatus status = PCDM_SS_OK;
89   {
90     try {
91       OCC_CATCH_SIGNALS
92       for (; !myStack.IsEmpty(); myStack.Pop()) {
93
94         Handle(CDM_Document) theDocument = myStack.Top();
95         if( theDocument == myMainDocument || theDocument->IsModified()) {
96
97           if(!PCDM::FindStorageDriver(theDocument)){
98             Standard_SStream aMsg;
99             aMsg <<"No storage driver does exist for this format: " << theDocument->StorageFormat() << (char)0;
100             Standard_Failure::Raise(aMsg);
101           }
102
103           if(!theMetaDataDriver->FindFolder(theDocument->RequestedFolder())) {
104             Standard_SStream aMsg; aMsg << "could not find the active dbunit";
105             aMsg << TCollection_ExtendedString(theDocument->RequestedFolder())<< (char)0;
106             Standard_NoSuchObject::Raise(aMsg);
107           }
108           TCollection_ExtendedString theName=theMetaDataDriver->BuildFileName(theDocument);
109
110           CDF_Timer theTimer;
111           Handle(PCDM_StorageDriver) aDocumentStorageDriver = PCDM::StorageDriver(theDocument);
112
113           aDocumentStorageDriver->Write(theDocument,theName);
114           status = aDocumentStorageDriver->GetStoreStatus();
115
116           theTimer.ShowAndRestart("Driver->Write: ");
117
118           aMetaData = theMetaDataDriver->CreateMetaData(theDocument,theName);
119           theTimer.ShowAndStop("metadata creating: ");
120
121           theDocument->SetMetaData(aMetaData);
122
123           CDM_ReferenceIterator it(theDocument);
124           for(; it.More();it.Next()) {
125             theMetaDataDriver->CreateReference(aMetaData,it.Document()->MetaData(),it.ReferenceIdentifier(),it.DocumentVersion());
126           }
127         }
128       }
129     }
130
131     catch (CDF_MetaDataDriverError) {
132       CAUGHT(aStatusAssociatedText,TCollection_ExtendedString("metadatadriver failed; reason:"));
133       status = PCDM_SS_DriverFailure;
134     }
135     catch (Standard_Failure) {
136       CAUGHT(aStatusAssociatedText,TCollection_ExtendedString("driver failed; reason:"));
137       status = PCDM_SS_Failure; 
138     }
139   }
140
141   return status;
142 }