Suppose you start out with the "Simple Document" document type, but after a while decide you want to switch to your own document type with custom fields and/or parts. You can easily migrate your existing documents to the new document type using a document task. A document task is some task that you want to perform on a set of documents.
To start, make sure you're in the Administrator role.
In the Tools menu, select 'Document tasks'.
Select 'create a new document task'.
If you want to change all your documents, select 'Add documents resulting from a query', and enter the following query:
select name where documentType = 'SimpleDocument'
and press the execute button.
If you want to limit the conversion to the documents in a certain collection, use instead:
select name where documentType = 'SimpleDocument' and InCollection('MyCollection')
Press the next button. Now you need to enter a script (in Javascript) to perform the desired action.
The most basic script to change the document type is as follows:
var document = repository.getDocument(variantKey, true);
document.changeDocumentType("MyNewDocumentType");
document.save();
However, it might be that you want to initialiase a field to some value, or move the data from one part to another. The listing below shows an example of how to do that. The easiest way to move the data from one part to another would be to move it as a byte array, however a better way is to provide a "PartDataSource", since then everything is done with streams, meaning that it will also work fine for arbitrarily large parts.
var document = repository.getDocument(variantKey, true);
document.changeDocumentType("MyDocType");
var oldPart = document.getPart("SimpleDocumentContent");
// Here we provide an implementation of the Java interface
// PartDataSource by means of Javascript...
var partSource = new Object();
partSource.part = oldPart;
partSource.createInputStream = function() {
return this.part.getDataStream();
};
partSource.getSize = function() {
return this.part.getSize();
};
var javaPartSource = new Packages.org.outerj.daisy.repository.PartDataSource(partSource);
// Set a new part
document.setPart("MyPart", oldPart.getMimeType(), javaPartSource);
// And remove the old part
document.deletePart("SimpleDocumentContent");
// Set some field
document.setField("MyField", new java.lang.String("boe"));
document.save();
// Note: if the document would not be valid (e.g. required fields
// not supplied with a value yet), you can pass a boolean to
// the save method to disable validation, like this:
// document.save(false);
See also the javadocs of the repository API (included in the binary Daisy download) for more information on what methods you have at your disposal.
When you're done entering the script, press the Start button.