Example usage for org.json JSONObject getLong

List of usage examples for org.json JSONObject getLong

Introduction

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

Prototype

public long getLong(String key) throws JSONException 

Source Link

Document

Get the long value associated with a key.

Usage

From source file:com.hichinaschool.flashcards.libanki.Models.java

public void save(JSONObject m, boolean templates) {
    if (m != null && m.has("id")) {
        try {//  w ww. ja  va  2s  .  c om
            m.put("mod", Utils.intNow());
            m.put("usn", mCol.usn());
            // TODO: fix empty id problem on _updaterequired (needed for model adding)
            if (m.getLong("id") != 0) {
                _updateRequired(m);
            }
            if (templates) {
                _syncTemplates(m);
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
    mChanged = true;
    // runHook("newModel")
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/** Delete model, and all its cards/notes. */
public void rem(JSONObject m) {
    mCol.modSchema();/*w  w w  . j  av  a 2  s .  c  o m*/
    try {
        long id = m.getLong("id");
        boolean current = current().getLong("id") == id;
        // delete notes/cards
        mCol.remCards(Utils.arrayList2array(mCol.getDb().queryColumn(Long.class,
                "SELECT id FROM cards WHERE nid IN (SELECT id FROM notes WHERE mid = " + id + ")", 0)));
        // then the model
        mModels.remove(id);
        save();
        // GUI should ensure last model is not deleted
        if (current) {
            setCurrent(mModels.values().iterator().next());
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/** Add or update an existing model. Used for syncing and merging. */
public void update(JSONObject m) {
    try {// w w w .  java2 s . c o  m
        mModels.put(m.getLong("id"), m);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    // mark registry changed, but don't bump mod time
    save();
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/** Note ids for M */
public ArrayList<Long> nids(JSONObject m) {
    try {//  w ww.j  a va2s  . com
        return mCol.getDb().queryColumn(Long.class, "SELECT id FROM notes WHERE mid = " + m.getLong("id"), 0);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/**
 * Number of notes using m/*from   w  w w .  ja v  a  2 s . c  o m*/
 * @param m The model to the count the notes of.
 * @return The number of notes with that model.
 */
public int useCount(JSONObject m) {
    try {
        return mCol.getDb().queryScalar("select count() from notes where mid = " + m.getLong("id"));
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

public void addField(JSONObject m, JSONObject field) {
    // only mod schema if model isn't new
    try {//from ww  w  . java2s .  c  om
        if (m.getLong("id") != 0) {
            mCol.modSchema();
        }
        JSONArray ja = m.getJSONArray("flds");
        ja.put(field);
        m.put("flds", ja);
        _updateFieldOrds(m);
        save(m);
        _transformFields(m, new TransformFieldAdd());
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.hichinaschool.flashcards.libanki.Models.java

public void _transformFields(JSONObject m, TransformFieldVisitor fn) {
    // model hasn't been added yet?
    try {/*ww  w .j  av  a 2 s. c  o m*/
        if (m.getLong("id") == 0) {
            return;
        }
        ArrayList<Object[]> r = new ArrayList<Object[]>();
        Cursor cur = null;

        try {
            cur = mCol.getDb().getDatabase()
                    .rawQuery("select id, flds from notes where mid = " + m.getLong("id"), null);
            while (cur.moveToNext()) {
                r.add(new Object[] {
                        Utils.joinFields((String[]) fn.transform(Utils.splitFields(cur.getString(1)))),
                        Utils.intNow(), mCol.usn(), cur.getLong(0) });
            }
        } finally {
            if (cur != null) {
                cur.close();
            }
        }
        mCol.getDb().executeMany("update notes set flds=?,mod=?,usn=? where id = ?", r);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/** Note: should col.genCards() afterwards. */
public void addTemplate(JSONObject m, JSONObject template) {
    try {/*  w  ww . j a v a2s .  c  o m*/
        if (m.getLong("id") != 0) {
            mCol.modSchema();
        }
        JSONArray ja = m.getJSONArray("tmpls");
        ja.put(template);
        m.put("tmpls", ja);
        _updateTemplOrds(m);
        save(m);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

/**
 * Removing a template//  w ww .  j  av a  2 s .  com
 * 
 * @return False if removing template would leave orphan notes.
 */
public boolean remTemplate(JSONObject m, JSONObject template) {
    try {
        assert (m.getJSONArray("tmpls").length() > 1);
        // find cards using this template
        JSONArray ja = m.getJSONArray("tmpls");
        int ord = -1;
        for (int i = 0; i < ja.length(); ++i) {
            if (ja.get(i).equals(template)) {
                ord = i;
                break;
            }
        }
        String sql = "select c.id from cards c, notes f where c.nid=f.id and mid = " + m.getLong("id")
                + " and ord = " + ord;
        long[] cids = Utils.toPrimitive(mCol.getDb().queryColumn(Long.class, sql, 0));
        // all notes with this template must have at least two cards, or we could end up creating orphaned notes
        sql = "select nid, count() from cards where nid in (select nid from cards where id in "
                + Utils.ids2str(cids) + ") group by nid having count() < 2 limit 1";
        if (mCol.getDb().queryScalar(sql, false) != 0) {
            return false;
        }
        // ok to proceed; remove cards
        mCol.modSchema();
        mCol.remCards(cids);
        // shift ordinals
        mCol.getDb().execute(
                "update cards set ord = ord - 1, usn = ?, mod = ? where nid in (select id from notes where mid = ?) and ord > ?",
                new Object[] { mCol.usn(), Utils.intNow(), m.getLong("id"), ord });
        JSONArray tmpls = m.getJSONArray("tmpls");
        JSONArray ja2 = new JSONArray();
        for (int i = 0; i < tmpls.length(); ++i) {
            if (template.equals(tmpls.getJSONObject(i))) {
                continue;
            }
            ja2.put(tmpls.get(i));
        }
        m.put("tmpls", ja2);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    _updateTemplOrds(m);
    save(m);
    return true;
}

From source file:com.hichinaschool.flashcards.libanki.Models.java

public void moveTemplate(JSONObject m, JSONObject template, int idx) {
    try {/*from  www  .  j a va  2  s . c  om*/
        JSONArray ja = m.getJSONArray("tmpls");
        int oldidx = -1;
        ArrayList<JSONObject> l = new ArrayList<JSONObject>();
        HashMap<Integer, Integer> oldidxs = new HashMap<Integer, Integer>();
        for (int i = 0; i < ja.length(); ++i) {
            if (ja.get(i).equals(template)) {
                oldidx = i;
                if (idx == oldidx) {
                    return;
                }
            }
            JSONObject t = ja.getJSONObject(i);
            oldidxs.put(t.hashCode(), t.getInt("ord"));
            l.add(t);
        }
        l.remove(oldidx);
        l.add(idx, template);
        m.put("tmpls", new JSONArray(l));
        _updateTemplOrds(m);
        // generate change map - We use StringBuilder
        StringBuilder sb = new StringBuilder();
        ja = m.getJSONArray("tmpls");
        for (int i = 0; i < ja.length(); ++i) {
            JSONObject t = ja.getJSONObject(i);
            sb.append("when ord = ").append(oldidxs.get(t.hashCode())).append(" then ").append(t.getInt("ord"));
            if (i != ja.length() - 1) {
                sb.append(" ");
            }
        }
        // apply
        save(m);
        mCol.getDb().execute(
                "update cards set ord = (case " + sb.toString()
                        + " end),usn=?,mod=? where nid in (select id from notes where mid = ?)",
                new Object[] { mCol.usn(), Utils.intNow(), m.getLong("id") });
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}