常用代码 OAF(转)

来源:这里教程网 时间:2026-03-03 16:04:40 作者:

进入进行查询


在VOimpl里中增加方法:

import oracle.jbo.domain.Number; import oracle.apps.fnd.framework.OAException; ... public void initQuery(String employeeNumber) { if ((employeeNumber != null) &&   (!("".equals(employeeNumber.trim())))) { // Do the following conversion for type consistency. Number empNum = null; try { empNum = new Number(employeeNumber); }   catch(Exception e)   { throw new OAException("AK", "FWK_TBX_INVALID_EMP_NUMBER"); } setWhereClause("EMPLOYEE_ID = :1"); setWhereClauseParams(null); // Always reset setWhereClauseParam(0, empNum); executeQuery(); } } // end initQuery() 在AM建立方法,进行VO查询的调用

import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.common.MessageToken;
 ...
  /*****************************************************************************
    * Initializes the detail employee query.
    *****************************************************************************
    */
    public void initDetails(String employeeNumber)
    {
      EmpfullVOImpl vo = getEmpfullVO1();
      if (vo == null)
      {
        MessageToken[] errTokens = { new MessageToken("OBJECT_NAME", "EmployeeFullVO1")};
        throw new OAException("AK", "FWK_TBX_OBJECT_NOT_FOUND", errTokens);
      } 
      vo.initQuery(employeeNumber);
     
    } // end initDetails()
 
页面被调用时,CO执行下面代码,对前面函数进行调用和执行(调用之前,传入变量的参数..这个不能忘记)
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    // Get the employeeNumber parameter from the URL
        String employeeNumber = pageContext.getParameter("employeeNumber");
      
        // Now we want to initialize the query for our single employee
        // with all of its details. 
        OAApplicationModule am = pageContext.getApplicationModule(webBean);
        Serializable[] parameters = { employeeNumber };
        am.invokeMethod("initDetails", parameters);
  }


设置窗口的title 和 message的使用

String employeeName = pageContext.getParameter("employeeName");

// Always use a translated value from Message Dictionary when setting // strings in your controllers. // Instantiate an array of message tokens and set the value for the // EMP_NAME token. MessageToken[] tokens = { new MessageToken("EMP_NAME", employeeName)};

// Now, get the translated message text including the token value. String pageHeaderText =   pageContext.getMessage("AK", "FWK_TBX_T_EMP_HEADER_TEXT", tokens);

// Set the employee-specific page title (which also appears in // the breadcrumbs). Note that we know this controller is // associated with the pageLayout region, which is why we cast the   // webBean to an OAPageLayoutBean before calling setTitle. ((OAPageLayoutBean)webBean).setTitle(pageHeaderText);


Create 基本程序

在AM中,VO实例化代码

public void createEmployee() { OAViewObject vo = (OAViewObject)getEmpfullVO1();

// Per the coding standards, this is the proper way to initialize a   // VO that is used for both inserts and queries. See View Objects   // in Detail in the Developer's Guide for additional information.   if (!vo.isPreparedForExecution()) { vo.executeQuery(); }

Row row = vo.createRow(); vo.insertRow(row); // Required per OA Framework Model Coding Standard M69 row.setNewRowState(Row.STATUS_INITIALIZED);

} // end createEmployee()

CO 的processRequest中增加 VO实例化调用

if (!pageContext.isFormSubmission()) { OAApplicationModule am = pageContext.getApplicationModule(webBean); am.invokeMethod("createEmployee", null); }

CO 的processFormRequest中增加对按钮"Apply"的处理

if (pageContext.getParameter("Apply") != null) { // Generally in the tutorial application and the labs, we've illustrated // all BC4J interaction on the server (except for the AMs, of course). Here, // we're dealing with the VO directly so the comments about the reasons // why we're obtaining values from the VO and not the request make sense // in context.   OAViewObject vo = (OAViewObject)am.findViewObject("EmpfullVO1");

// Note that we have to get this value from the VO because the EO will // assemble it during its validation cycle. // For performance reasons, we should generally be calling getEmployeeName() // on the EmployeeFullVORowImpl object, but we don't want to do this // on the client so we're illustrating the interface-appropriate call. If // we implemented this code in the AM where it belongs, we would use the // other approach. String employeeName =   (String)vo.getCurrentRow().getAttribute("FullName"); // We need to get a String so we can pass it to the MessageToken array below. Note // that we are getting this value from the VO (we could also get it from // the Bean as shown in the Drilldown to Details lab) because the item style. is messageStyledText,   // so the value isn't put on the request like a messaqeTextInput value is. Number employeeNumber =   (Number)vo.getCurrentRow().getAttribute("EmployeeId"); String employeeNum = String.valueOf(employeeNumber.intValue()); // Simply telling the transaction to commit will cause all the Entity Object validation // to fire. // // Note: there's no reason for a developer to perform. a rollback. This is handled by // the framework if errors are encountered.

am.invokeMethod("apply");

// Assuming the "commit" succeeds, navigate back to the "Search" page with   // the user's search criteria intact and display a "Confirmation" message // at the top of the page.

MessageToken[] tokens = { new MessageToken("EMP_NAME", employeeName),   new MessageToken("EMP_NUMBER", employeeNum) }; OAException confirmMessage =   new OAException("AK", "FWK_TBX_T_EMP_CREATE_CONFIRM", tokens,   OAException.CONFIRMATION, null); // Per the UI guidelines, we want to add the confirmation message at the   // top of the search/results page and we want the old search criteria and // results to display.

pageContext.putDialogMessage(confirmMessage); // retain AM pageContext.forwardImmediately("OA.jsp?page=/lyf4/oracle/apps/ak/emp/webui/EmpSearchPG",   null,   OAWebBeanConstants.KEEP_MENU_CONTEXT,   null, null, true,   OAWebBeanConstants.ADD_BREAD_CRUMB_NO); }

在AM中增加apply函数对事务commit

public void apply() { // 这里只有一个事务吗? getTransaction().commit(); }


调用页面代码

pageContext.setForwardURL("OA.jsp?page=/lyf4/oracle/apps/ak/emp/webui/EmployeePG", null, OAWebBeanConstants.KEEP_MENU_CONTEXT,   null, null, true, // Retain AM OAWebBeanConstants.ADD_BREAD_CRUMB_YES, OAWebBeanConstants.IGNORE_MESSAGES);


处理back button

页面初始化时候,建立一有名字的事务

public void processRequest(OAPageContext pageContext, OAWebBean webBean) { super.processRequest(pageContext, webBean); if (!pageContext.isBackNavigationFired(false)) { TransactionUnitHelper.startTransactionUnit(pageContext, "empCreateTxn");

if (!pageContext.isFormSubmission()) { OAApplicationModule am = pageContext.getApplicationModule(webBean); am.invokeMethod("createEmployee", null); } } else { if (!TransactionUnitHelper.isTransactionUnitInProgress(pageContext,   "empCreateTxn",   true)) { // We got here through some use of the browser "Back" button, so we   // want to display a stale data error and disallow access to the page. // If this were a real application, we would probably display a more // context-specific message telling the user she can't use the browser // "Back" button and the "Create" page. Instead, we wanted to illustrate // how to display the Applications standard NAVIGATION ERROR message.

OADialogPage dialogPage = new OADialogPage(NAVIGATION_ERROR); pageContext.redirectToDialogPage(dialogPage); } }

}


在AM中,建立数据回滚函数

public void rollbackEmployee() { Transaction txn = getTransaction(); // This small optimization ensures that we don't perform. a rollback   // if we don't have to. if (txn.isDirty()) { txn.rollback(); }   } // end rollbackEmployee()

在需要时,对事务进行关闭

if (TransactionUnitHelper.isTransactionUnitInProgress(pageContext, "empCreateTxn", false {   am.invokeMethod("rollbackEmployee"); TransactionUnitHelper.endTransactionUnit(pageContext, "empCreateTxn"); }


检查主键的唯一性,同时在缓存和数据进行检查(在EO中进行数据验证检查)

if (value != null) { OADBTransaction transaction = getOADBTransaction(); Object[] employeeKey = { value }; EntityDefImpl empDefinition = empEOImpl.getDefinitionObject(); empEOImpl employee =   (empEOImpl)empDefinition.findByPrimaryKey(transaction,   new Key(employeeKey));

//findByPrimaryKey 函数会先检查缓存,然后检查数据库

if (employee != null) { throw // EO name // EO PK // Attribute Name // Attribute value // Message product short name new OAAttrValException(OAException.TYP_ENTITY_OBJECT,   getEntityDef().getFullName(), getPrimaryKey(),   "EmployeeId", value, "AK",   "FWK_TBX_T_EMP_ID_UNIQUE"); // Message name } }


进行数据的初始化,应该在EO的create函数中进行...

下面是取序列,然后对员工id进行初始化

public void create(AttributeList attributeList) { super.create(attributeList); OADBTransaction transaction = getOADBTransaction(); Number employeeId = transaction.getSequenceValue("FWK_TBX_EMPLOYEES_S"); setEmployeeId(employeeId);

}


日期验证

protected void validateStartDate(Date value) {

if (value != null) {

OADBTransaction transaction = getOADBTransaction(); long sysdate = transaction.getCurrentDBDate().dateValue().getTime(); long startDate = value.dateValue().getTime();

if (startDate < sysdate) { throw // EO name // EO PK // Attribute Name // Attribute value // Message product short name new OAAttrValException(OAException.TYP_ENTITY_OBJECT,   getEntityDef().getFullName(), getPrimaryKey(),   "StartDate", value, "AK",   "FWK_TBX_T_START_DATE_PAST"); // Message name }

}

}


VO循环迭代

public void deleteEmployee(String employeeNumber) { // First, we need to find the selected employee in our VO. // When we find it, we call remove( ) on the row which in turn // calls remove on the associated EmployeeEOImpl object. int empToDelete = Integer.parseInt(employeeNumber);

OAViewObject vo = (OAViewObject)getempVO1();   empVORowImpl row = null;

// This tells us the number of rows that have been fetched in the // row set, and will not pull additional rows in like some of the // other "get count" methods. int fetchedRowCount = vo.getFetchedRowCount();

// We use a separate iterator -- even though we could step through the // rows without it -- because we don't want to affect row currency. RowSetIterator deleteIter = vo.createRowSetIterator("deleteIter"); if (fetchedRowCount > 0)   {   deleteIter.setRangeStart(0);   deleteIter.setRangeSize(fetchedRowCount); for (int i = 0; i < fetchedRowCount; i++) { row = (empVORowImpl)deleteIter.getRowAtRangeIndex(i);

// For performance reasons, we generate ViewRowImpls for all // View Objects. When we need to obtain an attribute value, // we use the named accessors instead of a generic String lookup. // Number primaryKey = (Number)row.getAttribute("EmployeeId"); Number primaryKey = row.getEmployeeId(); if (primaryKey.compareTo(empToDelete) == 0) { // This performs the actual delete. row.remove(); getTransaction().commit();   break; // only one possible selected row in this case } } } // Always close the iterator when you're done. deleteIter.closeRowSetIterator(); } }


删除确认窗口

else if ("delete".equals(pageContext.getParameter(EVENT_PARAM))) { // The user has clicked a "Delete" icon so we want to display a "Warning" // dialog asking if she really wants to delete the employee. Note that we   // configure the dialog so that pressing the "Yes" button submits to   // this page so we can handle the action in this processFormRequest( ) method.

String employeeNumber = pageContext.getParameter("empNum"); String employeeName = pageContext.getParameter("empName");

MessageToken[] tokens = { new MessageToken("EMP_NAME", employeeName)}; OAException mainMessage = new OAException("AK", "FWK_TBX_T_EMP_DELETE_WARN", tokens);

// Note that even though we're going to make our Yes/No buttons submit a // form, we still need some non-null value in the constructor's Yes/No   // URL parameters for the buttons to render, so we just pass empty   // Strings for this. OADialogPage dialogPage = new OADialogPage(OAException.WARNING,   mainMessage, null, "", "");

// Always use Message Dictionary for any Strings you want to display. String yes = pageContext.getMessage("AK", "FWK_TBX_T_YES", null); String no = pageContext.getMessage("AK", "FWK_TBX_T_NO", null);

// We set this value so the code that handles this button press is   // descriptive. dialogPage.setOkButtonItemName("DeleteYesButton");

// The following configures the Yes/No buttons to be submit buttons, // and makes sure that we handle the form. submit in the originating // page (the "Employee" summary) so we can handle the "Yes" // button selection in this controller. dialogPage.setOkButtonToPost(true); dialogPage.setNoButtonToPost(true); dialogPage.setPostToCallingPage(true);

// Now set our Yes/No labels instead of the default OK/Cancel. dialogPage.setOkButtonLabel(yes);   dialogPage.setNoButtonLabel(no);

// We need to keep hold of the employeeNumber and employeeName. // The OADialogPage gives us a convenient means   // of doing this. Note that the use of the Hashtable is   // most appropriate for passing multiple parameters. See the OADialogPage   // javadoc for an alternative when dealing with a single parameter. java.util.Hashtable formParams = new java.util.Hashtable(1);   formParams.put("empNum", employeeNumber);   formParams.put("empName", employeeName); dialogPage.setFormParameters(formParams);   pageContext.redirectToDialogPage(dialogPage); }


删除调用及显示删除信息

else if (pageContext.getParameter("DeleteYesButton") != null) {   // User has confirmed that she wants to delete this employee. // Invoke a method on the AM to set the current row in the VO and   // call remove() on this row.   String employeeNumber = pageContext.getParameter("empNum"); String employeeName = pageContext.getParameter("empName"); Serializable[] parameters = { employeeNumber }; OAApplicationModule am = pageContext.getApplicationModule(webBean); am.invokeMethod("deleteEmployee", parameters); // Now, redisplay the page with a confirmation message at the top. Note // that the deleteEmployee() method in the AM commits, and our code // won't get this far if any exceptions are thrown.

MessageToken[] tokens = { new MessageToken("EMP_NAME", employeeName) }; OAException message = new OAException("AK",   "FWK_TBX_T_EMP_DELETE_CONFIRM", tokens, OAException.CONFIRMATION, null); pageContext.putDialogMessage(message); }

相关推荐