Example usage for com.google.gson JsonObject addProperty

List of usage examples for com.google.gson JsonObject addProperty

Introduction

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

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

From source file:carsharing.starter.automotive.iot.ibm.com.mobilestarterapp.AnalyzeMyDriving.java

License:Open Source License

public void sendLocation(final Location location) {
    if (connectDeviceClient()) {
        final GregorianCalendar cal = new GregorianCalendar();
        final TimeZone gmt = TimeZone.getTimeZone("GMT");
        cal.setTimeZone(gmt);/*  w w w.j  a v  a 2  s  .  c om*/
        final SimpleDateFormat formattedCal = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        formattedCal.setCalendar(cal);
        final String timestamp = formattedCal.format(cal.getTime());

        final double speed = Math.max(0.0, location.getSpeed() * 60 * 60 / 1000);
        final double longitude = location.getLongitude();
        final double latitude = location.getLatitude();
        final String mobileAppDeviceId = FirstPage.mobileAppDeviceId;
        final String status = tripID != null ? "Unlocked" : "Locked";

        if (tripID == null) {
            // this trip should be completed, so lock device now
            userUnlocked = false;
        }

        final JsonObject event = new JsonObject();
        final JsonObject data = new JsonObject();
        event.add("d", data);
        data.addProperty("trip_id", tripID);
        data.addProperty("speed", speed);
        data.addProperty("lng", longitude);
        data.addProperty("lat", latitude);
        data.addProperty("ts", timestamp);
        data.addProperty("id", mobileAppDeviceId);
        data.addProperty("status", status);

        final ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (deviceClient.publishEvent("sensorData", event, 0)) {
            Log.d("MQTT", "publish event " + event.toString());
            supportActionBar.setTitle(speedMessage + " - Data sent (" + (++transmissionCount) + ")");
        } else {
            Log.d("MQTT", "ERROR in publishing event " + event.toString());
            supportActionBar.setTitle("Data Transmission Error.");
        }
    }
}

From source file:cashPR.AddUpdateCashVoucher.java

private JsonObject saveVoucher(ArrayList<CashPaymentReceiptModel> detail) {
    final JsonObject jResultObj = new JsonObject();
    Connection dataConnection = null;
    if (dataConnection == null) {
        dataConnection = helper.getConnMpAdmin();
    }/*from   w w  w. j  av a2 s .com*/
    if (dataConnection != null) {
        try {
            dataConnection.setAutoCommit(false);
            String sql = null;
            PreparedStatement psLocal = null;
            if (detail.get(0).getRef_no().equalsIgnoreCase("")) {
                sql = "INSERT INTO CPRHD (VDATE, TOT_BAL, AC_CD, USER_ID, CTYPE, branch_cd,REF_NO) "
                        + "VALUES (?, ?, ?, ?, ?, ?, ?)";
                //            ref_no = lb.generateCPNo(type);
                if (detail.get(0).getType() == 0) {
                    detail.get(0).setRef_no(lb.generateKey(dataConnection, "CPRHD", "REF_NO", "CP", 7));
                } else {
                    detail.get(0).setRef_no(lb.generateKey(dataConnection, "CPRHD", "REF_NO", "CR", 7));
                }
            } else if (!detail.get(0).getRef_no().equalsIgnoreCase("")) {
                if (detail.get(0).getType() == 0) {
                    CashPaymentUpdate cp = new CashPaymentUpdate();
                    cp.deleteEntry(dataConnection, detail.get(0).getRef_no());
                } else if (detail.get(0).getType() == 1) {
                    CashReciept cr = new CashReciept();
                    cr.deleteEntry(dataConnection, detail.get(0).getRef_no());
                }
                sql = "DELETE FROM CPRDT WHERE REF_NO='" + detail.get(0).getRef_no() + "'";
                psLocal = dataConnection.prepareStatement(sql);
                psLocal.executeUpdate();

                sql = "DELETE FROM payment WHERE REF_NO='" + detail.get(0).getRef_no() + "'";
                psLocal = dataConnection.prepareStatement(sql);
                psLocal.executeUpdate();

                sql = "UPDATE CPRHD SET VDATE=?, TOT_BAL=?, AC_CD=?, USER_ID=?, CTYPE=?, "
                        + "EDIT_NO=EDIT_NO+1, TIME_STAMP=CURRENT_TIMESTAMP,branch_cd=? WHERE REF_NO=?";
            }
            psLocal = dataConnection.prepareStatement(sql);
            psLocal.setString(1, detail.get(0).getVdate());
            psLocal.setDouble(2, detail.get(0).getTot_amt());
            psLocal.setString(3, detail.get(0).getAc_cd());
            psLocal.setString(4, detail.get(0).getUser_id());
            psLocal.setInt(5, detail.get(0).getType());
            psLocal.setString(6, detail.get(0).getBranch_cd());
            psLocal.setString(7, detail.get(0).getRef_no());
            psLocal.executeUpdate();

            sql = "Update CPRHD set INIT_TIMESTAMP = TIME_STAMP where ref_no='" + detail.get(0).getRef_no()
                    + "'";
            psLocal = dataConnection.prepareStatement(sql);
            psLocal.executeUpdate();

            sql = "INSERT INTO CPRDT (SR_NO, DOC_REF_NO, BAL, REMARK, REF_NO) " + "VALUES (?, ?, ?, ?, ?)";
            psLocal = dataConnection.prepareStatement(sql);
            for (int i = 0; i < detail.size(); i++) {

                {
                    psLocal.setInt(1, i + 1);
                    psLocal.setString(2, detail.get(i).getDoc_ref_no());
                    psLocal.setDouble(3, detail.get(i).getAmt());
                    psLocal.setString(4, detail.get(i).getRemark());
                    psLocal.setString(5, detail.get(0).getRef_no());
                    psLocal.executeUpdate();

                    sql = "INSERT INTO PAYMENT (CASH_AMT, BANK_CD, BANK_NAME, BANK_BRANCH, CHEQUE_NO, CHEQUE_DATE, BANK_AMT, CARD_NAME, CARD_AMT, REF_NO,USER_ID,vou_date)"
                            + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
                    PreparedStatement pstLocal = dataConnection.prepareStatement(sql);
                    if (detail.get(i).getType() == 0) {
                        pstLocal.setDouble(1, detail.get(i).getAmt() * -1);
                    } else {
                        pstLocal.setDouble(1, detail.get(i).getAmt() * 1);
                    }
                    pstLocal.setString(2, "");
                    pstLocal.setString(3, "");
                    pstLocal.setString(4, "");
                    pstLocal.setString(5, "");
                    pstLocal.setString(6, null);
                    pstLocal.setDouble(7, 0.00);
                    pstLocal.setString(8, "");
                    pstLocal.setDouble(9, 0.00);
                    pstLocal.setString(10, detail.get(0).getRef_no());
                    pstLocal.setString(11, detail.get(i).getUser_id());
                    pstLocal.setString(12, detail.get(i).getVdate());
                    pstLocal.executeUpdate();

                }
            }

            if (detail.get(0).getType() == 0) {
                CashPaymentUpdate cp = new CashPaymentUpdate();
                cp.addEntry(dataConnection, detail.get(0).getRef_no());
            } else if (detail.get(0).getType() == 1) {
                CashReciept cr = new CashReciept();
                cr.addEntry(dataConnection, detail.get(0).getRef_no());
            }

            dataConnection.commit();
            dataConnection.setAutoCommit(true);
            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
        } catch (SQLNonTransientConnectionException ex1) {
            ex1.printStackTrace();
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            ex.printStackTrace();
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
            try {
                dataConnection.rollback();
                dataConnection.setAutoCommit(true);
            } catch (Exception e) {
            }
        }
    }
    return jResultObj;
}

From source file:cashPR.DeleteCashBill.java

private JsonObject saveVoucher(String ref_no, int type) {
    final JsonObject jResultObj = new JsonObject();
    Connection dataConnection = null;
    if (dataConnection == null) {
        dataConnection = helper.getConnMpAdmin();
    }/*from   w  ww .jav  a2  s  . com*/
    if (dataConnection != null) {
        try {
            String sql = "SELECT doc_ref_no FROM billadjst WHERE dr_doc_ref_no='" + ref_no
                    + "' OR cr_doc_ref_no='" + ref_no + "'";
            PreparedStatement pstLocal = dataConnection.prepareStatement(sql);
            ResultSet rsLocal = pstLocal.executeQuery();
            if (rsLocal.next()) {
                jResultObj.addProperty("result", -1);
                jResultObj.addProperty("Cause", "Voucher is in adjustment process");
            } else {
                dataConnection.setAutoCommit(false);
                PreparedStatement psLocal = null;
                if (type == 0) {
                    CashPaymentUpdate cp = new CashPaymentUpdate();
                    cp.deleteEntry(dataConnection, ref_no);
                } else if (type == 1) {
                    CashReciept cr = new CashReciept();
                    cr.deleteEntry(dataConnection, ref_no);
                }
                sql = "DELETE FROM CPRDT WHERE REF_NO='" + ref_no + "'";
                psLocal = dataConnection.prepareStatement(sql);
                psLocal.executeUpdate();

                sql = "DELETE FROM payment WHERE REF_NO='" + ref_no + "'";
                psLocal = dataConnection.prepareStatement(sql);
                psLocal.executeUpdate();

                sql = "delete from  CPRHD WHERE REF_NO=?";
                psLocal = dataConnection.prepareStatement(sql);
                psLocal.setString(1, ref_no);
                psLocal.executeUpdate();

                dataConnection.commit();
                dataConnection.setAutoCommit(true);
                jResultObj.addProperty("result", 1);
                jResultObj.addProperty("Cause", "success");
            }
        } catch (SQLNonTransientConnectionException ex1) {
            ex1.printStackTrace();
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            ex.printStackTrace();
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
            try {
                dataConnection.rollback();
                dataConnection.setAutoCommit(true);
            } catch (Exception e) {
            }
        }
    }
    return jResultObj;
}

From source file:cashPR.GetCashPaymentHeader.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w ww.  j a va2s  . co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Connection dataConnection = null;

    final JsonObject jResultObj = new JsonObject();
    final String from_date = request.getParameter("from_date");
    final String to_date = request.getParameter("to_date");
    final String v_type = request.getParameter("v_type");
    final String branch_cd = request.getParameter("branch_cd");
    if (dataConnection == null) {
        dataConnection = helper.getConnMpAdmin();
    }

    if (dataConnection != null) {
        try {
            String sql = "select c.AC_CD,c.REF_NO,VDATE,a.FNAME,c1.BAL,c1.REMARK,c.branch_cd from CPRHD c left join CPRDT c1 on c.REF_NO=c1.REF_NO"
                    + " left join ACNTMST a on c.AC_CD=a.AC_CD where VDATE>=? and VDATE<=? and CTYPE=? ";
            if (!branch_cd.equalsIgnoreCase("0")) {
                sql += " and branch_cd=" + branch_cd;
            }
            sql += " order by VDATE,ref_no";
            PreparedStatement pstLocal = dataConnection.prepareStatement(sql);
            pstLocal.setString(1, from_date);
            pstLocal.setString(2, to_date);
            pstLocal.setString(3, v_type);
            ResultSet rsLocal = pstLocal.executeQuery();
            JsonArray array = new JsonArray();
            while (rsLocal.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("REF_NO", rsLocal.getString("REF_NO"));
                object.addProperty("VDATE", rsLocal.getString("VDATE"));
                object.addProperty("FNAME", rsLocal.getString("FNAME"));
                object.addProperty("BAL", rsLocal.getString("BAL"));
                object.addProperty("REMARK", rsLocal.getString("REMARK"));
                object.addProperty("AC_CD", rsLocal.getString("AC_CD"));
                object.addProperty("BRANCH_CD", rsLocal.getString("BRANCH_CD"));
                array.add(object);
            }
            //                response.getWriter().print(array.toString());
            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
            jResultObj.add("data", array);
        } catch (SQLNonTransientConnectionException ex1) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
        }
    }
    response.getWriter().print(jResultObj);
}

From source file:catalog.CloudantUtil.java

License:Apache License

/**
 * create a document in the cloudant database
 *
 * @throws UnsupportedEncodingException/*from www  . j av a  2  s .co  m*/
 */
public static JsonObject createDocument(Credential credential, String jsonObj)
        throws UnsupportedEncodingException {
    JsonObject obj = new JsonParser().parse(jsonObj).getAsJsonObject();

    CloudantClient client = new CloudantClient(credential.user, credential.user, credential.password);
    Database db = client.database(credential.dbname, false);
    com.cloudant.client.api.model.Response res = db.post(obj);
    client.shutdown();

    JsonObject ret = new JsonObject();
    ret.addProperty("ok", true);
    ret.addProperty("id", res.getId());
    ret.addProperty("rev", res.getRev());
    return ret;
}

From source file:catalog.CloudantUtil.java

License:Apache License

public static JsonObject createDocParameterForWhisk(String doc) {
    JsonObject cloudantDoc = new JsonObject();
    String now = new Date().toString();
    cloudantDoc.addProperty("_id", DOC_ID);
    cloudantDoc.addProperty("date", now);
    // Create JSON object that will be passed as an argument to whisk cli
    JsonObject param = new JsonObject();
    if (doc != null && !doc.isEmpty()) {
        param.addProperty("doc", doc);
    } else {// w  w  w  .  ja va  2s  . com
        param.addProperty("doc", cloudantDoc.toString());
    }
    return param;
}

From source file:catalog.CloudantUtil.java

License:Apache License

public static JsonArray createDocumentArray(int numDocs) {
    // Array of docs for bulk
    JsonArray bulkDocs = new JsonArray();
    for (int i = 1; i <= numDocs; i++) {
        JsonObject cloudantDoc = new JsonObject();
        String now = new Date().toString();
        cloudantDoc.addProperty("_id", CloudantUtil.DOC_ID + i);
        cloudantDoc.addProperty("date", now);
        bulkDocs.add(cloudantDoc);/*from  w ww.  j ava2  s  . c  o m*/
    }
    return bulkDocs;
}

From source file:ccm.pay2spawn.configurator.Configurator.java

License:Open Source License

@Override
public void callback(Object... data) {
    int rewardID = (int) data[0];
    String type = (String) data[1];
    JsonObject newData = (JsonObject) data[2];

    if (rewardID == -1) {
        JsonObject object = new JsonObject();
        object.addProperty("type", type);
        object.add("data", newData);
        rewardData.add(object);/*w  w w.j a v a 2  s  .  c  o  m*/
        rewards.updateUI();
    } else {
        rewardData.get(rewardID).getAsJsonObject().add("data", newData);
        rewards.updateUI();
    }
}

From source file:ccm.pay2spawn.permissions.Group.java

License:Open Source License

public JsonElement toJson() {
    JsonObject root = new JsonObject();
    root.addProperty("name", getName());
    root.addProperty("parent", getParent());

    JsonArray nodes = new JsonArray();
    for (Node node : this.nodes)
        nodes.add(new JsonPrimitive(node.toString()));
    root.add("nodes", nodes);

    return root;/*from w w w.  j  a va 2s  .  co m*/
}

From source file:ccm.pay2spawn.permissions.Player.java

License:Open Source License

public JsonElement toJson() {
    JsonObject root = new JsonObject();
    root.addProperty("name", getName());
    JsonArray groups = new JsonArray();
    for (String group : this.getGroups())
        groups.add(new JsonPrimitive(group));
    root.add("groups", groups);

    JsonArray nodes = new JsonArray();
    for (Node node : this.overrideNodes)
        nodes.add(new JsonPrimitive(node.toString()));
    root.add("overrides", nodes);

    return root;/*from  w  w w .  j  av a2 s.co  m*/
}