use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method csvManualBasicExample.
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void csvManualBasicExample() throws IOException, ConfirmModSchemaException {
String file = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "text-anki-manual-csv-single-line.txt");
addFieldToCurrentModel("Third");
TextImporter i = new TextImporter(testCol, file);
i.setAllowHtml(true);
i.initMapping();
i.run();
Note n = testCol.getNote(testCol.getDb().queryLongScalar("select id from notes"));
assertThat(Arrays.asList(n.getFields()), contains("foo bar", "bar baz", "baz quux"));
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class Models method remTemplate.
/**
* Removing a template
*
* @return False if removing template would leave orphan notes.
* @throws ConfirmModSchemaException
*/
public boolean remTemplate(Model m, JSONObject template) throws ConfirmModSchemaException {
if (m.getJSONArray("tmpls").length() <= 1) {
return false;
}
// find cards using this template
JSONArray tmpls = m.getJSONArray("tmpls");
int ord = -1;
for (int i = 0; i < tmpls.length(); ++i) {
if (tmpls.getJSONObject(i).equals(template)) {
ord = i;
break;
}
}
if (ord == -1) {
throw new IllegalArgumentException("Invalid template proposed for delete");
}
// the code in "isRemTemplateSafe" was in place here in libanki. It is extracted to a method for reuse
List<Long> cids = getCardIdsForModel(m.getLong("id"), new int[] { ord });
if (cids == null) {
Timber.d("remTemplate getCardIdsForModel determined it was unsafe to delete the template");
return false;
}
// ok to proceed; remove cards
Timber.d("remTemplate proceeding to delete the template and %d cards", cids.size());
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 > ?", mCol.usn(), mCol.getTime().intTime(), m.getLong("id"), ord);
tmpls = m.getJSONArray("tmpls");
JSONArray tmpls2 = new JSONArray();
for (int i = 0; i < tmpls.length(); ++i) {
if (template.equals(tmpls.getJSONObject(i))) {
continue;
}
tmpls2.put(tmpls.getJSONObject(i));
}
m.put("tmpls", tmpls2);
_updateTemplOrds(m);
save(m);
Timber.d("remTemplate done working");
return true;
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class Models method moveField.
public void moveField(Model m, JSONObject field, int idx) throws ConfirmModSchemaException {
mCol.modSchema();
JSONArray flds = m.getJSONArray("flds");
ArrayList<JSONObject> l = new ArrayList<>();
int oldidx = -1;
for (int i = 0; i < flds.length(); ++i) {
l.add(flds.getJSONObject(i));
if (field.equals(flds.getJSONObject(i))) {
oldidx = i;
if (idx == oldidx) {
return;
}
}
}
// remember old sort field
String sortf = Utils.jsonToString(m.getJSONArray("flds").getJSONObject(m.getInt("sortf")));
// move
l.remove(oldidx);
l.add(idx, field);
m.put("flds", new JSONArray(l));
// restore sort field
flds = m.getJSONArray("flds");
for (int i = 0; i < flds.length(); ++i) {
if (Utils.jsonToString(flds.getJSONObject(i)).equals(sortf)) {
m.put("sortf", i);
break;
}
}
_updateFieldOrds(m);
save(m);
_transformFields(m, new TransformFieldMove(idx, oldidx));
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class Storage method _upgradeClozeModel.
private static void _upgradeClozeModel(Collection col, Model m) throws ConfirmModSchemaException {
m.put("type", Consts.MODEL_CLOZE);
// convert first template
JSONObject t = m.getJSONArray("tmpls").getJSONObject(0);
for (String type : new String[] { "qfmt", "afmt" }) {
t.put(type, t.getString(type).replaceAll("\\{\\{cloze:1:(.+?)\\}\\}", "{{cloze:$1}}"));
}
t.put("name", "Cloze");
// delete non-cloze cards for the model
JSONArray tmpls = m.getJSONArray("tmpls");
ArrayList<JSONObject> rem = new ArrayList<>();
for (int i = 1; i < tmpls.length(); i++) {
JSONObject ta = tmpls.getJSONObject(i);
if (!ta.getString("afmt").contains("{{cloze:")) {
rem.add(ta);
}
}
for (JSONObject r : rem) {
col.getModels().remTemplate(m, r);
}
JSONArray newTmpls = new JSONArray();
newTmpls.put(tmpls.getJSONObject(0));
m.put("tmpls", newTmpls);
col.getModels()._updateTemplOrds(m);
col.getModels().save(m);
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class Storage method _upgrade.
private static void _upgrade(Collection col, int ver) {
try {
if (ver < 3) {
// new deck properties
for (Deck d : col.getDecks().all()) {
d.put("dyn", 0);
d.put("collapsed", false);
col.getDecks().save(d);
}
}
if (ver < 4) {
col.modSchemaNoCheck();
ArrayList<Model> clozes = new ArrayList<>();
for (Model m : col.getModels().all()) {
if (!m.getJSONArray("tmpls").getJSONObject(0).getString("qfmt").contains("{{cloze:")) {
m.put("type", Consts.MODEL_STD);
} else {
clozes.add(m);
}
}
for (Model m : clozes) {
try {
_upgradeClozeModel(col, m);
} catch (ConfirmModSchemaException e) {
// Will never be reached as we already set modSchemaNoCheck()
throw new RuntimeException(e);
}
}
col.getDb().execute("UPDATE col SET ver = 4");
}
if (ver < 5) {
col.getDb().execute("UPDATE cards SET odue = 0 WHERE queue = 2");
col.getDb().execute("UPDATE col SET ver = 5");
}
if (ver < 6) {
col.modSchemaNoCheck();
for (Model m : col.getModels().all()) {
m.put("css", new JSONObject(Models.defaultModel).getString("css"));
JSONArray ar = m.getJSONArray("tmpls");
for (int i = 0; i < ar.length(); i++) {
JSONObject t = ar.getJSONObject(i);
if (!t.has("css")) {
continue;
}
m.put("css", m.getString("css") + "\n" + t.getString("css").replace(".card ", ".card" + t.getInt("ord") + 1));
t.remove("css");
}
col.getModels().save(m);
}
col.getDb().execute("UPDATE col SET ver = 6");
}
if (ver < 7) {
col.modSchemaNoCheck();
col.getDb().execute("UPDATE cards SET odue = 0 WHERE (type = " + Consts.CARD_TYPE_LRN + " OR queue = 2) AND NOT odid");
col.getDb().execute("UPDATE col SET ver = 7");
}
if (ver < 8) {
col.modSchemaNoCheck();
col.getDb().execute("UPDATE cards SET due = due / 1000 WHERE due > 4294967296");
col.getDb().execute("UPDATE col SET ver = 8");
}
if (ver < 9) {
col.getDb().execute("UPDATE col SET ver = 9");
}
if (ver < 10) {
col.getDb().execute("UPDATE cards SET left = left + left * 1000 WHERE queue = " + Consts.QUEUE_TYPE_LRN);
col.getDb().execute("UPDATE col SET ver = 10");
}
if (ver < 11) {
col.modSchemaNoCheck();
for (Deck d : col.getDecks().all()) {
if (d.getInt("dyn") != 0) {
int order = d.getInt("order");
// failed order was removed
if (order >= 5) {
order -= 1;
}
JSONArray terms = new JSONArray(Arrays.asList(d.getString("search"), d.getInt("limit"), order));
d.put("terms", new JSONArray());
d.getJSONArray("terms").put(0, terms);
d.remove("search");
d.remove("limit");
d.remove("order");
d.put("resched", true);
d.put("return", true);
} else {
if (!d.has("extendNew")) {
d.put("extendNew", 10);
d.put("extendRev", 50);
}
}
col.getDecks().save(d);
}
for (DeckConfig c : col.getDecks().allConf()) {
JSONObject r = c.getJSONObject("rev");
r.put("ivlFct", r.optDouble("ivlFct", 1));
if (r.has("ivlfct")) {
r.remove("ivlfct");
}
r.put("maxIvl", 36500);
col.getDecks().save(c);
}
for (Model m : col.getModels().all()) {
JSONArray tmpls = m.getJSONArray("tmpls");
for (int ti = 0; ti < tmpls.length(); ++ti) {
JSONObject t = tmpls.getJSONObject(ti);
t.put("bqfmt", "");
t.put("bafmt", "");
}
col.getModels().save(m);
}
col.getDb().execute("update col set ver = 11");
}
// if (ver < 12) {
// col.getDb().execute("create table if not exists synclog (" + " id integer not null,"
// + " type integer not null,"+ " mod integer not null" + ")");
// col.getDb().execute("update col set ver = 12");
// _updateIndices(col.getDb());
// }
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
Aggregations