Search in sources :

Example 21 with AnyMemoDBOpenHelper

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

the class MnemosyneXMLImporter method convert.

@Override
public void convert(String src, String dest) throws Exception {
    URL mXMLUrl = new URL("file:///" + src);
    cardList = new LinkedList<Card>();
    System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    xr.setContentHandler(this);
    xr.parse(new InputSource(mXMLUrl.openStream()));
    if (!new File(dest).exists()) {
        amFileUtil.createDbFileWithDefaultSettings(new File(dest));
    }
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
    try {
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : InputSource(org.xml.sax.InputSource) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) URL(java.net.URL) XMLReader(org.xml.sax.XMLReader) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 22 with AnyMemoDBOpenHelper

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

the class CSVImporter 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);
    CSVReader reader;
    if (separator == null) {
        reader = new CSVReader(new FileReader(src));
    } else {
        reader = new CSVReader(new FileReader(src), separator);
    }
    try {
        final CardDao cardDao = helper.getCardDao();
        String[] nextLine;
        final List<Card> cardList = new LinkedList<Card>();
        for (int lineNo = 1; (nextLine = reader.readNext()) != null; ++lineNo) {
            if (nextLine.length == 1 && nextLine[0].isEmpty()) {
                // ignore blank lines
                continue;
            }
            if (nextLine.length < 2) {
                Log.w(TAG, "Malformed CSV file at line " + lineNo + ": " + Arrays.toString(nextLine));
                throw new Exception("Malformed CSV file at line " + lineNo + ". Please make sure the CSV's first column is question, second one is answer, the optional third is category, and the optional fourth one is note");
            }
            String note = "";
            String category = "";
            if (nextLine.length >= 3) {
                category = nextLine[2];
            }
            if (nextLine.length >= 4) {
                note = nextLine[3];
            }
            Card card = new Card();
            Category cat = new Category();
            LearningData ld = new LearningData();
            cat.setName(category);
            card.setCategory(cat);
            card.setLearningData(ld);
            card.setQuestion(nextLine[0]);
            card.setAnswer(nextLine[1]);
            card.setNote(note);
            cardList.add(card);
        }
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
        reader.close();
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) CSVReader(com.opencsv.CSVReader) LinkedList(java.util.LinkedList) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) FileReader(java.io.FileReader) File(java.io.File) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 23 with AnyMemoDBOpenHelper

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

the class Supermemo2008XMLImporter method convert.

@Override
public void convert(String src, String dest) throws Exception {
    URL mXMLUrl = new URL("file:///" + src);
    cardList = new LinkedList<Card>();
    characterBuf = new StringBuffer();
    System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver");
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    xr.setContentHandler(this);
    xr.parse(new InputSource(mXMLUrl.openStream()));
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(dest);
    try {
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : InputSource(org.xml.sax.InputSource) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) SAXParser(javax.xml.parsers.SAXParser) URL(java.net.URL) XMLReader(org.xml.sax.XMLReader) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 24 with AnyMemoDBOpenHelper

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

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

the class CardProvider method query.

/**
 * The query returns null if the db is not valid.
 */
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    List<String> uriSegments = uri.getPathSegments();
    String dbPath = AMEnv.DEFAULT_ROOT_PATH + uriSegments.get(0);
    if (!new File(dbPath).exists()) {
        return null;
    }
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), dbPath);
    try {
        CardDao cardDao = helper.getCardDao();
        Cursor resultCursor = null;
        switch(sUriMatcher.match(uri)) {
            case COUNT_URI:
                {
                    long count = cardDao.getTotalCount(null);
                    resultCursor = buildCursorFromCount(count);
                    break;
                }
            case RANDOM_URI:
                {
                    int count = Integer.valueOf(uriSegments.get(2));
                    List<Card> cards = cardDao.getRandomCards(null, count);
                    resultCursor = buildCursorFromCards(cards);
                    break;
                }
            case ORD_URI:
                {
                    int ord = Integer.valueOf(uriSegments.get(2));
                    Card card = cardDao.getByOrdinal(ord);
                    resultCursor = buildCursorFromCard(card);
                    break;
                }
            case ID_URI:
                {
                    int id = Integer.valueOf(uriSegments.get(2));
                    Card card = cardDao.getById(id);
                    resultCursor = buildCursorFromCard(card);
                    break;
                }
            case START_URI:
                {
                    int start = Integer.valueOf(uriSegments.get(2));
                    int count = Integer.valueOf(uriSegments.get(4));
                    List<Card> cards = cardDao.getCardsByOrdinalAndSize(start, count);
                    resultCursor = buildCursorFromCards(cards);
                    break;
                }
            case ALL_URI:
                {
                    List<Card> cards = cardDao.getAllCards(null);
                    resultCursor = buildCursorFromCards(cards);
                    break;
                }
            default:
                throw new IllegalArgumentException("No matching handler for uri: " + uri);
        }
        if (resultCursor == null) {
            Log.e(TAG, "No case matched for uri: " + uri);
        }
        return resultCursor;
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) ArrayList(java.util.ArrayList) List(java.util.List) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) File(java.io.File) 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 (androidx.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