Difference between revisions of "Document Template Classes"
imported>Aeric |
imported>Aeric |
||
Line 1: | Line 1: | ||
===About Print Template Classes=== | ===About Print Template Classes=== | ||
You can use a method in a Print Template Class to manipulate the data that a Print Template has available for processing. Here's how it works: | You can use a method in a Print Template Class to manipulate the data that a Print Template has available for processing. Here's how it works: | ||
Line 6: | Line 4: | ||
:* When a [[Print Template]] is processed, it gets its data from the [{{DOCHOST}}/javadocs/com/platform/api/TemplateContext.html TemplateContext] | :* When a [[Print Template]] is processed, it gets its data from the [{{DOCHOST}}/javadocs/com/platform/api/TemplateContext.html TemplateContext] | ||
:* The TemplateContext contains a HashMap for each record the Print Template | :* The TemplateContext contains a HashMap for each record sent to the Print Template, indexed by an Object name (a string). The HashMap contains name/value pairs for each field in that record. | ||
:* To a Print Template, then, a data object is simply a HashMap that is present in the TemplateContext. | :* To a Print Template, then, a data object is simply a HashMap that is present in the TemplateContext. | ||
Line 22: | Line 16: | ||
:* Get a field from data HashMap: <tt>HashMap.get("''fieldName''");</tt> | :* Get a field from data HashMap: <tt>HashMap.get("''fieldName''");</tt> | ||
:* Add or update a field in a data HashMap: <tt>HashMap.put("''fieldName''",''value'');</tt><br>where <tt>''value''</tt> is typically a string or a nested HashMap. | :* Add or update a field in a data HashMap: <tt>HashMap.put("''fieldName''",''value'');</tt><br>where <tt>''value''</tt> is typically a string or a nested HashMap. | ||
===Configuring a Print Template to use a Specified Class and Method=== | ===Configuring a Print Template to use a Specified Class and Method=== | ||
{{TBD|select the class, and the method to use}} | |||
Methods that take following arguments are listed: | Methods that take following arguments are listed: | ||
Line 33: | Line 25: | ||
:* <tt>String</tt> (recordID) - The ID of the record on which it was invoked. | :* <tt>String</tt> (recordID) - The ID of the record on which it was invoked. | ||
::(Method return values are ignored. Such methods generally return <tt>void</tt>, but they don't have to.) | |||
===Accessing Lookup Target Records=== | ===Accessing Lookup Target Records=== | ||
Data from lookup-target records is stored as a nested map. For example, to get data for the template variable <tt>$Order.customer.name</tt>: | Data from lookup-target records is stored as a nested map. For example, to get data for the template variable <tt>$Order.customer.name</tt>: | ||
# <tt>HashMap orderMap = TemplateContext.get("Order")</tt><br> gets the Order HashMap from the context. | # <tt>HashMap orderMap = TemplateContext.get("Order")</tt><br> gets the Order HashMap from the context. | ||
# <tt>HashMap customerMap = orderMap.get("customer")</tt><br> gets the lookup-target record for the customer field. | # <tt>HashMap customerMap = orderMap.get("customer")</tt><br> gets the lookup-target record for the customer field. | ||
# <tt>String name = customerMap.get("name")</tt><br> gets the customer's name. | # <tt>String name = customerMap.get("name")</tt><br> gets the customer's name. | ||
===Accessing Related Records=== | |||
A Print Template is always invoked on a specific record. Data from related-object records that look up to that record is contained in an ArrayList of HashMaps. | |||
{{TBD|syntax details}} | |||
===Examples=== | ===Examples=== |
Revision as of 22:42, 6 April 2012
About Print Template Classes
You can use a method in a Print Template Class to manipulate the data that a Print Template has available for processing. Here's how it works:
- When a Print Template is processed, it gets its data from the TemplateContext
- The TemplateContext contains a HashMap for each record sent to the Print Template, indexed by an Object name (a string). The HashMap contains name/value pairs for each field in that record.
- To a Print Template, then, a data object is simply a HashMap that is present in the TemplateContext.
- That object is passed to a method in your Print Template class. By manipulating the HashMaps it contains, you change the data that the Print Template will be processing. You can even add new "objects", by adding additional HashMaps.
Coding a Method in a Print Template Class
In a Print Template, a data reference has the form $objectName.fieldname. To access, create, or modify data in your code, you will do one or more of the following operations:
- Retrieve an object's HashMap from the TemplateContext: TemplateContext.get("objectName");
- Add or update a data HashMap: TemplateContext.put("objectName", HashMap);
- Get a field from data HashMap: HashMap.get("fieldName");
- Add or update a field in a data HashMap: HashMap.put("fieldName",value);
where value is typically a string or a nested HashMap.
Configuring a Print Template to use a Specified Class and Method
__TBD: select the class, and the method to use__
Methods that take following arguments are listed:
- com.platform.api.TemplateContext - The container that the print template gets its data from.
- String (objectName) - The name of the object the print template was invoked on.
- String (recordID) - The ID of the record on which it was invoked.
- (Method return values are ignored. Such methods generally return void, but they don't have to.)
Accessing Lookup Target Records
Data from lookup-target records is stored as a nested map. For example, to get data for the template variable $Order.customer.name:
- HashMap orderMap = TemplateContext.get("Order")
gets the Order HashMap from the context. - HashMap customerMap = orderMap.get("customer")
gets the lookup-target record for the customer field. - String name = customerMap.get("name")
gets the customer's name.
Accessing Related Records
A Print Template is always invoked on a specific record. Data from related-object records that look up to that record is contained in an ArrayList of HashMaps.
__TBD: syntax details__
Examples
Changing Field Data
This example substitutes the name of the state for its abbreviation, when processing a Customer record:
public void processData(TemplateContext context, String object, String record) { HashMap customerMap = (HashMap)context.get("Customer"); String state = customerMap.get("state").toString(); if ( "CA".equals(state) ) { customerMap.put("state","California"); } }
Example: Adding a New Field
This example adds a new "Good Customer" field to the record.
public void processData(TemplateContext context, String object, String record) { HashMap customerMap = (HashMap)context.get("Customer"); customerMap.put("rating", "Good Customer"); } }
After that method has run, the Print Template can use the variable $Customer.rating, just as though that field was defined in the platform object.
Example: Adding a New Object
This code creates a new ProductSupplier "object" (as far as the Print Template is concerned), and adds it to the TenantContext (context).
public void processData(TemplateContext context, String object, String record) { HashMap<String, Object> productSupplierMap = new HashMap<String, Object>(); productSupplierMap.put("supplierName", "Stuff R' Us"); productSupplierMap.put("phone", "408-555-0987"); context.put("ProductSupplier", productSupplierMap); }
After that method has run, the Print Template can use the variable $ProductSupplier.phone, just as though the data had originated in the platform.