API Quick Starts (Code Examples) > Forms Service API Quick Starts > Quick Start: Prepopulating dynamic forms using the web service API

Quick Start: Prepopulating dynamic forms using the web service API
The following code example prepopulates a dynamic form with a dynamic data source. That is, the data source is created at run-time and is not contained within an XML file or created during design time. The form is rendered as HTML.
This code example contains three user-defined methods:
createDataSource: Creates an org.w3c.dom.Document object that represents the data source that is used to prepopulate the form. This user-defined method returns an org.w3c.dom.Document object.
convertDataSource: Converts an org.w3c.dom.Document object to a BLOB object. This method accepts an org.w3c.dom.Document object as an input parameter and returns a BLOB object.
renderPOForm: Uses the Forms ES service web service API to render a dynamic purchase order form. The BLOB object that was returned by the convertDataSource method is used to prepopulate the form.
All of these methods are invoked from within the Java servlet’s doPost method. (See Prepopulating Dynamic Forms.)
/*
 * Ensure that you create the Java proxy classes to use 
 * base64 encoding. This is required to populate a BLOB 
 * object with data or retrieve data from a BLOB object.
 * 
 * For information, see "Creating Java proxy classes using Apache Axis" 
 * in Programming with LiveCycle ES.  
 */
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
 
 
//Import DOM libraries
import org.w3c.dom.Element;
import javax.xml.parsers.*;
 
//Import Java Tranformation classes
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import javax.xml.rpc.holders.LongHolder;
import javax.xml.rpc.holders.StringHolder;
import com.adobe.idp.services.BLOB;
import com.adobe.idp.services.FormsService;
import com.adobe.idp.services.FormsServiceServiceLocator;
import com.adobe.idp.services.HTMLRenderSpec;
import com.adobe.idp.services.HTMLToolbar;
import com.adobe.idp.services.TransformTo;
import com.adobe.idp.services.URLSpec;
import com.adobe.idp.services.holders.BLOBHolder;
import com.adobe.idp.services.holders.FormsResultHolder;
public class RenderPrepopulateFormWS extends HttpServlet implements Servlet {
 
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
			doPost(req,resp);
 
	}
 
 
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
			
		
		//Create an org.w3c.dom.Document object
		org.w3c.dom.Document myDom = createDataSource();
						
		//Convert the org.w3c.dom.Document object
		//to a BLOB object
		BLOB formData = convertDataSource(myDom);
			
		//Render the dynamic form using data located within the 
		//BLOB object
		renderPOForm(resp,formData);
	 }
	 
	//Creates an org.w3c.dom.Document object
	 private org.w3c.dom.Document createDataSource()
	 {
		org.w3c.dom.Document document = null;
		
		try
		{
			//Create DocumentBuilderFactory and DocumentBuilder objects
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
 
			//Create a new Document object
			document = builder.newDocument(); 
 
			//Create the root element and append it to the XML DOM
			Element root = (Element)document.createElement("transaction");
			document.appendChild(root);
			
			//Create the header element
			Element header = (Element)document.createElement("header");
			root.appendChild(header);
 
			//Create the txtPONum element and append it to the
			//header element
			Element txtPONum = (Element)document.createElement("txtPONum");
			txtPONum.appendChild(document.createTextNode("8745236985"));
			header.appendChild(txtPONum);
 
			//Create the dtmDate element and append it to the 
			//header element	
			Element dtmDate = (Element)document.createElement("dtmDate");
			dtmDate.appendChild(document.createTextNode("2007-02-08"));
			header.appendChild(dtmDate);
			
			//Create the orderedByAddress element and append 
			//it to the header element
			Element orderedByAddress =(Element)document.createElement("orderedByAddress");
			orderedByAddress.appendChild(document.createTextNode("222, Any Blvd"));
			header.appendChild(orderedByAddress);
 
			//Create the txtOrderedByPhone element and append 
			//it to the header element
			Element txtOrderedByPhone = (Element)document.createElement("txtOrderedByPhone");
			txtOrderedByPhone.appendChild(document.createTextNode("(555) 555-2334"));
			header.appendChild(txtOrderedByPhone);
 
			//Create the txtOrderedByFax element and append
			//it to the header element
			Element txtOrderedByFax = (Element)document.createElement("txtOrderedByFax");	
			txtOrderedByFax.appendChild(document.createTextNode("(555) 555-9334"));
			header.appendChild(txtOrderedByFax);
 
			//Create the txtOrderedByContactName element and append
			//it to the header element
			Element txtOrderedByContactName = (Element)document.createElement("txtOrderedByContactName");
			txtOrderedByContactName.appendChild(document.createTextNode("Frank Jones"));
			header.appendChild(txtOrderedByContactName);
 
			//Create the deliverToAddress element and append
			//it to the header element
			Element deliverToAddress = (Element)document.createElement("deliverToAddress");
			deliverToAddress.appendChild(document.createTextNode("555, Any Blvd"));
			header.appendChild(deliverToAddress);
 
			//Create the txtDeliverToPhone element and append
			//it to the header element
			Element txtDeliverToPhone = (Element)document.createElement("txtDeliverToPhone");
			txtDeliverToPhone.appendChild(document.createTextNode("(555) 555-9098"));
			header.appendChild(txtDeliverToPhone);
 
			//Create the txtDeliverToFax element and append
			//it to the header element
			Element txtDeliverToFax = (Element)document.createElement("txtDeliverToFax");
			txtDeliverToFax.appendChild(document.createTextNode("(555) 555-9000"));
			header.appendChild(txtDeliverToFax);
 
			//Create the txtDeliverToContactName element and
			//append it to the header element
			Element txtDeliverToContactName = (Element)document.createElement("txtDeliverToContactName");
			txtDeliverToContactName.appendChild(document.createTextNode("Jerry Johnson"));
			header.appendChild(txtDeliverToContactName);
 
			//Create the detail element and append it to the root
			Element detail = (Element)document.createElement("detail");
			root.appendChild(detail);
 
			//Create the txtPartNum element and append it to the
			//detail element
			Element txtPartNum = (Element)document.createElement("txtPartNum");
			txtPartNum.appendChild(document.createTextNode("00010-100"));
			detail.appendChild(txtPartNum);
 
			//Create the txtDescription element and append it
			//to the detail element
			Element txtDescription = (Element)document.createElement("txtDescription");
			txtDescription.appendChild(document.createTextNode("Monitor"));
			detail.appendChild(txtDescription);
 
			//Create the numQty element and append it to 
			//the detail element
			Element numQty = (Element)document.createElement("numQty");
			numQty.appendChild(document.createTextNode("1"));
			detail.appendChild(numQty);
 
			//Create the numUnitPrice element and append it
			//to the detail element
			Element numUnitPrice = (Element)document.createElement("numUnitPrice");
			numUnitPrice.appendChild(document.createTextNode("350.00"));
			detail.appendChild(numUnitPrice);
 
			//Create another detail element named detail2 and 
			//append it to root
			Element detail2 = (Element)document.createElement("detail");
			root.appendChild(detail2);
 
			//Create the txtPartNum element and append it to the
			//detail2 element
			Element txtPartNum2 = (Element)document.createElement("txtPartNum");
			txtPartNum2.appendChild(document.createTextNode("00010-200"));
			detail2.appendChild(txtPartNum2);
 
			//Create the txtDescription element and append it 
			//to the detail2 element
			Element txtDescription2 = (Element)document.createElement("txtDescription");
			txtDescription2.appendChild(document.createTextNode("Desk lamps"));
			detail2.appendChild(txtDescription2);
 
			//Create the numQty element and append it to the 
			//detail2 element
			Element numQty2 = (Element)document.createElement("numQty");
			numQty2.appendChild(document.createTextNode("3"));
			detail2.appendChild(numQty2);
 
			//Create the NUMUNITPRICE element
			Element numUnitPrice2 = (Element)document.createElement("numUnitPrice");
			numUnitPrice2.appendChild(document.createTextNode("55.00"));
			detail2.appendChild(numUnitPrice2);
		  }
	 catch (Exception e) {
			  System.out.println("The following exception occurred: "+e.getMessage());
		   }
	return document;
	}
	 
	 
	 //Converts an org.w3c.dom.Document object to a BLOB object 
	 private BLOB convertDataSource(org.w3c.dom.Document myDOM)
	 {
		byte[] mybytes = null; 
	 	
		try
		{
		//Create a Java Transformer object
		TransformerFactory transFact = TransformerFactory.newInstance();
		Transformer transForm = transFact.newTransformer(); 
 
		//Create a Java ByteArrayOutputStream object
		ByteArrayOutputStream myOutStream = new ByteArrayOutputStream(); 
 
		//Create a Java Source object
		javax.xml.transform.dom.DOMSource myInput = new DOMSource(myDOM);
	
		//Create a Java Result object
		javax.xml.transform.stream.StreamResult myOutput = new StreamResult(myOutStream); 	
 
		//Populate the Java ByteArrayOutputStream object
		transForm.transform(myInput,myOutput);
 
		// Get the size of the ByteArrayOutputStream buffer
		 int myByteSize = myOutStream.size();
	
		//Allocate myByteSize to the byte array
		 mybytes = new byte[myByteSize];
			 
		//Copy the content to the byte array
		 mybytes = myOutStream.toByteArray();
		}
		catch (Exception e) {
		  System.out.println("The following exception occured: "+e.getMessage());
	  }
	 
	 //Create a BLOB object and copy the contents of the byte array
	 BLOB formData = new BLOB();
	 formData.setBinaryData(mybytes);
	 return formData; 
	 }
	 
	 
	 //Render the purchase order form using 
	 //the specified BLOB object
	 private void renderPOForm(HttpServletResponse resp, BLOB formData)
	 {
		try
		{
			
			com.adobe.idp.services.FormsServiceServiceLocator sl = new FormsServiceServiceLocator();
			FormsService formsOb = sl.getFormsService();
				
			((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub.
			USERNAME_PROPERTY, "administrator");
			((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub.
			PASSWORD_PROPERTY, "password");
									  
			//Create an HTMLRenderSpec object to store
			//HTML run-time options
			HTMLRenderSpec htmlRS = new HTMLRenderSpec();
			htmlRS.setHTMLToolbar(HTMLToolbar.Vertical);
									
			//Specify URI values used by the Forms service
			URLSpec uriValues = new URLSpec(); 
			uriValues.setApplicationWebRoot("http://localhost:8080/FormsServiceClientApp");
			uriValues.setContentRootURI("C:\\Adobe");
			uriValues.setTargetURL("http://localhost:8080/FormsServiceClientApp/HandleData");
						
			//Create class holder objects				
			BLOBHolder outRenderPDFFormResultDoc = new BLOBHolder(); 
			FormsResultHolder formsResult = new FormsResultHolder(); 
			BLOBHolder blobHolder = new BLOBHolder(); 
			LongHolder longHolder = new LongHolder(); 
			StringHolder stringHolder = new StringHolder();  
			StringHolder stringHolder2 = new StringHolder(); 
								
			//Invoke the renderHTMLForm method to render 
			//an HTML form
			formsOb.renderHTMLForm(
				  "PO.xdp",                  
				  TransformTo.MSDHTML,
				  null, 			     
				  htmlRS ,
				  "",        
				  uriValues,				
				  null, 					
				  outRenderPDFFormResultDoc,
				  blobHolder,
				  longHolder,
				  stringHolder,
				  stringHolder2,
				  formsResult); 
		
			//Create a BLOB object that contains form data
			BLOB formData2 = formsResult.value.getOutputContent();
				
			//Get the content type of the respose and
			//set the HttpServletResponse object’s content type
			String contentType = formData2.getContentType();
			resp.setContentType(contentType);
		
			//Create a ServletOutputStream object
			ServletOutputStream oOutput = resp.getOutputStream();
							
			//Create a byte array that stores form data in the BLOB object
			byte[] cContent = formData.getBinaryData();
		
			//Write a byte stream back to the web browser. 
			//Pass the byte array
			oOutput.write(cContent);
		}catch (Exception e) {
			System.out.println("The following error occurred: " +e.getMessage());
		  }	
		}
}

API Quick Starts (Code Examples) > Forms Service API Quick Starts > Quick Start: Prepopulating dynamic forms using the web service API

Programming with LiveCycle ES (LiveDocs)
Adobe LiveCycle ES Update 1

 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/livecycle/8.2/programLC/programmer/help/000083.html