[daisy] Programmatically create a link (VariantKey)

Bruno Dumon bruno at outerthought.org
Tue Dec 5 03:19:56 CST 2006


On Tue, 2006-12-05 at 08:16 +0100, Paul Focke wrote:
> On Monday 04 December 2006 21:58, Robert Cecil wrote:
> > Hi,
> >
> > In one of my flow scripts I am calling into the Repository to connect
> > two instances of documents. I have mapped a field in one of my doc
> > types as a Link to the second doc type via a query (documentType =
> > second type). Programmatically, I am creating an instance of both,
> > saving the second (the target of the link), and calling a setField()
> > method on the first, passing in the instance variable of the second
> > as a parameter. When I run this form I get:
> >
> > Expected was a link (org.outerj.daisy.repository.VariantKey) but got
> > a org.outerj.daisy.repository.commonimpl.DocumentImpl
> >
> > How do I 'wrap' the document instance with a Link? I could not find a
> > createLink(Document doc), createVariantKey(), or createLink() method
> > in the API.
> >
> > Thanks
> >
> > Rob Cecil
> 
> 
> Hi, 
> 
> The link field expects a variant key.  It seems like you are passing the 
> entire document.  Use the getVariantKey() method to get the variant key.  
> 
> So in your flowscript you'll have something like this : 
> 
> var document = repository.createDocument(...);
> var linkTargetDocument = repository.createDocument(...);
> // do stuff
> linkTargetDocument.save();
> document.setField("linkField", linkTargetDocument.getVariantKey());
> document.save();
> 

Just some more information:

A VariantKey instance can be created simply using "new",
i.e. new VariantKey(documentId, branchId, languageId)

The branchId and languageId in the VariantKey object can be set to the
value -1, which means the branch and language should default to the
branch and language of the document containing the link.

This is useful if you're ever going to use document variants. When
duplicating an existing variant into a new variant, the branch and
language of the link in the link-type field will then automatically
adjust to the branch and language of the new variant, otherwise they
would point to some fixed branch and language.

So to summarize, the recommended way to set a link field would be:

document.setField("linkfield", new VariantKey(linkTargetDocument.getId(), -1, -1));

-- 
Bruno Dumon                             http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno at outerthought.org                          bruno at apache.org



More information about the daisy mailing list