Showing posts with label OAF. Show all posts
Showing posts with label OAF. Show all posts

Saturday, May 6, 2017

Dynamic EO and VO Creation in OAF

Call the below methods in your PR to create dynamic EO and VO.

//Method  to create an EO dynamically.
============================
    public EntityDefImpl dynamicEO(OAPageContext pageContext,
                                   OAWebBean webBean, String baseTable) {
        UtilCO utilCO = new UtilCO();
        AttributeDefImpl newAttrDef1 = null;
        OAViewRowImpl rowImpl = null;
        String attributeName = null;
        String columnName = null;
        String dataType = null;
        String tableName = null;
        Class javaType = null;
        int tabColRow = 0;
        OAApplicationModuleImpl am =
            (OAApplicationModuleImpl)pageContext.getApplicationModule(webBean);
        OAViewObject tabColVO =
            (OAViewObject)am.findViewObject("tableColumnsVO1");
        tabColVO.setWhereClause(null);
        tabColVO.setWhereClause("table_name = '" + baseTable + "'");
        tabColVO.executeQuery();
        tableName = utilCO.caseConvert(baseTable);
        EntityDefImpl newEntity = new EntityDefImpl(tableName + "EO");
        newEntity.setSource(baseTable); //Table Name
        newEntity.setFullName(this.getClass().getPackage().getName() + "." +
                              tableName + "EO"); //packagename.eoName
        tabColRow = tabColVO.getRowCount();
        try {
            RowSetIterator rowIter = tabColVO.createRowSetIterator("rowIter");
            rowIter.setRangeStart(0);
            rowIter.setRangeSize(tabColRow);
            for (int i = 0; i < tabColRow; i++) {
                rowImpl = (OAViewRowImpl)rowIter.getRowAtRangeIndex(i);
                if (utilCO.notNull(rowImpl.getAttribute("ColumnName")) &&
                    utilCO.notNull(rowImpl.getAttribute("DataType"))) {
                    columnName = rowImpl.getAttribute("ColumnName").toString();
                    dataType = rowImpl.getAttribute("DataType").toString();
                    attributeName =
                            utilCO.caseConvert(columnName); //ColumnName
                } else {
                    throw new OAException("Exception when creating dynamic EO.");
                }
                javaType = getJavaType(dataType);

                //  addAttribute(java.lang.String attrName, java.lang.String columnName,
                //             java.lang.Class javaType,  boolean isPrimaryKey,
                //             boolean isDiscriminator,   boolean isPersistent
                //            )
                newAttrDef1 =
                        newEntity.addAttribute(attributeName, columnName, javaType,
                                               false, false, true);
                //Setting Who Column values by default
                if ("CREATION_DATE".equals(columnName) ||
                    "LAST_UPDATE_DATE".equals(columnName)) {
                    newAttrDef1.setDefaultValue(pageContext.getCurrentDBDate());
                } else if ("CREATED_BY".equals(columnName) ||
                           "LAST_UPDATED_BY".equals(columnName)) {
                    newAttrDef1.setDefaultValue(pageContext.getUserId());
                } else if ("LAST_UPDATE_LOGIN".equals(columnName)) {
                    newAttrDef1.setDefaultValue(pageContext.getLoginId());
                }

            }
            rowIter.closeRowSetIterator();
        } catch (Exception e) {
            throw new OAException("Exception in rowIterate Method: " +
                                  e.toString(), OAException.ERROR);
        }
        newAttrDef1 =
                newEntity.addAttribute("RowID", "rowid", RowID.class, true,
                                       false, true);
        //newAttrDef1.setPrimaryKey(true);
        newEntity.resolveDefObject();
        newEntity.registerDefObject();
        if (am.findViewObject(tableName + "VO") != null) {
            System.out.println("VO Already exists. Skipping creation.");
        } else {
            am.createViewObject(tableName + "VO",
                                createEOBasedVO(newEntity, tableName));
        }
        return newEntity;
    }


    public Class getJavaType(String dataType) {
        Class javaType = null;

        if ("VARCHAR2".equals(dataType.toUpperCase())) {
            javaType = String.class;
        } else if ("NUMBER".equals(dataType.toUpperCase())) {
            javaType = Number.class;
        } else if ("DATE".equals(dataType.toUpperCase())) {
            javaType = Date.class;
        } else if ("BLOB".equals(dataType.toUpperCase())) {
            javaType = BlobDomain.class;
        } else if ("CLOB".equals(dataType.toUpperCase())) {
            javaType = ClobDomain.class;
        } else {
            throw new OAException("Unknow Data Type found..." + dataType);
        }

        return javaType;
    }


//Dynamic Vo Based on EO:
======================
    public ViewDefImpl createEOBasedVO(EntityDefImpl eoImpl,
                                       String tableName) {
        ViewDefImpl newView = new ViewDefImpl(tableName + "VO");
        this.getClass().getPackage();
        newView.setFullName(this.getClass().getPackage().getName() + "." +
                            tableName + "VO");
        newView.addEntityUsage("e", eoImpl.getFullName(), false, false);
        newView.addAllEntityAttributes("e");
        newView.setFetchSize((short)30);
        newView.setComponentClass(null);//Since ComponentClass is set as null the vo created is of type Oracle.jbo.viewObject.
//This causes problem when assigning the vo to a bean or when getting handle of the bean. To avoid this, pass
//class OAViewObjectImpl. This will change the viewobject class to OAViewObject.
        newView.setRowClass(null);
        newView.setSelectClauseFlags(ViewDefImpl.CLAUSE_GENERATE_RT);
        newView.setWhereClauseFlags(ViewDefImpl.CLAUSE_GENERATE_RT);
        newView.setFromClauseFlags(ViewDefImpl.CLAUSE_GENERATE_RT);
        newView.resolveDefObject();
        newView.registerDefObject();
        return newView;
    }

Thursday, September 13, 2012

Callable Statement in OAF



To Call a Procedure :

        OADBTransactionImpl txn =
            (OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction();

        CallableStatement cs =
            txn.createCallableStatement("begin :1 := check_Approval_Status(:2,:3); end;",
                                        OADBTransaction.DEFAULT);

        try {
            cs.registerOutParameter(1, Types.VARCHAR);
            cs.setString(1, "retStatus");
            cs.setInt(2, scoreCardId);
            cs.setInt(3, personId);
            String outParamValue = null;
            cs.execute();
            outParamValue = cs.getString(1);
            cs.close();
            if (outParamValue.equals("N")) {
                OASubmitButtonBean oas =
                    (OASubmitButtonBean)webBean.findChildRecursive("MgrTransfer");
                oas.setDisabled(true);
            }

        } catch (SQLException sqle) {
            throw new OAException("Error in Staffing Query",
                                  OAException.ERROR);
        }

Example 2 :

OADBTransactionImpl OADBTxn =
                                (OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction();
                            String query_String = new String();
                            query_String =
                                    "BEGIN insert_into_table(:1,:2,:3,:4,:5);END;";

                            OracleCallableStatement stmt =
                                (OracleCallableStatement)OADBTxn.createCallableStatement(query_String,
                                                                                         -1);
                            try {
                                stmt.setInt(1, jObjectiveId);
                                stmt.setString(2, jName);
                                stmt.setInt(3, jScorecardId);
                                stmt.setString(4, jGroupCode);
                                stmt.setInt(5, jOwningPersonId);
                                stmt.execute();
                                stmt.close();
                            } catch (SQLException e) {
                    throw new OAException("Error in Staffing Query : " +
                                          e, OAException.ERROR);
                            }


To call a Function 

         OADBTransactionImpl txn = (OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction();
         
              CallableStatement cs =
         
                txn.createCallableStatement("begin :1 := xx_pra_func(:2); end;",OADBTransaction.DEFAULT);
         
              try {
         
                cs.registerOutParameter(1, Types.VARCHAR);
         
                cs.setString(1, "ValuesI");
                  cs.setInt(2, 100);
         
                String outParamValue = null;
         
                cs.execute();
         
                outParamValue = cs.getString(1);
         
                cs.close();
                  throw new OAException("Function us "+outParamValue);
         
              } catch (SQLException sqle) {
                  throw new OAException("Error in Staffing Query", OAException.ERROR);
              }

Prepared Statment in OAF


 OADBTransactionImpl OADBTxn =
                                (OADBTransactionImpl)pageContext.getApplicationModule(webBean).getOADBTransaction();
                try {
                    Connection conn =
                        pageContext.getApplicationModule(webBean).getOADBTransaction().getJdbcConnection();
                    String Query =
                        "select nvl((SELECT approval_status from apps.XX_SAMPLE_TABLE" +
                        " where objective_id=:1),'Approved') status from dual";
                    PreparedStatement stmt = conn.prepareStatement(Query);
                    stmt.setInt(1, objectiveId);
                    for (ResultSet resultset = stmt.executeQuery();
                         resultset.next(); ) {
                        pageContext.writeDiagnostics(this, "Query Executed",
                                                     1);
                        String result = resultset.getString("status");//Get the result of the query and store in the string result
                    }
                } catch (Exception exception) {
                    throw new OAException("Error in Staffing Query" +
                                          exception, OAException.ERROR);
                }

Tuesday, September 11, 2012

Performing Row Iteration in Contoller


   
  OAApplicationModule am1;
        OAWebBean oawebbean1 =
            webBean.findIndexedChildRecursive("ScorecardObjectivesRN");
        am1 = pageContext.getApplicationModule(oawebbean1);
        OAViewObject ScorecardObjectivesVO;
        ScorecardObjectivesVO =
                (OAViewObject)am1.findViewObject("ScorecardObjectivesVO");
        Row row[] = ScorecardObjectivesVO.getAllRowsInRange();
        for (int i = 0; i < row.length; i++) {
            ScorecardObjectivesVORowImpl sbrow =
                (ScorecardObjectivesVORowImpl)row[i];
            String groupCode = sbrow.getGroupCode();
            if (groupCode.equals("BO")) {
//Enter Your Code
            } else {
//Enter Else Part
            }

        }

How to call a VO in another AM in same page from controller



Suppose you want to access an VO in Your Page which is attached to a particular Region of the page and not to the Main AM, you can get the VO using the following Code :


        OAApplicationModule am1;
        OAWebBean oawebbean1 =
            webBean.findIndexedChildRecursive("mysampleRN");//Identify the Region to which your AM is attached. In this case mysampleRN is the Region.
        am1 = pageContext.getApplicationModule(oawebbean1);
        OAViewObject mysampleVO;
         mysampleVO  =
                (OAViewObject)am1.findViewObject(" mysampleVO");

Accessing a VO From Controller


OAApplicationModule am = 
                    pageContext.getApplicationModule(webBean);
                OAViewObject mySampleVO;
                if ((OAViewObject)am.findViewObject(" mySampleVO ") != 
                    null) {
                     mySampleVO = 
                            (OAViewObject)am.findViewObject(" mySampleVO ");
}