Example usage for java.sql ResultSet first

List of usage examples for java.sql ResultSet first

Introduction

In this page you can find the example usage for java.sql ResultSet first.

Prototype

boolean first() throws SQLException;

Source Link

Document

Moves the cursor to the first row in this ResultSet object.

Usage

From source file:ProcessRequest.java

public void calculateTicketDetail(Integer ticketID, JSONObject recordObject)
        throws SQLException, JSONException {
    //first, make sure we have a valid product
    String productQuery = "SELECT * FROM " + DbSingleton.PRODUCT_TABLE_NAME + " WHERE "
            + DbSingleton.ProductSchema.COLUMN_ID + "='"
            + recordObject.getString(DbSingleton.ProductSchema.COLUMN_ID) + "'";
    ResultSet productResultSet = executeQuery(productQuery);
    productResultSet.last();//from w  w  w  .  j av  a2s  . co  m
    int rowCount = productResultSet.getRow();
    productResultSet.first();
    String productID = null;
    Double factor = null;
    if (rowCount < 1) {
        throw new SQLException("no products defined!");
    } else {
        do {
            if (recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_PRODUCT_ID)
                    .equals(productResultSet.getString(DbSingleton.ProductSchema.COLUMN_ID))) {
                productID = productResultSet.getString(DbSingleton.ProductSchema.COLUMN_ID);
                factor = productResultSet.getDouble(DbSingleton.ProductSchema.COLUMN_FACTOR);
                break;
            }
        } while (productResultSet.next());
        if (productID == null) {
            throw new SQLException("no such product id");
        } else {
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_PRODUCT_NAME);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_PRODUCT_NAME,
                    productResultSet.getString(DbSingleton.ProductSchema.COLUMN_NAME));
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_INVENTORY_NAME);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_INVENTORY_NAME,
                    productResultSet.getString(DbSingleton.ProductSchema.COLUMN_INVENTORY_NAME));
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_PRICE_PER_UOM);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_PRICE_PER_UOM,
                    productResultSet.getString(DbSingleton.ProductSchema.COLUMN_T1_PRICE));
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_UOM);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_UOM,
                    productResultSet.getString(DbSingleton.ProductSchema.COLUMN_UOM));
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_FACTOR);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_FACTOR,
                    productResultSet.getString(DbSingleton.ProductSchema.COLUMN_FACTOR));
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_PRODUCT_TAXABLE);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_PRODUCT_TAXABLE,
                    (productResultSet.getBoolean(DbSingleton.ProductSchema.COLUMN_PRODUCT_TAXABLE)) ? 1 : 0);
        }
    }

    String uom = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_UOM);
    if (uom.equals("CUYD") || uom.equals("EACH")) {
        //grab units
        recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_NET_LBS);
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_NET_LBS,
                recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNITS));
    } else {
        //grab net weight
        if (recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS) < recordObject
                .getDouble(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS)) {
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_NET_LBS);
            Double net = recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS)
                    - recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS)
                    - recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_NET_LBS, net);
        }
        recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_UNITS);
        if (factor != 0) {
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_UNITS,
                    recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_NET_LBS) / factor);
        } else {
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_UNITS, 0);
        }
    }

    recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TOTAL);
    recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TOTAL,
            recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNITS)
                    * recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_PRICE_PER_UOM));

    if (productResultSet.getDouble(DbSingleton.ProductSchema.COLUMN_MIN_COST) > 0) {
        if (recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNITS) < productResultSet
                .getDouble(DbSingleton.ProductSchema.COLUMN_MIN_COST)
                && recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNITS) > 0) {
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TOTAL);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TOTAL,
                    recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_MIN_COST));
            recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_PRICE_PER_UOM);
            recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_PRICE_PER_UOM,
                    recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TOTAL)
                            / recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNITS));
        }
    }

    //String ticketQuery = "SELECT * FROM "+DbSingleton.TICKET_TABLE_NAME+" WHERE "+DbSingleton.TicketSchema.COLUMN_TICKET_ID+"="+recordObject.getInt(DbSingleton.TicketDetailSchema.COLUMN_TICKET_ID);
    String ticketQuery = "SELECT * FROM " + DbSingleton.TICKET_TABLE_NAME + " WHERE "
            + DbSingleton.TicketSchema.COLUMN_TICKET_ID + "=" + ticketID;
    ResultSet ticketResultSet = executeQuery(ticketQuery);
    ticketResultSet.first();

    String query = "SELECT * FROM " + DbSingleton.CUSTOMER_TABLE_NAME + " WHERE "
            + DbSingleton.CustomerSchema.COLUMN_ID + "='"
            + ticketResultSet.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ID) + "'";
    ResultSet customerResultSet = executeQuery(query);
    customerResultSet.last();
    rowCount = customerResultSet.getRow();
    customerResultSet.first();
    if (customerResultSet.getBoolean(DbSingleton.CustomerSchema.COLUMN_TAXABLE)) {
        if (productResultSet.getBoolean(DbSingleton.ProductSchema.COLUMN_PRODUCT_TAXABLE)) {
            String iniQuery = "SELECT * FROM INISetting WHERE INIKey=TaxRate";
            ResultSet iniResultSet = executeQuery(iniQuery);
            iniResultSet.last();
            rowCount = iniResultSet.getRow();
            iniResultSet.first();
            if (rowCount < 1) {
                throw new SQLException("could not get tax info for a taxable item!");
            } else {
                Double taxRate = iniResultSet.getDouble("INIValue");
                recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TAX);
                recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TAX,
                        recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_NET_LBS) * taxRate);
            }
        }
    }

    recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_TOTAL);
    recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_TOTAL,
            recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TOTAL)
                    + recordObject.getDouble(DbSingleton.TicketDetailSchema.COLUMN_UNIT_TAX));
}

From source file:ProcessRequest.java

public JSONObject parseWeight(JSONObject requestObject) throws SQLException, JSONException {
    String host = null;//from ww w . j ava2  s .  co m
    int port = 0;
    int timeout = 0;
    int termChar = 0;
    int weightBeginPos = 0;
    int weightEndPos = 0;
    int statusBeginPos = 0;
    int statusEndPos = 0;
    int normalChar = 0;
    int motionChar = 0;
    int bufferLength = 0;
    String overCapacity = null;
    if (requestObject.has("host") && requestObject.has("port")) {
        host = requestObject.getString("host");
        port = requestObject.getInt("port");
        return getCondecWeight(host, port);
    } else if (requestObject.has("scale_id")) {
        ResultSet rs = null;
        Integer scaleID = requestObject.getInt("scale_id");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "OverCapacityWeight'");
        rs.first();
        overCapacity = rs.getString("INIValue");
        if (overCapacity.equals("0")) {
            overCapacity = "--------";
        }
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "IPAddress'");
        rs.first();
        host = rs.getString("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "IPPort'");
        rs.first();
        port = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "Timeout'");
        rs.first();
        timeout = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "TerminationChar'");
        rs.first();
        termChar = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "WeightBeginPos'");
        rs.first();
        weightBeginPos = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "WeightEndPos'");
        rs.first();
        weightEndPos = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID
                + "StatisBeginPos' OR INIKey='Scale" + scaleID + "StatusBeginPos'");
        rs.first();
        statusBeginPos = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID
                + "StatisEndPos' OR INIKey='Scale" + scaleID + "StatusEndPos'");
        rs.first();
        statusEndPos = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "NormalChar'");
        rs.first();
        normalChar = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "BufferLength'");
        rs.first();
        bufferLength = rs.getInt("INIValue");
        rs = executeQuery("SELECT * FROM INISetting WHERE INIKey='Scale" + scaleID + "MotionChar'");
        rs.first();
        motionChar = rs.getInt("INIValue");
        if (host != null && port != 0 && timeout != 0 && termChar != 0 && weightBeginPos != 0
                && weightEndPos != 0 && statusBeginPos != 0 && statusEndPos != 0 && normalChar != 0
                && motionChar != 0 && bufferLength != 0 && overCapacity != null) {
            return getWeight(host, port, bufferLength, weightBeginPos, weightEndPos, termChar, statusBeginPos,
                    statusEndPos, normalChar, motionChar, timeout, overCapacity);
        } else {
            return getCondecWeight();
        }
    } else {
        return getCondecWeight();
    }
}

From source file:ProcessRequest.java

public void parseQueryResults(ResultSet rs, String table, OutputStream os, boolean append)
        throws SQLException, JSONException, IOException {
    //JSONArray resultJSONArray = new JSONArray();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columns = rsmd.getColumnCount();
    rs.last();// ww  w  .j  a  va  2s  .  c  o m
    int rows = rs.getRow();
    os.write(new String("total rows: " + rows).getBytes());
    rs.first();
    int rowCount = 0;
    while (rs.next()) {
        if (!rs.isFirst() || append) {
            os.write(new String(",\n").getBytes());
            os.write(new String("" + rowCount).getBytes());
        }
        if (rowCount >= 69)
            System.out.println("break point");
        rowCount++;
        JSONObject result = new JSONObject();
        JSONObject resultMeta = new JSONObject();
        resultMeta.put("table", table);
        result.put("metadata", resultMeta);
        for (int i = 1; i <= columns; i++) {
            //out.println("<td>"+rs.getString(i)+"</td>");
            int type = rsmd.getColumnType(i);
            //result.put(rsmd.getColumnName(i), rs.get)
            switch (type) {
            case Types.BIT:
                result.put(rsmd.getColumnName(i), rs.getBoolean(i));
                break;
            case Types.TINYINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.SMALLINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.INTEGER:
                //System.out.println(rsmd.getColumnName(i) + "  type: "+type);
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.BIGINT:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.FLOAT:
                result.put(rsmd.getColumnName(i), rs.getFloat(i));
                break;
            case Types.REAL:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.DOUBLE:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.NUMERIC:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.DECIMAL:
                result.put(rsmd.getColumnName(i), rs.getDouble(i));
                break;
            case Types.CHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.VARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.LONGVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.DATE: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.TIME: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.TIMESTAMP: {
                java.util.Date date = rs.getDate(i);
                result.put(rsmd.getColumnName(i), date.getTime());
                break;
            }
            case Types.BINARY:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.VARBINARY:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.LONGVARBINARY:
                result.put(rsmd.getColumnName(i), rs.getLong(i));
                break;
            case Types.NULL:
                result.put(rsmd.getColumnName(i), "");
                break;
            case Types.BOOLEAN:
                result.put(rsmd.getColumnName(i), rs.getBoolean(i));
                break;
            case Types.ROWID:
                result.put(rsmd.getColumnName(i), rs.getInt(i));
                break;
            case Types.NCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.NVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.LONGNVARCHAR:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            case Types.SQLXML:
            case Types.NCLOB:
            case Types.DATALINK:
            case Types.REF:
            case Types.OTHER:
            case Types.JAVA_OBJECT:
            case Types.DISTINCT:
            case Types.STRUCT:
            case Types.ARRAY:
            case Types.BLOB:
            case Types.CLOB:
            default:
                result.put(rsmd.getColumnName(i), rs.getString(i));
                break;
            }
        }
        //if(table.equals("Ticket"))
        //System.out.println(result.toString(5));
        //if(result.getInt("TicketNumber")==126868)
        //   System.out.println("break point");
        //resultJSONArray.put(result);
        os.write(result.toString(5).getBytes());
    }
    //return resultJSONArray;
}

From source file:ProcessRequest.java

public int newTicket(JSONObject recordObject) throws SQLException, JSONException {
    String ticketDate = null;/*from  ww  w  .ja v a  2s  . c o  m*/
    try {
        ticketDate = mDateFormat
                .format(new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_TICKET_DATE)));
    } catch (JSONException jsone) {
        ticketDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_TICKET_DATE);
    }

    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_SIGNATURE_PICTURE).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_SIGNATURE_PICTURE);
    }
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_TMS_ROW_GUID);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_ID);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_DATE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_CHECK_NUMBER);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_VOIDED);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_COMPLETED);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_INVOICE_CLEARED);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_TYPE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_SITE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_STATION_ID);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_INVOICE_NUMBER);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_LAST_MODIFIED);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_HOLD_CHECK);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_INDUSTRIAL);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_INDUSTRIAL, 0);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_CHECK_NUMBER, 0);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_VOIDED, 0);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_COMPLETED, 0);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_INVOICE_CLEARED, 0);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_TYPE, "S");
    recordObject.put(DbSingleton.TicketSchema.COLUMN_SITE, "SITE1");
    recordObject.put(DbSingleton.TicketSchema.COLUMN_STATION_ID, 1);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE, ticketDate);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE, ticketDate);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE, ticketDate);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE, ticketDate);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_DATE, ticketDate);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_HOLD_CHECK, 0);
    if (recordObject.getBoolean(DbSingleton.TicketSchema.COLUMN_PENDING)) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_PENDING);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_PENDING, 1);
    } else {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_PENDING);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_PENDING, 0);
    }
    if (recordObject.getBoolean(DbSingleton.TicketSchema.COLUMN_TICKET_PRINTED)) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_PRINTED);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_PRINTED, 1);
    } else {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_PRINTED);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_PRINTED, 0);
    }

    if (!recordObject.getString(DbSingleton.TicketSchema.COLUMN_DIRECTION).equals("O")
            || !recordObject.getString(DbSingleton.TicketSchema.COLUMN_DIRECTION).equals("I")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_DIRECTION);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_DIRECTION, "I");
    }

    /////////////////CUSTOMER SCOPE: anything that requires customer table is done in here.
    String query = "SELECT * FROM " + DbSingleton.CUSTOMER_TABLE_NAME + " WHERE "
            + DbSingleton.CustomerSchema.COLUMN_ID + "='"
            + recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ID) + "'";
    ResultSet customerResultSet = executeQuery(query);
    customerResultSet.last();
    int rowCount = customerResultSet.getRow();
    customerResultSet.first();
    if (rowCount < 1)
        throw new SQLException(LOG_TAG + ": no such customer id!");
    /*
    int rowCount = 0;
    try {
       rowCount = java.lang.reflect.Array.getLength(customerResultSet.getArray(0).getArray());
    } catch(SQLException sqle) { }
    if(rowCount<1)
       throw new SQLException(LOG_TAG+": no such customer id!");
    */
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_NAME) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_NAME).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_NAME);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_NAME,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_NAME));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ADDRESS) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ADDRESS).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ADDRESS);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ADDRESS,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_ADDRESS));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_CITY) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_CITY).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_CITY);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_CITY,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_CITY));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_STATE) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_STATE).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_STATE);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_STATE,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_STATE));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ZIP) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ZIP).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ZIP);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ZIP,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_ZIP));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ACCOUNT_TYPE) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ACCOUNT_TYPE).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ACCOUNT_TYPE);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_ACCOUNT_TYPE,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_ACCOUNT_TYPE));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TYPE_CODE) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TYPE_CODE).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TYPE_CODE);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TYPE_CODE,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_TYPE_CODE));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_COUNTY) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_COUNTY).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_COUNTY);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_COUNTY,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_COUNTY));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_BUSINESS_NAME_ID) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_BUSINESS_NAME_ID).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_BUSINESS_NAME_ID);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_BUSINESS_NAME_ID,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_BUSINESS_NAME_ID));
    }
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_DEALER_LICENSE_NUMBER) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_DEALER_LICENSE_NUMBER).equals("")) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_DEALER_LICENSE_NUMBER);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_DEALER_LICENSE_NUMBER,
                customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEALER_LICENSE_NUMBER));
    }
    int reconciliationType = customerResultSet.getInt(DbSingleton.CustomerSchema.COLUMN_RECONCILIATION_TYPE);
    switch (reconciliationType) {
    case 0:
        recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, reconciliationType);
        break;
    case 1:
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_DIRECTION).equals("I"))
            recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, reconciliationType);
        else
            recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, 0);
        break;
    case 2:
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_DIRECTION).equals("O"))
            recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, reconciliationType);
        else
            recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, 0);
        break;
    case 3:
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_DIRECTION).equals("I"))
            recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, 1);
        else
            recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, 2);
        break;
    default:
        throw new SQLException(
                "unknown reconciliation type!  this shouldn't even be possible.  how did you do this?  please call support.");
    }
    /*
    if(reconciliationType==null || recordObject.getString(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE).equals("")) {
       recordObject.remove(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE);
       recordObject.put(DbSingleton.TicketSchema.COLUMN_RECONCILIATION_TYPE, customerResultSet.getInt(DbSingleton.CustomerSchema.COLUMN_RECONCILIATION_TYPE));
       if()
    }
    */
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TAXABLE);
    if (customerResultSet.getBoolean(DbSingleton.CustomerSchema.COLUMN_TAXABLE))
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TAXABLE, 1);
    else
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_TAXABLE, 0);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_VALID_ADDRESS);
    if (customerResultSet.getBoolean(DbSingleton.CustomerSchema.COLUMN_VALID_ADDRESS))
        recordObject.put(DbSingleton.TicketSchema.COLUMN_VALID_ADDRESS, 1);
    else
        recordObject.put(DbSingleton.TicketSchema.COLUMN_VALID_ADDRESS, 0);
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_CUSTOMER_INVOICEABLE);
    if (customerResultSet.getBoolean(DbSingleton.CustomerSchema.COLUMN_INVOICEABLE)) {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_INVOICEABLE, 1);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_INVOICE_NUMBER, 0); //TODO: CustomerAccountType: -1 for CASH|CASHONLY, 0 for CHARGE
    } else {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_CUSTOMER_INVOICEABLE, 0);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_INVOICE_NUMBER, -1); //TODO: CustomerAccountType: -1 for CASH|CASHONLY, 0 for CHARGE
    }
    recordObject.remove(DbSingleton.TicketSchema.COLUMN_REGISTERED);
    if (customerResultSet.getBoolean(DbSingleton.CustomerSchema.COLUMN_REGISTERED))
        recordObject.put(DbSingleton.TicketSchema.COLUMN_REGISTERED, 1);
    else
        recordObject.put(DbSingleton.TicketSchema.COLUMN_REGISTERED, 0);
    //////////////////

    //////////////////HAULER
    //NOTE: the code below is for mobile 2 only, since customer is always hauler
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_HAULER_ID) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_HAULER_ID).equals("")) {
        if (customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_HAULER_ID) == null
                || customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_HAULER_ID)
                        .equals("")) {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_ID, "CASH");
        } else {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_ID,
                    customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_HAULER_ID));
        }
    } else if (!customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_REQUIRE_HAULER_ID).equals("0")) {
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_HAULER_ID) == null
                || recordObject.getString(DbSingleton.TicketSchema.COLUMN_HAULER_ID).equals("")) {
            throw new SQLException("HAULER IS A REQUIRED FIELD!");
        }
    }
    String haulerQuery = "SELECT * FROM Hauler WHERE HaulerID='"
            + recordObject.getString(DbSingleton.TicketSchema.COLUMN_HAULER_ID) + "'";
    ResultSet haulerResultSet = executeQuery(haulerQuery);
    haulerResultSet.last();
    rowCount = haulerResultSet.getRow();
    haulerResultSet.first();
    if (rowCount > 0) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_NAME);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_NAME, haulerResultSet.getString("HaulerName"));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ADDRESS);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_ADDRESS,
                haulerResultSet.getString("HaulerAddress"));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_CITY);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_CITY, haulerResultSet.getString("HaulerCity"));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_STATE);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_STATE,
                haulerResultSet.getString("HaulerState"));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ZIP);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_ZIP, haulerResultSet.getString("HaulerZip"));
    } else {
        haulerQuery = "SELECT * FROM Hauler WHERE HaulerID='CASH'";
        haulerResultSet = executeQuery(haulerQuery);
        haulerResultSet.last();
        rowCount = haulerResultSet.getRow();
        haulerResultSet.first();
        if (rowCount > 0) {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_NAME);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_NAME,
                    haulerResultSet.getString("HaulerName"));
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ADDRESS);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_ADDRESS,
                    haulerResultSet.getString("HaulerAddress"));
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_CITY);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_CITY,
                    haulerResultSet.getString("HaulerCity"));
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_STATE);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_STATE,
                    haulerResultSet.getString("HaulerState"));
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ZIP);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_HAULER_ZIP,
                    haulerResultSet.getString("HaulerZip"));
        } else {
            if (customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_REQUIRE_HAULER_ID).equals("0")) {
                recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_NAME);
                recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ADDRESS);
                recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_CITY);
                recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_STATE);
                recordObject.remove(DbSingleton.TicketSchema.COLUMN_HAULER_ZIP);
            } else {
                throw new SQLException(
                        "hauler is required, but there is no CASH hauler defined.  you must specify a hauler.");
            }
        }
    }
    ////////////////////

    ////////////////////TRUCK
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_ID) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_ID).equals("")) {
        if (customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_TRUCK_ID) == null
                || customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_TRUCK_ID).equals("")) {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRUCK_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_ID, "CASH:CASH");
        } else {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRUCK_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_ID,
                    customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_TRUCK_ID));
        }
    } else if (!customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_REQUIRE_TRUCK_ID).equals("0")) {
        //throw something, because this is a required field
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_ID) == null
                || recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_ID).equals("")) {
            throw new SQLException("TRUCK IS A REQUIRED FIELD!");
        }
    }
    String truckQuery = "SELECT * FROM Truck WHERE TruckID='"
            + recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_ID) + "'";
    ResultSet truckResultSet = executeQuery(truckQuery);
    truckResultSet.last();
    rowCount = truckResultSet.getRow();
    truckResultSet.first();
    if (rowCount > 0) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRUCK_NUMBER);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_NUMBER,
                truckResultSet.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_NUMBER));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE,
                truckResultSet.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRUCK_TARE_WEIGHT);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_TARE_WEIGHT,
                truckResultSet.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_TARE_WEIGHT));
    }
    ////////////////////

    ////////////////////TRAILER
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_ID) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_ID).equals("")) {
        if (customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_TRAILER_ID) == null
                || customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_TRAILER_ID)
                        .equals("")) {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRAILER_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_ID, "CASH:CASH");
        } else {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRAILER_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_ID,
                    customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_TRAILER_ID));
        }
    } else if (!customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_REQUIRE_TRAILER_ID).equals("0")) {
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_ID) == null
                || recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_ID).equals("")) {
            throw new SQLException("TRAILER IS A REQUIRED FIELD!");
        }
    }
    String trailerQuery = "SELECT * FROM Trailer WHERE TrailerID='"
            + recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_ID) + "'";
    //JSONArray trailerResultSet = parseQueryResult(executeQuery(trailerQuery), "Trailer");
    ResultSet trailerResultSet = executeQuery(trailerQuery);
    trailerResultSet.last();
    rowCount = trailerResultSet.getRow();
    trailerResultSet.first();
    if (rowCount > 0) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRAILER_NUMBER);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_NUMBER,
                trailerResultSet.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_NUMBER));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE,
                trailerResultSet.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE));
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_TRAILER_TARE_WEIGHT);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_TARE_WEIGHT,
                trailerResultSet.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_TARE_WEIGHT));
    }
    ////////////////////

    ////////////////////DRIVER
    if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_DRIVER_ID) == null
            || recordObject.getString(DbSingleton.TicketSchema.COLUMN_DRIVER_ID).equals("")) {
        if (customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_DRIVER_ID) == null
                || customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_DRIVER_ID)
                        .equals("")) {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_DRIVER_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_DRIVER_ID, "CASH");
        } else {
            recordObject.remove(DbSingleton.TicketSchema.COLUMN_DRIVER_ID);
            recordObject.put(DbSingleton.TicketSchema.COLUMN_DRIVER_ID,
                    customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_DEFAULT_DRIVER_ID));
        }
    } else if (!customerResultSet.getString(DbSingleton.CustomerSchema.COLUMN_REQUIRE_DRIVER_ID).equals("0")) {
        if (recordObject.getString(DbSingleton.TicketSchema.COLUMN_DRIVER_ID) == null
                || recordObject.getString(DbSingleton.TicketSchema.COLUMN_DRIVER_ID).equals("")) {
            throw new SQLException("DRIVER IS A REQUIRED FIELD!");
        }
    }
    String driverQuery = "SELECT * FROM Driver WHERE DriverID='"
            + recordObject.getString(DbSingleton.TicketSchema.COLUMN_DRIVER_ID) + "'";
    ResultSet driverResultSet = executeQuery(driverQuery);
    driverResultSet.last();
    rowCount = driverResultSet.getRow();
    driverResultSet.first();
    if (rowCount > 0) {
        recordObject.remove(DbSingleton.TicketSchema.COLUMN_DRIVER_NAME);
        recordObject.put(DbSingleton.TicketSchema.COLUMN_DRIVER_NAME,
                driverResultSet.getString(DbSingleton.TicketSchema.COLUMN_DRIVER_NAME));
    }
    ////////////////////

    return insertNewTicket(recordObject);
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

/**
 * Retrieve the model summary//from   ww  w. j  a v a  2 s.  c  o m
 */
@Override
public ModelSummary getModelSummary(long modelId) throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement getStatement = null;
    ResultSet result = null;
    try {
        connection = dbh.getDataSource().getConnection();
        getStatement = connection.prepareStatement(SQLQueries.GET_MODEL_SUMMARY);
        getStatement.setLong(1, modelId);
        result = getStatement.executeQuery();
        if (result.first() && result.getBinaryStream(1) != null) {
            return MLDBUtil.getModelSummaryFromInputStream(result.getBinaryStream(1));
        } else {
            throw new DatabaseHandlerException("Summary not available for model: " + modelId);
        }
    } catch (Exception e) {
        throw new DatabaseHandlerException("An error occurred while retrieving the summary " + "of model "
                + modelId + ": " + e.getMessage(), e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, getStatement);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public String getAStringModelConfiguration(long analysisId, String configKey) throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement model = null;
    ResultSet result = null;
    try {//from   w w  w  . j a v a  2s .  c  om
        connection = dbh.getDataSource().getConnection();
        model = connection.prepareStatement(SQLQueries.GET_A_MODEL_CONFIGURATION);
        model.setLong(1, analysisId);
        model.setString(2, configKey);
        result = model.executeQuery();
        if (result.first()) {
            return result.getString(1);
        } else {
            return null;
        }
    } catch (SQLException e) {
        String msg = String.format(
                "An error occurred white retrieving [model config] %s  associated with [model id] %s : %s",
                configKey, analysisId, e.getMessage());
        throw new DatabaseHandlerException(msg, e);
    } finally {
        // Close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, model, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public double getADoubleModelConfiguration(long analysisId, String configKey) throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement model = null;
    ResultSet result = null;
    try {//from w  ww  .j  av a  2  s  .co  m
        connection = dbh.getDataSource().getConnection();
        model = connection.prepareStatement(SQLQueries.GET_A_MODEL_CONFIGURATION);
        model.setLong(1, analysisId);
        model.setString(2, configKey);
        result = model.executeQuery();
        if (result.first()) {
            return result.getDouble(1);
        } else {
            return -1;
        }
    } catch (SQLException e) {
        String msg = String.format(
                "An error occurred white retrieving [model config] %s  associated with [model id] %s : %s",
                configKey, analysisId, e.getMessage());
        throw new DatabaseHandlerException(msg, e);
    } finally {
        // Close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, model, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public boolean getABooleanModelConfiguration(long analysisId, String configKey)
        throws DatabaseHandlerException {
    Connection connection = null;
    PreparedStatement model = null;
    ResultSet result = null;
    try {//from   w  w  w  .  jav a 2  s . c o m
        connection = dbh.getDataSource().getConnection();
        model = connection.prepareStatement(SQLQueries.GET_A_MODEL_CONFIGURATION);
        model.setLong(1, analysisId);
        model.setString(2, configKey);
        result = model.executeQuery();
        if (result.first()) {
            return result.getBoolean(1);
        } else {
            return false;
        }
    } catch (SQLException e) {
        String msg = String.format(
                "An error occurred white retrieving [model config] %s  associated with [model id] %s : %s",
                configKey, analysisId, e.getMessage());
        throw new DatabaseHandlerException(msg, e);
    } finally {
        // Close the database resources
        MLDatabaseUtils.closeDatabaseResources(connection, model, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public MLStorage getModelStorage(long modelId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {/*from  w w  w . j av  a 2  s.  c  om*/
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_MODEL_STORAGE);
        statement.setLong(1, modelId);
        result = statement.executeQuery();
        if (result.first()) {
            MLStorage storage = new MLStorage();
            storage.setType(result.getString(1));
            storage.setLocation(result.getString(2));
            return storage;
        } else {
            return null;
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting model storage for model id: " + modelId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}

From source file:org.wso2.carbon.ml.database.internal.MLDatabaseService.java

@Override
public String getDataTypeOfModel(long modelId) throws DatabaseHandlerException {

    Connection connection = null;
    ResultSet result = null;
    PreparedStatement statement = null;
    try {//  w w w. j a  v  a2  s.c  om
        connection = dbh.getDataSource().getConnection();
        statement = connection.prepareStatement(SQLQueries.GET_DATA_TYPE_OF_MODEL);
        statement.setLong(1, modelId);
        result = statement.executeQuery();
        if (result.first()) {
            return result.getString(1);
        } else {
            throw new DatabaseHandlerException("No data type is associated with model id: " + modelId);
        }
    } catch (SQLException e) {
        throw new DatabaseHandlerException(
                " An error has occurred while extracting data type for model id: " + modelId, e);
    } finally {
        // Close the database resources.
        MLDatabaseUtils.closeDatabaseResources(connection, statement, result);
    }
}