Example usage for org.json JSONObject getBoolean

List of usage examples for org.json JSONObject getBoolean

Introduction

In this page you can find the example usage for org.json JSONObject getBoolean.

Prototype

public boolean getBoolean(String key) throws JSONException 

Source Link

Document

Get the boolean value associated with a key.

Usage

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrSyncEngine.java

public static Object saveOrUpdateAccountFromJSON(long id, JSONObject jsonObjectAccount) {

    Account tEntity = em.get(Account.class, id);

    if (tEntity == null) {
        tEntity = new Account();
        tEntity.id = KEY_CREATE;//from  w w  w. java 2s.  com
    }

    try {
        //title
        try {
            tEntity.title = jsonObjectAccount.getString("name");
        } catch (Exception e) {
            tEntity.title = "---";
            Log.e(TAG, "Error parsing Account.name with");
        }
        tEntity.remoteKey = jsonObjectAccount.getString("key");
        //creation_date
        try {
            tEntity.creationDate = jsonObjectAccount.getLong("created_on");
        } catch (Exception e) {
            Log.e(TAG, "Error parsing Account.creationDate");
        }
        //last_transaction_date
        if (jsonObjectAccount.has("dateOfLastTransaction")) {
            try {
                tEntity.lastTransactionDate = jsonObjectAccount.getLong("dateOfLastTransaction");
            } catch (Exception e) {
                Log.e(TAG, "Error parsing Account.dateOfLastTransaction with : "
                        + jsonObjectAccount.getString("dateOfLastTransaction"));
            }
        }
        //currency, currency_name, currency_symbol
        Currency c = null;
        Collection<Currency> currencies = CurrencyCache.getAllCurrencies();
        if (jsonObjectAccount.has("currency_id")) {
            try {
                c = em.load(Currency.class,
                        getLocalKey(DatabaseHelper.CURRENCY_TABLE, jsonObjectAccount.getString("currency_id")));
                tEntity.currency = c;
            } catch (Exception e) {
                Log.e(TAG, "unable to load currency for account " + tEntity.title + " with "
                        + jsonObjectAccount.getString("currency_id"));
            }
            //server created account don't have a currency_id but all the properties to build one.
        }
        if (tEntity.currency == null && jsonObjectAccount.has("currency")) {
            //try if provided currency is in user's currency
            for (Currency currency : currencies) {
                if (currency.name.equals(jsonObjectAccount.getString("currency"))) {
                    tEntity.currency = currency;
                }
            }
            //load from data server if any
            if (tEntity.currency == null) {
                c = Currency.EMPTY;
                c.name = jsonObjectAccount.getString("currency");
                if (jsonObjectAccount.has("currency_name")) {
                    c.title = jsonObjectAccount.getString("currency_name");
                }
                if (jsonObjectAccount.has("currency_symbol")) {
                    c.symbol = jsonObjectAccount.getString("currency_symbol");
                }
                tEntity.currency = c;
                c.id = -1; //db put!
                em.saveOrUpdate(c);
            }
        } else if (tEntity.currency == null) {
            //no currency provided use default
            for (Currency currency : currencies) {
                if (currency.isDefault) {
                    tEntity.currency = currency;
                }
            }
            //still nothing : default set to empty
            if (tEntity.currency == null) {
                c = Currency.EMPTY;
                c.isDefault = true;
                tEntity.currency = c;
                //c.id=-1; //db put!
                //em.saveOrUpdate(c);
            }
        }
        CurrencyCache.initialize(em);
        //card_issuer
        if (jsonObjectAccount.has("card_issuer")) {
            tEntity.cardIssuer = jsonObjectAccount.getString("card_issuer");
        }
        //issuer
        if (jsonObjectAccount.has(DatabaseHelper.AccountColumns.ISSUER)) {
            tEntity.issuer = jsonObjectAccount.getString(DatabaseHelper.AccountColumns.ISSUER);
        }
        //number
        if (jsonObjectAccount.has("code")) {
            tEntity.number = jsonObjectAccount.getString("code");
        }
        //limit
        if (jsonObjectAccount.has("total_limit")) {
            tEntity.limitAmount = jsonObjectAccount.getLong("total_limit");
        }
        //is_active
        if (jsonObjectAccount.has("closed")) {
            if (jsonObjectAccount.getBoolean("closed")) {
                tEntity.isActive = false;
            } else {
                tEntity.isActive = true;
            }
        }
        //is_include_into_totals
        if (jsonObjectAccount.has(DatabaseHelper.AccountColumns.IS_INCLUDE_INTO_TOTALS)) {
            if (jsonObjectAccount.getBoolean(DatabaseHelper.AccountColumns.IS_INCLUDE_INTO_TOTALS)) {
                tEntity.isIncludeIntoTotals = true;
            } else {
                tEntity.isIncludeIntoTotals = false;
            }
        }
        //closing_day
        try {
            tEntity.closingDay = jsonObjectAccount.getInt(DatabaseHelper.AccountColumns.CLOSING_DAY);
        } catch (Exception e) {
        }
        //payment_day
        try {
            tEntity.paymentDay = jsonObjectAccount.getInt(DatabaseHelper.AccountColumns.PAYMENT_DAY);
        } catch (Exception e) {
        }
        //note
        if (jsonObjectAccount.has("description")) {
            tEntity.note = jsonObjectAccount.getString("description");
        }
        //sort_order
        if (jsonObjectAccount.has(DatabaseHelper.AccountColumns.SORT_ORDER)) {
            tEntity.sortOrder = jsonObjectAccount.getInt(DatabaseHelper.AccountColumns.SORT_ORDER);
        }
        if (jsonObjectAccount.has(DatabaseHelper.AccountColumns.TYPE)) {
            tEntity.type = jsonObjectAccount.getString(DatabaseHelper.AccountColumns.TYPE);
        }

        em.saveOrUpdate(tEntity);
        return tEntity;
    } catch (Exception e1) {
        e1.printStackTrace();
        return e1;
    }
}