Search in sources :

Example 41 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class ImportMergingTest method testMergeQATxtIntoDb.

@SmallTest
@Test
public void testMergeQATxtIntoDb() throws Exception {
    srcFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "qa-text-test.txt";
    destFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "qa-text-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);
    }
    amFileUtil.copyFileFromAsset("qa-text-test.txt", new File(srcFilePath));
    Converter converter = new QATxtImporter(amFileUtil);
    converter.convert(srcFilePath, destFilePath);
    helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
        List<Card> cards = helper.getCardDao().getAllCards(null);
        assertEquals(4, 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("This is question1", cards.get(2).getQuestion());
        assertEquals("Answer1", cards.get(2).getAnswer());
        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());
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) QATxtImporter(org.liberty.android.fantastischmemo.converter.QATxtImporter) Converter(org.liberty.android.fantastischmemo.converter.Converter) File(java.io.File) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) BaseTest(org.liberty.android.fantastischmemo.test.BaseTest) SmallTest(android.support.test.filters.SmallTest)

Example 42 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class CardDaoImpl method getNewCards.

public List<Card> getNewCards(Category filterCategory, Iterable<Card> exclusion, int limit) {
    try {
        LearningDataDao learningDataDao = getHelper().getLearningDataDao();
        CategoryDao categoryDao = getHelper().getCategoryDao();
        QueryBuilder<LearningData, Integer> learnQb = learningDataDao.queryBuilder();
        learnQb.selectColumns("id");
        learnQb.where().eq("acqReps", "0");
        QueryBuilder<Card, Integer> cardQb = this.queryBuilder();
        // The "isNotNull" statement is dummy so the "and()" can be cascaded
        // for the following conditions
        Where<Card, Integer> where = cardQb.where().isNotNull("learningData_id");
        if (filterCategory != null) {
            where.and().eq("category_id", filterCategory.getId());
        }
        if (exclusion != null) {
            List<Integer> exclusionList = new ArrayList<Integer>();
            for (Card c : exclusion) {
                exclusionList.add(c.getId());
            }
            where.and().notIn("id", exclusionList);
        }
        cardQb.setWhere(where);
        cardQb.join(learnQb).orderByRaw("cards.ordinal").limit((long) limit);
        List<Card> cs = cardQb.query();
        for (Card c : cs) {
            categoryDao.refresh(c.getCategory());
            learningDataDao.refresh(c.getLearningData());
        }
        return cs;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Card(org.liberty.android.fantastischmemo.entity.Card) LearningData(org.liberty.android.fantastischmemo.entity.LearningData)

Example 43 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class CardDaoImpl method getCardsByOrdinalAndSize.

public List<Card> getCardsByOrdinalAndSize(long startOrd, long size) {
    LearningDataDao learningDataDao = getHelper().getLearningDataDao();
    QueryBuilder<Card, Integer> qb = queryBuilder();
    qb.limit(size);
    try {
        Where<Card, Integer> where = qb.where().ge("ordinal", startOrd);
        qb.setWhere(where);
        qb.orderBy("ordinal", true);
        PreparedQuery<Card> pq = qb.prepare();
        List<Card> result = query(pq);
        for (Card c : result) {
            learningDataDao.refresh(c.getLearningData());
        }
        return result;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 44 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class CardDaoImpl method create.

@Override
public int create(Card c) {
    try {
        Integer cardOrdinal = c.getOrdinal();
        // Null ordinal means we need to put the max oridinal + 1 here
        if (cardOrdinal == null) {
            Card last = queryLastOrdinal();
            // If it is a new db the last oridinal will be null.
            if (last == null) {
                cardOrdinal = 1;
            } else {
                cardOrdinal = last.getOrdinal() + 1;
            }
            c.setOrdinal(cardOrdinal);
        } else {
            // We are adding the card at the middle. Should update other card's ordinal.
            UpdateBuilder<Card, Integer> updateBuilder = updateBuilder();
            updateBuilder.updateColumnExpression("ordinal", "ordinal + 1");
            updateBuilder.where().ge("ordinal", cardOrdinal).prepare();
            update(updateBuilder.prepare());
        }
        int res = super.create(c);
        return res;
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 45 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class CardPlayerActivity method gotoCardId.

// Query out the card id and display that card
protected void gotoCardId(final int cardId) {
    Card card = cardDao.queryForId(cardId);
    gotoCard(card);
}
Also used : Card(org.liberty.android.fantastischmemo.entity.Card)

Aggregations

Card (org.liberty.android.fantastischmemo.entity.Card)95 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)61 SmallTest (android.support.test.filters.SmallTest)43 Test (org.junit.Test)43 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)41 Category (org.liberty.android.fantastischmemo.entity.Category)30 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)28 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)25 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)20 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)13 File (java.io.File)12 ArrayList (java.util.ArrayList)11 SQLException (java.sql.SQLException)9 QueueManager (org.liberty.android.fantastischmemo.queue.QueueManager)9 IOException (java.io.IOException)6 Date (java.util.Date)6 LearnQueueManager (org.liberty.android.fantastischmemo.queue.LearnQueueManager)6 URL (java.net.URL)5 FileWriter (java.io.FileWriter)4 Scheduler (org.liberty.android.fantastischmemo.scheduler.Scheduler)4