Showing posts with label execute. Show all posts
Showing posts with label execute. Show all posts

Saturday, October 13, 2018

How to execute lov View Accessor Query?

UseCase:
At each  Employee record, You want to display list of departments, this list of departments is changed according to a parameter changed at run-time.

You click a button and change the parameter value, the LOV must execute its query to fetch the correct data after changing the parameter.

So you have a ViewAccessor  it's query has a parameter ( :bvDeptId ) to filter data,
this parameter is changed during run-time when you click a button you change this parameter but the LOV shows old data, so how to make it's query to be executed:


Departments as list of value (LOV) at EmployeesView

DepartmentsView1 is the ViewAccessor attached to EmployeesView1 ViewObject .
ViewAccessor has a parameter ( bvDeptId ) to filter data,

Solution:
Note: When you create  LOV, you bound an attribute of a ViewObject with a ViewAccessor, This ViewAccessor is a new instance of the ViewObject ( Departments ), this instance is different than instances exists at application module container, this is new instance.

Ways to make the LOV execute it's query:

1- Get the view object which the LOV created on  (EmployeesView1) then retrieve the LOV view accessor  (DepartmentsView1)  then executes it's query:
ApplicationModuleImpl am = ADFUtils.getAppImpl("AppModule");
ViewObject vo =  am.findViewObject("EmployeesView1");
RowSet lov =(RowSet) vo.getCurrentRow().getAttribute("DepartmentsView1"); lov.executeQuery();
2-From the binding container get the LOV attribute and retrieves it's attached view object  then executes it's query:
BindingContainer bindings = getBindingContainer();
//FacesCtrlListBinding
FacesCtrlLOVBinding lov = (FacesCtrlLOVBinding) bindings.get("DepartmentId");
ViewObject departmentVO = lov.getListIterBinding().getViewObject();
departmentVO .executeQuery();
3- You can execute custom method at applicationModuleImpl as described here:
http://come2adf.blogspot.com/2013/05/create-department-while-creating.html

Thursday, May 21, 2015

Fast Call 2 Database 4 DDL, DML, function and Procedure



public String print() {

        System.out.println(Calls2DatabaseHelper.exeucteSql("procedure", "Pro_Get_Employee_Name(?,?)", new String[] { "100" },

                                                           1));

        System.out.println(Calls2DatabaseHelper.exeucteSql("function", "fn_Get_Employee_Name(?)", new String[] { "100" },

                                                           1));

        String sql ="SELECT FIRST_NAME || ' ' ||  LAST_NAME Emp_Name FROM EMPLOYEES WHERE EMPLOYEE_ID = 100";

        System.out.println(Calls2DatabaseHelper.exeucteSql("select", sql, new String []{},

                                                           0));

        sql ="SELECT FIRST_NAME || ' ' ||  LAST_NAME Emp_Name FROM EMPLOYEES WHERE EMPLOYEE_ID = ?";

        System.out.println(Calls2DatabaseHelper.exeucteSql("select", sql, new String []{"100"},

                                                           0));

        sql ="update EMPLOYEES set  LAST_NAME = ? WHERE EMPLOYEE_ID = ?";

        System.out.println(Calls2DatabaseHelper.exeucteSql("update", sql, new String []{"MAhmoud","100"},

                                                           0));
        return "";


    }



public static DBTransaction getConnection() throws SQLException {

        return ADFUtils.getAppImpl().getDBTransaction();

    }

  

    public static String exeucteSql(String operaition, String signature, String[] inValues, int numOutValues) {
        String returnValue = ""
        String begin = " begin ";
        String end = " end; ";
        String sql = "";
        System.out.println(sql);
        if (operaition == "procedure") {
            signature+=";";
              sql = begin + signature + end;
            try (CallableStatement cs = getConnection().createCallableStatement(sql, 0)) {
                for (int i = 1; i <= inValues.length; i++) {
                    cs.setString(i, inValues[i - 1]);
                }
                for (int i = inValues.length + 1; i <= numOutValues + 1; i++) {
                    cs.registerOutParameter(i, OracleTypes.VARCHAR);
                }
                cs.execute();
                returnValue = cs.getString(2);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
            else  if (operaition == "function") {
            signature+=";";
            signature="?:= "+signature;
            sql = begin + signature + end;
            System.out.println(sql);
            try (CallableStatement cs = getConnection().createCallableStatement(sql, 0)) {
                cs.registerOutParameter(1, OracleTypes.VARCHAR);
                for (int i = 2; i <= inValues.length+1; i++) {
                    cs.setString(i, inValues[i - 2]);
                }
                cs.execute();
                returnValue = cs.getString(1);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        else if(operaition== "select"){
            if(inValues.length>0){
            try( PreparedStatement cs = getConnection().createPreparedStatement(signature, 0);) {
                for (int i = 1; i <= inValues.length; i++) {
                    cs.setString(i, inValues[i - 1]);
                }
                ResultSet rs = cs.executeQuery();
                if (rs.next()) {
                 returnValue =   rs.getString("Emp_Name");
                }
            } catch (SQLException e) {
                e.printStackTrace();
             } 
        }  else{
          Statement stmt;
            try {
                stmt = getConnection().createStatement(0);
                ResultSet rs = stmt.executeQuery(signature);
                if (rs.next()) {
                 returnValue =   rs.getString("Emp_Name");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        }
            else {
                try( PreparedStatement cs = getConnection().createPreparedStatement(signature, 0);) {
                    if(inValues.length>0){
                    for (int i = 1; i <= inValues.length; i++) {
                        cs.setString(i, inValues[i - 1]);
                    }
                    }
                    int numChangedRows = cs.executeUpdate();
                    getConnection().commit();
                    System.out.println("numChangedRows="+numChangedRows);
                } catch (SQLException e) {
                    e.printStackTrace();
                 } 
            }
        return returnValue;
        }

Download Link

http://sameh-nassar.blogspot.com/2010/12/calling-sql-statment-inside-java-code.html

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...