Search in sources :

Example 51 with CardDao

use of org.liberty.android.fantastischmemo.dao.CardDao 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>();
        while ((nextLine = reader.readNext()) != null) {
            if (nextLine.length < 2) {
                throw new Exception("Malformed CSV file. Please make sure the CSV's first column is question, second one is answer and the optinal third one is category");
            }
            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 52 with CardDao

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

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

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

the class SupermemoXMLImporter 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 55 with CardDao

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

the class CellsDBConverter method convertCellsToDb.

/* cardCells contains the question, answer, category and note
     * category and note is optionally.
     * learningDataCells contains all necessary learning data.
     * If learningDataCells, new learning data is used.
     * dbPath is the place to store converted database
     */
public void convertCellsToDb(Cells cardCells, Cells learningDataCells, String dbPath) throws IOException {
    int numberOfRows = cardCells.getRowCounts();
    int numberOfLearningDataRows = 0;
    if (learningDataCells != null) {
        numberOfLearningDataRows = learningDataCells.getRowCounts();
    }
    // We ignore the header row
    List<Card> cardList = new ArrayList<Card>(numberOfRows + 1);
    for (int i = 1; i < numberOfRows; i++) {
        List<String> row = cardCells.getRow(i);
        Card card = new Card();
        Category category = new Category();
        if (row.size() == 0) {
            Log.w(TAG, "Each row in spreadsheet should have at least 2 column: question and answer. Row number: " + i);
        }
        if (row.size() >= 1) {
            card.setQuestion(row.get(0));
        }
        if (row.size() >= 2) {
            card.setAnswer(row.get(1));
        }
        if (row.size() >= 3) {
            category.setName(row.get(2));
        }
        if (row.size() >= 4) {
            card.setNote(row.get(3));
        }
        // This can't be null because numberOfLearningDataRows is 0
        // if learningDataCells is 0.
        LearningData learningData;
        if (i < numberOfLearningDataRows) {
            learningData = getLearningDataFromRow(learningDataCells.getRow(i));
        } else {
            learningData = new LearningData();
        }
        card.setCategory(category);
        card.setLearningData(learningData);
        cardList.add(card);
    }
    if (cardList.size() == 0) {
        throw new IOException("Wrong spreadsheet format. The spreadsheet should contain at least 1 worksheet with at least 2 columns of questions and answers.");
    }
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, dbPath);
    try {
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Category(org.liberty.android.fantastischmemo.entity.Category) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Aggregations

CardDao (org.liberty.android.fantastischmemo.dao.CardDao)66 Card (org.liberty.android.fantastischmemo.entity.Card)61 SmallTest (android.support.test.filters.SmallTest)37 Test (org.junit.Test)37 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)37 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)28 Category (org.liberty.android.fantastischmemo.entity.Category)27 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)22 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)15 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)13 File (java.io.File)10 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)6 URL (java.net.URL)5 FileWriter (java.io.FileWriter)4 Date (java.util.Date)4 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3