Monday, 27 November 2017

Oracle ADF Table Type

Oracle ADF Table Type

package model.service;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import model.views.EmpVOImpl;
import model.views.EmpVORowImpl;
import java.util.List;
import oracle.jbo.domain.Struct;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;
import oracle.jdbc.internal.OracleCallableStatement;
import oracle.jdbc.internal.OracleTypes;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
//import oracle.sql.STRUCT;
import oracle.jbo.domain.Struct;

import oracle.jdbc.internal.OracleConnection;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Mon Nov 27 20:00:44 IST 2017
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class AppModuleAMImpl extends ApplicationModuleImpl {
    /**
     * This is the default constructor (do not remove).
     */
    public AppModuleAMImpl() {
    }

    /**
     * Container's getter for Employees1.
     * @return Employees1
     */
    public ViewObjectImpl getEmployees1() {
        return (ViewObjectImpl) findViewObject("Employees1");
    }

    /**
     * Container's getter for Employees2.
     * @return Employees2
     */
    public ViewObjectImpl getEmployees2() {
        return (ViewObjectImpl) findViewObject("Employees2");
    }

    /**
     * Container's getter for Departments1.
     * @return Departments1
     */
    public ViewObjectImpl getDepartments1() {
        return (ViewObjectImpl) findViewObject("Departments1");
    }

    /**
     * Container's getter for EmpManagerFkVLink1.
     * @return EmpManagerFkVLink1
     */
    public ViewLinkImpl getEmpManagerFkVLink1() {
        return (ViewLinkImpl) findViewLink("EmpManagerFkVLink1");
    }
 
    public List<EmpVORowImpl> validateEmployees(List<EmpVORowImpl> empList) throws Exception {
      OracleCallableStatement stmt = null;
      ArrayList<EmpVORowImpl> empRespList = null;

      if (empList != null && empList.size() > 0) {
             empRespList = new ArrayList<EmpVORowImpl>();

        try {
          // Call the pl/sql API to determine invoice status
          String sql =
    "begin emp_ddl_package.validate_emp_records(emp_rec_list=>:1,validation_status=>:2); end;";

          stmt =
              (OracleCallableStatement)getDBTransaction().createCallableStatement(sql,0);
        // ArrayList<STRUCT> inputEmpList  = new ArrayList<STRUCT>();
        ArrayList<Struct> inputEmpList  = new ArrayList<Struct>();
         
    //StructDescriptor structDescriptor = StructDescriptor.createDescriptor("EMP_REC_TYPE",stmt.getConnection());
    Struct empRowStruct=null;
      for (int i = 0; i < empList.size(); i++) {
            EmpVORowImpl empRow = empList.get(i);
            Object[] o =
              new Object[] { empRow.getEmployeeId(), empRow.getFirstName(),
                             empRow.getLastName() };
                             //, empRow.getMiddleName(),
                            // empRow.getEmployeeType(), empRow.getLocation(),
                            // empRow.getAddrLine1(), empRow.getAddrLine2(),
                            // empRow.getAddrLine3(), empRow.getCity() };

            //STRUCT empRowStruct = new STRUCT(structDescriptor,stmt.getConnection(), o);
              empRowStruct = (oracle.jbo.domain.Struct)stmt.getConnection().createStruct("EMP_REC_TYPE", o);
            //STRUCT(structDescriptor,stmt.getConnection(), o);
            inputEmpList.add(empRowStruct);
          }
//          ArrayDescriptor empArray =
//          ArrayDescriptor.createDescriptor("EMP_TBL_TYPE",  stmt.getConnection());
//          ARRAY empArrayTbl = new ARRAY(empArray, stmt.getConnection(), inputEmpList.toArray());
         
            Array empArrayTbl= ((OracleConnection) stmt.getConnection()).createOracleArray("EMP_TBL_TYPE", empRowStruct);
                                //stmt.getConnection().createOracleArray
         
         
          java.sql.Array  paramListStruct = null;

          stmt.registerOutParameter(1, OracleTypes.ARRAY, "EMP_TBL_TYPE");
          stmt.setArray(1, empArrayTbl);
          //stmt.setARRAY(1, empArrayTbl);
          stmt.registerOutParameter(2, OracleTypes.VARCHAR, 0, 1);
          stmt.execute();

          //get the output table from pl/sql API and return
          Array outputArray = stmt.getArray(1);
          String returnStatus = stmt.getString(2);

          if (returnStatus != null && "Success".equals(returnStatus) &&
              outputArray != null) {
            ResultSet rs = outputArray.getResultSet();
            EmpVORowImpl tempRow;

            while (rs != null && rs.next()) {
              Struct empRespStruct = (Struct)rs.getObject(2);
              //Object[] empAttributes = empRespStruct.getAttributes();
                Object[] empAttributes = empRespStruct.getAttributeValues();
              if (empAttributes != null) {
                tempRow = (EmpVORowImpl)getEmp2().createRow();
              tempRow.setAttributeValues(Arrays.asList(tempRow.getAttributeNames()), Arrays.asList(empAttributes));
                empRespList.add(tempRow);
              }
            }
          }
        } catch (SQLException sqlex) {
          throw new Exception("sql exception");
        } catch (Exception ex) {
          throw new Exception("exception");
        } finally {
          try {
            stmt.close();
          } catch (SQLException sqlex) {
            throw new Exception("sql exception");
          }
        }
      }

      return empRespList;
    }

    /**
     * Container's getter for Emp1.
     * @return Emp1
     */
    public EmpVOImpl getEmp1() {
        return (EmpVOImpl) findViewObject("Emp1");
    }

    /**
     * Container's getter for Emp2.
     * @return Emp2
     */
    public EmpVOImpl getEmp2() {
        return (EmpVOImpl) findViewObject("Emp2");
    }

    /**
     * Container's getter for EmpManagerFk1VLink1.
     * @return EmpManagerFk1VLink1
     */
    public ViewLinkImpl getEmpManagerFk1VLink1() {
        return (ViewLinkImpl) findViewLink("EmpManagerFk1VLink1");
    }
}

No comments:

Post a Comment