Working with Compound Documents

Assembling a Compound Document

The following routines assemble a compound document for simple text output. Several of the other compound document examples in this chapter use these routines. More complex assembly logic would be required for more complex compound documents. In general, Document assembly consists of taking child component documents and merging them together in the appropriate order. As a child component might itself be a compound document, assembly is a recursive process: For each child considered as a parent, we take the child component documents and merge them together in the appropriate order. And so on.

Java Example

// recursively outputs components for compound document
private void outputCompoundDocument(
    Document docParent,
    FileOutputStream outStream) throws Exception
{
    // output text for parent document
    outputContent(docParent, outStream);
            
    // iterate through child components
    DocumentSet components = docParent.get_ChildDocuments();
    Iterator iterChild = components.iterator();            
    while (iterChild.hasNext() == true)
    {
        // output text for child document (recursive call)
        Document docChild = (Document) iterChild.next();
        outputCompoundDocument(docChild, outStream);
    }            
}
        
// output content elements for document
private void outputContent(
    Document doc,
    FileOutputStream outStream) throws Exception
{
    // get content elements for document
    ContentElementList cel = doc.get_ContentElements();
    
    // iterate through content elements
    Iterator iterElement = cel.iterator();
    while (iterElement.hasNext() == true)
    {
        ContentElement ce = (ContentElement) iterElement.next();
                
        if (ce instanceof ContentTransfer)
        {
            // get input stream to content
            ContentTransfer ct = (ContentTransfer) ce;
            InputStream inStream = ct.accessContentStream();
                     
            // output content to book
            byte[] outputBytes = new byte[100];
            int byteCount = 0;
            while ((byteCount = inStream.read(outputBytes)) != -1)
            {
                outStream.write(outputBytes, 0, byteCount);                            
            }
        }        
    }        
}

C# Example

// recursively outputs components for compound document
private void outputCompoundDocument(
    IDocument docParent,
    FileStream outStream)
{
    // output text for parent document
    outputContent(docParent, outStream);
            
    // iterate through child components
    IDocumentSet components = docParent.ChildDocuments;
    foreach (IDocument docChild in components)
    {
        // output text for child document (recursive call)
        outputCompoundDocument(docChild, outStream);
    }        
}

// output content elements for document
private void outputContent(
    IDocument doc,
    FileStream outStream)
{
    // get content elements for document
    IContentElementList cel = doc.ContentElements;
    
    // iterate through content elements
    foreach (IContentElement ce in cel)
    {
        if (ce is IContentTransfer)
        {
            // get input stream to content
            IContentTransfer ct = (IContentTransfer) ce;
            Stream inStream = ct.AccessContentStream();
                     
            // output content to book
            const Int32 BYTE_MAX = 100;
            byte[] outputBytes = new byte[BYTE_MAX];
            int byteCount = 0;
            while ((byteCount = inStream.Read(outputBytes, 0, BYTE_MAX)) != 0)
            {
                outStream.Write(outputBytes, 0, byteCount);            
            }
        }        
    }        

}

Creating a Static Component Relationship

This example creates a compound document using static component relationships, where ComponentRelationshipType = STATIC_CR. In these relationships the document specified by the ParentComponent property gets bound to the version of the document explicitly set via the ChildComponent property.

Java Example

private void ExampleCreatingStaticCR(ObjectStore os) throws Exception
{
    /////////////////////////////////////////////////////////////////
    // construct compound document

    Document docBook = constructStaticBook(os);

    /////////////////////////////////////////////////////////
    // assemble compound document

    // start output stream for book
    String bookFile = "C:\\CDExample\\Output\\BookStaticCR.txt"; 
    FileOutputStream outStream = new FileOutputStream(bookFile);            

    // perform output
    outputCompoundDocument(docBook, outStream);            
    outStream.close();
            
}

private Document constructStaticBook(ObjectStore os) throws Exception
{
    // create parent document representing book as a whole
    Document docBook = Factory.Document.createInstance(os, null); 
    docBook.set_CompoundDocumentState(
        CompoundDocumentState.COMPOUND_DOCUMENT);
    docBook.checkin(null, CheckinType.MAJOR_VERSION);
    docBook.save(RefreshMode.REFRESH);            
            
    // create chapter components
    for (int chapterNumber = 1; chapterNumber < 4; chapterNumber++)
    {
        // get chapter text
        String chapterFile 
            = "C:\\CDExample\\BookText\\Chapter" + chapterNumber + ".txt";
        FileInputStream stream = new FileInputStream(chapterFile);
        ContentTransfer ct 
            = Factory.ContentTransfer.createInstance();
        ct.setCaptureSource(stream);
                
        // create content list
        ContentElementList list 
            = Factory.ContentElement.createList();
        list.add(ct);
                
        // create child document representing a chapter
        Document docChapter 
            = Factory.Document.createInstance(os, null); 
        docChapter.set_ContentElements(list);
        docChapter.checkin(null, CheckinType.MAJOR_VERSION);
        docChapter.save(RefreshMode.REFRESH);            

        // create component relationship for this chapter
        ComponentRelationship cr 
            = Factory.ComponentRelationship.createInstance(os, null);
        cr.set_ParentComponent(docBook);
        cr.set_ChildComponent(docChapter);
        cr.set_ComponentSortOrder(new Integer(chapterNumber));
        cr.set_ComponentRelationshipType(
            ComponentRelationshipType.STATIC_CR);            
        cr.save(RefreshMode.REFRESH);
    }
    return docBook;

}

C# Example

private void ExampleCreatingStaticCR(IObjectStore os)
{
    /////////////////////////////////////////////////////////////////
    // construct compound document

    IDocument docBook = constructStaticBook(os);

    /////////////////////////////////////////////////////////
    // assemble compound document

    // start output stream for book
    String bookFile = "C:\\CDExample\\Output\\BookStaticCR.txt";
    FileStream outStream = new FileStream(bookFile, FileMode.Create);

    // perform output
    outputCompoundDocument(docBook, outStream);
    outStream.Close();
            
}

private IDocument constructStaticBook(IObjectStore os)
{
    // create parent document representing book as a whole
    IDocument docBook = Factory.Document.CreateInstance(os, null); 
    docBook.CompoundDocumentState = CompoundDocumentState.COMPOUND_DOCUMENT;
    docBook.Checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
    docBook.Save(RefreshMode.REFRESH);

    // create chapter components
    for (int chapterNumber = 1; chapterNumber < 4; chapterNumber++)
    {
        // get chapter text
        String chapterFile 
            = "C:\\CDExample\\BookText\\Chapter" + chapterNumber + ".txt"
        FileStream stream = new FileStream(chapterFile, FileMode.Open);
        IContentTransfer ct = Factory.ContentTransfer.CreateInstance();
        ct.SetCaptureSource(stream);
                
        // create content list
        IContentElementList list = Factory.ContentElement.CreateList();
        list.Add(ct);
                
        // create child document representing a chapter
        IDocument docChapter = Factory.Document.CreateInstance(os, null);
        docChapter.ContentElements = list;
        docChapter.Checkin(
            AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
        docChapter.Save(RefreshMode.REFRESH);

        // create component relationship for this chapter
        IComponentRelationship cr 
            = Factory.ComponentRelationship.CreateInstance(os, null);
        cr.ParentComponent = docBook;
        cr.ChildComponent = docChapter;
        cr.ComponentSortOrder = chapterNumber;
        cr.ComponentRelationshipType = ComponentRelationshipType.STATIC_CR;
        cr.Save(RefreshMode.REFRESH);
    }   
    return docBook;

}

Creating a Dynamic Component Relationship

This example creates a compound document using dynamic component relationships, where ComponentRelationshipType = DYNAMIC_CR. In these relationships the document specified by the ParentComponent property gets bound to some version of the document set via the ChildComponent property. The version bound depends on the VersionBindType property setting; either the most recent version or the most recent major version gets bound.

Java Example

private void ExampleCreatingDynamicCR(ObjectStore os) throws Exception
{
    /////////////////////////////////////////////
    // construct compound document
    
    // create parent document representing book as a whole
    Document docBook = Factory.Document.createInstance(os, null); 
    docBook.set_CompoundDocumentState(
        CompoundDocumentState.COMPOUND_DOCUMENT);
    docBook.checkin(null, CheckinType.MAJOR_VERSION);
    docBook.save(RefreshMode.REFRESH);            
            
    // create chapter components
    for (int chapterNumber = 1; chapterNumber < 4; chapterNumber++)
    {
        // get chapter text for minor version
        String chapterFile 
            = "C:\\CDExample\\BookText\\Chapter" + chapterNumber + ".txt";
        FileInputStream stream = new FileInputStream(chapterFile);
        ContentTransfer ct = Factory.ContentTransfer.createInstance();
        ct.setCaptureSource(stream);
                
        // create content list
        ContentElementList list 
            = Factory.ContentElement.createList();
        list.add(ct);
                
        // create child document representing a chapter
        // checkin as minor version
        Document docChapter 
            = Factory.Document.createInstance(os, null); 
        docChapter.set_ContentElements(list);
        docChapter.checkin(null, CheckinType.MINOR_VERSION);
        docChapter.save(RefreshMode.REFRESH);            

        // get chapter text for major version
        String majorChapterFile 
            = "C:\\CDExample\\BookText\\MajorChapter" + chapterNumber + ".txt";
        FileInputStream majorStream 
            = new FileInputStream(majorChapterFile);
        ContentTransfer majorCt 
            = Factory.ContentTransfer.createInstance();
        majorCt.setCaptureSource(majorStream);
                
        // create content list
        ContentElementList majorList 
            = Factory.ContentElement.createList();
        majorList.add(majorCt);

        // checkin as major version
        docChapter.checkout(null, null, null, null);
        docChapter.save(RefreshMode.REFRESH);
        Document docMajorChapter 
            = (Document) docChapter.get_Reservation();
        docMajorChapter.set_ContentElements(majorList);
        docMajorChapter.checkin(null, CheckinType.MAJOR_VERSION);
        docMajorChapter.save(RefreshMode.REFRESH);            
                                
        // create component relationship for this chapter
        ComponentRelationship cr 
            = Factory.ComponentRelationship.createInstance(os, null);
        cr.set_ParentComponent(docBook);
        cr.set_ChildComponent(docChapter);
        cr.set_ComponentSortOrder(new Integer(chapterNumber));
        cr.set_ComponentRelationshipType(
            ComponentRelationshipType.DYNAMIC_CR);            
     	cr.set_VersionBindType(
            VersionBindType.LATEST_MAJOR_VERSION);
        cr.save(RefreshMode.REFRESH);
    }

    /////////////////////////////////////////////
    // assemble compound document

    // start output stream for book
    String bookFile = "C:\\CDExample\\Output\\BookDynamicCR.txt"; 
    FileOutputStream outStream = new FileOutputStream(bookFile);            

    // output compound document
    outputCompoundDocument(docBook, outStream);            
    outStream.close();
            
}

C# Example

private void ExampleCreatingDynamicCR(IObjectStore os)
{
    /////////////////////////////////////////////
    // construct compound document
    
    // create parent document representing book as a whole
    IDocument docBook = Factory.Document.CreateInstance(os, null); 
    docBook.CompoundDocumentState = CompoundDocumentState.COMPOUND_DOCUMENT;
    docBook.Checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
    docBook.Save(RefreshMode.REFRESH);            
            
    // create chapter components
    for (int chapterNumber = 1; chapterNumber < 4; chapterNumber++)
    {
        // get chapter text for minor version
        String chapterFile 
            = "C:\\CDExample\\BookText\\Chapter" + chapterNumber + ".txt";
        FileStream stream = new FileStream(chapterFile, FileMode.Open);
        IContentTransfer ct = Factory.ContentTransfer.CreateInstance();
        ct.SetCaptureSource(stream);
                
        // create content list
        IContentElementList list = Factory.ContentElement.CreateList();
        list.Add(ct);
                
        // create child document representing a chapter
        // checkin as minor version
        IDocument docChapter = Factory.Document.CreateInstance(os, null); 
        docChapter.ContentElements = list;
        docChapter.Checkin(
            AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MINOR_VERSION);
        docChapter.Save(RefreshMode.REFRESH);            

        // get chapter text for major version
        String majorChapterFile 
            = "C:\\CDExample\\BookText\\MajorChapter" + chapterNumber + ".txt";
        FileStream majorStream = new FileStream(majorChapterFile, FileMode.Open);
        IContentTransfer majorCt = Factory.ContentTransfer.CreateInstance();
        majorCt.SetCaptureSource(majorStream);
                
        // create content list
        IContentElementList majorList = Factory.ContentElement.CreateList();
        majorList.Add(majorCt);

        // checkin as major version
        docChapter.Checkout(ReservationType.OBJECT_STORE_DEFAULT, null, null, null);
        docChapter.Save(RefreshMode.REFRESH);
        IDocument docMajorChapter = (IDocument) docChapter.Reservation;
        docMajorChapter.ContentElements = majorList;
        docMajorChapter.Checkin(
            AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
        docMajorChapter.Save(RefreshMode.REFRESH);            
                                
        // create component relationship for this chapter
        IComponentRelationship cr 
            = Factory.ComponentRelationship.CreateInstance(os, null);
        cr.ParentComponent = docBook;
        cr.ChildComponent = docChapter;
        cr.ComponentSortOrder = chapterNumber;
        cr.ComponentRelationshipType = ComponentRelationshipType.DYNAMIC_CR;            
     	cr.VersionBindType = VersionBindType.LATEST_MAJOR_VERSION;
        cr.Save(RefreshMode.REFRESH);
    }

    /////////////////////////////////////////////
    // assemble compound document

    // start output stream for book
    String bookFile = "C:\\CDExample\\Output\\BookDynamicCR.txt";
    FileStream outStream = new FileStream(bookFile, FileMode.Create);

    // output compound document
    outputCompoundDocument(docBook, outStream);            
    outStream.Close();
           
}

Creating a Dynamic Label Component Relationship

This example creates a compound document using dynamic label component relationships, where ComponentRelationshipType = DYNAMIC_LABEL_CR. In these relationships the document specified by the ParentComponent property gets bound to some version of the document set via the ChildComponent property. The version bound depends on the VersionBindType property setting and on the LabelBindValue property. The VersionBindType determines the document versions eligible for binding; either all versions or all major versions become candidates for binding. The most recent eligible version that has a matching label gets bound. The ComponentBindingLabel property on the Document object specifies the label. A match occurs when both this label and the LabelBindValue have the same value, or this label has a non-null value and LabelBindValue a null value.

Java Example

private void ExampleCreatingDynamicLabelCR(ObjectStore os) throws Exception
{
    /////////////////////////////////////////////
    // construct compound document
            
    // create parent document representing insurance policy as a whole
    Document docPolicy = Factory.Document.createInstance(os, null); 
               
    docPolicy.set_CompoundDocumentState(
        CompoundDocumentState.COMPOUND_DOCUMENT);
    docPolicy.checkin(null, CheckinType.MAJOR_VERSION);
    docPolicy.save(RefreshMode.REFRESH);             

    // create paragraph components
    Document docParagraph2 = null;
    for (int paraNumber = 1; paraNumber < 4; paraNumber++)
    {
        // get chapter text
        String paraFile 
            = "C:\\CDExample\\InsurancePolicy\\Paragraph" + paraNumber + ".txt";
        FileInputStream stream = new FileInputStream(paraFile);
        ContentTransfer ct = Factory.ContentTransfer.createInstance();
        ct.setCaptureSource(stream);
                
        // create content list
        ContentElementList list = Factory.ContentElement.createList();
        list.add(ct);
                
        // create child document representing a paragraph
        Document docParagraph 
            = Factory.Document.createInstance(os, null); 
        docParagraph.set_ContentElements(list);
        docParagraph.checkin(null, CheckinType.MAJOR_VERSION);
        docParagraph.save(RefreshMode.REFRESH);            

        // save reference to paragraph 2
        if (paraNumber == 2)
        {
            docParagraph2 = docParagraph;
        }
                
        // create component relationship for this paragraph
        ComponentRelationship cr 
            = Factory.ComponentRelationship.createInstance(os, null);
        cr.set_ParentComponent(docPolicy);
        cr.set_ChildComponent(docParagraph);
        cr.set_ComponentSortOrder(new Integer(paraNumber));
        cr.set_ComponentRelationshipType(
            ComponentRelationshipType.STATIC_CR);            
        cr.save(RefreshMode.REFRESH);
    }

    // load state abbreviations
    String states[] =
        { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", 
          "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA",
          "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", 
          "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", 
          "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", 
          "VA", "WA", "WV", "WI", "WY"
        };            

    final String COMPONENT_BINDING_LABEL = "ComponentBindingLabel";

    // set paragraph 2 as compound document                    
    docParagraph2.set_CompoundDocumentState(
        CompoundDocumentState.COMPOUND_DOCUMENT);
    docParagraph2.save(RefreshMode.REFRESH);                 
            
    // create state sub-components for paragraph 2
    for (int index = 0; index < states.length; index++)
    {
        // get chapter text
        String stateFile 
            = "C:\\CDExample\\InsurancePolicy\\" + states[index] + ".txt";
        FileInputStream stream = new FileInputStream(stateFile);
        ContentTransfer ct = Factory.ContentTransfer.createInstance();
        ct.setCaptureSource(stream);
                
        // create content list
        ContentElementList list = Factory.ContentElement.createList();
        list.add(ct);
                
        // create state document
        Document docState = Factory.Document.createInstance(os, null); 
        docState.set_ContentElements(list);
        docState
            .getProperties()
            .putValue(COMPONENT_BINDING_LABEL, states[index]);                
        docState.checkin(null, CheckinType.MAJOR_VERSION);
        docState.save(RefreshMode.REFRESH);            

        // create component relationship for this state
        ComponentRelationship cr 
            = Factory.ComponentRelationship.createInstance(os, null);
        cr.set_ParentComponent(docParagraph2);
        cr.set_ChildComponent(docState);
        cr.set_ComponentRelationshipType(
            ComponentRelationshipType.DYNAMIC_LABEL_CR);            
        cr.set_VersionBindType(
            VersionBindType.LATEST_MAJOR_VERSION);
        cr.set_LabelBindValue(states[index]);
        cr.save(RefreshMode.REFRESH);
    }
            
    /////////////////////////////////////////////
    // assemble compound document

    // start output stream for book
    String bookFile 
        = "C:\\CDExample\\Output\\PolicyDynamicLabelCR.txt"; 
    FileOutputStream outStream = new FileOutputStream(bookFile);            
            
    // create policy for a specific state
    // iterate through child relationships for paragraph 2 
    String policyState = "AK";
    ComponentRelationshipSet crSet 
        = docParagraph2.get_ChildRelationships();
    Iterator iterCR = crSet.iterator();
    while (iterCR.hasNext() == true)
    {
        ComponentRelationship cr 
            = (ComponentRelationship) iterCR.next();
        cr.set_LabelBindValue(policyState);
        cr.save(RefreshMode.NO_REFRESH);
    }
            
    // refresh paragraph 2 
    // causes ChildDocuments property to reflect correct policy state
    docParagraph2.refresh();
            
    // output document
    outputCompoundDocument(docPolicy, outStream);
            
}

C# Example

private void ExampleCreatingDynamicLabelCR(IObjectStore os)
{
    /////////////////////////////////////////////
    // construct compound document
            
    // create parent document representing insurance policy as a whole
    IDocument docPolicy = Factory.Document.CreateInstance(os, null); 
               
    docPolicy.CompoundDocumentState = CompoundDocumentState.COMPOUND_DOCUMENT;
    docPolicy.Checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
    docPolicy.Save(RefreshMode.REFRESH);             

    // create paragraph components
    IDocument docParagraph2 = null;
    for (int paraNumber = 1; paraNumber < 4; paraNumber++)
    {
        // get chapter text
        String paraFile 
            = "C:\\CDExample\\InsurancePolicy\\Paragraph" + paraNumber + ".txt";
        FileStream stream = new FileStream(paraFile, FileMode.Open);
        IContentTransfer ct = Factory.ContentTransfer.CreateInstance();
        ct.SetCaptureSource(stream);
                
        // create content list
        IContentElementList list = Factory.ContentElement.CreateList();
        list.Add(ct);
                
        // create child document representing a paragraph
        IDocument docParagraph = Factory.Document.CreateInstance(os, null); 
        docParagraph.ContentElements = list;
        docParagraph.Checkin(
            AutoClassify.DO_NOT_AUTO_CLASSIFY,
            CheckinType.MAJOR_VERSION);
        docParagraph.Save(RefreshMode.REFRESH);            

        // save reference to paragraph 2
        if (paraNumber == 2)
        {
            docParagraph2 = docParagraph;
        }
                
        // create component relationship for this paragraph
        IComponentRelationship cr 
            = Factory.ComponentRelationship.CreateInstance(os, null);
        cr.ParentComponent = docPolicy;
        cr.ChildComponent = docParagraph;
        cr.ComponentSortOrder = paraNumber;
        cr.ComponentRelationshipType = ComponentRelationshipType.STATIC_CR;    
        cr.Save(RefreshMode.REFRESH);
    }

    // load state abbreviations
    String[] states =
        { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", 
          "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA",
          "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", 
          "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", 
          "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", 
          "VA", "WA", "WV", "WI", "WY"
        };            

    const String COMPONENT_BINDING_LABEL = "ComponentBindingLabel";

    // set paragraph 2 as compound document                    
    docParagraph2.CompoundDocumentState = CompoundDocumentState.COMPOUND_DOCUMENT;
    docParagraph2.Save(RefreshMode.REFRESH);                 
            
    // create state sub-components for paragraph 2
    for (int index = 0; index < states.Length; index++)
    {
        // get chapter text
        String stateFile 
            = "C:\\CDExample\\InsurancePolicy\\" + states[index] + ".txt";
        FileStream stream = new FileStream(stateFile, FileMode.Open);
        IContentTransfer ct = Factory.ContentTransfer.CreateInstance();
        ct.SetCaptureSource(stream);
                
        // create content list
        IContentElementList list = Factory.ContentElement.CreateList();
        list.Add(ct);
                
        // create state document
        IDocument docState = Factory.Document.CreateInstance(os, null); 
        docState.ContentElements = list;
        docState.Checkin(
            AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
        docState.Save(RefreshMode.REFRESH);            
                
        docState
            .Properties
            .GetProperty(COMPONENT_BINDING_LABEL)
            .SetObjectValue(states[index]);
        docState.Save(RefreshMode.REFRESH);            

        // create component relationship for this state
        IComponentRelationship cr 
            = Factory.ComponentRelationship.CreateInstance(os, null);
        cr.ParentComponent = docParagraph2;
        cr.ChildComponent = docState;
        cr.ComponentRelationshipType = ComponentRelationshipType.DYNAMIC_LABEL_CR;
        cr.VersionBindType = VersionBindType.LATEST_MAJOR_VERSION;
        cr.LabelBindValue = states[index];
        cr.Save(RefreshMode.REFRESH);
    }
            
    /////////////////////////////////////////////
    // assemble compound document

    // start output stream for book
    String bookFile = "C:\\CDExample\\Output\\PolicyDynamicLabelCR.txt"; 
    FileStream outStream = new FileStream(bookFile, FileMode.Create);            
            
    // create policy for a specific state
    // iterate through child relationships for paragraph 2 
    String policyState = "AK";
    IComponentRelationshipSet crSet = docParagraph2.ChildRelationships;

    foreach (IComponentRelationship cr in crSet)
    {
        cr.LabelBindValue = policyState;
        cr.Save(RefreshMode.NO_REFRESH);
    }
            
    // refresh paragraph 2 
    // causes ChildDocuments property to reflect correct policy state
    docParagraph2.Refresh();
            
    // output document
    outputCompoundDocument(docPolicy, outStream);
            
}

Deleting Components

This example uses the ComponentCascadeDelete property to automatically delete child components upon deletion of the parent component. Note that, using the ComponentPreventDelete property, compound document integrity can be protected by preventing the deletion of the parent or a child component.

Java Example

private void ExampleDeletingComponents(ObjectStore os) throws Exception
{
    // create book compound document
    Document docBook = constructStaticBook(os);
                
    // iterate through component relationships
    // set cascade delete
    Document docChapter = null;
    Iterator crIter = docBook.get_ChildRelationships().iterator();
    while (crIter.hasNext() == true)
    {
        // update component relationship
        ComponentRelationship cr = (ComponentRelationship) crIter.next();
        cr.set_ComponentCascadeDelete(
            ComponentCascadeDeleteAction.CASCADE_DELETE);
        cr.save(RefreshMode.REFRESH);                

        // retain last chapter
        docChapter = cr.get_ChildComponent();
    }
            
    // delete book
    // causes cascade delete for all chapters
    docBook.delete();
    docBook.save(RefreshMode.REFRESH);
            
    // attempt to refresh chapter (now deleted)
    try
    {
        docChapter.refresh();
    }
    catch (Exception exc)
    {
        System.out.println("Chapters cascade-deleted (as expected): "
                        + exc.getMessage());
    }
}

C# Example

private void ExampleDeletingComponents(IObjectStore os)
{
    // create book compound document
    IDocument docBook = constructStaticBook(os);
                
    // iterate through component relationships
    // set cascade delete
    IDocument docChapter = null;
    foreach (IComponentRelationship cr in docBook.ChildRelationships)
    {
        // update component relationship
        cr.ComponentCascadeDelete = ComponentCascadeDeleteAction.CASCADE_DELETE;
        cr.Save(RefreshMode.REFRESH);                

        // retain last chapter
        docChapter = cr.ChildComponent;
    }
            
    // delete book
    // causes cascade delete for all chapters
    docBook.Delete();
    docBook.Save(RefreshMode.REFRESH);
            
    // attempt to refresh chapter (now deleted)
    try
    {
        docChapter.Refresh();
    }
    catch (System.Exception exc)
    {
        Console.WriteLine("Chapters cascade-deleted (as expected): "
                        		+ exc.Message);
    }
	
}

Versioning Parents

This example uses the CopyToReservation property to control which of the existing child components for a document automatically become child components for the next version of the document. By default, the next version of the parent document—created by document check-out and check-in—has the same relationship to all of the same child components as the previous version. You can change this so that the next version only has some (or none) of the same child component as the previous version.

Java Example

private void ExampleVersioningParents(ObjectStore os) throws Exception
{
    // create book compound document
    Document docBook1 = constructStaticBook(os);
            
    // iterate through component relationships
    // set copy-to-reservation property
    int b1ChapterCount = 0;
    Iterator crIter1 = docBook1.get_ChildRelationships().iterator();
    while (crIter1.hasNext() == true)
    {
        // increment count
        b1ChapterCount++;
                
        // update component relationship
        ComponentRelationship cr 
            = (ComponentRelationship) crIter1.next();
        cr.set_CopyToReservation(Boolean.TRUE);
        cr.save(RefreshMode.REFRESH);                
    }

    // create new version of book
    docBook1.checkout(null, null, null, null);
    docBook1.save(RefreshMode.REFRESH);
    Document docBook2 = (Document) docBook1.get_Reservation();
    docBook2.checkin(null, CheckinType.MAJOR_VERSION);
    docBook2.save(RefreshMode.REFRESH);                 
            
    // new version should point to same chapters as previous version
    int b2ChapterCount = 0;
    Iterator crIter2 = docBook2.get_ChildRelationships().iterator();
    while (crIter2.hasNext() == true)
    {
        // increment count
        crIter2.next();
        b2ChapterCount++;
    }
            
    System.out.println("Book 1 chapter count: " + b1ChapterCount);
    System.out.println("Book 2 chapter count: " + b2ChapterCount);
}

C# Example

private void ExampleVersioningParents(IObjectStore os)
{
    // create book compound document
    IDocument docBook1 = constructStaticBook(os);
            
    // iterate through component relationships
    // set copy-to-reservation property
    int b1ChapterCount = 0;
    foreach (IComponentRelationship cr in docBook1.ChildRelationships)
    {
        // increment count
        b1ChapterCount++;
                
        // update component relationship
        cr.CopyToReservation = true;
        cr.Save(RefreshMode.REFRESH);                
    }

    // create new version of book
    docBook1.Checkout(ReservationType.OBJECT_STORE_DEFAULT, null, null, null);
    docBook1.Save(RefreshMode.REFRESH);
    IDocument docBook2 = (IDocument) docBook1.Reservation;
    docBook2.Checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
    docBook2.Save(RefreshMode.REFRESH);                 
            
    // new version should point to same chapters as previous version
    int b2ChapterCount = 0;
    foreach (IComponentRelationship cr in docBook2.ChildRelationships)
    {
        // increment count
        b2ChapterCount++;
    }
            
    Console.WriteLine("Book 1 chapter count: " + b1ChapterCount);
    Console.WriteLine("Book 2 chapter count: " + b2ChapterCount);

}