Search in sources :

Example 26 with AnyMemoDBOpenHelper

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();
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) ArrayList(java.util.ArrayList) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 27 with AnyMemoDBOpenHelper

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);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Converter(org.liberty.android.fantastischmemo.converter.Converter) File(java.io.File) CSVImporter(org.liberty.android.fantastischmemo.converter.CSVImporter) Card(org.liberty.android.fantastischmemo.entity.Card) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) BaseTest(org.liberty.android.fantastischmemo.test.BaseTest) SmallTest(androidx.test.filters.SmallTest)

Example 28 with AnyMemoDBOpenHelper

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();
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Category(org.liberty.android.fantastischmemo.entity.Category) CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 29 with AnyMemoDBOpenHelper

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);
        }
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) File(java.io.File)

Example 30 with AnyMemoDBOpenHelper

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);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) SettingDao(org.liberty.android.fantastischmemo.dao.SettingDao)

Aggregations

AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)32 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)28 Card (org.liberty.android.fantastischmemo.entity.Card)28 File (java.io.File)13 Category (org.liberty.android.fantastischmemo.entity.Category)11 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)10 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)9 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)9 IOException (java.io.IOException)6 URL (java.net.URL)5 FileWriter (java.io.FileWriter)4 ArrayList (java.util.ArrayList)4 SmallTest (androidx.test.filters.SmallTest)3 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 Test (org.junit.Test)3 InputSource (org.xml.sax.InputSource)3 XMLReader (org.xml.sax.XMLReader)3