use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class LearnQueueManager method refill.
private synchronized void refill() {
final AnyMemoDBOpenHelper dbOpenHelper = AnyMemoDBOpenHelperManager.getHelper(context, dbPath);
final CardDao cardDao = dbOpenHelper.getCardDao();
dumpLearnQueue();
List<Card> exclusionList = new ArrayList<Card>(learnQueue.size() + dirtyCache.size());
exclusionList.addAll(learnQueue);
exclusionList.addAll(dirtyCache);
try {
if (newCache.size() == 0) {
List<Card> cs = cardDao.getNewCards(filterCategory, exclusionList, cacheSize);
if (cs.size() > 0) {
newCache.addAll(cs);
}
}
if (reviewCache.size() == 0) {
List<Card> cs = cardDao.getCardsForReview(filterCategory, exclusionList, cacheSize, reviewOrdering);
if (cs.size() > 0) {
reviewCache.addAll(cs);
}
}
while (learnQueue.size() < learnQueueSize && !reviewCache.isEmpty()) {
learnQueue.add(reviewCache.get(0));
reviewCache.remove(0);
}
while (learnQueue.size() < learnQueueSize && !newCache.isEmpty()) {
learnQueue.add(newCache.get(0));
newCache.remove(0);
}
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(dbOpenHelper);
}
flushDirtyCache();
dumpLearnQueue();
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class ImportMergingTest method testMergeCsvIntoDb.
@SmallTest
@Test
public void testMergeCsvIntoDb() throws Exception {
srcFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "csv-test.csv";
destFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "csv-test.db";
new File(srcFilePath).delete();
new File(destFilePath).delete();
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
try {
// Create an new db with some contents
helper.getCardDao().createCards(newDbCardList);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
// Now convert the csv-test.csv into csv-test.db which will be merged
// into existing csv-test.db
amFileUtil.copyFileFromAsset("csv-test.csv", new File(srcFilePath));
Converter converter = new CSVImporter(amFileUtil);
converter.convert(srcFilePath, destFilePath);
// verify the content of csv-test has merged cards
helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
try {
List<Card> cards = helper.getCardDao().getAllCards(null);
assertEquals(6, cards.size());
assertEquals(1, (int) cards.get(0).getId());
assertEquals(1, (int) cards.get(0).getOrdinal());
assertEquals("old question 1", cards.get(0).getQuestion());
assertEquals("old answer 1", cards.get(0).getAnswer());
assertEquals(2, (int) cards.get(1).getId());
assertEquals(2, (int) cards.get(1).getOrdinal());
assertEquals("old question 2", cards.get(1).getQuestion());
assertEquals("old answer 2", cards.get(1).getAnswer());
assertEquals(3, (int) cards.get(2).getId());
assertEquals(3, (int) cards.get(2).getOrdinal());
assertEquals("Question1", cards.get(2).getQuestion());
assertEquals("Answer1", cards.get(2).getAnswer());
assertEquals("Category1", cards.get(2).getCategory().getName());
assertEquals("Note1", cards.get(2).getNote());
assertEquals(4, (int) cards.get(3).getId());
assertEquals(4, (int) cards.get(3).getOrdinal());
assertEquals("Question2", cards.get(3).getQuestion());
assertEquals("Answer2", cards.get(3).getAnswer());
assertEquals("Category1", cards.get(3).getCategory().getName());
assertEquals("Note2", cards.get(3).getNote());
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class CsvImporterTest method verify.
@Override
protected void verify(String destFilePath) throws Exception {
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
try {
CardDao cardDao = helper.getCardDao();
CategoryDao categoryDao = helper.getCategoryDao();
List<Card> cards = cardDao.queryForAll();
List<Category> categories = categoryDao.queryForAll();
for (Card c : cards) {
categoryDao.refresh(c.getCategory());
}
assertEquals(4, cards.size());
assertEquals(3, categories.size());
assertEquals("Question1", cards.get(0).getQuestion());
assertEquals("Answer1", cards.get(0).getAnswer());
assertEquals("Category1", cards.get(0).getCategory().getName());
assertEquals("Question2", cards.get(1).getQuestion());
assertEquals("Answer2", cards.get(1).getAnswer());
assertEquals("Category1", cards.get(1).getCategory().getName());
assertEquals("Question3", cards.get(2).getQuestion());
assertEquals("Answer3", cards.get(2).getAnswer());
assertEquals("Category2", cards.get(2).getCategory().getName());
assertEquals("Question4", cards.get(3).getQuestion());
assertEquals("Answer4", cards.get(3).getAnswer());
assertEquals("", cards.get(3).getCategory().getName());
} finally {
helper.close();
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class DatabaseUtil method checkDatabase.
/*
* Check if the database is in the correct format
*/
public boolean checkDatabase(String dbPath) {
if (!(new File(dbPath)).exists()) {
return false;
}
AnyMemoDBOpenHelper helper = null;
try {
helper = AnyMemoDBOpenHelperManager.getHelper(mContext, dbPath);
helper.getCardDao().getTotalCount(null);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (helper != null) {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
}
use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.
the class DatabaseUtil method readDefaultSetting.
public Setting readDefaultSetting() {
String emptyDbPath = mContext.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + AMEnv.EMPTY_DB_NAME;
AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, emptyDbPath);
try {
SettingDao settingDao = helper.getSettingDao();
return settingDao.queryForId(1);
} finally {
AnyMemoDBOpenHelperManager.releaseHelper(helper);
}
}
Aggregations