Sunday, September 29, 2013

ADF Use Cases

Creating and Executing ViewCriteria Programmatically

Sometimes you need dynamic ViewCriteria that you can handle at runtime ,
here is the solution ,you can create and apply ViewCriteria Programmatically-

Sample UseCase-
Suppose you have Department VO
You want to filter this VO for DepartmentId 10
Do this using this code snippet


**Get ViewObject*/
ViewObject vo = getAm().getDepartments1();
/**Create ViewCriteria on ViewObject*/
ViewCriteria vc = vo.createViewCriteria();
/**Create ViewCriteriaRow for that Criteria*/
ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
/**Set the values for ViewCriteriaRow*/
vcRow.setAttribute("DepartmentId", 10);
/**Add row to ViewCriteria*/
vc.addRow(vcRow);
/**Apply Criteria on ViewObject*/
vo.applyViewCriteria(vc);
/**Execute ViewObject*/
vo.executeQuery();

public pcAMImpl getAm() {
pcAMImpl am = (pcAMImpl)resolvElDC("pcAMDataControl");
return am;
}
========================================================================

TO REFRESH (PPR) ANY UI COMPONENT:

AdfFacesContext.getCurrentInstance().addPartialTarget(UIComponent);

========================================================================

TO REFRESH THE PAGE:
 protected void refreshPage() {
  FacesContext fctx = FacesContext.getCurrentInstance();
  String refreshpage = fctx.getViewRoot().getViewId();
  ViewHandler ViewH = fctx.getApplication().getViewHandler();
  UIViewRoot UIV = ViewH.createView(fctxc, refreshpage);
  UIV.setViewId(refreshpage);
  fctx.setViewRoot(UIV);
 }

=======================================================================

TO GET CURRENT ROW FROM ITERATOR BINDING:
 BindingContext btx = BindingContext.getCurrent();
 DCBindingContainer dcbct = (DCBindingContainer)btx.getCurrentBindingsEntry();
 DCIteratorBinding binding = dcbct.findIteratorBinding("EmployeeIterator");
 Row currentRow = binding.getCurrentRow();
     OR
 DCIteratorBinding iterBind= (DCIteratorBinding)dcbct .get("testIterator");
 String attribute = (String)iterBind.getCurrentRow().getAttribute("empName");
     OR
 FacesContext ctx = FacesContext.getCurrentInstance();
 ExpressionFactory exf = ctx.getApplication().getExpressionFactory();
 ValueExpression vale = exf.createValueExpression(ctx.getELContext(), "# {bindings.EmployeeIterator.currentRow.dataProvider}", Emp.class);
 Emp name= (Emp)vale.getValue(ctx.getELContext());

======================================================================
TO GET OPERATION BINDING:
 BindingContext.getCurrent().getCurrentBindingsEntry().getOperationBinding
 ("CreateInsert2").execute();
     OR
 BindingContainer bindings = getBindings();
 OperationBinding glOP = bindings.getOperationBinding("Commit");
 glOP.execute();
 if (!glOP.getErrors().isEmpty()) { //in case operation binding returns error
 } else{
 }

=======================================================================

FOR FACES MESSAGE POPUP -To show warning/error/info messages on view page
 FacesContext fctx = FacesContext.getCurrentInstance();
 FacesMessage msg=new FacesMessage(Severity,String Summary, String Message);
 fctx.addMessage(inputText.getId(), msg);

For Eg:
 FacesContext fc=FacesContext.getCurrentInstance();
 FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Save It" ,"Before Navigating to details Click Save..");
 fc.addMessage(null,msg);

=======================================================================
How to Invoke Stored Function with Only IN Arguments

public static int NUMBER = Types.NUMERIC;
public static int DATE = Types.DATE;
public static int VARCHAR2 = Types.VARCHAR;

protected Object callStoredFunction(int sqlReturnType, String stmt,
Object[] bindVars) {
CallableStatement st = null;
try {

st = getDBTransaction().createCallableStatement(
"begin ? := "+stmt+";end;",0);

st.registerOutParameter(1, sqlReturnType);
if (bindVars != null) {

for (int z = 0; z < bindVars.length; z++) {
// 4. Set the value of user-supplied bind vars in the stmt
st.setObject(z + 2, bindVars[z]);
}
}
st.executeUpdate();
return st.getObject(1);
}

// In StoredProcTestModuleImpl.java
public String callFuncWithThreeArgs(Number n, Date d, String v) {
return (String)callStoredFunction(VARCHAR2,
"devguidepkg.func_with_three_args(?,?,?)",
new Object[]{n,d,v});

}
==========================================================
To Create the new row in DB: 
EntityDefImpl productDef = ProductBaseEOImpl.getDefinitionObject();
ProductBaseEOImpl newProduct =
(ProductBaseEOImpl)productDef.createInstance2(getDBTransaction(),null);
newProduct.setProductName(name);
newProduct.setProductStatus(status);
newProduct.setShippingClassCode(shipCode);

-=========================================================


To Get the View Object in bean:
  private static ViewObject getPanelMemberVO() {
        String amDef = "model.panelmember.PanelMembersAM";
        String config = "PanelMembersAMLocal";
        ApplicationModule am =
            Configuration.createRootApplicationModule(amDef, config);
        ViewObject vo = am.findViewObject("PanelMembersVO1");
        return vo;
    }

To Get the AM Instance;
    private static ApplicationModule getPanelMemberAM() {
        String amDef = "model.panelmember.PanelMembersAM";
        String config = "PanelMembersAMLocal";
        ApplicationModule am =
            Configuration.createRootApplicationModule(amDef, config);
        return am;
    }

To retrieve the List of data from database using custom method (AM,VO)
    public List<PanelMembersUtilBean> executeAndShowResults(String EmpLocation, String EmpSkills) {
        ViewObject vo = getPanelMemberVO();
        vo.setNamedWhereClauseParam("EmpLocation", EmpLocation);
        vo.setNamedWhereClauseParam("EmpSkills", EmpSkills);
        vo.executeQuery();
        List<PanelMembersUtilBean> panelmembersList =
            new ArrayList<PanelMembersUtilBean>();
        while (vo.hasNext()) {
            Row curUser = vo.next();
            PanelMembersUtilBean panelMembersUtilBean =
                new PanelMembersUtilBean();
            panelMembersUtilBean.setEMail(curUser.getAttribute("EmpEmail").toString());
            String s = (curUser.getAttribute("EmpId")).toString();
            panelMembersUtilBean.setEmpId(Long.parseLong(s));
            panelMembersUtilBean.setEmpName(curUser.getAttribute("EmpName").toString());
            panelmembersList.add(panelMembersUtilBean);
                 }
                 return panelmembersList;
    }


To retrieve the Entity Object:
private OrderEOImpl retrieveOrderById(long orderId) {
EntityDefImpl orderDef = OrderEOImpl.getDefinitionObject();
Key orderKey = OrderEOImpl.createPrimaryKey(new DBSequence(orderId));
return (OrderEOImpl)orderDef.findByPrimaryKey(getDBTransaction(),orderKey);
}
    
public HomeStuffBean loadFlag(String rowid) {
        HomeStuffBean sb;
        EntityImpl addstuffEnt = retrieveOrderById(orderId);
        if (addstuffEnt != null) {
            sb = new HomeStuffBean();
            sb.setMsisdn((String)addstuffEnt.getAttribute("Msisdn"));
            sb.setMyProfile((String)addstuffEnt.getAttribute("Myprofile"));
return sb;

}

No comments:

Post a Comment