Sunday 31 May 2015

Useful groovy expressions and resuable Java code in ADF

---------- Groovy Expressions ----------

1. Generate sequence using groovy.

(new oracle.jbo.server.SequenceImpl
("SEQUENCE_NAME",adf.object.getDBTransaction())).getSequenceNumber()

2. Fetching record of View Accessor in any attribute of View Object.

Suppose View accessor name is DepartmentVA

if (null!= DepartmentVA.first())
return DepartmentVA.first().getAttribute('DepartmentName');


2. Reusable JAVA code

import javax.el.ExpressionFactory;
import javax.faces.context.FacesContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.binding.OperationBinding;

    /**
     * This methos returns the operation bindings of the passed method name
     * @param operation name
     * @return
     */
    public static OperationBinding getOperBindings(String operation) {
        OperationBinding oper = null;
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExpressionFactory exp = facesContext.getApplication().getExpressionFactory();
        DCBindingContainer bindingContainer =
            (DCBindingContainer) exp.createValueExpression(facesContext.getELContext(), "#{bindings}",
                                                           DCBindingContainer.class).getValue(facesContext.getELContext());
        oper = bindingContainer.getOperationBinding(operation);
        System.out.println("Exit - [Util : getOperBindings]");
        return oper;
    }

----------------------------------------------------------------------------------------------------------------------
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;

    /**
     * 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);
    }

----------------------------------------------------------------------------------------------------------------------
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;

     /**
      * 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);
     }


----------------------------------------------------------------------------------------------------------------------
import oracle.adf.model.BindingContext;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;


      /**
       * Method will be used to execute the BC operations
       * such as : Commit, Rollback, Execute etc.
       * @param operation
       */

      public static OperationBinding commitUsingOperationBinding(String operation) {
          BindingContainer bindings =
              BindingContext.getCurrent().getCurrentBindingsEntry();
          OperationBinding oper = (OperationBinding)bindings.get(operation);
          oper.execute();
          return oper;
      }

----------------------------------------------------------------------------------------------------------------------
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.jbo.RowSetIterator;

    /**
     * Method to retrieve row set iterator by iterator name
     * @param iteratorName
     * @return RowSetIterator
     */

    public static RowSetIterator getRowSetIter(String iteratorName) {
        DCBindingContainer dcBindings =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iteratorBinding =
            (DCIteratorBinding)dcBindings.get(iteratorName);
        RowSetIterator rowSetIterator =
            iteratorBinding.getViewObject().createRowSetIterator(null);
        rowSetIterator.reset();
        return rowSetIterator;
    }

---------------------------------------------------------------------------------------------------------------------- 
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

     /**
      * Refresh page from root
      */

     public static void rootRefresh() {
         FacesContext facesContext = FacesContext.getCurrentInstance();
         String refreshpage = facesContext.getViewRoot().getViewId();
         ViewHandler viewHandler =
             facesContext.getApplication().getViewHandler();
         UIViewRoot viewroot =
             viewHandler.createView(facesContext, refreshpage);
         viewroot.setViewId(refreshpage);
         facesContext.setViewRoot(viewroot);
     }

----------------------------------------------------------------------------------------------------------------------

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import javax.servlet.http.HttpServletRequest;

    /**
     *
How to navigate to the mentioned URL in Application
     * @param url
     */

   
public static void navigateTO(String url) {
        String urlToNavigate = url;
        ExternalContext ectx =
            FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest)ectx.getRequest();
        String completeUrl = null;
        completeUrl = request.getContextPath() + urlToNavigate;
        try {
            ectx.redirect(completeUrl);
        } catch (IOException e) {
          
        }
    } 



*Comments and feedback are most welcomed, Thanks.

No comments:

Post a Comment