summaryrefslogtreecommitdiff
path: root/src/CDF/CDF_StoreList.cxx
blob: f5af63e753ae1838f1528f92bb948c9093a0bbc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// File:	CDF_StoreList.cxx
// Created:	Fri Aug  8 16:03:07 1997
// Author:	Jean-Louis Frenkel
//		<rmi@frilox.paris1.matra-dtv.fr>


#include <CDF_StoreList.ixx>

#include <Standard_ErrorHandler.hxx>
#include <Standard_Macro.hxx>

#include <CDM_ReferenceIterator.hxx>

#include <PCDM.hxx>
#include <PCDM_Document.hxx>
#include <PCDM_StorageDriver.hxx>

#include <CDF_MetaDataDriverError.hxx>
#include <CDF_MetaDataDriver.hxx>

#include <CDF_Session.hxx>
#include <CDF_Application.hxx>
#include <CDF_Timer.hxx>

static void CAUGHT(TCollection_ExtendedString& status,const TCollection_ExtendedString& what) {
  Handle(Standard_Failure) F = Standard_Failure::Caught();
  status += what;
  status += F->GetMessageString();
}

CDF_StoreList::CDF_StoreList(const Handle(CDM_Document)& aDocument) {
  myMainDocument = aDocument;
  Add(aDocument);
}

void CDF_StoreList::Add(const Handle(CDM_Document)& aDocument) {

  if(!myItems.Contains(aDocument) && aDocument != myMainDocument) myItems.Add(aDocument);
  myStack.Push(aDocument);
  
  CDM_ReferenceIterator it(aDocument);
  for (;it.More();it.Next()) {
    if(it.Document()->IsModified())  Add(it.Document());
  }
}
Standard_Boolean CDF_StoreList::IsConsistent () const {
  Standard_Boolean yes = Standard_True;
  CDM_MapIteratorOfMapOfDocument it (myItems); 
  for ( ; it.More() && yes ; it.Next()) {
    yes = it.Key()->HasRequestedFolder();
  }
  return yes && myMainDocument->HasRequestedFolder();
}
void CDF_StoreList::Init() {
  myIterator = CDM_MapIteratorOfMapOfDocument(myItems);
}
Standard_Boolean CDF_StoreList::More() const {
  return myIterator.More();
}

void CDF_StoreList::Next() {
  myIterator.Next();
}

Handle(CDM_Document) CDF_StoreList::Value() const {
  return myIterator.Key();
}
PCDM_StoreStatus CDF_StoreList::Store (Handle(CDM_MetaData)& aMetaData, TCollection_ExtendedString& aStatusAssociatedText) {

  Handle(CDF_MetaDataDriver) theMetaDataDriver = CDF_Session::CurrentSession()->MetaDataDriver();

  PCDM_StoreStatus status = PCDM_SS_OK;
  {
    try {
      OCC_CATCH_SIGNALS
      for (; !myStack.IsEmpty(); myStack.Pop()) {

        Handle(CDM_Document) theDocument = myStack.Top();
        if( theDocument == myMainDocument || theDocument->IsModified()) {

          if(!PCDM::FindStorageDriver(theDocument)){
            Standard_SStream aMsg;
            aMsg <<"No storage driver does exist for this format: " << theDocument->StorageFormat() << (char)0;
            Standard_Failure::Raise(aMsg);
          }

          if(!theMetaDataDriver->FindFolder(theDocument->RequestedFolder())) {
            Standard_SStream aMsg; aMsg << "could not find the active dbunit";
            aMsg << TCollection_ExtendedString(theDocument->RequestedFolder())<< (char)0;
            Standard_NoSuchObject::Raise(aMsg);
          }
          TCollection_ExtendedString theName=theMetaDataDriver->BuildFileName(theDocument);

          CDF_Timer theTimer;
          Handle(PCDM_StorageDriver) aDocumentStorageDriver = PCDM::StorageDriver(theDocument);

          aDocumentStorageDriver->Write(theDocument,theName);
          status = aDocumentStorageDriver->GetStoreStatus();

          theTimer.ShowAndRestart("Driver->Write: ");

          aMetaData = theMetaDataDriver->CreateMetaData(theDocument,theName);
          theTimer.ShowAndStop("metadata creating: ");

          theDocument->SetMetaData(aMetaData);

          CDM_ReferenceIterator it(theDocument);
          for(; it.More();it.Next()) {
            theMetaDataDriver->CreateReference(aMetaData,it.Document()->MetaData(),it.ReferenceIdentifier(),it.DocumentVersion());
          }
        }
      }
    }

    catch (CDF_MetaDataDriverError) {
      CAUGHT(aStatusAssociatedText,TCollection_ExtendedString("metadatadriver failed; reason:"));
      status = PCDM_SS_DriverFailure;
    }
    catch (Standard_Failure) {
      CAUGHT(aStatusAssociatedText,TCollection_ExtendedString("driver failed; reason:"));
      status = PCDM_SS_Failure; 
    }
  }

  return status;
}