Search in sources :

Example 6 with AnyMemoDBOpenHelper

use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.

the class Mnemosyne2CardsImporterTest 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();
        LearningDataDao learningDataDao = helper.getLearningDataDao();
        List<Card> cards = cardDao.queryForAll();
        List<Category> categories = categoryDao.queryForAll();
        for (Card c : cards) {
            categoryDao.refresh(c.getCategory());
            learningDataDao.refresh(c.getLearningData());
        }
        assertEquals(4, cards.size());
        assertEquals(3, categories.size());
        // LOL, the test cards has a typo
        assertEquals("Qustion1", 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("Category2", 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) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 7 with AnyMemoDBOpenHelper

use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.

the class MnemosyneXMLImporterTest 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();
        LearningDataDao learningDataDao = helper.getLearningDataDao();
        List<Card> cards = cardDao.queryForAll();
        List<Category> categories = categoryDao.queryForAll();
        for (Card c : cards) {
            categoryDao.refresh(c.getCategory());
            learningDataDao.refresh(c.getLearningData());
        }
        assertEquals(11, cards.size());
        assertEquals(2, categories.size());
        assertEquals("q1", cards.get(0).getQuestion());
        assertEquals("a1", cards.get(0).getAnswer());
        assertEquals("<Standard>", cards.get(0).getCategory().getName());
        assertEquals("q2", cards.get(1).getQuestion());
        assertEquals("a2", cards.get(1).getAnswer());
        assertEquals("<Standard>", cards.get(1).getCategory().getName());
        assertEquals("q3", cards.get(2).getQuestion());
        assertEquals("a3", cards.get(2).getAnswer());
        assertEquals("<Standard>", cards.get(3).getCategory().getName());
        assertNotNull(cards.get(3).getQuestion());
        assertNotNull(cards.get(3).getAnswer());
        assertEquals("<Standard>", 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) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 8 with AnyMemoDBOpenHelper

use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.

the class QATxtImporterTest method verify.

@Override
protected void verify(String destFilePath) throws Exception {
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
        CardDao cardDao = helper.getCardDao();
        List<Card> cards = cardDao.queryForAll();
        assertEquals(2, cards.size());
        assertEquals("This is question1", cards.get(0).getQuestion());
        assertEquals("Answer1", cards.get(0).getAnswer());
        assertEquals(1, (int) cards.get(0).getOrdinal());
        assertEquals(1, (int) cards.get(0).getId());
        assertEquals("Question2", cards.get(1).getQuestion());
        assertEquals("Answer2", cards.get(1).getAnswer());
        assertEquals(2, (int) cards.get(1).getOrdinal());
        assertEquals(2, (int) cards.get(1).getId());
    } finally {
        helper.close();
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 9 with AnyMemoDBOpenHelper

use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.

the class RecentListFragment method loadRecentItemsWithDetails.

private List<RecentItem> loadRecentItemsWithDetails() {
    final List<RecentItem> ril = loadRecentItemsWithName();
    for (final RecentItem ri : ril) {
        try {
            Context context = getContext();
            if (context == null) {
                break;
            }
            AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), ri.dbPath);
            CardDao dao = helper.getCardDao();
            ri.dbInfo = context.getString(R.string.stat_total) + dao.getTotalCount(null) + " " + getContext().getString(R.string.stat_new) + dao.getNewCardCount(null) + " " + getContext().getString(R.string.stat_scheduled) + dao.getScheduledCardCount(null);
            ril.set(ri.index, ri);
            AnyMemoDBOpenHelperManager.releaseHelper(helper);
        } catch (Exception e) {
            Log.e(TAG, "Recent list throws exception (Usually can be safely ignored)", e);
        }
    }
    return ril;
}
Also used : Context(android.content.Context) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 10 with AnyMemoDBOpenHelper

use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper in project AnyMemo by helloworld1.

the class DatabaseUtil method mergeDatabases.

public void mergeDatabases(String destPath, String srcPath) throws Exception {
    if (destPath.equals(srcPath)) {
        throw new IllegalArgumentException("Cannot merge database into itself");
    }
    AnyMemoDBOpenHelper destHelper = AnyMemoDBOpenHelperManager.getHelper(mContext, destPath);
    AnyMemoDBOpenHelper srcHelper = AnyMemoDBOpenHelperManager.getHelper(mContext, srcPath);
    final CardDao cardDaoDest = destHelper.getCardDao();
    final LearningDataDao learningDataDaoSrc = srcHelper.getLearningDataDao();
    final CategoryDao categoryDaoSrc = srcHelper.getCategoryDao();
    final CardDao cardDaoSrc = srcHelper.getCardDao();
    final List<Card> srcCards = cardDaoSrc.queryForAll();
    cardDaoSrc.callBatchTasks(new Callable<Void>() {

        public Void call() throws Exception {
            for (Card c : srcCards) {
                // Make sure to create a new ordinal
                c.setOrdinal(null);
                learningDataDaoSrc.refresh(c.getLearningData());
                categoryDaoSrc.refresh(c.getCategory());
            }
            return null;
        }
    });
    cardDaoDest.createCards(srcCards);
    System.out.println("DatabaseUtils release destPath");
    AnyMemoDBOpenHelperManager.releaseHelper(destHelper);
    System.out.println("DatabaseUtils release srcPath");
    AnyMemoDBOpenHelperManager.releaseHelper(srcHelper);
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

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