Search in sources :

Example 31 with Category

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

the class AnyMemoDBOpenHelper method onCreate.

@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
    Log.v(TAG, "Now we are creating a new database!");
    Log.i(TAG, "Newly created db version: " + database.getVersion());
    try {
        TableUtils.createTable(connectionSource, Card.class);
        TableUtils.createTable(connectionSource, Deck.class);
        TableUtils.createTable(connectionSource, Setting.class);
        TableUtils.createTable(connectionSource, Filter.class);
        TableUtils.createTable(connectionSource, Category.class);
        TableUtils.createTable(connectionSource, LearningData.class);
        getSettingDao().create(new Setting());
        getCategoryDao().create(new Category());
        if (database.getVersion() == 0) {
            convertOldDatabase(database);
        }
    } catch (SQLException e) {
        throw new RuntimeException("Database creation error: " + e.toString());
    }
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) SQLException(java.sql.SQLException) Setting(org.liberty.android.fantastischmemo.entity.Setting)

Example 32 with Category

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

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

the class CardDaoTest method testSearchLastOrdinalWithcategoryIfExists.

@SmallTest
@Test
public void testSearchLastOrdinalWithcategoryIfExists() throws Exception {
    setupThreeCategories();
    CardDao cardDao = helper.getCardDao();
    CategoryDao categoryDao = helper.getCategoryDao();
    List<Category> cts = categoryDao.queryForEq("name", "My category");
    Category ct = cts.get(0);
    Card c = cardDao.queryLastOrdinal(ct);
    assertEquals(8, (int) c.getId());
}
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 34 with Category

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

the class QuizletDownloadHelper method downloadCardset.

/**
 * Download cardsets list from Quizlet and save to a db file
 *
 * @param setId
 *            cardset ID
 * @param authToken
 *            oauth token
 * @return The path of saved db file
 * @throws IOException
 *             IOException If http response code is not 2xx
 * @throws JSONException
 *             If the response is invalid JSON
 */
public String downloadCardset(String setId, String authToken) throws IOException, JSONException {
    URL url;
    // needs authtoken
    if (authToken != null) {
        url = new URL(AMEnv.QUIZLET_API_ENDPOINT + "/sets/" + setId);
    } else {
        String urlString = String.format(AMEnv.QUIZLET_API_ENDPOINT + "/sets/" + "%1$s?client_id=%2$s", URLEncoder.encode(setId, "UTF-8"), URLEncoder.encode(AMEnv.QUIZLET_CLIENT_ID, "UTF-8"));
        url = new URL(urlString);
    }
    String response = makeApiCall(url, authToken);
    JSONObject rootObject = new JSONObject(response);
    JSONArray flashcardsArray = rootObject.getJSONArray("terms");
    int termCount = rootObject.getInt("term_count");
    boolean hasImage = rootObject.getBoolean("has_images");
    List<Card> cardList = new ArrayList<Card>(termCount);
    // handle image
    String dbname = downloaderUtils.validateDBName(rootObject.getString("title")) + ".db";
    String imagePath = AMEnv.DEFAULT_IMAGE_PATH + dbname + "/";
    if (hasImage) {
        FileUtils.forceMkdir(new File(imagePath));
    }
    for (int i = 0; i < flashcardsArray.length(); i++) {
        JSONObject jsonItem = flashcardsArray.getJSONObject(i);
        String question = jsonItem.getString("term");
        String answer = jsonItem.getString("definition");
        // Download images, ignore image downloading error.
        try {
            if (jsonItem.has("image") && !jsonItem.isNull("image") && hasImage) {
                JSONObject imageItem = jsonItem.getJSONObject("image");
                String imageUrl = imageItem.getString("url");
                String downloadFilename = Uri.parse(imageUrl).getLastPathSegment();
                downloaderUtils.downloadFile(imageUrl, imagePath + downloadFilename);
                answer += "<br /><img src=\"" + downloadFilename + "\"/>";
            }
        } catch (Exception e) {
            Log.e(TAG, "Error downloading image.", e);
        }
        Card card = new Card();
        card.setQuestion(question);
        card.setAnswer(answer);
        card.setCategory(new Category());
        card.setLearningData(new LearningData());
        cardList.add(card);
    }
    /* Make a valid dbname from the title */
    String dbpath = AMEnv.DEFAULT_ROOT_PATH;
    String fullpath = dbpath + dbname;
    amFileUtil.deleteFileWithBackup(fullpath);
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(fullpath);
    try {
        CardDao cardDao = helper.getCardDao();
        cardDao.createCards(cardList);
        long count = helper.getCardDao().getTotalCount(null);
        if (count <= 0L) {
            throw new RuntimeException("Downloaded empty db.");
        }
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
    return fullpath;
}
Also used : Category(org.liberty.android.fantastischmemo.entity.Category) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) URL(java.net.URL) JSONException(org.json.JSONException) IOException(java.io.IOException) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) JSONObject(org.json.JSONObject) File(java.io.File) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 35 with Category

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

the class ShareScreen method onClick.

@Override
public void onClick(View v) {
    try {
        String dbpath = dbnameView.getText().toString();
        Log.v(TAG, dbpath);
        if (v == saveButton || v == savePrevButton) {
            AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(this, dbpath);
            CardDao cardDao = helper.getCardDao();
            try {
                Card card = new Card();
                card.setQuestion(questionView.getText().toString());
                card.setAnswer(answerView.getText().toString());
                card.setNote(noteView.getText().toString());
                card.setCategory(new Category());
                card.setLearningData(new LearningData());
                cardDao.createCard(card);
                if (v == savePrevButton) {
                    Intent myIntent = new Intent(this, PreviewEditActivity.class);
                    /* This should be the newly created id */
                    myIntent.putExtra("id", card.getId());
                    myIntent.putExtra(PreviewEditActivity.EXTRA_DBPATH, dbpath);
                    startActivity(myIntent);
                }
                finish();
            } finally {
                AnyMemoDBOpenHelperManager.releaseHelper(helper);
            }
        } else if (v == cancelButton) {
            finish();
        } else if (v == dbnameView) {
            Intent myIntent = new Intent(this, FileBrowserActivity.class);
            myIntent.putExtra(FileBrowserActivity.EXTRA_FILE_EXTENSIONS, ".db");
            startActivityForResult(myIntent, ACTIVITY_FB);
        }
    } catch (Exception e) {
        AMGUIUtility.displayError(this, getString(R.string.error_text), "", e);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Category(org.liberty.android.fantastischmemo.entity.Category) Intent(android.content.Intent) LearningData(org.liberty.android.fantastischmemo.entity.LearningData) CardDao(org.liberty.android.fantastischmemo.dao.CardDao) Card(org.liberty.android.fantastischmemo.entity.Card)

Aggregations

Category (org.liberty.android.fantastischmemo.entity.Category)35 Card (org.liberty.android.fantastischmemo.entity.Card)30 CardDao (org.liberty.android.fantastischmemo.dao.CardDao)27 CategoryDao (org.liberty.android.fantastischmemo.dao.CategoryDao)19 SmallTest (android.support.test.filters.SmallTest)17 Test (org.junit.Test)17 AbstractExistingDBTest (org.liberty.android.fantastischmemo.test.AbstractExistingDBTest)17 LearningData (org.liberty.android.fantastischmemo.entity.LearningData)15 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)11 LearningDataDao (org.liberty.android.fantastischmemo.dao.LearningDataDao)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 LinkedList (java.util.LinkedList)2 QueueManager (org.liberty.android.fantastischmemo.queue.QueueManager)2 Intent (android.content.Intent)1