Search in sources :

Example 1 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 2 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 {
    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)

Example 3 with AnyMemoDBOpenHelper

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

the class DatabaseUtilsTest method testMergeDatabase.

@SmallTest
@Test
@SuppressWarnings("unused")
public void testMergeDatabase() throws Exception {
    // Create testing DB to merge
    String path2 = "/sdcard/testdb2.db";
    new File(path2).delete();
    AnyMemoDBOpenHelper helper2 = AnyMemoDBOpenHelperManager.getHelper(getContext(), path2);
    // DAOs to use
    CardDao destCardDao = helper.getCardDao();
    CategoryDao destCategoryDao = helper.getCategoryDao();
    LearningDataDao destLearningDataDao = helper.getLearningDataDao();
    CardDao srcCardDao = helper2.getCardDao();
    CategoryDao srcCategoryDao = helper2.getCategoryDao();
    LearningDataDao srcLearningDataDao = helper2.getLearningDataDao();
    // Create some categories in both db to test category merge
    Category destCat1 = destCategoryDao.createOrReturn("cat1");
    Category destCat2 = destCategoryDao.createOrReturn("cat2");
    Category srcCat2 = srcCategoryDao.createOrReturn("cat2");
    Category srcCat3 = srcCategoryDao.createOrReturn("cat3");
    // Modify the learning data and category for some cards in src
    Card destCard20 = destCardDao.queryForId(20);
    destCategoryDao.refresh(destCard20.getCategory());
    destLearningDataDao.refresh(destCard20.getLearningData());
    destCard20.setCategory(destCat1);
    LearningData destLd20 = destCard20.getLearningData();
    // Use a specific acq_reps for testing
    destLd20.setAcqReps(15);
    destLearningDataDao.update(destLd20);
    destCardDao.update(destCard20);
    // Another card for testing duplicated category in src and dest
    Card destCard21 = destCardDao.queryForId(21);
    destCategoryDao.refresh(destCard21.getCategory());
    destLearningDataDao.refresh(destCard21.getLearningData());
    destCard21.setCategory(destCat2);
    LearningData destLd21 = destCard21.getLearningData();
    destLd21.setAcqReps(16);
    destLearningDataDao.update(destLd21);
    destCardDao.update(destCard21);
    // Now set up the cards in the src db
    // Card 1 with a duplicated category as dest db
    Card srcCard1 = new Card();
    srcCard1.setQuestion("card1");
    LearningData srcLd1 = new LearningData();
    srcLd1.setAcqReps(8);
    srcCard1.setLearningData(srcLd1);
    srcCard1.setCategory(srcCat2);
    srcCardDao.createCard(srcCard1);
    // Card 2 with a new category
    Card srcCard2 = new Card();
    srcCard2.setQuestion("card2");
    LearningData srcLd2 = new LearningData();
    srcLd2.setAcqReps(10);
    srcCard2.setLearningData(srcLd2);
    srcCard2.setCategory(srcCat3);
    srcCardDao.createCard(srcCard2);
    // Card 3 with default lenaring data and category
    Card srcCard3 = new Card();
    srcCard3.setQuestion("card3");
    srcCard3.setCategory(new Category());
    srcCard3.setLearningData(new LearningData());
    srcCardDao.createCard(srcCard3);
    // Now merge them!
    databaseUtil.mergeDatabases(TestHelper.SAMPLE_DB_PATH, path2);
    // Original 28 plus 3 merged cards
    assertEquals(31, destCardDao.queryForAll().size());
    assertEquals(31, destLearningDataDao.queryForAll().size());
    // 5 categories: "", "french-body-parts.db", "cat1", "cat2", "cat3"
    assertEquals(5, destCategoryDao.queryForAll().size());
    // Query out the cards we are interested in
    Card mergedCard20 = destCardDao.queryForId(20);
    destLearningDataDao.refresh(mergedCard20.getLearningData());
    destCategoryDao.refresh(mergedCard20.getCategory());
    Card mergedCard21 = destCardDao.queryForId(21);
    destLearningDataDao.refresh(mergedCard21.getLearningData());
    destCategoryDao.refresh(mergedCard21.getCategory());
    Card mergedCard29 = destCardDao.queryForId(29);
    destLearningDataDao.refresh(mergedCard29.getLearningData());
    destCategoryDao.refresh(mergedCard29.getCategory());
    Card mergedCard30 = destCardDao.queryForId(30);
    destLearningDataDao.refresh(mergedCard30.getLearningData());
    destCategoryDao.refresh(mergedCard30.getCategory());
    Card mergedCard31 = destCardDao.queryForId(31);
    destLearningDataDao.refresh(mergedCard31.getLearningData());
    destCategoryDao.refresh(mergedCard31.getCategory());
    // Now verify
    // The original cards in dest should be intact
    assertEquals(destCard20.getOrdinal(), mergedCard20.getOrdinal());
    assertEquals(destCard20.getQuestion(), mergedCard20.getQuestion());
    assertEquals("cat1", mergedCard20.getCategory().getName());
    assertEquals(15, (int) mergedCard20.getLearningData().getAcqReps());
    assertEquals(destCard21.getOrdinal(), mergedCard21.getOrdinal());
    assertEquals(destCard21.getQuestion(), mergedCard21.getQuestion());
    assertEquals("cat2", mergedCard21.getCategory().getName());
    assertEquals(16, (int) mergedCard21.getLearningData().getAcqReps());
    // The newly merged cards
    assertEquals(29, (int) mergedCard29.getOrdinal());
    assertEquals("card1", mergedCard29.getQuestion());
    assertEquals("cat2", mergedCard29.getCategory().getName());
    assertEquals(8, (int) mergedCard29.getLearningData().getAcqReps());
    assertEquals(30, (int) mergedCard30.getOrdinal());
    assertEquals("card2", mergedCard30.getQuestion());
    assertEquals("cat3", mergedCard30.getCategory().getName());
    assertEquals(10, (int) mergedCard30.getLearningData().getAcqReps());
    assertEquals(31, (int) mergedCard31.getOrdinal());
    assertEquals("card3", mergedCard31.getQuestion());
    assertEquals("", mergedCard31.getCategory().getName());
    assertEquals(0, (int) mergedCard31.getLearningData().getAcqReps());
    // Clearn up
    AnyMemoDBOpenHelperManager.releaseHelper(helper2);
    new File(path2).delete();
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Category(org.liberty.android.fantastischmemo.entity.Category) CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) File(java.io.File) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) 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)

Example 4 with AnyMemoDBOpenHelper

use of org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper 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 5 with AnyMemoDBOpenHelper

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

the class Mnemosyne2CardsImporter method convert.

@Override
public void convert(String src, String dest) throws Exception {
    // Make the tmp directory tmp/[src file name]/
    String srcFilename = FilenameUtils.getName(src);
    File tmpDirectory = new File(AMEnv.DEFAULT_TMP_PATH + srcFilename);
    FileUtils.deleteDirectory(tmpDirectory);
    FileUtils.forceMkdir(tmpDirectory);
    AnyMemoDBOpenHelper helper = null;
    try {
        // First unzip the file since cards is just a zip archive
        // Example content of cards
        // $ ls
        // METADATA  cards.xml  musicnotes
        AMZipUtils.unZipFile(new File(src), tmpDirectory);
        // Make sure the XML file exists.
        File xmlFile = new File(tmpDirectory + "/cards.xml");
        if (!xmlFile.exists()) {
            throw new Exception("Could not find the cards.xml after extracting " + src);
        }
        List<Card> cardList = xmlToCards(xmlFile);
        if (!new File(dest).exists()) {
            amFileUtil.createDbFileWithDefaultSettings(new File(dest));
        }
        helper = AnyMemoDBOpenHelperManager.getHelper(dest);
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
        // The last step is to see if there are images to import.
        Collection<File> imageFiles = FileUtils.listFiles(tmpDirectory, new SuffixFileFilter(new String[] { "jpg", "png", "bmp" }, IOCase.INSENSITIVE), DirectoryFileFilter.DIRECTORY);
        if (!imageFiles.isEmpty()) {
            String destDbName = FilenameUtils.getName(dest);
            File imageDir = new File(AMEnv.DEFAULT_IMAGE_PATH + destDbName);
            FileUtils.forceMkdir(imageDir);
            for (File imageFile : imageFiles) {
                FileUtils.copyFileToDirectory(imageFile, imageDir);
            }
        }
    } finally {
        if (helper != null) {
            AnyMemoDBOpenHelperManager.releaseHelper(helper);
        }
        FileUtils.deleteDirectory(tmpDirectory);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) File(java.io.File) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) 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 (android.support.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