Search in sources :

Example 11 with Category

use of org.liberty.android.fantastischmemo.entity.Category 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 Category

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

the class MnemosyneXMLImporter method startElement.

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("mnemosyne")) {
        try {
            timeOfStart = Long.parseLong(atts.getValue("time_of_start"));
            /* Convert to local time */
            /*
                Calendar nc = Calendar.getInstance();
                TimeZone tz = nc.getTimeZone();
                int offset = tz.getOffset(timeOfStart);
                timeOfStart -= offset;
                */
            Log.v(TAG, "Time of start: " + timeOfStart);
        } catch (Exception e) {
            Log.e(TAG, "parse time_of_start error", e);
        }
    }
    if (localName.equals("item")) {
        card = new Card();
        LearningData learningData = new LearningData();
        card.setLearningData(learningData);
        card.setCategory(new Category());
        card.setId(count);
        card.setOrdinal(count);
        count += 1;
        // Id field is not used here. We use "count" variable instead
        // String idAttr = atts.getValue("id");
        String grAttr = atts.getValue("gr");
        if (grAttr != null) {
            learningData.setGrade(Integer.parseInt(grAttr));
        }
        String eAttr = atts.getValue("e");
        if (eAttr != null) {
            learningData.setEasiness(Float.parseFloat(eAttr));
        }
        String acrpAttr = atts.getValue("ac_rp");
        String uAttr = atts.getValue("u");
        String rtrpAttr = atts.getValue("rt_rp");
        if (rtrpAttr != null) {
            learningData.setRetReps(Integer.parseInt(rtrpAttr));
        }
        if (acrpAttr != null) {
            int acrp = Integer.parseInt(acrpAttr);
            if (uAttr != null) {
                if (Integer.parseInt(uAttr) == 1) {
                /* Commented out for testing */
                // acrp = 0;
                }
            }
            if (Integer.valueOf(rtrpAttr) != 0 && acrp == 0) {
                /* This is a workaround for the malformed
                     * XML file.
                     */
                acrp = Integer.valueOf(rtrpAttr) / 2 + 1;
            }
            learningData.setAcqReps(acrp);
        }
        String lpsAttr = atts.getValue("lps");
        if (lpsAttr != null) {
            learningData.setLapses(Integer.parseInt(lpsAttr));
        }
        String acqrplAttr = atts.getValue("ac_rp_l");
        if (acqrplAttr != null) {
            learningData.setAcqRepsSinceLapse(Integer.parseInt(acqrplAttr));
        }
        String rtrplAttr = atts.getValue("rt_rp_l");
        if (rtrplAttr != null) {
            learningData.setRetRepsSinceLapse(Integer.parseInt(rtrplAttr));
        }
        String lrpAttr = atts.getValue("l_rp");
        if (lrpAttr != null && timeOfStart != 0L) {
        // Do nothing now
        }
        String nrpAttr = atts.getValue("n_rp");
        if (nrpAttr != null && lrpAttr != null) {
            long lrp = timeOfStart * 1000 + Math.round(Double.parseDouble(lrpAttr)) * MILLSECS_PER_DAY;
            long nrp = timeOfStart * 1000 + Math.round(Double.parseDouble(nrpAttr)) * MILLSECS_PER_DAY;
            learningData.setLastLearnDate(new Date(lrp));
            learningData.setNextLearnDate(new Date(nrp));
        }
    }
    characterBuf = new StringBuffer();
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) SAXException(org.xml.sax.SAXException) Date(java.util.Date) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 13 with Category

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

the class QATxtImporter method convert.

public void convert(String src, String dest) throws Exception {
    if (!new File(dest).exists()) {
        amFileUtil.createDbFileWithDefaultSettings(new File(dest));
    }
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
    try {
        final CardDao cardDao = helper.getCardDao();
        BufferedReader txtfile = new BufferedReader(new FileReader(src));
        String line;
        int count = 0;
        List<Card> cardList = new LinkedList<Card>();
        boolean isQ = false;
        StringBuffer qBuf = null;
        StringBuffer aBuf = null;
        while ((line = txtfile.readLine()) != null) {
            /* remove BOM */
            line = line.replace("\uFEFF", "");
            String head = "";
            if (line.length() >= 2) {
                head = line.substring(0, 2);
            }
            if (line.equals("")) {
                continue;
            } else if (head.equals("Q:")) {
                if (isQ == true) {
                    /* next line */
                    qBuf.append("<br />" + line.replaceAll("Q:\\s*", ""));
                } else {
                    isQ = true;
                    /* Save item when the Q is after A
                         * because it is a new item */
                    if (count != 0) {
                        Card card = new Card();
                        card.setQuestion(qBuf.toString());
                        card.setAnswer(aBuf.toString());
                        card.setCategory(new Category());
                        card.setLearningData(new LearningData());
                        cardList.add(card);
                    }
                    count += 1;
                    qBuf = new StringBuffer();
                    qBuf.append(line.replaceAll("Q:\\s*", ""));
                }
            } else if (head.equals("A:")) {
                if (isQ == true) {
                    isQ = false;
                    aBuf = new StringBuffer();
                    aBuf.append(line.replaceAll("A:\\s*", ""));
                } else {
                    aBuf.append("<br />" + line.replaceAll("A:\\s*", ""));
                }
            } else {
                if (isQ) {
                    qBuf.append("<br />" + line);
                } else {
                    aBuf.append("<br />" + line);
                }
            }
        }
        /* Last item need to be added manually */
        count += 1;
        Card card = new Card();
        card.setQuestion(qBuf.toString());
        card.setAnswer(aBuf.toString());
        card.setCategory(new Category());
        card.setLearningData(new LearningData());
        cardList.add(card);
        txtfile.close();
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) LinkedList(java.util.LinkedList) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 14 with Category

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

the class Supermemo2008XMLImporter method startElement.

public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("Question")) {
        characterBuf = new StringBuffer();
        card = new Card();
        card.setLearningData(new LearningData());
        card.setCategory(new Category());
    }
    if (localName.equals("Answer")) {
        characterBuf = new StringBuffer();
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 15 with Category

use of org.liberty.android.fantastischmemo.entity.Category 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)

Aggregations

Category (org.liberty.android.fantastischmemo.entity.Category)35 Card (org.liberty.android.fantastischmemo.entity.Card)30 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)27 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)19 SmallTest (android.support.test.filters.SmallTest)17 Test (org.junit.Test)17 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)17 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)15 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)11 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 LinkedList (java.util.LinkedList)2 QueueManager (org.liberty.android.fantastischmemo.queue.QueueManager)2 Intent (android.content.Intent)1