ADFUtils.java For ADF Application

package com.kunal.view;

import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.model.binding.DCParameter;
import oracle.binding.AttributeBinding;
import oracle.binding.BindingContainer;
import oracle.binding.ControlBinding;

import oracle.jbo.ApplicationModule;
import oracle.jbo.Key;
import oracle.jbo.Row;


public class ADFUtils {

  /**
   * This method is to get an application module data control by name.
   * @param name application module data control name
   * @return ApplicationModule
   */
  public static ApplicationModule getApplicationModuleForDataControl(String name) {
    return (ApplicationModule)JSFUtils.resolveExpression("#{data."+name+".dataProvider}");
  }
  /**
   * method to get value of a bound attribute
   * @param attributeName of the bound value in the pageDef
   * @return value of the attribute
   */
  public static Object getBoundAttributeValue(String attributeName) {
    return findControlBinding(attributeName).getInputValue();
  }

  /**
   * method for setting the value of a bound attribute
   * @param attributeName of the bound value in the pageDef
   * @param value to set
   */
  public static void setBoundAttributeValue(String attributeName, Object value) {
    findControlBinding(attributeName).setInputValue(value);
  }

  /**
   * Give you the evaluated value of a pageDef parameter.
   * @param pageDefName reference to the page definition file of the page with the parameter
   * @param parameterName name of the pagedef parameter
   * @return evaluated value of the parameter as a String
   */
  public static Object getPageDefParameterValue(String pageDefName,
                                                String parameterName) {
    BindingContainer bindings = findBindingContainer(pageDefName);
    DCParameter param = ((DCBindingContainer)bindings).findParameter(parameterName);
    return param.getValue();
  }

  /**
   * Method to find a DCControlBinding as an AttributeBinding
   * @param bindingContainer binding container
   * @param attributeName name of the attribute binding.
   * @return the control value binding with the name passed in.
   *
   */
  public static AttributeBinding findControlBinding(BindingContainer bindingContainer,
                                                    String attributeName) {
    if (attributeName != null) {
      if (bindingContainer != null) {
        ControlBinding ctrlBinding = bindingContainer.getControlBinding(attributeName);
        if (ctrlBinding instanceof AttributeBinding) {
          return (AttributeBinding)ctrlBinding;
        }
      }
    }
    return null;
  }

  /**
   * Method to find a DCControlBinding as a JUCtrlValueBinding
   * @param attributeName name of the attribute binding.
   * @return the control value binding with the name passed in.
   *
   */
  public static AttributeBinding findControlBinding(String attributeName) {
    return findControlBinding(getBindingContainer(), attributeName);
  }

  /**
   * Method to get the current page's binding container.
   * @return the current page's binding container
   */
  public static BindingContainer getBindingContainer() {
    return (BindingContainer)JSFUtils.resolveExpression("#{bindings}");
  }

  /**
   * Mthod which will return the Binding Container as a DCBindingContainer.
   * @return current binding container as a DCBindingContainer
   */
  public static DCBindingContainer getDCBindingContainer() {
    return (DCBindingContainer)getBindingContainer();
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding.
   * Uses the value of the 'valueAttrName' attribute as the key for
   * the SelectItem key.
   * @param iteratorName ADF iterator binding name
   * @param valueAttrName name of the value attribute to use
   * @param displayAttrName name of the attribute from iterator rows to display
   * @return ADF Faces SelectItem for an iterator binding
   */
  public static List<SelectItem> selectItemsForIterator(String iteratorName,
                                                        String valueAttrName,
                                                        String displayAttrName) {
    return selectItemsForIterator(findIterator(iteratorName), valueAttrName,
                                  displayAttrName);
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding with description.
   * Uses the value of the 'valueAttrName' attribute as the key for
   * the SelectItem key.
   * @param iteratorName ADF iterator binding name
   * @param valueAttrName name of the value attribute to use
   * @param displayAttrName name of the attribute from iterator rows to display
   * @param descriptionAttrName name of the attribute to use for description
   * @return ADF Faces SelectItem for an iterator binding with description
   */
  public static List<SelectItem> selectItemsForIterator(String iteratorName,
                                                        String valueAttrName,
                                                        String displayAttrName,
                                                        String descriptionAttrName) {
    return selectItemsForIterator(findIterator(iteratorName), valueAttrName,
                                  displayAttrName, descriptionAttrName);
  }

  /**
   * Method to get List of attribute values for an iterator.
   * @param iteratorName ADF iterator binding name
   * @param valueAttrName value attribute to use
   * @return List of attribute values for an iterator
   */
  public static List attributeListForIterator(String iteratorName,
                                              String valueAttrName) {
    return attributeListForIterator(findIterator(iteratorName), valueAttrName);
  }

  /**
   * Method to get List of Key objects for rows in an iterator.
   * @param iteratorName iterabot binding name
   * @return List of Key objects for rows
   */
  public static List<Key> keyListForIterator(String iteratorName) {
    return keyListForIterator(findIterator(iteratorName));
  }

  /**
   * Method to get List of Key objects for rows in an iterator.
   * @param iter iterator binding
   * @return List of Key objects for rows
   */
  public static List<Key> keyListForIterator(DCIteratorBinding iter) {
    List attributeList = new ArrayList();
    for (Row r: iter.getAllRowsInRange()) {
      attributeList.add(r.getKey());
    }
    return attributeList;
  }

  /**
   * Method to get List of Key objects for rows in an iterator using key attribute.
   * @param iteratorName iterator binding name
   * @param keyAttrName name of key attribute to use
   * @return List of Key objects for rows
   */
  public static List<Key> keyAttrListForIterator(String iteratorName,
                                                 String keyAttrName) {
    return keyAttrListForIterator(findIterator(iteratorName), keyAttrName);
  }

  /**
   * Method to get List of Key objects for rows in an iterator using key attribute.
   * @param iter iterator binding
   * @param keyAttrName name of key attribute to use
   * @return List of Key objects for rows
   */
  public static List<Key> keyAttrListForIterator(DCIteratorBinding iter,
                                                 String keyAttrName) {
    List attributeList = new ArrayList();
    for (Row r: iter.getAllRowsInRange()) {
      attributeList.add(new Key(new Object[] { r.getAttribute(keyAttrName) }));
    }
    return attributeList;
  }

  /**
   * Method to get a List of attribute values for an iterator.
   * @param iter iterator binding
   * @param valueAttrName name of value attribute to use
   * @return List of attribute values
   */
  public static List attributeListForIterator(DCIteratorBinding iter,
                                              String valueAttrName) {
    List attributeList = new ArrayList();
    for (Row r: iter.getAllRowsInRange()) {
      attributeList.add(r.getAttribute(valueAttrName));
    }
    return attributeList;
  }

  /**
   * Method to find an iterator binding in the current binding container by name.
   * @param name iterator binding name
   * @return iterator binding
   */
  public static DCIteratorBinding findIterator(String name) {
    DCIteratorBinding iter = getDCBindingContainer().findIteratorBinding(name);
    if (iter == null) {
      throw new RuntimeException("Iterator '" + name + "' not found");
    }
    return iter;
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding with description.
   * Uses the value of the 'valueAttrName' attribute as the key for
   * the SelectItem key.
   * @param iter ADF iterator binding
   * @param valueAttrName name of value attribute to use for key
   * @param displayAttrName name of the attribute from iterator rows to display
   * @param descriptionAttrName name of the attribute for description
   * @return ADF Faces SelectItem for an iterator binding with description
   */
  public static List<SelectItem> selectItemsForIterator(DCIteratorBinding iter,
                                                        String valueAttrName,
                                                        String displayAttrName,
                                                        String descriptionAttrName) {
    List<SelectItem> selectItems = new ArrayList<SelectItem>();
    for (Row r: iter.getAllRowsInRange()) {
      selectItems.add(new SelectItem(r.getAttribute(valueAttrName),
                                     (String)r.getAttribute(displayAttrName),
                                     (String)r.getAttribute(descriptionAttrName)));
    }
    return selectItems;
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding.
   * Uses the value of the 'valueAttrName' attribute as the key for
   * the SelectItem key.
   * @param iter ADF iterator binding
   * @param valueAttrName name of value attribute to use for key
   * @param displayAttrName name of the attribute from iterator rows to display
   * @return ADF Faces SelectItem for an iterator binding
   */
  public static List<SelectItem> selectItemsForIterator(DCIteratorBinding iter,
                                                        String valueAttrName,
                                                        String displayAttrName) {
    List<SelectItem> selectItems = new ArrayList<SelectItem>();
    for (Row r: iter.getAllRowsInRange()) {
      selectItems.add(new SelectItem(r.getAttribute(valueAttrName),
                                     (String)r.getAttribute(displayAttrName)));
    }
    return selectItems;
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding.
   * Uses the rowKey of each row as the SelectItem key.
   * @param iteratorName ADF iterator binding name
   * @param displayAttrName name of the attribute from iterator rows to display
   * @return ADF Faces SelectItem for an iterator binding
   */
  public static List<SelectItem> selectItemsByKeyForIterator(String iteratorName,
                                                             String displayAttrName) {
    return selectItemsByKeyForIterator(findIterator(iteratorName), displayAttrName);
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding with discription.
   * Uses the rowKey of each row as the SelectItem key.
   * @param iteratorName ADF iterator binding name
   * @param displayAttrName name of the attribute from iterator rows to display
   * @param descriptionAttrName name of the attribute for description
   * @return ADF Faces SelectItem for an iterator binding with discription
   */
  public static List<SelectItem> selectItemsByKeyForIterator(String iteratorName,
                                                             String displayAttrName,
                                                             String descriptionAttrName) {
    return selectItemsByKeyForIterator(findIterator(iteratorName), displayAttrName,
                                       descriptionAttrName);
  }

  /**
   * Mthod to get List of ADF Faces SelectItem for an iterator binding with discription.
   * Uses the rowKey of each row as the SelectItem key.
   * @param iter ADF iterator binding
   * @param displayAttrName name of the attribute from iterator rows to display
   * @param descriptionAttrName name of the attribute for description
   * @return ADF Faces SelectItem for an iterator binding with discription
   */
  public static List<SelectItem> selectItemsByKeyForIterator(DCIteratorBinding iter,
                                                             String displayAttrName,
                                                             String descriptionAttrName) {
    List<SelectItem> selectItems = new ArrayList<SelectItem>();
    for (Row r: iter.getAllRowsInRange()) {
      selectItems.add(new SelectItem(r.getKey(),
                                     (String)r.getAttribute(displayAttrName),
                                     (String)r.getAttribute(descriptionAttrName)));
    }
    return selectItems;
  }

  /**
   * Method to get List of ADF Faces SelectItem for an iterator binding.
   * Uses the rowKey of each row as the SelectItem key.
   * @param iter ADF iterator binding
   * @param displayAttrName name of the attribute from iterator rows to display
   * @return List of ADF Faces SelectItem for an iterator binding
   */
  public static List<SelectItem> selectItemsByKeyForIterator(DCIteratorBinding iter,
                                                             String displayAttrName) {
    List<SelectItem> selectItems = new ArrayList<SelectItem>();
    for (Row r: iter.getAllRowsInRange()) {
      selectItems.add(new SelectItem(r.getKey(),
                                     (String)r.getAttribute(displayAttrName)));
    }
    return selectItems;
  }

  /**
   * Method to find the BindingContainer for a page definition by name.
   * Typically used to refer eagerly to page definition parameters. It is
   * not best practice to reference or set bindings in binding containers
   * that are not the one for the current page.
   * @param pageDefName name of the page defintion XML file to use
   * @return BindingContainer ref for the named definition
   */
  private static BindingContainer findBindingContainer(String pageDefName) {
    BindingContext bctx = getDCBindingContainer().getBindingContext();
    BindingContainer foundContainer = bctx.findBindingContainer(pageDefName);
    return foundContainer;
  }
}


Comments

Popular posts from this blog

Setting up the environment for Angular2 and Hello World Example in Angular2

Build a Simple ReactJS application using react-cli

Customizing the ReactJS sample application created using react-cli