Tuesday, May 29, 2012

Some handy utility methods for XPath

Some handy utility methods for XPath:

import com.collaxa.cube.xml.xpath.SimpleNamespaceContext;
import java.util.*;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.*;
import oracle.bpel.services.common.util.XMLUtil;
import oracle.tip.pc.infra.exception.PCException;

import oracle.xml.xpath.JXPathFactory;
import org.w3c.dom.*;

    public class XPathUtils
{

    public XPathUtils()
    {
    }

    public static NodeList selectNodes(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        NodeList value = (NodeList)xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static NodeList selectNodes(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        NodeList value = (NodeList)xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static NodeList selectNodes(Element pElement, String xpathExpression, NamespaceContext namespaceContext)
        throws Exception
    {
        XPath xPath = createXPath(namespaceContext);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        NodeList value = (NodeList)xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static Object evaluate(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Object value = xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static Object evaluate(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Object value = xPath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
        return value;
    }

    public static Object selectSingleNode(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Node value = (Node)xPath.evaluate(xpathExpression, doc, XPathConstants.NODE);
        return value;
    }

    public static Object selectSingleNode(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Node value = (Node)xPath.evaluate(xpathExpression, doc, XPathConstants.NODE);
        return value;
    }

    public static String valueOf(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
           
        String value = (String)xPath.evaluate(xpathExpression, doc, XPathConstants.STRING);
        return value;
    }

    public static String valueOf(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        String value = (String)xPath.evaluate(xpathExpression, doc, XPathConstants.STRING);
        return value;
    }

    public static Number numberValueOf(Element pElement, String xpathExpression)
        throws Exception
    {
        XPath xPath = createXPath();
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Number nValue = (Number)xPath.evaluate(xpathExpression, doc, XPathConstants.NUMBER);
        return nValue;
    }

    public static Number numberValueOf(Element pElement, String xpathExpression, Map prefixNamespaceMapping)
        throws Exception
    {
        XPath xPath = createXPath(prefixNamespaceMapping);
        Document doc = null;
        if(pElement instanceof Document)
            doc = (Document)pElement;
        else
            doc = pElement != null ? pElement.getOwnerDocument() : null;
        Number nValue = (Number)xPath.evaluate(xpathExpression, doc, XPathConstants.NUMBER);
        return nValue;
    }



Update the Human task payload via JAVA API - Oracle SOA Suite

Update the Human task payload via JAVA API - Oracle SOA Suite:

Oracle SOA Suite provides java API’s to manipulate the Human task data.The below java code will help us to update the Human task payload.

This is the Remote Java client  to get the Workflow service and perform the required operations on the task.

The XPath of the input element will be used to update the payload data.

UpdateWorkflowPayload.java

import java.util.*;
import oracle.bpel.services.workflow.IWorkflowConstants;
import oracle.bpel.services.workflow.client.*;
import oracle.bpel.services.workflow.query.ITaskQueryService;
import oracle.bpel.services.workflow.repos.*;
import oracle.bpel.services.workflow.task.ITaskService;
import oracle.bpel.services.workflow.task.model.Task;
import oracle.bpel.services.workflow.verification.IWorkflowContext;
import org.w3c.dom.Element;
public class UpdateWorkflowPayload {     
   public static void updatePayload()
    {
        List displayColumns = new ArrayList();
        displayColumns.add("TASKNUMBER");
        displayColumns.add("TITLE");
        displayColumns.add("STATE");
        displayColumns.add("CREATOR");
        List optionalInfo = new ArrayList();      
        optionalInfo.add("Comments");
        optionalInfo.add("Payload");      
    try {
              String userid="weblogic";
              String password = "password";       
              String serverUrl ="t3://localhost:8000"; // host:port of the soa server
              Map<IWorkflowServiceClientConstants.CONNECTION_PROPERTY, String> connProperties = new HashMap<IWorkflowServiceClientConstants.CONNECTION_PROPERTY, String>();              connProperties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.CLIENT_TYPE,WorkflowServiceClientFactory.REMOTE_CLIENT);              connProperties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_PROVIDER_URL,serverUrl);
              connProperties.put(IWorkflowServiceClientConstants.CONNECTION_PROPERTY.EJB_INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");       
              IWorkflowServiceClient wfSvcClient = WorkflowServiceClientFactory.getWorkflowServiceClient(connProperties, null, null);   
              IWorkflowContext ctx = wfSvcClient.getTaskQueryService().authenticate(userid, password.toCharArray(), "jazn.com" );
              ITaskQueryService querySvc = wfSvcClient.getTaskQueryService();
              ITaskService taskSvc = wfSvcClient.getTaskService();       
              Predicate statePredicate =new Predicate(TableConstants.WFTASK_STATE_COLUMN,Predicate.OP_EQ,IWorkflowConstants.TASK_STATE_ASSIGNED);
              Predicate compositenamePredicate = new Predicate(TableConstants.COMPONENT_COMPOSITE_NAME_COLUMN,Predicate.OP_EQ,"HumanWorkflow");
              Predicate predicate = new Predicate(statePredicate, Predicate.AND, compositenamePredicate);
            
       
              List tasks =querySvc.queryTasks(ctx, displayColumns, optionalInfo, ITaskQueryService.AssignmentFilter.MY,null, statePredicate, null, 0, 0);
              
              for (int i = 0; i < tasks.size(); i++) {
                       Task task = (Task)tasks.get(i);
                       String taskId = task.getSystemAttributes().getTaskId();                
                       String title = task.getTitle();
                       String state = task.getSystemAttributes().getState();
                       Task currentTask=querySvc.getTaskDetailsById(ctx,taskId);                   
                 
                       Element payload = currentTask.getPayloadAsElement();
                 
                       Map namespacemap = new HashMap();
                       namespacemap.put("ns0", "http://xmlns.oracle.com/bpel/workflow/task");
                                     
                       setPayloadValue(payload,"/ns0:task/ns0:payload/ns0:Name",namespacemap,"Albin Issac");
                       setPayloadValue(payload,"/ns0:task/ns0:payload/ns0:Number",namespacemap,"024");
                       setPayloadValue(payload,"/ns0:task/ns0:payload/ns0:Dep",namespacemap,"I.T");
                       setPayloadValue(payload,"/ns0:task/ns0:payload/ns0:Place",namespacemap,"KAMPLAR");
                 
                       currentTask.setPayloadAsElement(payload);    
                       taskSvc.updateTaskOutcome(ctx, currentTask,"APPROVE"); 
                 
                   }
                         
          } catch (Exception e) {
              e.printStackTrace();
          
          }
    }
  
   public static void setPayloadValue(Element payload,String xpath,Map namespacemap,String value) {
        try {
            XPathUtils.setNodeValue(payload, namespacemap, xpath,value);
        } catch (Exception e) {
        }
    }
   public static void main(String[] args) {
        updatePayload();
    }
}



Saturday, May 26, 2012

Exposing the BPEL process as a Http Service using HTTP Binding Adapter – Oracle SOA Suite

Exposing the BPEL process as a Http Service using HTTP Binding Adapter – Oracle SOA Suite:

By default the BPEL processes can be invoked via SOAP service, sometimes we may need to invoke the service from the HTTP client by using the HTTP methods – GET/POST

The HTTP Binding Adapter will help us to expose the BPEL process as an HTTP Service.

If we are exposing the service as a HTTP service, the marshaling/unmarshaling overhead related to soap can be avoided when the client invoke the service.

Steps to Expose the BPEL process as a HTTP Service:
  • Create a new SOA project with the Composite Template as Empty
  • Open the Composite.xml and Insert HTTP Binding Adapter to Exposed Services section.
  • Configure the Adapter
1.       Select the Interface Option as “Define from operation and schema specified later”
2.       Select the operation type, verb(HTTP Method) and payload type accordingly
3.       Provide the Request/Response Message Schema


 



Friday, May 25, 2012

Updating the BPEL Component preference value from JAVA – Oracle SOA Suite

Updating the BPEL Component preference value from JAVA – Oracle SOA Suite:

Sometimes we may need to update the preference value for the BPEL component in Oracle SOA Suite 11g or Oracle SOA Suite 12c, the below java code will help us to update the preference value.

This update is not a cluster-aware,remember that you must repeat the same process for each managed server of your environment.
Before executing the code change the mbeanName accordingly.

   String mBeanName ="*:*,j2eeType=SCAComposite.SCAComponent,revision=*,SCAComposite=\"Preference\",name=Preference";




import java.util.*;
import javax.management.*;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.*;
import javax.naming.Context;

public class UpdatePreferenceValue {  

    public static MBeanServerConnection getMbeanServerConnection(String host,
                                                                 int port,
                                                                 String userName,
                                                                 String password) throws Exception {
        String jndiroot = "/jndi/";
        MBeanServerConnection m_connection = null;
        try {
            Hashtable jndiProps = new Hashtable();
            jndiProps.put(Context.SECURITY_PRINCIPAL, userName);
            jndiProps.put(Context.SECURITY_CREDENTIALS, password);
            jndiProps.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");

            JMXServiceURL serviceURL =new JMXServiceURL("t3", host, port, jndiroot +"weblogic.management.mbeanservers.runtime");
            JMXConnector m_connector =JMXConnectorFactory.newJMXConnector(serviceURL, jndiProps);
            m_connector.connect();
            m_connection = m_connector.getMBeanServerConnection();

        } catch (Exception e) {

            e.printStackTrace();
        }
        return m_connection;
    }


    private static CompositeDataSupport UpdatePreferenceData(CompositeDataSupport cds,String[] keys,String[] newValues) throws Exception {
        Map<String, Object> items =new HashMap<String, Object>(cds.getCompositeType().keySet().size());

        for (String key : cds.getCompositeType().keySet()) {
            boolean matched = false;
            int i = 0;

            for (; i < keys.length; i++) {
                String searchKey = keys[i];

                if (searchKey.equalsIgnoreCase(key)) {
                    items.put(key, newValues[i]);
                    matched = true;
                    break;
                }
            }       

            if (!matched) {             
                    items.put(key, cds.get(key));
            }
         
        }
    

        return new CompositeDataSupport(cds.getCompositeType(), items);
    }



Monday, May 21, 2012

Using Regular Expressions with Oracle BPEL


Using Regular Expressions with Oracle BPEL:

Sometimes we may have to validate the data based on the Regular Expressions.

xp20:matches() is a function which can be used to compare data with regular expressions. This function returns true when the input matches the given expression.

xp20:matches(intputString, regexPattern)
  • inputString - The input string
  • regexPattern - The regular expression pattern

Validation of numeric fields :

xp20:matches(bpws:getVariableData('inputString'),'^[0-9]*$')=true()

Validation of alpha fields :

xp20:matches(bpws:getVariableData('inputString'),'^[A-Za-z]*$')=true()

Validation for list of values:

xp20:matches(bpws:getVariableData('inputString'),'^(A|B|C|D)$') =true()

The matches function returns true if inputString variable contains any one of these values (A, B, C, and D).

The expression can be replaced with other regular expressions for similar comparisons.