AgileApps Support Wiki Pre Release

Difference between revisions of "Java API:Document Management"

From AgileApps Support Wiki
imported>Aeric
imported>Aeric
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
The Document Management [[Java API]]s  are used to generate and manage documents.
The Document Management [[Java API]]s  are used to generate and access documents.
== generateDocument ==
== generateDocument ==
{{:generateDocument}}
{{:generateDocument}}
Line 7: Line 7:


== Example: Generate an Attachment ==
== Example: Generate an Attachment ==
{{Code:Generate an Attachment}}
{{:Code:Generate an Attachment}}
<noinclude>
<noinclude>
__FORCETOC__
__FORCETOC__

Latest revision as of 02:28, 17 January 2014

The Document Management Java APIs are used to generate and access documents.

generateDocument

Generates a document based on an HTML Document Template.

Syntax

Result result;
result = Functions.generateDocument(String object, String recordID, 
                                    String templateID, String format);

Parameters

object
The name or identifier of the object that contains the record of interest.
recordID
The identifier of the record to pass to the template.
printTemplate
The identifier of the Document Template.
To get the template ID:
  • Go to GearIcon.png > Objects > {object} > Document Templates
  • Click the Wrench icon to edit the view or create a new one
  • Add the recordID field to the view
  • In the listing, find the ID of the Document Template you'll be using
format
CONSTANTS.DOCUMENT.HTML -or- CONSTANTS.DOCUMENT.PDF
This option applies to HTML and PDF templates. Word templates always produce Word files. PowerPoint templates produce PowerPoint files.

Return

Result object
Usage
Example
This example calls generateDocument on a case to create an HTML document.
String printTemplate = "";     // Code this value
String recordID = "";          // Get this value from incoming parameters
  ...
Result result = Functions.generateDocument("cases", recordID, printTemplate, CONSTANTS.DOCUMENT.HTML);
int resultCode = result.getCode();
if(resultCode < 0)
{
    String msg = "Some Message";
    Logger.info(msg + ":\n" + result.getMessage(), "Doc"); // Log details
    Functions.throwError(msg + ".");                       // Error message
}
else
{
    String doc_id = result.getID();
    PlatformFileBean file = Functions.getDocument(doc_id);

    // Additional business logic...
}


getDocument

Retrieves a document specified by its ID.

Syntax
Result result = Functions.getDocument(String documentID);
Parameters
documentID - The ID of a document stored in the platform.
Returns
Result object that contains the document in the form of a PlatformFileBean.
Usage
  1. Use result.getParameters() to get the params from the Result object.
  2. Call getPlatformFileBean() on the params, passing the document ID as a string.
  3. If needed, call getBytes() on the PlatformFileBean to get document content in a byte array.
Example
This example logs the size and name associated with a document.
Result result = Functions.getDocument(documentId);
Parameters params = result.getParameters();
PlatformFileBean file = params.getPlatformFileBean(documentId);
byte[] bytes = file.getBytes();
String msg = "Name:"+file.getName()+", size:"+file.getEncodedFileContent().length();
Logger.info(msg, "Document");


Example: Generate an Attachment

This example uses a Document Template to generate a PDF or HTML document, and then attaches the document to the current case. It is expected that the method will be invoked from a Rule.

In outline, the process is:

  1. Get the record ID from the incoming method parameters.
  2. Use the generateDocument API to create a PDF (or HTML) document from an existing template.
  3. Use the getDocument API to retrieve it, in the form of a PlatformFileBean.
  4. Use the addRecord API to attach the document to the case.
package com.platform.yourCompany.yourPackage;

import com.platform.api.*;
import com.platform.beans.*;
//import java.util.*;

public class UtilityFunctions
{
  // This signature allows the method to be invoked from a rule.
  // We assume it is invoked on a Case record.
  public void generateAttachment(com.platform.api.Parameters inParams)
     throws Exception
  {
     String documentTitle = "PUT TITLE OF GENERATED DOCUMENT HERE";
     String templateID = "PUT ID OF DOCUMENT TEMPLATE HERE";

     // Get the record ID from the incoming parameters
     String objectID = inParams.get("object_id");
     String recordID = inParams.get("id");

     // Generate the document
     Result result = Functions.generateDocument(objectID, recordID, templateID, 
                                                CONSTANTS.DOCUMENT.HTML);
                                          // or CONSTANTS.DOCUMENT.PDF 
     int resultCode = result.getCode();
     if (resultCode < 0) {
        String msg = "Document generation failed";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
     }

     // Retrieve the document as a PlatformFileBean
     String docID = result.getId();
     result = Functions.getDocument(docID);
     resultCode = result.getCode();
     if (resultCode < 0) {
        String msg = "Failed to retrieve the document";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
     }
     Parameters docParams = result.getParameters();
     PlatformFileBean fileBean = docParams.getPlatformFileBean(docID);

     // Add the document as an attachment
     Parameters params = Functions.getParametersInstance();
     params.add("title", documentTitle);
     params.add("file_field", fileBean );
     params.add("related_to", objectID+":"+recordID);
     result = Functions.addRecord("attachments", params);
     resultCode = result.getCode();
     if (resultCode < 0) {
        String msg = "Failed to attach document to case";
        Logger.info(msg + ":\n" + result.getMessage(), "genAttachment");
        Functions.throwError(msg + ".");
        return;
     }
  }
}

Thumbsup.gif

Tip:
For a more general solution, add function parameters to specify the document title and the template to use. Then write a wrapper method to supply a specific template, something like this:

public void generate_A1_Attachment(com.platform.api.Parameters inParams)
   throws Exception
{
   String docTitle = "A1 Attachment";
   String templateID = "9ewr8aasd923234ased0897234d";
   generateAttachment(inParams, docTitle, templateID);
}

Then, when you create the invocation Rule, you'll choose which wrapper method to use.