/*
 *  Copyright 1995,1996,1997 by the Association for Information and Image Management Int'l
 *                                             1100 Wayne Avenue
 *                                             Silver Spring, MD 20910-5603
 *                                             Tel: 301/587-8202
 *                                             Fax: 301/587-2711
 *      All Rights Reserved.
 *      DMA (Document Management Alliance) working group.
 *
 *  
 *  Code example: retrieval of document content in DMA 1.0.
 *
 */

#ifndef NULL
#define NULL    0
#endif

// ANSI C header file
#include <stddef.h>

// DMA header files

#include <dmatypes.h>
#include <dmastr.h>
#include <dmaenums.h>
#include <dmarc.h>  
#include <dmacom.h>
#include <dmaiface.h>
#include <dmaids.h>
#include <dmaidvar.h>

// Code example: retrieval of document content in DMA 1.0.

// Assumes that connections to system and doc space have already been made
// and that the doc id of the target document has been found
// (by search or navigation)

extern LPIdmaDocSpace iDocSpace; // Ref to interface for the connected doc space
extern LPMALLOC iMalloc;    // An IMalloc interface obtained from IdmaSystemManager::GetMalloc

// Utility that is assumed to exist
extern Abort(DmaRC rc, char *msg);

// DmaString utilities

pDmaString MakeDmaString(DmaWChar* content)
{
    pDmaString newString;

    DMA_CREATE_STRING(iMalloc->Alloc, wcslen(content), content, &amp;newString)
    return newString;
}

void FreeDMAString(pDmaString pString)
{
    DMA_FREE_STRING(iMalloc->Free, pString)
    return;
}


// Procedure to retrieve content via docID to local file
//    localFile is in URL form, e.g. "file://\\temp\foo.txt"
// Retrieves only the first content element of the first rendition

void MakeDocumentContentLocal(pDmaString documentID, wchar_t *localFile)
{
pDmaString              localFileURL = NULL;
IdmaProperties*         iDocVer      = NULL;
IdmaListOfObject*       iRenditions  = NULL;
IdmaListOfObject*       iContentEls  = NULL;
IdmaContentTransfer*    iFirstContent = NULL;
IdmaProperties*         iFirstRendition = NULL;

// Connect to the document version object.  This makes a snapshot
// of the persistent doc ver in the client's process space.

DmaRC rc = iDocSpace->ConnectObject (documentID, IID_IdmaProperties, 
                                      (pDmapv)&amp;iDocVer);
if (rc != DMARC_OK)
   Abort (rc, "Failed to connect to doc ver");

// NOTE: for brevity, further error checking is omitted.  All DMA calls
//   should be followed by code similar to the above

//  Get the first rendition 
rc = iDocVer->GetPropValObjectById(&dmaProp_Renditions, IID_IdmaListOfObject,
                                     (pDmapv) &iRenditions);
rc = iRenditions->GetObject(0, IID_IdmaProperties,(pDmapv)  &iFirstRendition);

// Get first content element
rc = iFirstRendition->GetPropValObjectById(&dmaProp_ContentElements,
                       IID_IdmaListOfObject, (pDmapv) &iContentEls);
rc = iContentEls->GetObject(0, IID_IdmaContentTransfer,(pDmapv)  &iFirstContent);

// From the content transfer element, get a file access object,
// and use it to copy the content to a local file
localFileURL = MakeDmaString(localFile);

rc = iFirstContent->CopyToResource (localFileURL, DMA_CREATE_REPLACE_EXISTING);

// cleanup
if (iRenditions != NULL) iRenditions->Release();
if (iFirstRendition != NULL) iFirstRendition->Release();
if (iContentEls!= NULL) iContentEls ->Release();
if (iFirstContent!= NULL) iFirstContent ->Release();
if (iDocVer != NULL) iDocVer->Release();
FreeDMAString(localFileURL);
}