0031013: Application Framework - Storage status is wrong after a failure
[occt.git] / src / CDF / CDF_StoreList.cxx
index c78c79a..91149ae 100644 (file)
 // 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) {
@@ -88,30 +87,34 @@ PCDM_StoreStatus CDF_StoreList::Store (Handle(CDM_MetaData)& aMetaData, TCollect
         Handle(CDM_Document) theDocument = myStack.First();
         if( theDocument == myMainDocument || theDocument->IsModified()) {
 
-          if(!PCDM::FindStorageDriver(theDocument)){
+          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;
-            Standard_Failure::Raise(aMsg);
+            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;
-            Standard_NoSuchObject::Raise(aMsg);
+            throw Standard_NoSuchObject(aMsg.str().c_str());
           }
           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);
@@ -122,12 +125,12 @@ PCDM_StoreStatus CDF_StoreList::Store (Handle(CDM_MetaData)& aMetaData, TCollect
       }
     }
 
-    catch (CDF_MetaDataDriverError) {
-      CAUGHT(aStatusAssociatedText,TCollection_ExtendedString("metadatadriver failed; reason:"));
+    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:"));
+    catch (Standard_Failure const& anException) {
+      CAUGHT(anException, aStatusAssociatedText, TCollection_ExtendedString("driver failed; reason:"));
       status = PCDM_SS_Failure; 
     }
   }