Search in sources :

Example 11 with CategoryDao

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

the class Mnemosyne2CardsExporter method createXMLFile.

private void createXMLFile(String dbPath, File xmlFile) throws IOException {
    AnyMemoDBOpenHelper helper = null;
    PrintWriter outXml = null;
    try {
        helper = AnyMemoDBOpenHelperManager.getHelper(dbPath);
        CardDao cardDao = helper.getCardDao();
        CategoryDao categoryDao = helper.getCategoryDao();
        LearningDataDao learningDataDao = helper.getLearningDataDao();
        int cardCount = (int) cardDao.countOf();
        outXml = new PrintWriter(new BufferedWriter(new FileWriter(xmlFile)));
        outXml.printf("<openSM2sync number_of_entries=\"%d\">\n", cardCount);
        // First card tags (categories)
        Map<String, String> categoryOidMap = new HashMap<String, String>();
        Map<Integer, String> cardIdOidMap = new HashMap<Integer, String>(cardCount * 4 / 3);
        Iterator<Category> categoryIterator = categoryDao.iterator();
        while (categoryIterator.hasNext()) {
            Category category = categoryIterator.next();
            String tagName = "__UNTAGGED__";
            String oId = generateOid();
            if (!Strings.isNullOrEmpty(category.getName())) {
                tagName = category.getName();
            }
            categoryOidMap.put(tagName, oId);
            outXml.printf("<log type=\"10\" o_id=\"%s\"><name>%s</name></log>\n", oId, AMStringUtils.encodeXML(tagName));
        }
        // Then cards
        Iterator<Card> cardIterator = cardDao.iterator();
        while (cardIterator.hasNext()) {
            Card card = cardIterator.next();
            String front = card.getQuestion();
            String back = card.getAnswer();
            String oId = generateOid();
            cardIdOidMap.put(card.getId(), oId);
            outXml.printf("<log type=\"16\" o_id=\"%s\"><b>%s</b><f>%s</f></log>\n", oId, AMStringUtils.encodeXML(back), AMStringUtils.encodeXML(front));
        }
        // Then learningData
        // <log card_t="1" fact_v="1.1" e="2.5" gr="-1" tags="5SfWDFGwqrlnGLDQxHHyG0" rt_rp_l="0" lps="0" l_rp="-1" n_rp="-1" ac_rp_l="0" rt_rp="0" ac_rp="0" type="6" o_id="7IXjCysHuCDtXo8hlFrK55" fact="7xmRCBH0WP0DZaxeFn5NLw"></log>
        Iterator<Card> cardIterator2 = cardDao.iterator();
        while (cardIterator2.hasNext()) {
            Card card = cardIterator2.next();
            categoryDao.refresh(card.getCategory());
            learningDataDao.refresh(card.getLearningData());
            String fact = cardIdOidMap.get(card.getId());
            String category = card.getCategory().getName();
            if (Strings.isNullOrEmpty(category)) {
                category = "__UNTAGGED__";
            }
            String tags = categoryOidMap.get(category);
            String oId = generateOid();
            // Needs to converter to unix time
            LearningData learningData = card.getLearningData();
            long l_rp = learningData.getLastLearnDate().getTime() / 1000;
            long n_rp = learningData.getNextLearnDate().getTime() / 1000;
            outXml.printf("<log card_t=\"1\" fact_v=\"1.1\" e=\"%f\" gr=\"%d\" tags=\"%s\" rt_rp_l=\"%d\" lps=\"%d\" l_rp=\"%d\" n_rp=\"%d\" ac_rp_l=\"%d\" rt_rp=\"%d\" ac_rp=\"%d\" type=\"6\" o_id=\"%s\" fact=\"%s\"></log>\n", learningData.getEasiness(), learningData.getGrade(), tags, learningData.getRetRepsSinceLapse(), learningData.getLapses(), l_rp, n_rp, learningData.getAcqRepsSinceLapse(), learningData.getRetReps(), learningData.getAcqReps(), oId, fact);
        }
        outXml.print("</openSM2sync>\n");
    } finally {
        if (helper != null) {
            AnyMemoDBOpenHelperManager.releaseHelper(helper);
        }
        if (outXml != null) {
            outXml.close();
        }
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) BufferedWriter(java.io.BufferedWriter) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) PrintWriter(java.io.PrintWriter)

Example 12 with CategoryDao

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

the class MnemosyneXMLExporter method convert.

public void convert(String src, String dest) throws Exception {
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
    String dbName = FilenameUtils.getName(dest);
    PrintWriter outxml = null;
    try {
        final CardDao cardDao = helper.getCardDao();
        final LearningDataDao learningDataDao = helper.getLearningDataDao();
        final CategoryDao categoryDao = helper.getCategoryDao();
        // Populate all category field in a transaction.
        List<Card> cardList = cardDao.callBatchTasks(new Callable<List<Card>>() {

            public List<Card> call() throws Exception {
                List<Card> cards = cardDao.queryForAll();
                for (Card c : cards) {
                    categoryDao.refresh(c.getCategory());
                    learningDataDao.refresh(c.getLearningData());
                }
                return cards;
            }
        });
        if (cardList == null || cardList.size() == 0) {
            throw new IOException("Read empty or corrupted file");
        }
        outxml = new PrintWriter(new BufferedWriter(new FileWriter(dest)));
        if (outxml.checkError()) {
            throw new IOException("Can't open: " + dest);
        }
        int count = 0;
        long timeOfStart = 0L;
        String id, u, gr, e, ac_rp, rt_rp, lps, ac_rp_l, rt_rp_l, l_rp, n_rp, question, answer, category;
        // Now write the xml to the file
        for (Card card : cardList) {
            // At the first item, we write all metadata
            if (count == 0) {
                // timeOfStart = item.getDatelearnUnix();
                // 2000-01-01 12:00
                timeOfStart = 946728000L;
                outxml.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
                outxml.print("<mnemosyne core_version=\"1\" time_of_start=\"" + timeOfStart + "\" >\n");
                outxml.print("<category active=\"1\">\n");
                outxml.print("<name>" + dbName + "</name>\n");
                outxml.print("</category>\n");
            }
            // Convert the learning data
            LearningData ld = card.getLearningData();
            id = "" + card.getOrdinal();
            gr = "" + ld.getGrade();
            e = "" + ld.getEasiness();
            ac_rp = "" + ld.getAcqReps();
            rt_rp = "" + ld.getRetReps();
            lps = "" + ld.getLapses();
            ac_rp_l = "" + ld.getAcqRepsSinceLapse();
            rt_rp_l = "" + ld.getRetRepsSinceLapse();
            ;
            if (ac_rp.equals("0")) {
                u = "1";
            } else {
                u = "0";
            }
            // Add 1 here to avoid rounding problem
            l_rp = Long.toString((ld.getLastLearnDate().getTime() / 1000 - timeOfStart) / 86400);
            n_rp = Long.toString((ld.getNextLearnDate().getTime() / 1000 - timeOfStart) / 86400 + 1);
            // Replace the illegal symbols from the question and answer
            question = card.getQuestion();
            answer = card.getAnswer();
            category = card.getCategory().getName();
            if (question == null) {
                question = "";
            }
            if (answer == null) {
                answer = "";
            }
            if (category == null) {
                category = "";
            }
            question = question.replaceAll("&", "&amp;");
            question = question.replaceAll("<", "&lt;");
            question = question.replaceAll(">", "&gt;");
            question = question.replaceAll("'", "&apos;");
            question = question.replaceAll("\"", "&quot;");
            answer = answer.replaceAll("&", "&amp;");
            answer = answer.replaceAll("<", "&lt;");
            answer = answer.replaceAll(">", "&gt;");
            answer = answer.replaceAll("'", "&apos;");
            answer = answer.replaceAll("\"", "&quot;");
            category = category.replaceAll("&", "&amp;");
            category = category.replaceAll("<", "&lt;");
            category = category.replaceAll(">", "&gt;");
            category = category.replaceAll("'", "&apos;");
            category = category.replaceAll("\"", "&quot;");
            outxml.print("<item id=\"" + id + "\" u=\"" + u + "\" gr=\"" + gr + "\" e=\"" + e + "\" ac_rp=\"" + ac_rp + "\" rt_rp=\"" + rt_rp + "\" lps=\"" + lps + "\" ac_rp_l=\"" + ac_rp_l + "\" rt_rp_l=\"" + rt_rp_l + "\" l_rp=\"" + l_rp + "\" n_rp=\"" + n_rp + "\">\n");
            if (category.equals("")) {
                outxml.print("<cat>" + dbName + "</cat>\n");
            } else {
                outxml.print("<cat>" + category + "</cat>\n");
            }
            outxml.print("<Q>" + question + "</Q>\n");
            outxml.print("<A>" + answer + "</A>\n");
            outxml.print("</item>\n");
            if (outxml.checkError()) {
                throw new IOException("Error writing xml on id: " + id);
            }
            count += 1;
        }
        outxml.print("</mnemosyne>");
        outxml.close();
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
        if (outxml != null) {
            outxml.close();
        }
    }
}
Also used : CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) FileWriter(java.io.FileWriter) IOException(java.io.IOException) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) IOException(java.io.IOException) Card(org.liberty.android.fantastischmemo.entity.Card) BufferedWriter(java.io.BufferedWriter) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) List(java.util.List) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) PrintWriter(java.io.PrintWriter)

Example 13 with CategoryDao

use of org.liberty.android.fantastischmemo.dao.CategoryDao 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 14 with CategoryDao

use of org.liberty.android.fantastischmemo.dao.CategoryDao 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 15 with CategoryDao

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

the class CardDaoTest method testQueryPrevCardWithCategory.

@SmallTest
@Test
public void testQueryPrevCardWithCategory() 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 c5 = cardDao.queryForId(5);
    Card c2 = cardDao.queryPrevCard(c5, ct);
    assertEquals(2, (int) c2.getId());
    Card c8 = cardDao.queryPrevCard(c2, ct);
    assertEquals(8, (int) c8.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