/*
 *  Copyright 1995,1996,1997 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: Creation of a new simple document 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>

// Assumes:
//   connection to doc space has already been made
//   documents are single rendition, single content element
//   documents are not versioned.

extern LPIdmaDocSpace iDocSpace; /* interface on the connected doc space */

// Utilities assumed to exist
extern pDmaString MakeDmaString(DmaWChar*);
extern void FreeDmaString(pDmaString);

Abort(DmaRC rc, char *msg);

DmaWChar *docURL = L"file://\\project\\foo.txt"; /* URL of content file */

// While this example builds the document structure bottom up (from
//    content transfer element up to doc ver), the reverse could also be done.

void StoreDocInDocSpace(DmaWChar *docURL)
{
IdmaObjectFactory*      iObjectFactory = NULL;
IdmaContentTransfer*    iContent = NULL;
IdmaListOfObject*       iContentEls = NULL;
IdmaEditListOfObject*   iEditContentList = NULL;
IdmaEditListOfObject*   iEditRenditionList = NULL;
IdmaConnection*         iDocConnection = NULL;
IdmaEditProperties*     iDocVer = NULL;
IdmaEditProperties*     iContentProps = NULL;
IdmaEditProperties*     iRenditionProps = NULL;

pDmaString docURLString = MakeDmaString(docURL);
pDmaString single = MakeDmaString(L"single");
pDmaString plainText = MakeDmaString(L"MIME::text/plain");

//  Create a content transfer element, and set its Capture Resource.
//  NOTE: the object factory in the doc space is used to create
//   the objects that are used in storing a new document -- since
//   those objects may be doc space dependent

DmaRC rc =iDocSpace->QueryInterface(IID_IdmaObjectFactory,(pDmapv) &iObjectFactory);
if (rc != DMARC_OK)
    Abort (rc, "Failed to get object factory interface");

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

rc = iObjectFactory->CreateObject(&dmaClass_ContentTransfer,
                                IID_IdmaContentTransfer,(pDmapv) &iContent);
rc = iContent->SetCaptureResource(docURLString);

// set required properties on the content transfer object
rc = iContent->QueryInterface(IID_IdmaEditProperties,(pDmapv) &iContentProps);
rc = iContentProps->PutPropValStringById(&dmaProp_ComponentType, single);

// Create a rendition, and link in content transfer object
rc = iObjectFactory->CreateObject(&dmaClass_Rendition,
                                IID_IdmaProperties,(pDmapv) &iRenditionProps);
rc = iRenditionProps->GetPropValObjectById(&dmaProp_ContentElements, 
                        IID_IdmaEditListOfObject, (pDmapv) &iEditContentList);
rc = iEditContentList->InsertObject(0, (pDmapv) &iContent);

// Set required property - Rendition Type - on the rendition object
rc = iRenditionProps->PutPropValStringById(&dmaProp_RenditionType, plainText);

// Now make a doc version object and insert the rendition onto its rendition list
rc = iObjectFactory->CreateObject(&dmaClass_DocVersion,
                                IID_IdmaEditProperties,(pDmapv) &iDocVer);
// NOTE: since IdmaEditProperties inherits from IdmaProperties,
//   no QueryInterface is needed
rc = iDocVer->GetPropValObjectById(&dmaProp_Renditions, IID_IdmaEditListOfObject,
                            (pDmapv) &iEditRenditionList);
rc = iEditRenditionList->InsertObject(0, (pDmapv) &iRenditionProps);

// NOTE: if we were using a sub-class of doc version with properties
//  like title, author, ... we could set them here 

// Make the new document persistent
rc = iDocVer->QueryInterface(IID_IdmaConnection, (pDmapv) &iDocConnection);
rc = iDocConnection->ExecuteChange(NULL, NULL, DMA_FALSE);

// cleanup
if (iObjectFactory != NULL) iObjectFactory->Release();
if (iContent != NULL) iContent->Release();
if (iContentEls != NULL) iContentEls->Release();
if (iEditContentList != NULL) iEditContentList->Release();
if (iEditRenditionList != NULL) iEditRenditionList->Release();
if (iDocConnection != NULL) iDocConnection->Release();
if (iDocVer != NULL) iDocVer->Release();
if (iContentProps != NULL) iContentProps->Release();
if (iRenditionProps != NULL) iRenditionProps->Release();

FreeDmaString(docURLString);
FreeDmaString(single);
FreeDmaString(plainText);
}