Search in sources :

Example 21 with CategoryDao

use of org.liberty.android.fantastischmemo.dao.CategoryDao in project AnyMemo by helloworld1.

the class CSVExporter method convert.

@Override
public void convert(String src, String dest) throws Exception {
    new File(dest).delete();
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
    final CardDao cardDao = helper.getCardDao();
    final CategoryDao categoryDao = helper.getCategoryDao();
    CSVWriter writer;
    if (separator == null) {
        writer = new CSVWriter(new FileWriter(dest));
    } else {
        writer = new CSVWriter(new FileWriter(dest), separator);
    }
    try {
        final List<Card> cardList = cardDao.queryForAll();
        // Populate all category field in a transaction.
        categoryDao.callBatchTasks(new Callable<Void>() {

            public Void call() throws Exception {
                for (Card c : cardList) {
                    categoryDao.refresh(c.getCategory());
                }
                return null;
            }
        });
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
        if (cardList.size() == 0) {
            throw new IOException("Can't retrieve cards for database: " + src);
        }
        String[] entries = new String[4];
        for (Card card : cardList) {
            entries[0] = card.getQuestion();
            entries[1] = card.getAnswer();
            entries[2] = card.getCategory().getName();
            entries[3] = card.getNote();
            writer.writeNext(entries);
        }
    } finally {
        writer.close();
    }
}
Also used : CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) FileWriter(java.io.FileWriter) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) IOException(java.io.IOException) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) File(java.io.File) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 22 with CategoryDao

use of org.liberty.android.fantastischmemo.dao.CategoryDao in project AnyMemo by helloworld1.

the class TabTxtImporterTest 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 23 with CategoryDao

use of org.liberty.android.fantastischmemo.dao.CategoryDao in project AnyMemo by helloworld1.

the class CardDaoTest method testSearchLastOrdinalWithcategoryIfExists.

@SmallTest
@Test
public void testSearchLastOrdinalWithcategoryIfExists() throws Exception {
    setupThreeCategories();
    CardDao cardDao = helper.getCardDao();
    CategoryDao categoryDao = helper.getCategoryDao();
    List<Category> cts = categoryDao.queryForEq("name", "My category");
    Category ct = cts.get(0);
    Card c = cardDao.queryLastOrdinal(ct);
    assertEquals(8, (int) c.getId());
}
Also used : 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) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

Aggregations

CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)23 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)22 Card (org.liberty.android.fantastischmemo.entity.Card)20 Category (org.liberty.android.fantastischmemo.entity.Category)19 SmallTest (android.support.test.filters.SmallTest)13 Test (org.junit.Test)13 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)13 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)10 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)8 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)5 FileWriter (java.io.FileWriter)3 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 CSVWriter (com.opencsv.CSVWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1