package com.icecondor.eaglet.db.activity;

import java.security.InvalidParameterException;
import java.util.UUID;

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

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.icecondor.eaglet.Constants;
import com.icecondor.eaglet.db.Database;
import com.icecondor.eaglet.db.Sqlitable;

public class Base implements Sqlitable {
    protected final JSONObject json;

    public Base() {
        json = new JSONObject();
        try {
            json.put("id", UUID.randomUUID());
            json.put("class", getClass().getName());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getTableName() {
        return Database.ACTIVITIES_TABLE;
    }

    static public Cursor getAll(Database db) {
        if(db == null) {
            Log.d(Constants.APP_TAG, "getAll called with null db!");
            throw new InvalidParameterException();
        } else {
            return db.
                    getReadonly().
                    query(Database.ACTIVITIES_TABLE, null,
                          null, null, null, null, "created_at desc", "150");
        }
    }

    @Override
    public ContentValues getAttributes() {
        ContentValues cv = new ContentValues();
        cv.put(Database.ACTIVITIES_UUID, UUID.randomUUID().toString());
        return cv;
    }


}

@@ -1338,24 +1338,42 @@
 ntValues();%0A
+        try %7B%0A    
         cv.p
@@ -1405,38 +1405,109 @@
 ID, 
-UUID.randomUUID().toString());
+json.getString(%22id%22));%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A   

return existing uuid