org.prx.prp.utility.ParsedJSONObject.java Source code

Java tutorial

Introduction

Here is the source code for org.prx.prp.utility.ParsedJSONObject.java

Source

//  Copyright (c) 2010 Mahesh Sharma,Matt MacDonald
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.

package org.prx.prp.utility;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.sqlite.SQLiteConstraintException;
import android.util.Log;

public class ParsedJSONObject {
    String[] names;
    String[] values;
    boolean toBeDeleted;
    Integer id;

    public ParsedJSONObject(JSONObject object) {
        int index;
        values = new String[names.length];
        for (index = 0; index < this.names.length; index++) {
            try {
                values[index] = object.getString(names[index]);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        checkForDelete(object);
    }

    private void checkForDelete(JSONObject object) {
        try {
            if (object.getString("deleted_at") != "null")
                toBeDeleted = true;
            else
                toBeDeleted = false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void storeId(JSONObject object) {
        try {
            this.id = object.getInt("id");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public ParsedJSONObject(JSONObject object, String[] columnNames) {
        int index;
        names = columnNames;
        values = new String[columnNames.length];
        for (index = 0; index < columnNames.length; index++) {
            try {
                values[index] = object.getString(columnNames[index]);
            } catch (JSONException e) {
                values[index] = "NULL";
            }
        }
        storeId(object);
        checkForDelete(object);
    }

    public void sync(String modelName) {
        try {
            if (this.toBeDeleted) {
                DatabaseAdapter.deleteRecord(modelName, new String[] { "id" }, new String[] { id.toString() });
            } else {
                ContentValues params = new ContentValues();
                for (int index = 0; index < names.length; index++) {
                    params.put(names[index], values[index]);
                }
                DatabaseAdapter.insertRecord(modelName, params);
            }

        } catch (SQLiteConstraintException e) {
            Log.d("PRPAND", e.getMessage());
        }

    }

    public int getId() {
        return id;
    }
}