Example usage for com.google.gson JsonParser JsonParser

List of usage examples for com.google.gson JsonParser JsonParser

Introduction

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

Prototype

@Deprecated
public JsonParser() 

Source Link

Usage

From source file:JsonConversion.java

public static void main(String[] args) {
    JSONObject obj = new JSONObject();
    obj.put("name", "mkyong.com");
    obj.put("age", new Integer(100));

    JSONArray list = new JSONArray();
    JSONObject obj1 = new JSONObject();
    obj1.put("1", "msg 1");
    list.add(obj1);/*from  ww w.ja va 2  s.c  o  m*/

    JSONObject obj2 = new JSONObject();
    obj2.put("2", "msg 2");
    list.add(obj2);

    JSONObject obj3 = new JSONObject();
    obj3.put("3", "msg 3");
    list.add(obj3);

    obj.put("messages", list);
    String jsonStr1 = obj.toJSONString();
    String jsonStr = "{\"endLong\"=\"78.3769397\", \"endTime\"=\"16:48:28\", \"endLat\"=\"17.4466785\", \"tripData\"=\"[{Longitude:78.3769388,Latitude:17.4466736},{Longitude:78.3769388,Latitude:17.4466736}]\", \"endLoc\"=\"Deloitte Dr,Hyderabad,India500081\",\"emailId\"=\"user15@gmail.com\", \"tripId\"=\"30\"}";
    /*try {
            
    FileWriter file = new FileWriter("c:\\test.json");
    file.write(obj.toJSONString());
    file.flush();
    file.close();
            
    } catch (IOException e) {
    e.printStackTrace();
    }*/
    System.out.print(obj);
    JsonParser parser = new JsonParser();
    int firstIndex = jsonStr.indexOf("[");
    int lastIndex = jsonStr.indexOf("]");
    StringBuilder sb = new StringBuilder(jsonStr);
    sb.deleteCharAt(firstIndex - 1);
    sb.deleteCharAt(lastIndex);
    String str1 = sb.toString();
    Object objFinal = parser.parse(str1);
    JsonObject jsonObject = (JsonObject) objFinal;
    String name = String.valueOf(jsonObject.get("endLong"));
    System.out.println(name);

    Map msg = (Map) jsonObject.get("tripData");

}

From source file:JavaTestRunner.java

License:Apache License

public static Object runQuery(JavaQuery query, String combinedInput) {
    JsonElement jelement = new JsonParser().parse(combinedInput);
    JsonObject jobject = jelement.getAsJsonObject();
    return runQuery(query, jobject);
}

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.ja  v a2  s . c o  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();
    {/*  w w w  .jav  a  2  s  .  com*/
        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 w ww  .  jav  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();
    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;/* w w  w . ja va  2  s . co m*/
    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 www.  j a v  a 2  s .  co  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;/*from   ww w .j a va 2s  .  co  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 ww  .ja  v  a 2s .co 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 av  a 2 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") || 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;
}