Android Open Source - Gazetti_Newspaper_Reader Bookmark Data Source






From Project

Back to project page Gazetti_Newspaper_Reader.

License

The source code is released under:

MIT License

If you think the Android project Gazetti_Newspaper_Reader listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package in.sahildave.gazetti.bookmarks.sqlite;
/*  w  w w  .  j a  v a  2 s . c  o  m*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import com.crashlytics.android.Crashlytics;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sahil on 13/10/14.
 */
public class BookmarkDataSource {
    private final static String TAG = BookmarkDataSource.class.getName();

    public SQLiteDatabase database;
    public SQLiteHelper dbhelper;
    private String[] allColumns = {
            SQLiteHelper.COLUMN_ID,
            SQLiteHelper.COLUMN_NEWSPAPER_NAME,
            SQLiteHelper.COLUMN_CATEGORY_NAME,
            SQLiteHelper.COLUMN_NEWS_HEADLINE,
            SQLiteHelper.COLUMN_NEWS_BODY,
            SQLiteHelper.COLUMN_NEWS_ARTICLE_URL,
            SQLiteHelper.COLUMN_NEWS_PUB_DATE,
            SQLiteHelper.COLUMN_NEWS_IMAGE_URL};

    public BookmarkDataSource(Context context) {
        dbhelper = SQLiteHelper.getInstance(context);
    }

    public void open() throws SQLException {
        database = dbhelper.getWritableDatabase();
    }

    public void close() {
        dbhelper.close();
    }

    public void createBookmarkModelEntry(BookmarkModel bookmarkModel) {
        ContentValues values = new ContentValues();
        try {
            values.put(SQLiteHelper.COLUMN_NEWSPAPER_NAME, bookmarkModel.getNewspaperName());
            values.put(SQLiteHelper.COLUMN_CATEGORY_NAME, bookmarkModel.getCategoryName());
            values.put(SQLiteHelper.COLUMN_NEWS_HEADLINE, bookmarkModel.getmArticleHeadline());
            values.put(SQLiteHelper.COLUMN_NEWS_BODY, bookmarkModel.getmArticleBody());
            values.put(SQLiteHelper.COLUMN_NEWS_ARTICLE_URL, bookmarkModel.getmArticleURL());
            values.put(SQLiteHelper.COLUMN_NEWS_PUB_DATE, bookmarkModel.getmArticlePubDate());
            values.put(SQLiteHelper.COLUMN_NEWS_IMAGE_URL, bookmarkModel.getmArticleImageURL());
        } catch (SQLiteConstraintException sqe){
            Crashlytics.log("Exception while creating sqlite entry - " + sqe.getMessage());
        }
        long insertId = database.insert(SQLiteHelper.TABLE_READ_IT_LATER, null,
                values);
        //Log.d(TAG, "Created entry "+insertId);

        Cursor cursor = database.query(SQLiteHelper.TABLE_READ_IT_LATER,
                allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        cursor.close();
    }

    public void deleteBookmarkModelEntry(String mArticleURL) {
        //Log.d(TAG, "Deleting "+mArticleURL);

        database.delete(
                SQLiteHelper.TABLE_READ_IT_LATER,
                SQLiteHelper.COLUMN_NEWS_ARTICLE_URL + "=?",
                new String[]{mArticleURL});
    }

    public boolean isBookmarked(String mArticleURL) {
        Cursor cursor = null;
        boolean returnBoolean = false;
        try {
            cursor = database.query(
                    SQLiteHelper.TABLE_READ_IT_LATER,
                    allColumns,
                    SQLiteHelper.COLUMN_NEWS_ARTICLE_URL+ "=?",
                    new String[]{mArticleURL},
                    null, null, null);
            returnBoolean  = cursor.getCount() > 0;
            cursor.close();
        } catch (SQLiteException sqe){
            Crashlytics.log("SQLITE ERROR "+sqe.getMessage());
        } catch (Exception e) {
            Crashlytics.log("Exception ERROR " + e.getMessage());
        }
        //Log.d(TAG, "Is bookmarked for "+mArticleURL+" -- "+returnBoolean);
        return returnBoolean;
    }

    public List<BookmarkModel> getAllBookmarkModels() {
        //Log.d(TAG, "Getting all Bookmarks");
        List<BookmarkModel> bookmarkModelList = new ArrayList<BookmarkModel>();
        Cursor cursor = database.query(SQLiteHelper.TABLE_READ_IT_LATER,
                allColumns, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            BookmarkModel bookmarkObject = cursorToBookmarkModel(cursor);
            bookmarkModelList.add(bookmarkObject);
            cursor.moveToNext();
        }
        // make sure to close the cursor
        cursor.close();
        return bookmarkModelList;
    }

    private BookmarkModel cursorToBookmarkModel(Cursor cursor) {
        BookmarkModel BookmarkModel = new BookmarkModel();
        BookmarkModel.setNewspaperName(cursor.getString(1));
        BookmarkModel.setCategoryName(cursor.getString(2));
        BookmarkModel.setmArticleHeadline(cursor.getString(3));
        BookmarkModel.setmArticleBody(cursor.getString(4));
        BookmarkModel.setmArticleURL(cursor.getString(5));
        BookmarkModel.setmArticlePubDate(cursor.getString(6));
        BookmarkModel.setmArticleImageURL(cursor.getString(7));
        return BookmarkModel;
    }
}




Java Source Code List

in.sahildave.gazetti.StarterApplication.java
in.sahildave.gazetti.bookmarks.BookmarkAdapter.java
in.sahildave.gazetti.bookmarks.BookmarkDetailActivity.java
in.sahildave.gazetti.bookmarks.BookmarkDetailFragment.java
in.sahildave.gazetti.bookmarks.BookmarkListActivity.java
in.sahildave.gazetti.bookmarks.BookmarkListFragment.java
in.sahildave.gazetti.bookmarks.BookmarkLoadingCallback.java
in.sahildave.gazetti.bookmarks.sqlite.BookmarkDataSource.java
in.sahildave.gazetti.bookmarks.sqlite.BookmarkModel.java
in.sahildave.gazetti.bookmarks.sqlite.SQLiteHelper.java
in.sahildave.gazetti.homescreen.HomeScreenActivity.java
in.sahildave.gazetti.homescreen.HomeScreenFragment.java
in.sahildave.gazetti.homescreen.adapter.AddCellDialogFragment.java
in.sahildave.gazetti.homescreen.adapter.CellModel.java
in.sahildave.gazetti.homescreen.adapter.EditCellDialogFragment.java
in.sahildave.gazetti.homescreen.adapter.GridAdapter.java
in.sahildave.gazetti.homescreen.adapter.NewsCatModel.java
in.sahildave.gazetti.homescreen.newcontent.DialogNewContentExpListAdaper.java
in.sahildave.gazetti.homescreen.newcontent.DialogNewContent.java
in.sahildave.gazetti.news_activities.ArticleLoadingCallback.java
in.sahildave.gazetti.news_activities.WebViewFragment.java
in.sahildave.gazetti.news_activities.WebsiteDetailActivity.java
in.sahildave.gazetti.news_activities.WebsiteDetailFragment.java
in.sahildave.gazetti.news_activities.WebsiteListActivity.java
in.sahildave.gazetti.news_activities.WebsiteListFragment.java
in.sahildave.gazetti.news_activities.adapter.NavDrawerListAdapter.java
in.sahildave.gazetti.news_activities.adapter.NewsAdapter.java
in.sahildave.gazetti.news_activities.adapter.RobotoLight.java
in.sahildave.gazetti.news_activities.fetch.firstPost.java
in.sahildave.gazetti.news_activities.fetch.hindu.java
in.sahildave.gazetti.news_activities.fetch.indianExpressBusiness.java
in.sahildave.gazetti.news_activities.fetch.indianExpress.java
in.sahildave.gazetti.news_activities.fetch.toi.java
in.sahildave.gazetti.preference.FeedSelectFragment.java
in.sahildave.gazetti.preference.FeedSelectSettingsActivity.java
in.sahildave.gazetti.preference.LicensesActivity.java
in.sahildave.gazetti.preference.PreferenceExpListAdapter.java
in.sahildave.gazetti.preference.SettingsActivity.java
in.sahildave.gazetti.util.BitmapTransform.java
in.sahildave.gazetti.util.ConfigService.java
in.sahildave.gazetti.util.Constants.java
in.sahildave.gazetti.util.GazettiEnums.java
in.sahildave.gazetti.util.JsonHelper.java
in.sahildave.gazetti.util.NewsCatFileUtil.java
in.sahildave.gazetti.util.ShareButtonListener.java
in.sahildave.gazetti.util.UserPrefUtil.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenExpListAdapter.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenFragmentExpList.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenFragmentFirst.java
in.sahildave.gazetti.welcomescreen.WelcomeScreenViewPagerActivity.java