The following routines assemble a compound document for simple text output. Several of the other compound document examples 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 in the appropriate order. As a child component might itself be a compound document, assembly is a recursive process: for each child that is considered as a parent, the child component documents are taken and merged 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);
}
}
}
}
This example creates a compound document by using static component relationships, where ComponentRelationshipType = STATIC_CR. In these relationships, the document that is specified by the ParentComponent property gets bound to the version of the document that is explicitly set by 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";
// non-Windows: String bookFile = "/tmp/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";
// non-Windows: String chapterFile = "/tmp/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);
// Ceate 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;
}
This example creates a compound document by using dynamic component relationships, where ComponentRelationshipType = DYNAMIC_CR. In these relationships, the document that is specified by the ParentComponent property gets bound to some version of the document set by the ChildComponent property. The version that is 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";
// non-Windows: String chapterFile = "/tmp/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";
// non-Windows: String majorChapterFile = "/tmp/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";
// non-Windows: String bookFile = "/tmp/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();
}
This example creates a compound document by using dynamic label component relationships, where ComponentRelationshipType = DYNAMIC_LABEL_CR. In these relationships, the document that is specified by the ParentComponent property gets bound to some version of the document set by the ChildComponent property. The version that is 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";
// non-Windows: String paraFile = "/tmp/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 subcomponents for paragraph 2.
for (int index = 0; index < states.length; index++)
{
// Get chapter text.
String stateFile
= "C:\\CDExample\\InsurancePolicy\\" + states[index] + ".txt";
// non-Windows: String stateFile = "/tmp/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";
// non-Windows: String bookFile = "/tmp/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 subcomponents 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);
}
This example uses the ComponentCascadeDelete property to automatically delete child components upon deletion of the parent component. Note that, when 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);
}
}
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, which is 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 relationship so that the next version has only 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);
}