Example usage for com.google.gson JsonPrimitive isBoolean

List of usage examples for com.google.gson JsonPrimitive isBoolean

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive isBoolean.

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains a boolean value.

Usage

From source file:ProcessRequest.java

public void updateProduct(JSONObject recordObject) throws JSONException, SQLException {
    String valuesPortion = "";

    recordObject.remove(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED);
    recordObject.put(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date()));
    recordObject.remove("TMSRowGUID");

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    String productID = null;/*from  w  ww.  j a  va2s  . co  m*/

    //construct the SQL query from the JSON elements.
    //the valuesPortion String will contain the values we want to update in the record
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'");
                    valuesPortion += entry.getKey() + "=";
                    valuesPortion += "'" + value.getAsString() + "'";
                    if (entry.getKey().equals(DbSingleton.ProductSchema.COLUMN_ID)) {
                        productID = value.getAsString();
                    }
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString()));
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString()));
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += value.getAsString();
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString()));
                    if (value.getAsBoolean()) {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "1";
                    } else {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "0";
                    }
                }
            }
        }
    }

    //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")";
    if (productID == null) {
        throw new SQLException("no customer id found!");
    }
    String updateQuery = "UPDATE " + DbSingleton.PRODUCT_TABLE_NAME + " SET " + valuesPortion + " WHERE "
            + DbSingleton.ProductSchema.COLUMN_ID + "='" + productID + "'";
    //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID";

    executeQuery(updateQuery);
}

From source file:ProcessRequest.java

public void newProduct(JSONObject recordObject) throws JSONException, SQLException {
    String valuesPortion = "";
    String columnsPortion = "";

    recordObject.remove(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED);
    recordObject.put(DbSingleton.ProductSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date()));
    recordObject.remove("TMSRowGUID");

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    {/*from ww w  .j  ava  2  s.  c om*/
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                if (!columnsPortion.equals("")) {
                    columnsPortion += ",";
                }
                columnsPortion += entry.getKey();
            }
        }
    }

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    valuesPortion += "'" + value.getAsString() + "'";
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        valuesPortion += new Long(value.getAsLong()).toString();
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    if (value.getAsBoolean())
                        valuesPortion += "1";
                    else
                        valuesPortion += "0";
                }
            }
        }
    }

    String insertQuery = "INSERT INTO " + DbSingleton.PRODUCT_TABLE_NAME + "(" + columnsPortion + ") VALUES ("
            + valuesPortion + ")";
    String identQuery = "SELECT IDENT_CURRENT ('" + DbSingleton.PRODUCT_TABLE_NAME + "') AS ProductID";

    executeQuery(insertQuery);
}

From source file:ProcessRequest.java

public void updateCustomer(JSONObject recordObject) throws JSONException, SQLException {
    String valuesPortion = "";

    String dob = null;//from www .ja  v  a2s .  c  om
    try {
        dob = mDateFormat
                .format(new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_DOB)));
    } catch (JSONException jsone) {
        dob = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_DOB);
    } finally {
        recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_DOB, dob);
    }

    String issueDate = null;
    try {
        issueDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE)));
    } catch (JSONException jsone) {
        issueDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE);
    } finally {
        recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE, issueDate);
    }

    String expirationDate = null;
    try {
        expirationDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE)));
    } catch (JSONException jsone) {
        expirationDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE);
    } finally {
        recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE, expirationDate);
    }

    recordObject.remove(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED);
    recordObject.put(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date()));
    recordObject.remove("TMSRowGUID");

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    String customerID = null;

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'");
                    valuesPortion += entry.getKey() + "=";
                    valuesPortion += "'" + value.getAsString() + "'";
                    if (entry.getKey().equals(DbSingleton.CustomerSchema.COLUMN_ID)) {
                        customerID = value.getAsString();
                    }
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString()));
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString()));
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += value.getAsString();
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString()));
                    if (value.getAsBoolean()) {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "1";
                    } else {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "0";
                    }
                }
            }
        }
    }

    //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")";
    if (customerID == null) {
        throw new SQLException("no customer id found!");
    }
    String updateQuery = "UPDATE " + DbSingleton.CUSTOMER_TABLE_NAME + " SET " + valuesPortion + " WHERE "
            + DbSingleton.CustomerSchema.COLUMN_ID + "='" + customerID + "'";
    //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID";

    executeQuery(updateQuery);
}

From source file:ProcessRequest.java

public void newCustomer(JSONObject recordObject) throws JSONException, SQLException {
    String valuesPortion = "";
    String columnsPortion = "";

    String dob = null;//from ww w.ja v a2 s  .c om
    try {
        dob = mDateFormat
                .format(new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_DOB)));
    } catch (JSONException jsone) {
        dob = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_DOB);
    } finally {
        recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_DOB, dob);
    }

    String issueDate = null;
    try {
        issueDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE)));
    } catch (JSONException jsone) {
        issueDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE);
    } finally {
        recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_ISSUE_DATE, issueDate);
    }

    String expirationDate = null;
    try {
        expirationDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE)));
    } catch (JSONException jsone) {
        expirationDate = recordObject.getString(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE);
    } finally {
        recordObject.put(DbSingleton.CustomerSchema.COLUMN_DL_EXPIRATION_DATE, expirationDate);
    }

    recordObject.remove(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED);
    recordObject.put(DbSingleton.CustomerSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date()));
    recordObject.remove("TMSRowGUID");

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                if (!columnsPortion.equals("")) {
                    columnsPortion += ",";
                }
                columnsPortion += entry.getKey();
            }
        }
    }

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    valuesPortion += "'" + value.getAsString() + "'";
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        valuesPortion += new Long(value.getAsLong()).toString();
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    if (value.getAsBoolean())
                        valuesPortion += "1";
                    else
                        valuesPortion += "0";
                }
            }
        }
    }

    String insertQuery = "INSERT INTO " + DbSingleton.CUSTOMER_TABLE_NAME + "(" + columnsPortion + ") VALUES ("
            + valuesPortion + ")";
    //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.CUSTOMER_TABLE_NAME+"') AS CustomerID";
    //String customerIsHaulerQuery = "SELECT * FROM "+DbSingleton.CUSTOMER_TABLE_NAME+" "
    executeQuery(insertQuery);
    /*
    ResultSet identResultSet = executeQuery(identQuery);
    identResultSet.first();
    //System.out.println(identResultSet.getRow());
    int customerID = identResultSet.getInt(1);
    //System.out.println(ticketID);
    return customerID;
    */
}

From source file:ProcessRequest.java

public void updateTicket(JSONObject recordObject) throws SQLException, JSONException {
    //check for ticket existence
    //check for ticket detail existence
    String valuesPortion = "";

    String ticketDate = null;/*from w  ww.ja v  a  2  s .  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);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_DATE, ticketDate);
    }

    String invoiceDate = null;
    try {
        invoiceDate = mDateFormat
                .format(new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE)));
    } catch (JSONException jsone) {
        invoiceDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_INVOICE_DATE, invoiceDate);
    }

    String voucherRedeemableDate = null;
    try {
        voucherRedeemableDate = mDateFormat.format(new java.util.Date(
                recordObject.getLong(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE)));
    } catch (JSONException jsone) {
        voucherRedeemableDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_VOUCHER_REDEEMABLE_DATE, voucherRedeemableDate);
    }

    String voucherExpirationDate = null;
    try {
        voucherExpirationDate = mDateFormat.format(new java.util.Date(
                recordObject.getLong(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE)));
    } catch (JSONException jsone) {
        voucherExpirationDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_VOUCHER_EXPIRATION_DATE, voucherExpirationDate);
    }

    String paymentDate = null;
    try {
        paymentDate = mDateFormat
                .format(new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE)));
    } catch (JSONException jsone) {
        paymentDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_PAYMENT_DATE, paymentDate);
    }

    String trailerLastTareDate = null;
    try {
        trailerLastTareDate = mDateFormat.format(new java.util.Date(
                recordObject.getLong(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE)));
    } catch (JSONException jsone) {
        trailerLastTareDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRAILER_LAST_TARE_DATE, trailerLastTareDate);
    }

    String truckLastTareDate = null;
    try {
        truckLastTareDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE)));
    } catch (JSONException jsone) {
        truckLastTareDate = recordObject.getString(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE);
    } finally {
        recordObject.put(DbSingleton.TicketSchema.COLUMN_TRUCK_LAST_TARE_DATE, truckLastTareDate);
    }

    recordObject.remove(DbSingleton.TicketSchema.COLUMN_LAST_MODIFIED);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_LAST_MODIFIED, mDateFormat.format(new java.util.Date()));
    recordObject.remove("TMSRowGUID");

    //recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER);
    //recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber());

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    Integer ticketID = null;

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'");
                    valuesPortion += entry.getKey() + "=";
                    valuesPortion += "'" + value.getAsString() + "'";
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString()));
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        //String ticketNumber = new Long(value.getAsLong()).toString();
                        Long val = new Long(value.getAsLong());
                        if (entry.getKey().equals(DbSingleton.TicketSchema.COLUMN_TICKET_ID)) {
                            ticketID = val.intValue();
                        } else {
                            if (!valuesPortion.equals("")) {
                                valuesPortion += ",";
                            }
                            //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString()));
                            valuesPortion += entry.getKey() + "=";
                            valuesPortion += val.toString();
                        }
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString()));
                    if (value.getAsBoolean()) {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "1";
                    } else {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "0";
                    }
                }
            }
        }
    }

    //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")";
    if (ticketID == null) {
        throw new SQLException("no ticket id found!");
    }
    String updateQuery = "UPDATE " + DbSingleton.TICKET_TABLE_NAME + " SET " + valuesPortion + " WHERE "
            + DbSingleton.TicketSchema.COLUMN_TICKET_ID + "=" + ticketID;
    //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID";

    executeQuery(updateQuery);
}

From source file:ProcessRequest.java

public void updateTicketDetail(Integer ticketID, JSONObject recordObject) throws SQLException, JSONException {

    //check for ticket detail existence
    String valuesPortion = "";

    recordObject.remove(DbSingleton.TicketDetailSchema.COLUMN_LAST_MODIFIED);
    recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_LAST_MODIFIED,
            mDateFormat.format(new java.util.Date()));
    recordObject.remove("TMSRowGUID");

    String grossDate = null;// w  w  w  .  jav a 2s .c o m
    try {
        grossDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS_DT)));
    } catch (JSONException jsone) {
        grossDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS_DT);
    } finally {
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_GROSS_LBS_DT, grossDate);
    }

    String grossALTDate = null;
    try {
        grossALTDate = mDateFormat.format(new java.util.Date(
                recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_ALT_GROSS_LBS_DT)));
    } catch (JSONException jsone) {
        grossALTDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_ALT_GROSS_LBS_DT);
    } finally {
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_ALT_GROSS_LBS_DT, grossALTDate);
    }

    String tareDate = null;
    try {
        tareDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS_DT)));
    } catch (JSONException jsone) {
        tareDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS_DT);
    } finally {
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_TARE_LBS_DT, tareDate);
    }

    String tareALTDate = null;
    try {
        tareALTDate = mDateFormat.format(new java.util.Date(
                recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_ALT_TARE_LBS_DT)));
    } catch (JSONException jsone) {
        tareALTDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_ALT_TARE_LBS_DT);
    } finally {
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_ALT_TARE_LBS_DT, tareALTDate);
    }

    String deductDate = null;
    try {
        deductDate = mDateFormat.format(
                new java.util.Date(recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS_DT)));
    } catch (JSONException jsone) {
        deductDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS_DT);
    } finally {
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_DEDUCT_LBS_DT, deductDate);
    }

    String deductALTDate = null;
    try {
        deductALTDate = mDateFormat.format(new java.util.Date(
                recordObject.getLong(DbSingleton.TicketDetailSchema.COLUMN_ALT_DEDUCT_LBS_DT)));
    } catch (JSONException jsone) {
        deductALTDate = recordObject.getString(DbSingleton.TicketDetailSchema.COLUMN_ALT_DEDUCT_LBS_DT);
    } finally {
        recordObject.put(DbSingleton.TicketDetailSchema.COLUMN_ALT_DEDUCT_LBS_DT, deductALTDate);
    }

    //recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER);
    //recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber());

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    Integer ticketDetailID = null;

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'");
                    valuesPortion += entry.getKey() + "=";
                    valuesPortion += "'" + value.getAsString() + "'";
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString()));
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        Long val = new Long(value.getAsLong());
                        if (entry.getKey().equals(DbSingleton.TicketDetailSchema.COLUMN_TICKET_DETAIL_ID)) {
                            ticketDetailID = val.intValue();
                        } else {
                            if (!valuesPortion.equals("")) {
                                valuesPortion += ",";
                            }
                            //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString()));
                            valuesPortion += entry.getKey() + "=";
                            //String ticketNumber = new Long(value.getAsLong()).toString();
                            valuesPortion += val.toString();
                        }
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString()));
                    if (value.getAsBoolean()) {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "1";
                    } else {
                        valuesPortion += entry.getKey() + "=";
                        valuesPortion += "0";
                    }
                }
            }
        }
    }

    //String insertQuery = "INSERT INTO "+DbSingleton.TICKET_DETAIL_TABLE_NAME+"("+columnsPortion+") VALUES ("+valuesPortion+")";
    if (ticketDetailID == null) {
        throw new SQLException("no ticket detail id found!");
    }
    String updateQuery = "UPDATE " + DbSingleton.TICKET_DETAIL_TABLE_NAME + " SET " + valuesPortion + " WHERE "
            + DbSingleton.TicketDetailSchema.COLUMN_TICKET_DETAIL_ID + "=" + ticketDetailID;
    //String identQuery = "SELECT IDENT_CURRENT ('"+DbSingleton.TICKET_DETAIL_TABLE_NAME+"') AS TicketDetailID";

    executeQuery(updateQuery);
}

From source file:ProcessRequest.java

public int insertNewTicketDetail(JSONObject recordObject) throws SQLException, JSONException {
    String valuesPortion = "";
    String columnsPortion = "";

    //recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER);
    //recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber());

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    {//from  w  w  w .j  a  v a2 s.c  o m
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //if(value.isString() && value.getAsString().equals("")) {
                //don't want this null value/key pair
                //} else {
                if (!columnsPortion.equals("")) {
                    columnsPortion += ",";
                }
                columnsPortion += entry.getKey();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                //}
            }
        }
    }

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'");
                    valuesPortion += "'" + value.getAsString() + "'";
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString()));
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString()));
                        String ticketNumber = new Long(value.getAsLong()).toString();
                        valuesPortion += ticketNumber;
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : "+(new Boolean(value.getAsBoolean()).toString()));
                    if (value.getAsBoolean())
                        valuesPortion += "1";
                    else
                        valuesPortion += "0";
                }
            }
        }
    }

    String insertQuery = "INSERT INTO " + DbSingleton.TICKET_DETAIL_TABLE_NAME + "(" + columnsPortion
            + ") VALUES (" + valuesPortion + ")";
    String identQuery = "SELECT IDENT_CURRENT ('" + DbSingleton.TICKET_DETAIL_TABLE_NAME
            + "') AS TicketDetailID";

    executeQuery(insertQuery);
    ResultSet identResultSet = executeQuery(identQuery);
    identResultSet.first();
    //System.out.println(identResultSet.getRow());
    int ticketDetailID = identResultSet.getInt(1);
    //System.out.println(ticketID);

    return ticketDetailID;
}

From source file:ProcessRequest.java

public int insertNewTicket(JSONObject recordObject) throws SQLException, JSONException {
    String valuesPortion = "";
    String columnsPortion = "";

    recordObject.remove(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER);
    recordObject.put(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER, getNextTicketNumber());

    //get column names
    JsonParser parser = new JsonParser();
    JsonObject recordGson = parser.parse(recordObject.toString()).getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entrySet = recordGson.entrySet();
    {//from w w  w . j ava2s  . c  o m
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata") || entry.getKey().equals("TicketID")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //if(value.isString() && value.getAsString().equals("")) {
                //don't want this null value/key pair
                //} else {
                if (!columnsPortion.equals("")) {
                    columnsPortion += ",";
                }
                columnsPortion += entry.getKey();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                //}
            }
        }
    }

    //get values
    {
        Iterator<Map.Entry<String, JsonElement>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonElement> entry = iterator.next();
            if (entry.getKey().equals("metadata") || entry.getKey().equals("TicketID")) {
                //do nothing
            } else {
                JsonPrimitive value = entry.getValue().getAsJsonPrimitive();
                //System.out.println(entry.getKey()+" : "+value.getAsString());
                if (value.isString()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    //System.out.println(entry.getKey()+" : '"+value.getAsString()+"'");
                    valuesPortion += "'" + value.getAsString() + "'";
                } else if (value.isNumber()) {
                    if (value.getAsString().contains(".")) {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Double(value.getAsDouble()).toString()));
                        valuesPortion += new Double(value.getAsDouble()).toString();
                    } else {
                        if (!valuesPortion.equals("")) {
                            valuesPortion += ",";
                        }
                        //System.out.println(entry.getKey()+" : "+(new Long(value.getAsLong()).toString()));
                        String ticketNumber = new Long(value.getAsLong()).toString();
                        valuesPortion += ticketNumber;
                    }
                } else if (value.isBoolean()) {
                    if (!valuesPortion.equals("")) {
                        valuesPortion += ",";
                    }
                    if (value.getAsBoolean())
                        valuesPortion += "1";
                    else
                        valuesPortion += "0";
                }
            }
        }
    }

    String insertQuery = "INSERT INTO " + DbSingleton.TICKET_TABLE_NAME + "(" + columnsPortion + ") VALUES ("
            + valuesPortion + ")";
    String identQuery = "SELECT IDENT_CURRENT ('" + DbSingleton.TICKET_TABLE_NAME + "') AS TicketID";

    executeQuery(insertQuery);
    ResultSet identResultSet = executeQuery(identQuery);
    identResultSet.first();
    //System.out.println(identResultSet.getRow());
    int ticketID = identResultSet.getInt(1);
    //System.out.println(ticketID);

    ////////////////////TICKET NUMBER CHECK
    boolean uniqueTicketNumber = false;
    while (!uniqueTicketNumber) {
        String ticketNumberCheckQuery = "SELECT * FROM " + DbSingleton.TICKET_TABLE_NAME + " WHERE "
                + DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER + "="
                + recordObject.getInt(DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER);
        ResultSet ticketNumberCheckResultSet = executeQuery(ticketNumberCheckQuery);
        int rowCount = 0;
        ticketNumberCheckResultSet.last();
        rowCount = ticketNumberCheckResultSet.getRow();
        ticketNumberCheckResultSet.first();
        if (rowCount < 1)
            throw new SQLException(LOG_TAG + ": wat...");
        else if (rowCount > 1) {
            uniqueTicketNumber = false;
        } else {
            uniqueTicketNumber = true;
        }
        if (!uniqueTicketNumber) {
            //update the ticket for the next iteration of this loop, attempting, again, to get a unique ticket number
            String uniqueTicketNumberQuery = "UPDATE " + DbSingleton.TICKET_TABLE_NAME + " SET "
                    + DbSingleton.TicketSchema.COLUMN_TICKET_NUMBER + "=" + getNextTicketNumber() + " WHERE "
                    + DbSingleton.TicketSchema.COLUMN_TICKET_ID + "=" + ticketID;
            executeQuery(uniqueTicketNumberQuery);
        }
    }
    ////////////////////

    return ticketID;
}

From source file:ProcessRequest.java

public JSONObject parseQuery(JSONObject requestObject, OutputStream os)
        throws JSONException, SQLException, IOException {
    String query = "";
    String tableName = "";

    JSONArray selectionColumns = requestObject.getJSONArray("selection");
    query += "SELECT " + selectionColumns.getString(0);
    for (int i = 1; i < selectionColumns.length(); i++) {
        query += "," + selectionColumns.getString(i);
    }//from  w  ww.jav  a  2  s . c om
    query += " FROM ";
    tableName = requestObject.getString("table");
    query += tableName;
    if (requestObject.has("where")) {
        query += " WHERE ";
        query += requestObject.getString("where");
    }
    query += ";";

    JSONObject responseObject = new JSONObject();
    JSONArray resultJSONArray;

    //System.out.println(query);
    if (os != null) {
        os.write(new String("{\"result_set\":[").getBytes());
        parseQueryResults(executeQuery(query), tableName, os, false);
    }

    resultJSONArray = parseQueryResults(executeQuery(query), tableName);
    //System.out.println(resultSet.toString(5));
    if (requestObject.has("join")) {
        JSONArray joinArray = requestObject.getJSONArray("join");
        List<String> tableList = new LinkedList<String>();
        tableList.add(tableName);
        for (int h = 0; h < joinArray.length(); h++) {
            //find the next link table
            JSONObject joinObject = null;
            for (int j = 0; j < joinArray.length(); j++) {
                for (int k = 0; k < tableList.size(); k++) {
                    if (joinArray.getJSONObject(j).getString("table").equals(tableList.get(k))) {
                        //break;   //this table has already been joined
                    } else if (joinArray.getJSONObject(j).getString("link_table").equals(tableList.get(k))) {
                        joinObject = joinArray.getJSONObject(j);
                    }
                }
            }
            if (joinObject == null) {
                throw new JSONException("join syntax was incorrect, no valid joins were found");
            }
            for (int i = 0; i < resultJSONArray.length(); i++) {
                //we now know the table to join, now search through the results looking for the link_table and query on each
                if (joinObject.getString("link_table").equals(
                        resultJSONArray.getJSONObject(i).getJSONObject("metadata").getString("table"))) {
                    //this result contains the correct link_table.  query against it's link_column value
                    JsonObject linkObject = new JsonParser().parse(resultJSONArray.getJSONObject(i).toString())
                            .getAsJsonObject();
                    JsonPrimitive linkValue = null;
                    if (linkObject.has(joinObject.getString("link_column"))) {
                        linkValue = linkObject.getAsJsonPrimitive(joinObject.getString("link_column"));
                    } else {
                        //just skip this result and notify the log console.
                        //however, this is most likely an error and should be fixed.
                        System.out.println("link column did not contain the following column (please fix): "
                                + joinObject.get("link_column"));
                    }

                    String predicate = "1=1";
                    if (linkValue.isBoolean()) {
                        if (linkValue.getAsBoolean() == true) {
                            predicate = joinObject.getString("column") + "=1";
                        } else {
                            predicate = joinObject.getString("column") + "=0";
                        }
                    }
                    if (linkValue.isNumber()) {
                        predicate = joinObject.getString("column") + "=" + linkValue.getAsString();
                    }
                    if (linkValue.isString()) {
                        predicate = joinObject.getString("column") + "='" + linkValue.getAsString() + "'";
                    }
                    String joinQuery = "";
                    if (joinObject.has("selection")) {
                        JSONArray joinSelectionColumns = joinObject.getJSONArray("selection");
                        joinQuery += "SELECT " + joinSelectionColumns.getString(0);
                        for (int k = 1; k < joinSelectionColumns.length(); k++) {
                            joinQuery += "," + joinSelectionColumns.getString(k);
                        }
                    } else {
                        joinQuery += "SELECT *";
                    }
                    //build and execute query, adding it to the result set
                    joinQuery += " FROM " + joinObject.getString("table") + " WHERE " + predicate;
                    if (joinObject.has("where")) {
                        String whereClause = joinObject.getString("where");
                        joinQuery += " AND " + whereClause;
                    }
                    joinQuery += ";";
                    //System.out.println("join query: "+joinQuery);
                    //JSONArray parsedResult = parseQueryResults(executeQuery(joinQuery), joinObject.getString("table"));
                    //System.out.println("join parsed result: "+parsedResult.toString(5));
                    if (os != null)
                        parseQueryResults(executeQuery(query), joinObject.getString("table"), os, true);
                    else
                        concatArray(resultJSONArray,
                                parseQueryResults(executeQuery(query), joinObject.getString("table")));
                }
            }
            tableList.add(joinObject.getString("table"));
        }
    }
    if (os != null)
        os.write(new String("]}\n").getBytes());
    responseObject.put("result_set", resultJSONArray);
    //System.out.println(responseObject.toString(5));
    return responseObject;
}

From source file:adams.data.report.ReportJsonUtils.java

License:Open Source License

/**
 * Creates a report from the JSON object.
 *
 * @param jobj   the object to get the data from
 * @return      the report, null if failed to create or find data
 *//*ww w .j a  v  a  2  s.  co m*/
public static Report fromJson(JsonObject jobj) {
    Report result;
    Field field;
    JsonPrimitive prim;

    result = new Report();
    for (Entry<String, JsonElement> entry : jobj.entrySet()) {
        prim = entry.getValue().getAsJsonPrimitive();
        if (prim.isBoolean()) {
            field = new Field(entry.getKey(), DataType.BOOLEAN);
            result.addField(field);
            result.setBooleanValue(field.getName(), prim.getAsBoolean());
        } else if (prim.isNumber()) {
            field = new Field(entry.getKey(), DataType.NUMERIC);
            result.addField(field);
            result.setNumericValue(field.getName(), prim.getAsNumber().doubleValue());
        } else {
            field = new Field(entry.getKey(), DataType.STRING);
            result.addField(field);
            result.setStringValue(field.getName(), prim.getAsString());
        }
    }

    return result;
}