use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class AnkiChinaSyncer method getChangedColJson.
private <T extends JSONObject> JSONObject getChangedColJson(int type, List<T> newModels) {
List<T> changedModel = new ArrayList<>();
boolean deleted;
Cursor cur = null;
Map<Long, Long> oldModel = new HashMap<>();
JSONObject modelsJson = new JSONObject();
try {
cur = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance()).getDb().getDatabase().query("SELECT id,mod FROM synclog WHERE type = " + type, null);
while (cur.moveToNext()) {
oldModel.put(cur.getLong(0), cur.getLong(1));
}
} finally {
if (cur != null && !cur.isClosed()) {
cur.close();
}
}
if (oldModel.isEmpty()) {
changedModel.addAll(newModels);
} else {
StringBuilder deletedModelSb = new StringBuilder();
for (Map.Entry<Long, Long> entry : oldModel.entrySet()) {
// System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
deleted = true;
for (T model : newModels) {
if (!changedModel.contains(model) && (oldModel.get(model.getLong("id")) == null || oldModel.get(model.getLong("id")) < model.getLong("mod"))) {
// 新增或修改过的deck
changedModel.add(model);
} else if (model.getLong("id") == entry.getKey()) {
// 未被删除的deck
deleted = false;
}
}
if (deleted) {
if (deletedModelSb.length() != 0) {
deletedModelSb.append(",");
}
deletedModelSb.append(entry.getKey());
}
}
if (deletedModelSb.length() > 0) {
modelsJson.put("delete", new JSONArray(deletedModelSb.toString().split(",")));
// modelsJson.put("delete", strArray2jsonArray(deletedModelSb.toString().split(",")));
}
}
JSONArray changedModelJson = new JSONArray();
for (T item : changedModel) {
changedModelJson.put(item);
}
modelsJson.put("replace", changedModelJson);
return modelsJson;
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class AnkiChinaSyncer method compareSyncTable.
private JSONObject compareSyncTable() {
mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
CollectionHelper.getInstance().lockCollection();
DB db = mCol.getDb();
db.execute("create index if not exists ix_synclog_id on synclog (id);)");
int num = db.queryScalar("SELECT id FROM synclog");
if (num == 0) {
// 表是空的,则直接同步所有内容到该表
Timber.w("no record in synclog table!");
}
JSONObject root = new JSONObject();
JSONObject colJson = new JSONObject();
JSONObject colJsonReplace = new JSONObject();
// JSONObject decksJson = new JSONObject();
// JSONObject modelsJson = new JSONObject();
// JSONObject dconfJson = new JSONObject();
colJsonReplace.put("crt", mCol.getCrt());
colJsonReplace.put("mod", mCol.getMod());
colJsonReplace.put("scm", mCol.getScm());
colJsonReplace.put("ver", mCol.getVer());
colJsonReplace.put("dty", mCol.getDirty() ? 1 : 0);
colJsonReplace.put("usn", mCol.getUsnForSync());
colJsonReplace.put("ls", mCol.getLs());
colJsonReplace.put("conf", mCol.getConf());
colJsonReplace.put("tags", mCol.getTagsJson());
colJson.put("replace", colJsonReplace);
root.put("col", colJson);
root.put("decks", getChangedColJson(SYNC_LOG_TYPE_DECKS, mCol.getDecks().all()));
root.put("dconf", getChangedColJson(SYNC_LOG_TYPE_DCONF, mCol.getDecks().allConf()));
root.put("models", getChangedColJson(SYNC_LOG_TYPE_MODELS, mCol.getModels().all()));
updateDialogProgress(SYNCING_DATA, "整理数据中", 6);
root.put("cards", getChangedCardsOrNote(SYNC_LOG_TYPE_CARD));
root.put("notes", getChangedCardsOrNote(SYNC_LOG_TYPE_NOTE));
root.put("revlog", getAddedRevLog());
CollectionHelper.getInstance().unlockCollection();
// root.put("revlog", getChangedCardsOrNote(SYNC_LOG_TYPE_REVLOG));
return root;
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class TemporaryModelTest method testTempModelStorage.
@Test
public void testTempModelStorage() throws Exception {
// Start off with clean state in the cache dir
TemporaryModel.clearTempModelFiles();
// Make sure save / retrieve works
String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), new JSONObject("{foo: bar}"));
Assert.assertNotNull("Saving temp model unsuccessful", tempModelPath);
JSONObject tempModel = TemporaryModel.getTempModel(tempModelPath);
Assert.assertNotNull("Temp model not read successfully", tempModel);
Assert.assertEquals(new JSONObject("{foo: bar}").toString(), tempModel.toString());
// Make sure clearing works
Assert.assertEquals(1, TemporaryModel.clearTempModelFiles());
Timber.i("The following logged NoSuchFileException is an expected part of verifying a file delete.");
try {
TemporaryModel.getTempModel(tempModelPath);
Assert.fail("Should have caught an exception here because the file is missing");
} catch (IOException e) {
// this is expected
}
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class SchedTest method test_deckDueV1.
@Test
public void test_deckDueV1() throws Exception {
Collection col = getColV1();
// add a note with default deck
Note note = col.newNote();
note.setItem("Front", "one");
col.addNote(note);
// and one that's a child
note = col.newNote();
note.setItem("Front", "two");
long default1 = col.getDecks().id("Default::1");
note.model().put("did", default1);
col.addNote(note);
// make it a review card
Card c = note.cards().get(0);
c.setQueue(QUEUE_TYPE_REV);
c.setDue(0);
c.flush();
// add one more with a new deck
note = col.newNote();
note.setItem("Front", "two");
JSONObject foobar = note.model().put("did", col.getDecks().id("foo::bar"));
col.addNote(note);
// and one that's a sibling
note = col.newNote();
note.setItem("Front", "three");
JSONObject foobaz = note.model().put("did", col.getDecks().id("foo::baz"));
col.addNote(note);
col.reset();
assertEquals(5, col.getDecks().allSortedNames().size());
DeckDueTreeNode tree = col.getSched().deckDueTree().get(0);
assertEquals("Default", tree.getLastDeckNameComponent());
// sum of child and parent
assertEquals(1, tree.getDid());
assertEquals(1, tree.getRevCount());
assertEquals(1, tree.getNewCount());
// child count is just review
DeckDueTreeNode child = tree.getChildren().get(0);
assertEquals("1", child.getLastDeckNameComponent());
assertEquals(default1, child.getDid());
assertEquals(1, child.getRevCount());
assertEquals(0, child.getNewCount());
// code should not fail if a card has an invalid deck
c.setDid(12345);
c.flush();
col.getSched().deckDueTree();
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class CardTemplateEditorTest method testDeleteTemplate.
@Test
public void testDeleteTemplate() {
String modelName = "Basic (and reversed card)";
// Start the CardTemplateEditor with a specific model, and make sure the model starts unchanged
JSONObject collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("modelId", collectionBasicModelOriginal.getLong("id"));
ActivityController<CardTemplateEditor> templateEditorController = Robolectric.buildActivity(CardTemplateEditor.class, intent).create().start().resume().visible();
saveControllerForCleanup(templateEditorController);
CardTemplateEditor testEditor = (CardTemplateEditor) templateEditorController.get();
Assert.assertFalse("Model should not have changed yet", testEditor.modelHasChanged());
Assert.assertEquals("Model should have 2 templates now", 2, testEditor.getTempModel().getTemplateCount());
// Try to delete the template - click delete, click confirm for card delete, click confirm again for full sync
ShadowActivity shadowTestEditor = shadowOf(testEditor);
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
advanceRobolectricLooper();
Assert.assertEquals("Wrong dialog shown?", "Delete the “Card 1” card type, and its 0 cards?", getDialogText(true));
clickDialogButton(DialogAction.POSITIVE, true);
advanceRobolectricLooper();
Assert.assertTrue("Model should have changed", testEditor.modelHasChanged());
Assert.assertEquals("Model should have 1 template now", 1, testEditor.getTempModel().getTemplateCount());
// Try to delete the template again, but there's only one
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_delete));
advanceRobolectricLooper();
Assert.assertEquals("Did not show dialog about deleting only card?", getResourceString(R.string.card_template_editor_cant_delete), getDialogText(true));
Assert.assertEquals("Change already in database?", collectionBasicModelOriginal.toString().trim(), getCurrentDatabaseModelCopy(modelName).toString().trim());
// Save the change to the database and make sure there's only one template after
JSONObject testEditorModelEdited = testEditor.getTempModel().getModel();
Assert.assertTrue("Unable to click?", shadowTestEditor.clickMenuItem(R.id.action_confirm));
advanceRobolectricLooper();
JSONObject collectionBasicModelCopyEdited = getCurrentDatabaseModelCopy(modelName);
Assert.assertNotEquals("model is unchanged?", collectionBasicModelOriginal, collectionBasicModelCopyEdited);
Assert.assertEquals("model did not save?", testEditorModelEdited.toString().trim(), collectionBasicModelCopyEdited.toString().trim());
}
Aggregations