Search in sources :

Example 16 with CategoryDao

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

Example 17 with CategoryDao

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

the class CsvImporterTest 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 18 with CategoryDao

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

the class CardDaoTest method testGetAllCardWithFilteringCategory.

@SmallTest
@Test
public void testGetAllCardWithFilteringCategory() throws SQLException {
    setupThreeCategories();
    CardDao cardDao = helper.getCardDao();
    CategoryDao categoryDao = helper.getCategoryDao();
    List<Category> cts = categoryDao.queryForEq("name", "My category");
    Category ct = cts.get(0);
    List<Card> cards = cardDao.getAllCards(ct);
    assertEquals(3, (int) cards.size());
}
Also used : 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) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) SmallTest(android.support.test.filters.SmallTest)

Example 19 with CategoryDao

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

the class CardDaoTest method testReviewCardsOrderOfAllDifferentEasiness.

@SmallTest
@Test
public void testReviewCardsOrderOfAllDifferentEasiness() throws SQLException {
    CardDao cardDao = helper.getCardDao();
    CategoryDao categoryDao = helper.getCategoryDao();
    setupThreeCategories();
    Card c2 = cardDao.queryForId(2);
    Card c5 = cardDao.queryForId(5);
    Card c13 = cardDao.queryForId(13);
    Card c14 = cardDao.queryForId(14);
    Card c15 = cardDao.queryForId(15);
    LearningDataDao learningDataDao = helper.getLearningDataDao();
    Date testDate = new Date((new Date().getTime() - 1));
    learningDataDao.refresh(c13.getLearningData());
    LearningData c13Ld = c13.getLearningData();
    c13Ld.setAcqReps(1);
    c13Ld.setNextLearnDate(testDate);
    c13Ld.setEasiness((float) 2.7);
    learningDataDao.update(c13Ld);
    learningDataDao.refresh(c14.getLearningData());
    LearningData c14Ld = c14.getLearningData();
    c14Ld.setAcqReps(1);
    c14Ld.setNextLearnDate(testDate);
    c14Ld.setEasiness((float) 2.6);
    learningDataDao.update(c14Ld);
    learningDataDao.refresh(c15.getLearningData());
    LearningData c15Ld = c15.getLearningData();
    c15Ld.setAcqReps(1);
    c15Ld.setNextLearnDate(testDate);
    c15Ld.setEasiness((float) 2.8);
    learningDataDao.update(c15Ld);
    learningDataDao.refresh(c2.getLearningData());
    LearningData c2Ld = c2.getLearningData();
    c2Ld.setAcqReps(1);
    c2Ld.setNextLearnDate(testDate);
    c2Ld.setEasiness((float) 3.0);
    learningDataDao.update(c2Ld);
    learningDataDao.refresh(c5.getLearningData());
    LearningData c5Ld = c5.getLearningData();
    c5Ld.setAcqReps(1);
    c5Ld.setNextLearnDate(testDate);
    c5Ld.setEasiness((float) 2.9);
    learningDataDao.update(c5Ld);
    List<Category> cts = categoryDao.queryForEq("name", "My category");
    Category ct = cts.get(0);
    List<Card> cards = cardDao.getCardsForReview(ct, null, 50, ReviewOrdering.HardestFirst);
    assertEquals(2, cards.size());
    assertEquals(5, (int) cards.get(0).getOrdinal());
    assertEquals(2, (int) cards.get(1).getOrdinal());
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Date(java.util.Date) 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 20 with CategoryDao

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

the class CategoryTest method testRemoveCategories.

@SmallTest
@Test
public void testRemoveCategories() throws Exception {
    CardDao cardDao = helper.getCardDao();
    CategoryDao categoryDao = helper.getCategoryDao();
    Category c1 = categoryDao.createOrReturn("c1");
    categoryDao.create(c1);
    Card nc = new Card();
    nc.setCategory(c1);
    cardDao.create(nc);
    categoryDao.refresh(nc.getCategory());
    assertEquals("c1", nc.getCategory().getName());
    categoryDao.removeCategory(c1);
    nc = cardDao.queryForId(nc.getId());
    categoryDao.refresh(nc.getCategory());
    assertEquals("", nc.getCategory().getName());
}
Also used : 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) SmallTest(android.support.test.filters.SmallTest) AbstractExistingDBTest(org.liberty.android.fantastischmemo.test.AbstractExistingDBTest) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest)

Aggregations

CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)23 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)22 Card (org.liberty.android.fantastischmemo.entity.Card)20 Category (org.liberty.android.fantastischmemo.entity.Category)19 SmallTest (android.support.test.filters.SmallTest)13 Test (org.junit.Test)13 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)13 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)10 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)8 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)5 FileWriter (java.io.FileWriter)3 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 CSVWriter (com.opencsv.CSVWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Callable (java.util.concurrent.Callable)1