Monday, September 29, 2014

Creating EJB query with Criteria Builder


Just documenting it for future purposes or any one in need of help

import javax.persistence.EntityManager;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

public class SampleCriteriaBuilder {

EntityManager entityManager = null;
CriteriaBuilder criteriaBuilder;
List<Predicate> andPredicates = new ArrayList<Predicate>();
TypedQuery<YourDTOObjectRoot> query;

    public SampleCriteriaBuilder(EntityManager entityManager) {
        super();

        this.entityManager = entityManager;

        criteriaBuilder = this.entityManager.getCriteriaBuilder();
        criteriaQuery = criteriaBuilder.createQuery(YourDTOObject.class);

        yourDTOObjectRoot = criteriaQuery.from(yourDTOObject.class);

        childTable1Root = yourDTOObjectRoot.join("childTable1", JoinType.LEFT);
        childTable2Root = yourDTOObjectRoot.join("childTable2", JoinType.LEFT);
                           
    }
    // Like clause
       
         Predicate likePredicate =
          criteriaBuilder.like(childTable1Root.get("someAttribute"),
 "%" + value + "%");
         andPredicates.add(likePredicate);
       
        // In clause

           String commaSepValue = "one,two,three,four";
            javax.persistence.criteria.CriteriaBuilder.In<String> inClause =
                criteriaBuilder.in(yourDTOObjectRoot.<String>get("someAttribute"));

           List commaSepValueList = commaSepValue.split(",");
            for (String valueTaken : commaSepValueList) {
                inClause.value(valueTaken);
            }

            andPredicates.add(inClause);
        // Or Clause
       
            Predicate orClause =
                criteriaBuilder.or(criteriaBuilder.equal(childTable1Root.get("someProperty"), "someValue"), criteriaBuilder.like(childTable1Root.get("someProperty"),"otherValue"));           
               
            andPredicates.add(orClause);
           
        //Comparision between two dates
       
        Path timeStampPath = yourDTOObjectRoot.<Date>get("timestamp");

        ParameterExpression<Date> dateFromExp = criteriaBuilder.parameter(Date.class);
        ParameterExpression<Date> dateToExp = criteriaBuilder.parameter(Date.class);

        Predicate predicate_date = criteriaBuilder.between(timeStampPath, dateFromExp, dateToExp);
        andPredicates.add(predicate_date);
                Predicate[] predicates = (Predicate[])andPredicates.toArray(new Predicate[andPredicates.size()]);

        criteriaQuery.where(predicates);

        query = entityManager.createQuery(criteriaQuery);

        query.setParameter(dateFromExp, dateFrom, TemporalType.DATE);
        query.setParameter(dateToExp, dateTo, TemporalType.DATE);
}

Thursday, September 25, 2014

Ant script - Pre-compile JSPX files - ADF application

I happened to work on implementing the pre-compile jsps for our application and on doing a web search I came across the following blog

https://blogs.oracle.com/groundside/entry/appc_from_ant and it was spot on

and had to just re-use it.

I made the changes according to our application and run the ant task.

Started compiling jspx files but suddenly it fallover with the following error.

There are 1 nested errors:

The type javax.servlet.jsp.tagext.BodyTagSupport cannot be resolved. It is indirectly referenced from required .class files
The type javax.el.ValueExpression cannot be resolved. It is indirectly referenced from required .class files


For some reason it was not able to figure out BodyTagSupport class or may be it could not find the class at designated place.

I have changed the script to include weblogic.jar to the class path and it was all fine.


<?xml version="1.0" encoding="UTF-8" ?>

<project name="AppcTest" default="precompile" basedir=".">
  <description>Sample build file using Ant to call WLS APPC</description>
  <property name="wls_root" value="/oracle/fmwhome"/>
  <property name="wls_home" value="${wls_root}/wlserver_10.3"/>
  <property name="adf_lib_root"
                    value="${wls_root}/oracle_common/modules"/>
  <property name="java_home" value="/oracle/javahome"/>
  <property name="common_lib_root"
                    value="${wls_home}/common/deployable-libraries"/>

  <property name="ear_root"   value="${ear.root}"/>

  <path id="wls.classpath">
    <pathelement path="${wls_home}/server/lib/weblogic.jar"></pathelement>
  </path>
  <echo message="Setting path"></echo>
  <property name="project.class.path.property" refid="wls.classpath"/>
  <echo>PATH = "${project.class.path.property}"</echo>
  <taskdef name="wlappc" classpathref='wls.classpath'
                  classname="weblogic.ant.taskdefs.j2ee.Appc"/>
  <target name="precompile" description="Calls WLS APPC to pre-compile an EAR">
    <wlappc source="${ear_root}/deploy/ear-file-name.ear" verbose="true"
            classpath="${adf_lib_root}/oracle.adf.share_11.1.1  
                               /adfsharembean.jar:${wls_root}/wlserver_10.3/server  
                               /lib/weblogic.jar">

      <library file="${common_lib_root}/jstl-1.2.war"/>
      <library file="${common_lib_root}/jsf-1.2.war"/>
      <library file="${adf_lib_root}/oracle.adf.view_11.1.1
                               /adf.oracle.domain.webapp.war"/>
      <library file="${adf_lib_root}/oracle.adf.model_11.1.1
                               /adf.oracle.domain.ear"/>
      <library file="${wls_root}/Oracle_SOA1/soa/modules
                         /oracle.soa.workflow_11.1.1/oracle.soa.workflow.wc.jar"/>
      <library file="${wls_root}/Oracle_SOA1/soa/modules
            /oracle.soa.worklist.webapp_11.1.1/oracle.soa.worklist.webapp.war"/>
    </wlappc>
  </target>
</project>

Custom ADF application through BPM workspace - Null Pointer Exception - when performed action on the last worklist item in the workspace

Issue

   While working on a custom ADF application which is accessed through the BPM workspace whose taskflows are generated from human tasks encountered the issue as described below.
 
   Accessing the last item in the worklist and perform action which deletes the item from the worklist.
 
   If the action is performed as part of workspace inbox, everything works fine.
  
   Worklist throws a null pointer exception if the user double clicks and opens up worklist item in the separate window and submits any outcome.

   The popup window goes blank and throws up the NPE. Close the window and refresh the inbox, the worklist item is gone as expected but with the error. 

   Please find the error below.

Caused By: java.lang.NullPointerException
        at oracle.bpel.worklistapp.util.WorklistServiceUtil.isTaskFlow(WorklistServiceUtil.java:1152)
        at oracle.bpel.worklistapp.tasklist.beans.controller.TaskController.launchTaskDetails(TaskController.java:506)
        at oracle.bpel.worklistapp.tasklist.beans.controller.TaskController.launchExternalWindow(TaskController.java:745)
        at oracle.bpel.worklistapp.tasklist.beans.controller.TaskController.fetchNewTask(TaskController.java:767)

Solution

   Here I go thinking to solve the issue started looking at
    
      1. Method bindings - whether they are pointing to the correct method, iterator etc.,
      2. Anything unusual in Page Definition file.
   Nothing un usual in the JSPX, pagedef. So started looking at the human task itself from which the ADF taskflow is generated.    
      3. Suspected that there is no outcome of such which is being executed, but everything is fine :(
      
   Flushed out all the thoughts and started looking at ADF taskflow and found the issue with the control flows.

   So, here is the solution which is simple but took a while to figure out. 
          
    1. Go to the bounded taskflow and create control flow between jspx and taskFlowRefresh return activity.
    2. Make sure the outcome of the return activity is "done".