Search in sources :

Example 46 with Card

use of org.liberty.android.fantastischmemo.entity.Card 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 47 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class LearnQueueManager method flushDirtyCache.

private synchronized void flushDirtyCache() {
    AnyMemoExecutor.submit(new Runnable() {

        public void run() {
            // Update the queue
            final AnyMemoDBOpenHelper dbOpenHelper = AnyMemoDBOpenHelperManager.getHelper(context, dbPath);
            final CardDao cardDao = dbOpenHelper.getCardDao();
            final LearningDataDao learningDataDao = dbOpenHelper.getLearningDataDao();
            try {
                learningDataDao.callBatchTasks(new Callable<Void>() {

                    public Void call() throws Exception {
                        Log.i(TAG, "Flushing dirty cache. # of cards to flush: " + dirtyCache.size());
                        while (!dirtyCache.isEmpty()) {
                            Card card = dirtyCache.take();
                            Log.i(TAG, "Flushing card id: " + card.getId() + " with learning data: " + card.getLearningData());
                            if (learningDataDao.update(card.getLearningData()) == 0) {
                                Log.w(TAG, "LearningDataDao update failed for : " + card.getLearningData());
                                throw new RuntimeException("LearningDataDao update failed! LearningData to update: " + card.getLearningData() + " current value: " + learningDataDao.queryForId(card.getLearningData().getId()));
                            }
                            if (cardDao.update(card) == 0) {
                                Log.w(TAG, "CardDao update failed for : " + card.getLearningData());
                                throw new RuntimeException("CardDao update failed. Card to update: " + card);
                            }
                        }
                        Log.i(TAG, "Flushing dirty cache done.");
                        return null;
                    }
                });
            } finally {
                AnyMemoDBOpenHelperManager.releaseHelper(dbOpenHelper);
            }
        }
    });
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Callable(java.util.concurrent.Callable) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 48 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class LearnQueueManager method position.

private synchronized void position(int cardId) {
    Iterator<Card> learnIterator = learnQueue.iterator();
    Iterator<Card> reviewCacheIterator = reviewCache.iterator();
    Iterator<Card> newCacheIterator = newCache.iterator();
    int learnQueueRotateDistance = 0;
    while (learnIterator.hasNext()) {
        Card c = learnIterator.next();
        if (c.getId() == cardId) {
            int index = learnQueue.indexOf(c);
            learnQueueRotateDistance = -index;
            Log.i(TAG, "Rotate index: " + index);
        }
    }
    Collections.rotate(learnQueue, learnQueueRotateDistance);
    while (reviewCacheIterator.hasNext()) {
        Card c = reviewCacheIterator.next();
        if (c.getId() == cardId) {
            reviewCacheIterator.remove();
        }
    }
    while (newCacheIterator.hasNext()) {
        Card c = newCacheIterator.next();
        if (c.getId() == cardId) {
            newCacheIterator.remove();
        }
    }
    final AnyMemoDBOpenHelper dbOpenHelper = AnyMemoDBOpenHelperManager.getHelper(context, dbPath);
    final CardDao cardDao = dbOpenHelper.getCardDao();
    final LearningDataDao learningDataDao = dbOpenHelper.getLearningDataDao();
    try {
        Card headCard = null;
        headCard = cardDao.queryForId(cardId);
        learningDataDao.refresh(headCard.getLearningData());
        learnQueue.add(0, headCard);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(dbOpenHelper);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) LearningDataDao(org.liberty.android.fantastischmemo.dao.LearningDataDao) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 49 with Card

use of org.liberty.android.fantastischmemo.entity.Card in project AnyMemo by helloworld1.

the class QuizletUploadHelper method uploadToQuizlet.

/**
 * Upload cardsets list to Quizlet from a db file
 *
 * @param db
 *            file
 * @param authToken
 *            oauth token
 * @return null
 * @throws IOException
 *             IOException If http response code is not 2xx
 */
public void uploadToQuizlet(File file, String authToken) throws IOException {
    // First read card because if it failed we don't even bother uploading.
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(file.getAbsolutePath());
    List<Card> cardList = null;
    try {
        final CardDao cardDao = helper.getCardDao();
        cardList = cardDao.queryForAll();
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
    // Following doing upload
    StringBuilder data = new StringBuilder();
    data.append(String.format("whitespace=%s", URLEncoder.encode("1", "UTF-8")));
    data.append(String.format("&title=%s", URLEncoder.encode(file.getName(), "UTF-8")));
    // Get cards from cardList
    for (int i = 0; i < cardList.size(); i++) {
        Card c = cardList.get(i);
        data.append(String.format("&terms[]=%s", URLEncoder.encode(c.getQuestion(), "UTF-8")));
        data.append(String.format("&definitions[]=%s", URLEncoder.encode(c.getAnswer(), "UTF-8")));
    }
    data.append(String.format("&lang_terms=%s", URLEncoder.encode("en", "UTF-8")));
    data.append(String.format("&lang_definitions=%s", URLEncoder.encode("en", "UTF-8")));
    data.append(String.format("&allow_discussion=%s", URLEncoder.encode("true", "UTF-8")));
    URL url = new URL("https://api.quizlet.com/2.0/sets");
    makePostApiCall(url, data.toString(), authToken);
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) URL(java.net.URL) Card(org.liberty.android.fantastischmemo.entity.Card)

Example 50 with Card

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

Aggregations

Card (org.liberty.android.fantastischmemo.entity.Card)95 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)61 SmallTest (android.support.test.filters.SmallTest)43 Test (org.junit.Test)43 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)41 Category (org.liberty.android.fantastischmemo.entity.Category)30 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)28 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)25 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)20 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)13 File (java.io.File)12 ArrayList (java.util.ArrayList)11 SQLException (java.sql.SQLException)9 QueueManager (org.liberty.android.fantastischmemo.queue.QueueManager)9 IOException (java.io.IOException)6 Date (java.util.Date)6 LearnQueueManager (org.liberty.android.fantastischmemo.queue.LearnQueueManager)6 URL (java.net.URL)5 FileWriter (java.io.FileWriter)4 Scheduler (org.liberty.android.fantastischmemo.scheduler.Scheduler)4