package com.codepath.apps.simplecptweets.models;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

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

/**
 * Created by qiming on 8/2/2016.
 */
@Table(name = "users")
public class User extends Model {
    // list attributes
    @Column(name = "name")
    private String name;
    @Column(name = "uid", index = true)
    private long uid;
    @Column(name = "screename")
    private String screeName;

    public String getName() {
        return name;
    }

    public String getProfileImageUrl() {
        return profileImageUrl;
    }

    public String getScreeName() {
        return screeName;
    }

    public long getUid() {
        return uid;
    }

    private String profileImageUrl;

    //
    public static User fromJSON(JSONObject jsonObject) {
        User u = new User();
        try {
            u.name = jsonObject.getString("name");
            u.uid = jsonObject.getLong("id");
            u.screeName = jsonObject.getString("screen_name");
            u.profileImageUrl = jsonObject.getString("profile_image_url");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return u;
    }
}

@@ -43,16 +43,54 @@
 odels;%0A%0A
+import android.provider.BaseColumns;%0A%0A
 import c
@@ -113,16 +113,16 @@
 .Model;%0A
-
 import c
@@ -199,16 +199,55 @@
 n.Table;
+%0Aimport com.activeandroid.query.Select;
 %0A%0Aimport
@@ -363,16 +363,38 @@
  %22users%22
+, id = BaseColumns._ID
 )%0Apublic
@@ -1357,16 +1357,659 @@
 turn u;%0A
-
     %7D%0A
+%0A    public static User findOrCreateFromJson(JSONObject json) %7B%0A        long rId = 0; // get just the remote id%0A        try %7B%0A            rId = json.getLong(%22id%22);%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        User existingUser =%0A                new Select().from(User.class).where(%22remote_id = ?%22, rId).executeSingle();%0A        if (existingUser != null) %7B%0A            // found and return existing%0A            return existingUser;%0A        %7D else %7B%0A            // create and return new user%0A            User user = User.fromJSON(json);%0A            user.save();%0A            return user;%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Modify User model