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;
    }