Jump to: navigation, search

Stardust/KnowledgeBase/Reports/HowToRunReportAndAttachAsProcessAttachment

< Stardust
Revision as of 06:17, 16 May 2012 by Ganesh.lawande.sungard.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This article will show how to run default or custom reports in Stardust and add them as process attachments. This article assumes that reader is already familiar with basics of Stardust, Stardust Bert report and DMS components.

Following sections explain how to run and attach reports with the help of an example process model and simple java application. All artifacts source code discussed here can be downloaded from Model and Source Code section at the end.

Demo Model

We will use the following example process definition. It is very simple process having only three steps. First step will read the Bert Report URL and its parameters from the user.

Second step is a simple java application, see source code below, which simply takes in report URL (from step 1) and runs the report. The returned report content (PDF in this case), is added into the document repository and then associated as a document in the current process attachments.

The third step is a plan manually activity where user can stop and view/verify if the run is attached to the process attachments. If everything goes well, you will see the report document in the process attachments list.


Process 1:

ReportRunDemoProcess.jpg

Report Runner Java App

package com.sungard.infinity.reports;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.eclipse.stardust.common.log.LogManager;
import org.eclipse.stardust.common.log.Logger;
import org.eclipse.stardust.engine.api.runtime.DmsUtils;
import org.eclipse.stardust.engine.api.runtime.Document;
import org.eclipse.stardust.engine.api.runtime.DocumentInfo;
import org.eclipse.stardust.engine.api.runtime.DocumentManagementService;
import org.eclipse.stardust.engine.api.runtime.WorkflowService;
import org.eclipse.stardust.engine.api.spring.SpringUtils;
import org.eclipse.stardust.engine.extensions.dms.data.DmsConstants;
import org.eclipse.stardust.ui.web.processportal.view.ActivityDetailsBean;
import org.eclipse.stardust.ui.web.viewscommon.services.ContextPortalServices;
 
public class ReportAttachmentAPI {
 
	private static final Logger log = LogManager
			.getLogger(ReportAttachmentAPI.class);
	private ActivityDetailsBean activityDetails;
 
	public ReportAttachmentAPI() {
		log.debug("Instanciating ReportAttachmentAPI");
		activityDetails = (ActivityDetailsBean) SpringUtils
				.getWebApplicationContext().getBean("activityDetailsBean");
	}
 
	/**
	 * @param reportRunURL
	 *            URL of the report to be run and attach
	 */
	public void runAndAttachReport(String reportRunURL) {
		log.debug("Got in val as : " + reportRunURL);
 
		DocumentManagementService dmsService = ContextPortalServices
				.getDocumentManagementService();
		WorkflowService workflowService = ContextPortalServices
				.getWorkflowService();
 
		String targetFolder = activityDetails.getProcessAttachmentsFolderId();
 
		// Create an instance of HttpClient.
		HttpClient client = new HttpClient();
		// Create a method instance.
		GetMethod method = new GetMethod(reportRunURL);
 
		// Provide custom retry handler is necessary
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(3, false));
 
		try {
			// Execute the method.
			int statusCode = client.executeMethod(method);
 
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
			}
 
			// Read the response body.
			byte[] responseBody = method.getResponseBody();
 
			// Deal with the response.
			// Use caution: ensure correct character encoding and is not binary
			// data
			log.debug(new String(responseBody));
 
			DocumentInfo docInfo = DmsUtils
					.createDocumentInfo("ReportRunAttachment_"
							+ (new Date()).getTime());
			// docInfo.setContentType("text/html");
			docInfo.setContentType("application/pdf");
			docInfo.setDescription("TestReportName1 desc");
			// docInfo.setOwner(wfService.getUser().getAccount());
			docInfo.setOwner(workflowService.getUser().getAccount());
 
			byte[] docContent = responseBody;
			DmsUtils.ensureFolderHierarchyExists(targetFolder, dmsService);
			Document doc = dmsService.createDocument(targetFolder, docInfo,
					docContent, null);
			List<Document> docList = new ArrayList<Document>();
			docList.add(doc);
 
			workflowService.setOutDataPath(activityDetails.getProcessInstance()
					.getOID(), DmsConstants.PATH_ID_ATTACHMENTS, docList);
 
		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			e.printStackTrace();
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
 
	}
 
}


Model and Source Code

Download the model and source code here