Search in sources :

Example 31 with CardDao

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

the class CardDaoTest method testGetByOrdinal.

@SmallTest
@Test
public void testGetByOrdinal() {
    CardDao cardDao = helper.getCardDao();
    Card card = cardDao.getByOrdinal(3);
    assertEquals(3, (int) card.getOrdinal());
}
Also used : 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 32 with CardDao

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

the class CardDaoTest method testQueryNextCardWithoutCategory.

@SmallTest
@Test
public void testQueryNextCardWithoutCategory() throws Exception {
    setupThreeCategories();
    CardDao cardDao = helper.getCardDao();
    Card c27 = cardDao.queryForId(27);
    Card c28 = cardDao.queryNextCard(c27, null);
    assertEquals(28, (int) c28.getOrdinal());
    Card c1 = cardDao.queryNextCard(c28, null);
    assertEquals(1, (int) c1.getOrdinal());
}
Also used : 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 33 with CardDao

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

the class CardDaoTest method testSwapAllQA.

@SmallTest
@Test
public void testSwapAllQA() throws Exception {
    CardDao cardDao = helper.getCardDao();
    // Randomly sample 2 cards
    Card c8 = cardDao.queryForId(8);
    Card c18 = cardDao.queryForId(18);
    String question8 = c8.getQuestion();
    String answer8 = c8.getAnswer();
    String question18 = c18.getQuestion();
    String answer18 = c18.getAnswer();
    cardDao.swapAllQA();
    c8 = cardDao.queryForId(8);
    c18 = cardDao.queryForId(18);
    assertEquals(answer8, c8.getQuestion());
    assertEquals(question8, c8.getAnswer());
    assertEquals(answer18, c18.getQuestion());
    assertEquals(question18, c18.getAnswer());
}
Also used : 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 34 with CardDao

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

the class CardDaoTest method testGetRandomCardsWithoutCategory.

@SmallTest
@Test
public void testGetRandomCardsWithoutCategory() throws Exception {
    CardDao cardDao = helper.getCardDao();
    // limit higher than total number of cards
    List<Card> cards = cardDao.getRandomCards(null, 50);
    assertEquals(28, cards.size());
    // limit lower than total number of cards
    List<Card> cards2 = cardDao.getRandomCards(null, 10);
    assertEquals(10, cards2.size());
}
Also used : 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 35 with CardDao

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

the class GoogleDriveUploadHelper method createSpreadsheet.

public Spreadsheet createSpreadsheet(String title, String dbPath) throws Exception {
    // First read card because if it failed we don't even bother uploading.
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(mContext, dbPath);
    List<Card> cardList = null;
    try {
        final CardDao cardDao = helper.getCardDao();
        final CategoryDao categoryDao = helper.getCategoryDao();
        final LearningDataDao learningDataDao = helper.getLearningDataDao();
        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;
            }
        });
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
    // Find the spreadsheets to delete after the process is done
    List<Document> spreadsheetsToDelete = DocumentFactory.findDocuments(title, authToken);
    // Create the AnyMemo folder if needed
    Folder folder = FolderFactory.createOrReturnFolder("AnyMemo", authToken);
    // Create new spreadsheet
    Document newSpreadsheetDocument = DocumentFactory.createSpreadsheet(title, authToken);
    List<Spreadsheet> spreadsheetList = SpreadsheetFactory.getSpreadsheets(authToken);
    Spreadsheet newSpreadsheet = spreadsheetList.get(0);
    // Create worksheets
    List<Worksheet> worksheetsToDelete = WorksheetFactory.getWorksheets(newSpreadsheet, authToken);
    // Delete the worksheets with the duplicated name
    // First create a dummy worksheet so we have at least one worksheet
    boolean needsToCreateDummy = true;
    for (Worksheet ws : worksheetsToDelete) {
        if (ws.getTitle().equals("dummy")) {
            needsToCreateDummy = false;
            break;
        }
    }
    // Dummy worksheet will be deleted at the last step
    if (needsToCreateDummy) {
        Worksheet dummyWorksheet = WorksheetFactory.createWorksheet(newSpreadsheet, "dummy", 1, 1, authToken);
        worksheetsToDelete.add(dummyWorksheet);
    }
    Iterator<Worksheet> worksheetToDeleteIterator = worksheetsToDelete.iterator();
    while (worksheetToDeleteIterator.hasNext()) {
        Worksheet ws = worksheetToDeleteIterator.next();
        if (ws.getTitle().equals("cards")) {
            WorksheetFactory.deleteWorksheet(newSpreadsheet, ws, authToken);
            worksheetToDeleteIterator.remove();
        }
        if (ws.getTitle().equals("learning_data")) {
            WorksheetFactory.deleteWorksheet(newSpreadsheet, ws, authToken);
            worksheetToDeleteIterator.remove();
        }
    }
    // setting up the worksheet size is critical.
    Worksheet cardsWorksheet = WorksheetFactory.createWorksheet(newSpreadsheet, "cards", cardList.size() + 1, 4, authToken);
    Cells cardCells = new Cells();
    // Add the header for cards first
    cardCells.addCell(1, 1, "question");
    cardCells.addCell(1, 2, "answer");
    cardCells.addCell(1, 3, "category");
    cardCells.addCell(1, 4, "note");
    for (int i = 0; i < cardList.size(); i++) {
        Card c = cardList.get(i);
        // THe first row is the header.
        cardCells.addCell(i + 2, 1, c.getQuestion());
        cardCells.addCell(i + 2, 2, c.getAnswer());
        cardCells.addCell(i + 2, 3, c.getCategory().getName());
        cardCells.addCell(i + 2, 4, c.getNote());
    }
    // upload card's rows into worksheet
    CellsFactory.uploadCells(newSpreadsheet, cardsWorksheet, cardCells, authToken);
    // Let GC free up memory
    cardCells = null;
    // Now deal with learning data
    Worksheet learningDataWorksheet = WorksheetFactory.createWorksheet(newSpreadsheet, "learning_data", cardList.size() + 1, 9, authToken);
    Cells learningDataCells = new Cells();
    // The first row is the header.
    learningDataCells.addCell(1, 1, "acqReps");
    learningDataCells.addCell(1, 2, "acqRepsSinceLapse");
    learningDataCells.addCell(1, 3, "easiness");
    learningDataCells.addCell(1, 4, "grade");
    learningDataCells.addCell(1, 5, "lapses");
    learningDataCells.addCell(1, 6, "lastLearnDate");
    learningDataCells.addCell(1, 7, "nextLearnDate");
    learningDataCells.addCell(1, 8, "retReps");
    learningDataCells.addCell(1, 9, "retRepsSinceLapse");
    for (int i = 0; i < cardList.size(); i++) {
        LearningData ld = cardList.get(i).getLearningData();
        learningDataCells.addCell(i + 2, 1, Integer.toString(ld.getAcqReps()));
        learningDataCells.addCell(i + 2, 2, Integer.toString(ld.getAcqRepsSinceLapse()));
        learningDataCells.addCell(i + 2, 3, Float.toString(ld.getEasiness()));
        learningDataCells.addCell(i + 2, 4, Integer.toString(ld.getGrade()));
        learningDataCells.addCell(i + 2, 5, Integer.toString(ld.getLapses()));
        learningDataCells.addCell(i + 2, 6, ISO8601_FORMATTER.format(ld.getLastLearnDate()));
        learningDataCells.addCell(i + 2, 7, ISO8601_FORMATTER.format(ld.getNextLearnDate()));
        learningDataCells.addCell(i + 2, 8, Integer.toString(ld.getRetReps()));
        learningDataCells.addCell(i + 2, 9, Integer.toString(ld.getRetRepsSinceLapse()));
    }
    // upload learning data rows into worksheet
    CellsFactory.uploadCells(newSpreadsheet, learningDataWorksheet, learningDataCells, authToken);
    learningDataCells = null;
    // Put new spreadsheet into the folder
    FolderFactory.addDocumentToFolder(newSpreadsheetDocument, folder, authToken);
    // Finally delete the unneeded worksheets ...
    for (Worksheet ws : worksheetsToDelete) {
        WorksheetFactory.deleteWorksheet(newSpreadsheet, ws, authToken);
    }
    // ... And spreadsheets with duplicated names.
    for (Document ss : spreadsheetsToDelete) {
        DocumentFactory.deleteDocument(ss, authToken);
    }
    return null;
}
Also used : CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) Callable(java.util.concurrent.Callable) 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)

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