Creating JavaScript sweep action handler to download document content
Adapt the following item in the JavaScript example:
Output directory: Change the output directory in the script. Because the JavaScript code is run on a Content Platform Engine server, the content download directory that you specify must satisfy the following requirements:
- It exists on the server as an existing local directory or a mapped network directory
- It is one to which the server has write permissions.
- It is accessible to the Datacap application that reads the files from this location during the scan task.
JavaScript sweep action script:
importPackage(Packages.com.filenet.api.core);
importPackage(Packages.com.filenet.api.constants);
importPackage(Packages.com.filenet.api.exception);
importPackage(Packages.com.filenet.api.sweep);
importPackage(Packages.com.filenet.api.engine);
/* Implement for custom job and queue sweeps. */
function onSweep(sweepObject, sweepItems) {
var hcc = HandlerCallContext.getInstance();
hcc.traceDetail("Entering DatacapSweepHandler.onSweep");
hcc.traceDetail("sweepObject = " +
sweepObject.getProperties().getIdValue(PropertyNames.ID) +
", sweepItems.length = " + sweepItems.length);
/* Iterate the sweepItems */
idx = 0;
for (idx = 0; idx < sweepItems.length; idx++) {
/* At the top of your loop, always check to make sure
* that the server is not shutting down.
* If it is, clean up and return control to the server.
*/
if (hcc != null && hcc.isShuttingDown()) {
throw new EngineRuntimeException(ExceptionCode.E_BACKGROUND_TASK_TERMINATED,
this.constructor.name + " is terminating prematurely because the server is shutting down");
}
var item = sweepItems[idx].getTarget();
hcc.traceDetail("sweepItems[" + idx + "]= " + item.getProperties().getIdValue("ID"));
try {
var CEObject = Document(item);
var celist = CEObject.get_ContentElements();
var docID = CEObject.getProperties().getIdValue("ID");
for (i = 0; i < celist.size(); i++)
{
var ce = celist.get(i);
var folderName = "X://sweep"; /* output directory */
this._downloadContent(folderName, docID, ce);
}
/* Set outcome to PROCESSED if item processed successfully.*/
sweepItems[idx].setOutcome(SweepItemOutcome.PROCESSED,
"item processed by " + this.constructor.name);
}
/* Set failure status on objects that fail to process.*/
catch (ioe) {
sweepItems[idx].setOutcome(SweepItemOutcome.FAILED, "DatacapSweepHandler: " +
ioe.rhinoException.getMessage());
}
}
hcc.traceDetail("Exiting DatacapSweepHandler.onSweep");
}
/* private function to download content elements.
* the folderName should use the forward slash "/" as path separator.
* If the file exists it will be overwritten.
*/
function _downloadContent(folderName, docID, ct)
{
var out = new java.io.FileOutputStream(folderName + "//" + docID +"."+ ct.get_RetrievalName());
var docLen = ct.get_ContentSize().intValue();
var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, docLen);
var stream = ct.accessContentStream();
stream.read(buf, 0, docLen);
out.write(buf);
out.flush();
stream.close();
out.close();
}
/* Called automatically when the handler is invoked by a custom sweep job
* or sweep policy. Specify properties required by the handler, if any.
* If you return an empty array, then all properties are fetched.
*/
function getRequiredProperties() {
var pnames = [];
return pnames.toString();
}
/* Implement for custom sweep policies.
* This method is not implemented because this is an example of a custom sweep job.
*/
function onPolicySweep (sweepObject, policyObject, sweepItems)
{}