Saturday, October 30, 2010

ADF UI - ADFUtil class to evaluate, set and invoke EL expressions

ADFUtil.java - The MUST need for any ADF developer. It contains the utility methods to evaluate EL expressions, execute methods specified as EL expressions. Here, I'm including the content of ADFUtil java class so that it can be handy when needed.

package com.mpapana.bean.util; import java.util.Map; import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.MethodExpression; import javax.el.ValueExpression; import javax.faces.context.FacesContext; import oracle.adf.model.BindingContext; import oracle.adf.model.DataControlFrame; /** * Provides various utility methods that are handy to * have around when working with ADF. */ public class ADFUtil { /** * When a bounded task flow manages a transaction (marked as requires-transaction,. * requires-new-transaction, or requires-existing-transaction), then the * task flow must issue any commits or rollbacks that are needed. This is * essentially to keep the state of the transaction that the task flow understands * in synch with the state of the transaction in the ADFbc layer. * * Use this method to issue a commit in the middle of a task flow while staying * in the task flow. */ public static void saveAndContinue() { Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap(); BindingContext context = (BindingContext)sessionMap.get(BindingContext.CONTEXT_ID); String currentFrameName = context.getCurrentDataControlFrame(); DataControlFrame dcFrame = context.findDataControlFrame(currentFrameName); dcFrame.commit(); dcFrame.beginTransaction(null); } /** * Programmatic evaluation of EL. * * @param el EL to evaluate * @return Result of the evaluation */ public static Object evaluateEL(String el) { FacesContext facesContext = FacesContext.getCurrentInstance(); ELContext elContext = facesContext.getELContext(); ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class); return exp.getValue(elContext); } /** * Programmatic invocation of a method that an EL evaluates to. * The method must not take any parameters. * * @param el EL of the method to invoke * @return Object that the method returns */ public static Object invokeEL(String el) { return invokeEL(el, new Class[0], new Object[0]); } /** * Programmatic invocation of a method that an EL evaluates to. * * @param el EL of the method to invoke * @param paramTypes Array of Class defining the types of the parameters * @param params Array of Object defining the values of the parametrs * @return Object that the method returns */ public static Object invokeEL(String el, Class[] paramTypes, Object[] params) { FacesContext facesContext = FacesContext.getCurrentInstance(); ELContext elContext = facesContext.getELContext(); ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); MethodExpression exp = expressionFactory.createMethodExpression(elContext, el, Object.class, paramTypes); return exp.invoke(elContext, params); } /** * Sets a value into an EL object. Provides similar functionality to * the <af:setActionListener> tag, except the from is * not an EL. You can get similar behavior by using the following... * setEL(to, evaluateEL(from)) * * @param el EL object to assign a value * @param val Value to assign */ public static void setEL(String el, Object val) { FacesContext facesContext = FacesContext.getCurrentInstance(); ELContext elContext = facesContext.getELContext(); ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class); exp.setValue(elContext, val); } }


Sample uses:

i. To evaluate an EL expression.
import com.mpapana.bean.util.ADFUtil; public class ExampleBean { public void sampleMethod() { String lookupName = (String)ADFUtil.evaluateEL("#{pageFlowScope.lookupName}"); } }


ii. To invoke a AMImpl's method added as a methodAction in pageDef file.

a) Invoking a method that takes no parameters
import com.mpapana.bean.util.ADFUtil; public class ExampleBean { public void sampleMethod(ActionEvent actionEvent) { ADFUtil.invokeEL("#{bindings.methodWithoutParams.execute}"); }


b) Invoking a method that takes parameters
import com.mpapana.bean.util.ADFUtil; public class ExampleBean { public void sampleMethod(ActionEvent actionEvent) { ADFUtil.invokeEL("#{bindings.methodWithParams.execute}", new Class[]{String.class,String.class},new Object[]{"First Param","Second Param"} ); } }


iii. To set value for an object which is in the form of EL expression.
import com.mpapana.bean.util.ADFUtil; public class ExampleBean { public void sampleMethod() { ADFUtil.setEL("#{pageFlowScope.showAttendance}","false"); } }

7 comments:

  1. Thank you very much..........

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

      Delete
    2. Great Article. Thank you for sharing! Really an awesome post for every one.

      IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      Delete

Related Posts with Thumbnails