Friday 2 December 2011

Creating Search Insert Update Delete On Same Page in OAF

I would provide an example of Creating Search Insert Update Delete On Same Page in OAF

Outline of the Development Steps

A. Create DB Components
-------------------------------------------

1. We have custom table : PA_RATES_OVERRIDES

2. Create a synonym

3. Create a Unique Index

4. Create a db package ABHI_PA_RATES_OVERRIDES

5. Create procedures

B. Create EO/ VOs/ AM

C. Create Page Design

D. Create Controller

E. Deploy Page

================================================================
ADD ROWS


  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    OverridesAMImpl adjmodinterface = (OverridesAMImpl)pageContext.getApplicationModule(webBean);
    if ("addRows".equals(pageContext.getParameter("event")))
    {
        adjmodinterface.addNewOverride();
    }
}


public void addNewOverride()
  {
    System.out.println("addNewOverride> Starts");
    int i = 0;
    int m = 0;
    OAViewObject localVOImpl = (OAViewObject)getOverridesFullVO1();
     if (!localVOImpl.isPreparedForExecution())
          {
            localVOImpl.executeQuery();
          }

    localVOImpl.last();
    localVOImpl.next();
    Row row = localVOImpl.createRow(); //create your row
    System.out.println("addNewOverride> createRow");
    localVOImpl.insertRow(row); //insert it
    System.out.println("addNewOverride> insertRow");
    row.setNewRowState(Row.STATUS_INITIALIZED);
    //CALL method in VO to select the flag for new row
    row.setAttribute("SelectFlag","Y");

    System.out.println(" addNewOverride finished");
   
  }

set the add rows pragmatically to N on page xml.

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

Query All Rows on Page

//Query the existing data on page
  public void queryAllRows(OAPageContext pageContext, OAWebBean webBean)
  {
    OAApplicationModule am = pageContext.getApplicationModule(webBean);
    Boolean executeQuery = BooleanUtils.getBoolean(false);
    Serializable[] parameters =  { executeQuery };
    Class[] paramTypes = { Boolean.class };
    am.invokeMethod("initTableQuery", parameters, paramTypes);
    OAAdvancedTableBean table =
      (OAAdvancedTableBean)webBean.findChildRecursive("OverrideAdvTbl");

    if (table == null)
    {
      MessageToken[] tokens = { new MessageToken("OBJECT_NAME", "OverrideAdvTbl") };
      throw new OAException("PA", "OBJECT_NOT_FOUND", tokens);
    }

    table.queryData(pageContext, true);
  }


  public void initTableQuery(Boolean executeQuery)
  {
    System.out.println("initTableQuery is invoked");
   OverridesFullVOImpl vo = getOverridesFullVO1();
   vo.initQuery(executeQuery);
  }

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

Save Data

else if (pageContext.getParameter("SaveDraft") != null )
      {
          if (Boolean.TRUE.equals(adjmodinterface.anyRowSelected()))
          {
          System.out.println("SaveDraft is clicked");
          dialogPageforSaveDraft(pageContext, webBean);
          }
        else
          {
        throw new OAException("PA", "NO_OVR_ROWS_SELECTED"); 
       
          }
      }

    else if (pageContext.getParameter("SaveYesButton") != null)
    {
            String a = adjmodinterface.saveDraft();
            System.out.println("SaveDraft is clicked"+a); 

            if (a.length() >2)
            {
           
            reExecuteVO(pageContext, webBean); 
            readOnlyTable(pageContext, webBean); 
           
            a = a.substring(2,a.length());
            MessageToken[] tokens = { new MessageToken("OVERRIDE_ID", a)};
            OAException message = new OAException("PA", "OVR_DIALOG_CNF_ERR", tokens, OAException.WARNING, null);
            pageContext.putDialogMessage(message);
           
            }
            else
            {
            reExecuteVO(pageContext, webBean); 
            readOnlyTable(pageContext, webBean); 
            OAException message = new OAException("PA", "OVR_SAVE_DIALOG_CNF", null, OAException.CONFIRMATION, null);
            pageContext.putDialogMessage(message);
            }
    }

public void dialogPageforSaveDraft(OAPageContext pageContext, OAWebBean webBean)
  {
      OAException mainMessage = new OAException("PA", "OVER_SAVE_DIALOG");
      OADialogPage dialogPage = new OADialogPage(OAException.INFORMATION,
        mainMessage, null, "", "");
      String yes = pageContext.getMessage("AK", "FWK_TBX_T_YES", null);
      String no = pageContext.getMessage("AK", "FWK_TBX_T_NO", null);
      dialogPage.setOkButtonItemName("SaveYesButton");
      dialogPage.setNoButtonItemName("SaveNoButton");
      dialogPage.setOkButtonToPost(true);
      dialogPage.setNoButtonToPost(true);
      dialogPage.setPostToCallingPage(true);
      dialogPage.setOkButtonLabel(yes);
      dialogPage.setNoButtonLabel(no);
      System.out.println(String.valueOf("Before calling Dialog page").concat(String.valueOf(dialogPage.getOkButtonItemName())));
      pageContext.redirectToDialogPage(dialogPage);
  }


----code in am----

public Boolean anyRowSelected()
{
    System.out.println("anyRowSelected is invoked");
    OAViewObject vo = getOverridesFullVO1();
    int fetchedRowCount = vo.getFetchedRowCount(); 
    if ( vo.findRowSetIterator("selectIter4") !=null)
    {
      RowSetIterator selectIter4 = vo.findRowSetIterator("selectIter4");
      selectIter4.closeRowSetIterator();
    }
   
    RowSetIterator selectIter4 = vo.createRowSetIterator("selectIter4"); 

    if (fetchedRowCount > 0) 
    { 
    System.out.println("anyRowSelected fetchedRowCount >0 ");
     selectIter4.setRangeStart(0); 
     selectIter4.setRangeSize(fetchedRowCount); 
        for (int i = 0; i < fetchedRowCount; i++) 
        { 
        System.out.println("anyRowSelected fetchedRowCount > for loop ");
        OverridesFullVORowImpl rowi = (OverridesFullVORowImpl)selectIter4.getRowAtRangeIndex(i); 
        String selectBox = rowi.getSelectFlag()+"";
             if("Y".equals(selectBox)) 
              { 
                  return Boolean.TRUE;
              } 
        } 
    } 
    selectIter4.closeRowSetIterator();
    System.out.println("anyRowSelected is finished");
    return Boolean.FALSE;
}

public String saveDraft()
  {
    //invoke method to retrieve the Metro Group ID, Metro ID and Job ID
    System.out.println("save draft is invoked");
    System.out.println("save draft started Called");
    String a = saveDraftSelected()+"";
    System.out.println("saveDraft return value: "+a);
    return a;
  }

public String saveDraftSelected()
{
    System.out.println("saveDraftSelected is invoked");
    OAViewObject vo = getOverridesFullVO1();
    int fetchedRowCount = vo.getFetchedRowCount(); 

    String DuplicateAdjustmentId="";


    if ( vo.findRowSetIterator("selectIter3") !=null)
    {
      RowSetIterator selectIter3 = vo.findRowSetIterator("selectIter3");
      selectIter3.closeRowSetIterator();
    }

    RowSetIterator selectIter3 = vo.createRowSetIterator("selectIter3"); 

    if (fetchedRowCount > 0) 
    { 
    System.out.println("saveDraftSelected fetchedRowCount >0 ");
    // Save the original range size and range start. 
     selectIter3.setRangeStart(0); 
     selectIter3.setRangeSize(fetchedRowCount); 
        //Creates a Transient Attribute of "String" type. 
        for (int i = 0; i < fetchedRowCount; i++) 
        { 
        System.out.println("saveDraftSelected fetchedRowCount > for loop ");
        OverridesFullVORowImpl rowi = (OverridesFullVORowImpl)selectIter3.getRowAtRangeIndex(i); 
        String selectBox = rowi.getSelectFlag()+"";
        //    String selectFlag = rowi.getSelectFlag()+"";  
            if ((!"null".equals(selectBox)) || (!"".equals(selectBox)))
             if("Y".equals(selectBox)) 
              { 
             //  Getting selected row. 
             System.out.println("saveDraftSelected fetchedRowCount > selectBox "+selectBox);
             //pass the below value in the VOImpl method to retrieve the Metro Group ID
              try
              {
             System.out.println("saveDraftSelected fetchedRowCount > selectBox in try");
             if (Boolean.FALSE.equals(checkIfRecordExist(rowi.getOverrideId())))
              {
                if (rowi.insertRow() != null)
                {
                  DuplicateAdjustmentId = DuplicateAdjustmentId +", "+rowi.getOverrideId();
                }
              }
             else
                if (rowi.updateRow() != null)
                  {
                    DuplicateAdjustmentId = DuplicateAdjustmentId +", "+rowi.getOverrideId();
                  }

             System.out.println("saveDraftSelected > setting metro group id"+selectBox);
              }
              catch(Exception e1)
              {
             System.out.println("saveDraftSelected fetchedRowCount > selectBox in catch");
              }
             
            } 
        } 
    } 
     selectIter3.closeRowSetIterator();
    System.out.println("approveSelected is finished");

   return DuplicateAdjustmentId;
}

----insert or update method in VORowImpl---------


    public String insertRow()
    {
        System.out.println("PLSQL insert row is called");

        try
        {
        OAApplicationModuleImpl oaapplicationmoduleimpl = (OAApplicationModuleImpl)getApplicationModule();
        OverridesAMImpl overheadsam = (OverridesAMImpl)oaapplicationmoduleimpl.getRootApplicationModule();
        OADBTransactionImpl oadbtransactionimpl = (OADBTransactionImpl)overheadsam.getOADBTransaction();

            String s = "begin PA_RATES_PKG_OVR.override_create_row( "
                       + "   v_OVERRIDE_ID        => :1  ,   "
                       + "   v_METRO_GROUP_ID    => :2   ,    "
                       + "   v_METRO_ID          => :3   ,    "
                       + "   v_JOB_ID            => :4   ,    "
                       + "   v_CURRENCY_CODE     => :5   ,    "
                       + "   v_TRVLR_FLAG        => :6   ,    "
                       + "   v_AMOUNT_OVERRIDE   => :7   ,    "
                       + "   v_COMMENTS          => :8   ,    "
                       + "   v_ENABLED_FLAG      => :9   ,    "
                       + "   v_MESSAGE_OUT       => :10       "
                       + " ); end; ";
            OracleCallableStatement oraclecallablestatement = (OracleCallableStatement)oadbtransactionimpl.createCallableStatement(s, -1);
            oraclecallablestatement.setNUMBER(1, getOverrideId());
            oraclecallablestatement.setNUMBER(2, getMetroGroupId());
            oraclecallablestatement.setNUMBER(3, getMetroId());
            oraclecallablestatement.setNUMBER(4, getJobId());
            oraclecallablestatement.setString(5, getCurrencyCode());
            oraclecallablestatement.setString(6, getTrvlrFlag());
            oraclecallablestatement.setNUMBER(7, getAmountOverride());
            oraclecallablestatement.setString(8, getComments());
            oraclecallablestatement.setString(9, getEnabledFlag());
            oraclecallablestatement.registerOutParameter(10, Types.VARCHAR);

            System.out.println("PLSQL insert row > 1"+getAmountOverride());
                System.out.println("PLSQL insert row > 2"+getOverrideId());
            System.out.println("PLSQL insert row > 3"+getMetroGroupId());
            System.out.println("PLSQL insert row > 4"+getMetroId());
            System.out.println("PLSQL insert row > 5"+getJobId());
            System.out.println("PLSQL insert row > 6"+getCurrencyCode());
            System.out.println("PLSQL insert row > 7"+getTrvlrFlag());
            System.out.println("PLSQL insert row > 7"+getEnabledFlag());
            System.out.println("PLSQL insert row > 10"+getComments());

            oraclecallablestatement.execute();
            String messageout = null;
            messageout = oraclecallablestatement.getString(10);
            System.out.println("PLSQL insert row > error"+messageout);
            return messageout;
        }
        catch(SQLException sqlexception)
        {
            System.out.println("PLSQL insert row > 1"+sqlexception.getMessage());
        }
        catch(Exception exception)
        {
           // throw OAException.wrapperException(exception);
        }

      return null;

    }


    public String deleteRow()
    {
        System.out.println("PLSQL deleteRow is called");
          //String messageout = "";
        try
        {
        System.out.println("PLSQL deleteRow is called >2");
        OAApplicationModuleImpl oaapplicationmoduleimpl = (OAApplicationModuleImpl)getApplicationModule();
        OverridesAMImpl overheadsam = (OverridesAMImpl)oaapplicationmoduleimpl.getRootApplicationModule();
        OADBTransactionImpl oadbtransactionimpl = (OADBTransactionImpl)overheadsam.getOADBTransaction();
        System.out.println("PLSQL deleteRow is called >3");

            System.out.println("PLSQL deleteRow > Override ID: "+getOverrideId());
            System.out.println("PLSQL deleteRow > getAmountOverride: "+getAmountOverride());
            System.out.println("PLSQL deleteRow > getEnabledFlag: "+getEnabledFlag());
            System.out.println("PLSQL deleteRow > getComments: "+getComments());
            System.out.println("PLSQL deleteRow > getMetroGroupId: "+getMetroGroupId());
            System.out.println("PLSQL deleteRow > getMetroId: "     +getMetroId());
            System.out.println("PLSQL deleteRow > getJobId: "       +getJobId());
            System.out.println("PLSQL deleteRow > getTrvlrFlag: "   +getTrvlrFlag());

       if (getOverrideId() == null)
       {
          System.out.println("PLSQL deleteRow > NPE Override ID: "+getOverrideId());
       }
       else
       {
           
        String s = "begin PA_RATES_PKG_OVR.override_delete_row( "
                      + "  v_OVERRIDE_ID           =>  :1     ,  "
                      + "     v_MESSAGE_OUT           =>  :2        "
                      + "    ); end; ";                     
            OracleCallableStatement oraclecallablestatement = (OracleCallableStatement)oadbtransactionimpl.createCallableStatement(s, 1);
            oraclecallablestatement.setNUMBER(1, getOverrideId());
            ((OracleCallableStatement)oraclecallablestatement).registerOutParameter(2, Types.VARCHAR);

            //oraclecallablestatement.executeUpdate();
            oraclecallablestatement.execute();

            String messageout = null;

            messageout = oraclecallablestatement.getString(2);

            System.out.println("PLSQL deleteRow > Executed Query: "+s);
            System.out.println("PLSQL deleteRow > v_MESSAGE_OUT :  "+messageout);

            //if ("DUPLICATE".equalsIgnoreCase(messageout) || "INVALID_TRX".equalsIgnoreCase(messageout))
            //{
            //   return messageout;
            //}
            OAExceptionUtils.checkErrors (oadbtransactionimpl);
       }

        }
        catch(SQLException sqlexception)
        {
            System.out.println("PLSQL deleteRow is called >3"+sqlexception.getMessage());
            //throw OAException.wrapperException(sqlexception);
        }
        catch(Exception exception)
        {
            System.out.println("PLSQL deleteRow is called > catch"+exception.getMessage());
        }
      return null;

    }







    public String updateRow()
    {
        System.out.println("PLSQL updateRow is called");
          //String messageout = "";
        try
        {
        System.out.println("PLSQL updateRow is called >2");
        OAApplicationModuleImpl oaapplicationmoduleimpl = (OAApplicationModuleImpl)getApplicationModule();
        OverridesAMImpl overheadsam = (OverridesAMImpl)oaapplicationmoduleimpl.getRootApplicationModule();
        OADBTransactionImpl oadbtransactionimpl = (OADBTransactionImpl)overheadsam.getOADBTransaction();
        System.out.println("PLSQL updateRow is called >3");

            System.out.println("PLSQL updateRow > Override ID: "+getOverrideId());
            System.out.println("PLSQL updateRow > getAmountOverride: "+getAmountOverride());
            System.out.println("PLSQL updateRow > getEnabledFlag: "+getEnabledFlag());
            System.out.println("PLSQL updateRow > getComments: "+getComments());
            System.out.println("PLSQL updateRow > getMetroGroupId: "+getMetroGroupId());
            System.out.println("PLSQL updateRow > getMetroId: "     +getMetroId());
            System.out.println("PLSQL updateRow > getJobId: "       +getJobId());
            System.out.println("PLSQL updateRow > getTrvlrFlag: "   +getTrvlrFlag());

       if (getOverrideId() == null)
       {
          System.out.println("PLSQL updateRow > NPE Override ID: "+getOverrideId());
       }
       else
       {
           
           
        String s = "begin PA_RATES_PKG_OVR.override_update_row( "
                      + "  v_OVERRIDE_ID           =>  :1     ,  "
                      + "  v_METRO_GROUP_ID    =>  :2  ,  "
                      + "  v_METRO_ID          =>  :3  ,  "
                      + "  v_JOB_ID            =>  :4  ,  "
                      + "  v_TRVLR_FLAG        =>  :5  ,  "
                      + "     v_AMOUNT_OVERRIDE     =>  :6     ,  "   
                      + "     v_COMMENTS                 =>  :7     ,    "
                      + "     v_ENABLED_FLAG             =>  :8     ,    "
                      + "     v_MESSAGE_OUT           =>  :9        "
                      + "    ); end; ";                     
            OracleCallableStatement oraclecallablestatement = (OracleCallableStatement)oadbtransactionimpl.createCallableStatement(s, 1);
            oraclecallablestatement.setNUMBER(1, getOverrideId());
            oraclecallablestatement.setNUMBER(2, getMetroGroupId());
            oraclecallablestatement.setNUMBER(3, getMetroId());
            oraclecallablestatement.setNUMBER(4, getJobId());
            oraclecallablestatement.setString(5, getTrvlrFlag());
            oraclecallablestatement.setNUMBER(6, getAmountOverride());
            oraclecallablestatement.setString(7, getComments());
            oraclecallablestatement.setString(8, getEnabledFlag());
            ((OracleCallableStatement)oraclecallablestatement).registerOutParameter(9, Types.VARCHAR);

            //oraclecallablestatement.executeUpdate();
            oraclecallablestatement.execute();

            String messageout = null;

            messageout = oraclecallablestatement.getString(9);

            System.out.println("PLSQL updateRow > Executed Query: "+s);
            System.out.println("PLSQL updateRow > v_MESSAGE_OUT :  "+messageout);

            if ("DUPLICATE".equalsIgnoreCase(messageout) || "INVALID_TRX".equalsIgnoreCase(messageout))
            {
               return messageout;
            }
            OAExceptionUtils.checkErrors (oadbtransactionimpl);
       }

        }
        catch(SQLException sqlexception)
        {
            System.out.println("PLSQL updateRow is called >3"+sqlexception.getMessage());
            //throw OAException.wrapperException(sqlexception);
        }
        catch(Exception exception)
        {
            System.out.println("PLSQL updateRow is called > catch"+exception.getMessage());
        }
      return null;

    }


------------------------------
method in am

    public Boolean checkIfRecordExist(Number number)
    {
        System.out.println(" checkIfRecordExist is invoked");
        OADBTransactionImpl oadbtransactionimpl = (OADBTransactionImpl)getOADBTransaction();
        //String s = null;
        String s1 = "select count(1) as counter from PA_LABOR_RATE_OVERRIDES where OVERRIDE_ID= :1 ";
       
        oracle.jbo.ViewObject vo = findViewObject("tempAdjVO");

        if (vo !=null)
         {
          vo.remove();
         }
       
        oracle.jbo.ViewObject viewobject = createViewObjectFromQueryStmt("tempAdjVO", s1);
        viewobject.setWhereClauseParam(0, number);
        Object obj = null;
        viewobject.executeQuery();
        if(viewobject.hasNext())
        {
            System.out.println(" checkIfRecordExist >viewobject.hasNext");
            oracle.jbo.Row row = viewobject.next();
            if(row.getAttribute(0) != null)
              {  //s = row.getAttribute(0).toString();
                try
                {
                int s2 = Integer.parseInt(row.getAttribute(0).toString());
                System.out.println(" checkIfRecordExist >S2"+s2);
                if( s2 >0 )
                {
                  return Boolean.TRUE;
                }
                }
                catch (Exception E2)
                {}
              }
        }
        viewobject.remove();
        return Boolean.FALSE;
    }

No comments:

Post a Comment