Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Stardust/KnowledgeBase/Reports/HowToRunReportAndAttachAsProcessAttachment

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 Stadust, Stardust Bert report and DMS components.

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

Demo Model

We will use the following example process definition. It is very simple process having three steps. First, will read the Bert Report URL and its parameters from the user. Second step is a simple java applicaion, see source code below, which simply takes the report URL and runs the reports. The returned report content (PDF in this case), is added into the document repository and then added as a document in the current process attachements.


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

Back to the top