0031013: Application Framework - Storage status is wrong after a failure
[occt.git] / src / CDF / CDF_StoreList.cxx
old mode 100755 (executable)
new mode 100644 (file)
index abe1e9c..91149ae
@@ -1,31 +1,40 @@
-// File:       CDF_StoreList.cxx
-// Created:    Fri Aug  8 16:03:07 1997
-// Author:     Jean-Louis Frenkel
-//             <rmi@frilox.paris1.matra-dtv.fr>
+// Created on: 1997-08-08
+// Created by: Jean-Louis Frenkel
+// Copyright (c) 1997-1999 Matra Datavision
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 
-#include <CDF_StoreList.ixx>
-
-#include <Standard_ErrorHandler.hxx>
-#include <Standard_Macro.hxx>
-
+#include <CDF_Application.hxx>
+#include <CDF_MetaDataDriver.hxx>
+#include <CDF_MetaDataDriverError.hxx>
+#include <CDF_Session.hxx>
+#include <CDF_StoreList.hxx>
+#include <CDM_Document.hxx>
+#include <CDM_MetaData.hxx>
 #include <CDM_ReferenceIterator.hxx>
-
 #include <PCDM.hxx>
 #include <PCDM_Document.hxx>
 #include <PCDM_StorageDriver.hxx>
+#include <Standard_ErrorHandler.hxx>
+#include <Standard_NoSuchObject.hxx>
+#include <TCollection_ExtendedString.hxx>
 
-#include <CDF_MetaDataDriverError.hxx>
-#include <CDF_MetaDataDriver.hxx>
-
-#include <CDF_Session.hxx>
-#include <CDF_Application.hxx>
-#include <CDF_Timer.hxx>
+IMPLEMENT_STANDARD_RTTIEXT(CDF_StoreList,Standard_Transient)
 
-static void CAUGHT(TCollection_ExtendedString& status,const TCollection_ExtendedString& what) {
-  Handle(Standard_Failure) F = Standard_Failure::Caught();
+static void CAUGHT(const Standard_Failure& theException,TCollection_ExtendedString& status,const TCollection_ExtendedString& what) {
   status += what;
-  status += F->GetMessageString();
+  status += theException.GetMessageString();
 }
 
 CDF_StoreList::CDF_StoreList(const Handle(CDM_Document)& aDocument) {
@@ -36,7 +45,7 @@ CDF_StoreList::CDF_StoreList(const Handle(CDM_Document)& aDocument) {
 void CDF_StoreList::Add(const Handle(CDM_Document)& aDocument) {
 
   if(!myItems.Contains(aDocument) && aDocument != myMainDocument) myItems.Add(aDocument);
-  myStack.Push(aDocument);
+  myStack.Prepend(aDocument);
   
   CDM_ReferenceIterator it(aDocument);
   for (;it.More();it.Next()) {
@@ -65,61 +74,64 @@ void CDF_StoreList::Next() {
 Handle(CDM_Document) CDF_StoreList::Value() const {
   return myIterator.Key();
 }
-CDF_StoreStatus CDF_StoreList::Store (Handle(CDM_MetaData)& aMetaData, TCollection_ExtendedString& aStatusAssociatedText) {
+PCDM_StoreStatus CDF_StoreList::Store (Handle(CDM_MetaData)& aMetaData, TCollection_ExtendedString& aStatusAssociatedText) {
 
   Handle(CDF_MetaDataDriver) theMetaDataDriver = CDF_Session::CurrentSession()->MetaDataDriver();
 
-  static CDF_StoreStatus status ;
-  status = CDF_SS_OK;
+  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;
-         
-         PCDM::StorageDriver(theDocument)->Write(theDocument,theName);
-         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());
-         }
-         
-       }
+      for (; !myStack.IsEmpty(); myStack.RemoveFirst()) {
+
+        Handle(CDM_Document) theDocument = myStack.First();
+        if( theDocument == myMainDocument || theDocument->IsModified()) {
+
+          Handle(CDF_Application) anApp = Handle(CDF_Application)::DownCast (theDocument->Application());
+          if (anApp.IsNull())
+          {
+            throw Standard_Failure("Document has no application, cannot save!");
+          }
+          Handle(PCDM_StorageDriver) aDocumentStorageDriver = 
+            anApp->WriterFromFormat(theDocument->StorageFormat());
+          if (aDocumentStorageDriver.IsNull())
+          {
+            Standard_SStream aMsg;
+            aMsg <<"No storage driver does exist for this format: " << theDocument->StorageFormat() << (char)0;
+            throw Standard_Failure(aMsg.str().c_str());
+          }
+
+          // Reset the store-status.
+          // It has sense in multi-threaded access to the storage driver - this way we reset the status for each call.
+          aDocumentStorageDriver->SetStoreStatus(PCDM_SS_OK);
+
+          if(!theMetaDataDriver->FindFolder(theDocument->RequestedFolder())) {
+            Standard_SStream aMsg; aMsg << "could not find the active dbunit";
+            aMsg << TCollection_ExtendedString(theDocument->RequestedFolder())<< (char)0;
+            throw Standard_NoSuchObject(aMsg.str().c_str());
+          }
+          TCollection_ExtendedString theName=theMetaDataDriver->BuildFileName(theDocument);
+
+          aDocumentStorageDriver->Write(theDocument,theName);
+          status = aDocumentStorageDriver->GetStoreStatus();
+          aMetaData = theMetaDataDriver->CreateMetaData(theDocument,theName);
+          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 = CDF_SS_DriverFailure;
+    catch (CDF_MetaDataDriverError const& anException) {
+      CAUGHT(anException, aStatusAssociatedText, TCollection_ExtendedString("metadatadriver failed; reason:"));
+      status = PCDM_SS_DriverFailure;
     }
-    catch (Standard_Failure) {
-      CAUGHT(aStatusAssociatedText,TCollection_ExtendedString("driver failed; reason:"));
-      status = CDF_SS_Failure; 
+    catch (Standard_Failure const& anException) {
+      CAUGHT(anException, aStatusAssociatedText, TCollection_ExtendedString("driver failed; reason:"));
+      status = PCDM_SS_Failure; 
     }
   }