/* ======================================================================================= "This sample program is provided AS IS and may be used, executed, copied and modified without royalty payment by customer (a) for its own instruction and study, (b) in order to develop applications designed to run with an IBM WebSphere product, either for customer's own internal use or for redistribution by customer, as part of such an application, in customer's own products." Product 5655-FLW, (C) COPYRIGHT International Business Machines Corp., 2006, 2010 All Rights Reserved * Licensed Materials - Property of IBM ======================================================================================= */ package bpc.samples.query; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import javax.faces.context.FacesContext; import bpc.samples.bean.CustomProcessInstanceBean; import com.ibm.bpc.clientcore.ClientException; import com.ibm.bpc.clientcore.GenericBPCQuery; import com.ibm.bpc.clientcore.exception.QueryException; import com.ibm.bpe.api.QueryResultSet; import com.ibm.bpe.clientmodel.BFMConnection; /** * CustomProcessInstanceQuery represents a customized query against * the BusinessFlowManagerService API. */ public class CustomProcessInstanceQuery extends GenericBPCQuery { static Logger logger = Logger.getLogger( "CustomClient" ) ; private BFMConnection connection ; // Add setter & getter to manage // property, e.g. in faces-config.xml private String orderNo; // A property which determines the // resulting where clause. private String department; // A property which determines the // resulting where clause. /** * Returns the BFM connection * @return the BFM connection */ public BFMConnection getConnection() { return connection ; } /** * Sets the BFM connection * @param c the BFM connection */ public void setConnection( BFMConnection c ) { connection = c; } /** *

Executes the query against the BusinessFlowManagerService

*

The BusinessFlowManagerService is determined by the connection property.

* @throws ClientException: Any exception is caught,nested and rethrown as a ClientException */ public List execute() throws ClientException { logger.entering(this.getClass().getName(),"execute()"); try { // Connect to the BFM API and query the process templates // logger.info( "Select: " + getSelectClause() + "\t Where: "+ this.getWhereClause() ); logger.info( "Order" + getOrderClause() + "\t Threshold" + getThreshold()) ; BFMConnection bfm = getConnection(); QueryResultSet apiResult = bfm.getBusinessFlowManagerService().query( getSelectClause(), getWhereClause(), getOrderClause(), getThreshold(), null ) ; // Then iterate over the result returned by the API and // and transform each row to a bean and add it to the // result list. // ArrayList result = new ArrayList( apiResult.size() ) ; while( apiResult.next() ) { result.add( transform( apiResult )) ; } return result ; } catch (Exception e) { e.printStackTrace(); String[] arg = {e.getMessage()}; throw new QueryException(arg, e ); } finally { logger.exiting(this.getClass().getName(),"execute()"); } } /** * Returns the where clause as specified in the faces-config.xml, optionally * augmented with custom properties, and the current user ID, if specified. * * You can specify queries using a placeholder that, at runtime, resolves to * the ID of the current user. To do so, you must define a where clause in * the faces-config.xml using a placeholder of the form: {0}. At runtime, * this placeholder will be replaced by the user ID of the current user, or * by 'UNAUTHENTICATED', if security is disabled. If you do not use such a * placeholder in your where clause, then the formatter leaves your where * clause unchanged. * * Optionally, if the custom properties orderNo, or department are provided, * they will be appended to the where clause. * * @see java.text.MessageFormat */ public String getWhereClause() { //optionally replace user ID placeholder with current user ID, if the //placeholder is used in the where clause of the faces-config.xml //Determine user ID String userName = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser(); if(userName == null) userName = "UNAUTHENTICATED"; //prepare message formatting String[] replaceParameters = {userName} ; //replace placeholder with a value: String customWhereClause = MessageFormat.format(super.getWhereClause(), (Object[])replaceParameters ); // Optionally AND with orderNo clauses if( orderNo != null && orderNo.length() > 0 ) { customWhereClause += " AND PROCESS_ATTRIBUTE0.VALUE='" + orderNo + "'"; } // Optionally AND with department clauses if( department != null && department.length() > 0 ) { customWhereClause += " AND PROCESS_ATTRIBUTE1.VALUE='" + department + "'"; } logger.info( "CustomProcessInstanceQuery.getWhereClause() returns " + customWhereClause ) ; return customWhereClause ; } /** Indicates the type of objects returned by this query * @return */ public String getType() { return CustomProcessInstanceBean.class.getName() ; } /** Gets the order number used in the where clause * * @return the orderNo or null */ public String getOrderNo() { return orderNo; } /** Sets the order number to be used in the where clause * * @param orderNo: the order number to be used. */ public void setOrderNo(String orderNo) { this.orderNo = orderNo; } /** Returns the department name used in the where clause * * @return the department */ public String getDepartment() { return department; } /** Sets the department name used in the where clause * * @param department: the department to be used */ public void setDepartment(String department) { this.department = department; } /* * Transforms a single row of the result set into a CustomProcessInstanceBean */ private Object transform( QueryResultSet resultRow ) { return new CustomProcessInstanceBean( resultRow, getConnection() ); } }