use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.
the class RemoteMediaServer method uploadChanges.
public JSONArray uploadChanges(File zip) throws UnknownHttpResponseException, MediaSyncException {
try {
// no compression, as we compress the zip file instead
Response resp = super.req("uploadChanges", new FileInputStream(zip), 0);
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, JSONArray.class);
} catch (IOException | NullPointerException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.
the class Syncer method mergeModels.
private void mergeModels(JSONArray rchg) throws UnexpectedSchemaChange {
for (int i = 0; i < rchg.length(); i++) {
Model r = new Model(rchg.getJSONObject(i));
Model l = mCol.getModels().get(r.getLong("id"));
// if missing locally or server is newer, update
if (l == null || r.getLong("mod") > l.getLong("mod")) {
// syncing algorithm should handle this in a better way.
if (l != null) {
if (l.getJSONArray("flds").length() != r.getJSONArray("flds").length()) {
throw new UnexpectedSchemaChange();
}
if (l.getJSONArray("tmpls").length() != r.getJSONArray("tmpls").length()) {
throw new UnexpectedSchemaChange();
}
}
mCol.getModels().update(r);
}
}
}
use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.
the class Syncer method newerRows.
private ArrayList<Object[]> newerRows(JSONArray data, String table, int modIdx) {
long[] ids = new long[data.length()];
for (int i = 0; i < data.length(); i++) {
ids[i] = data.getJSONArray(i).getLong(0);
}
HashMap<Long, Long> lmods = new HashMap<>();
Cursor cur = null;
try {
cur = mCol.getDb().getDatabase().query("SELECT id, mod FROM " + table + " WHERE id IN " + Utils.ids2str(ids) + " AND " + usnLim(), null);
while (cur.moveToNext()) {
lmods.put(cur.getLong(0), cur.getLong(1));
}
} finally {
if (cur != null && !cur.isClosed()) {
cur.close();
}
}
ArrayList<Object[]> update = new ArrayList<>();
for (int i = 0; i < data.length(); i++) {
JSONArray r = data.getJSONArray(i);
if (!lmods.containsKey(r.getLong(0)) || lmods.get(r.getLong(0)) < r.getLong(modIdx)) {
update.add(Utils.jsonArray2Objects(r));
}
}
mCol.log(table, data);
return update;
}
use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.
the class Syncer method getModels.
/**
* Models ********************************************************************
*/
private JSONArray getModels() {
JSONArray result = new JSONArray();
if (mCol.getServer()) {
for (JSONObject m : mCol.getModels().all()) {
if (m.getInt("usn") >= mMinUsn) {
result.put(m);
}
}
} else {
for (JSONObject m : mCol.getModels().all()) {
if (m.getInt("usn") == -1) {
m.put("usn", mMaxUsn);
result.put(m);
}
}
mCol.getModels().save();
}
return result;
}
use of com.ichi2.utils.JSONArray in project AnkiChinaAndroid by ankichinateam.
the class Syncer method mergeDecks.
private void mergeDecks(JSONArray rchg) {
JSONArray decks = rchg.getJSONArray(0);
for (int i = 0; i < decks.length(); i++) {
Deck r = new Deck(decks.getJSONObject(i));
Deck l = mCol.getDecks().get(r.getLong("id"), false);
// if missing locally or server is newer, update
if (l == null || r.getLong("mod") > l.getLong("mod")) {
mCol.getDecks().update(r);
}
}
JSONArray confs = rchg.getJSONArray(1);
for (int i = 0; i < confs.length(); i++) {
DeckConfig r = new DeckConfig(confs.getJSONObject(i));
DeckConfig l = mCol.getDecks().getConf(r.getLong("id"));
// if missing locally or server is newer, update
if (l == null || r.getLong("mod") > l.getLong("mod")) {
mCol.getDecks().updateConf(r);
}
}
}
Aggregations