Android Open Source - hubblog Database Provider






From Project

Back to project page hubblog.

License

The source code is released under:

MIT License

If you think the Android project hubblog 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 com.donskifarrell.Hubblog.Providers;
//from w ww .  j  av a 2  s  .c om
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import com.commonsware.cwac.loaderex.acl.SQLiteCursorLoader;
import com.donskifarrell.Hubblog.Interfaces.ActivityDataListener;
import com.donskifarrell.Hubblog.Interfaces.DataProvider;
import com.donskifarrell.Hubblog.Providers.Data.Article;
import com.donskifarrell.Hubblog.Providers.Data.MetadataTag;
import com.donskifarrell.Hubblog.Providers.Data.Site;
import com.google.inject.Inject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: donski
 * Date: 23/11/13
 * Time: 15:12
 */
public class DatabaseProvider implements LoaderManager.LoaderCallbacks<Cursor> {
    private ActivityDataListener listener;
    private DataProvider centralDataProvider;
    private DatabaseHelper databaseHelper;

    private static final int HUBBLOG_ARTICLE_LOADER = 0;
    private static final int HUBBLOG_DEFAULT_TAGS_LOADER = 1;

    private List<Site> sites;

    private static final String whereIdEquals = "_id=";

    @Inject
    public DatabaseProvider(ActivityDataListener activityDataListener, DataProvider dataProvider) {
        databaseHelper = new DatabaseHelper(activityDataListener.getContext(), this);

        sites = new LinkedList<Site>();
        centralDataProvider = dataProvider;

        listener = activityDataListener;
        listener.getSupportLoaderManager().initLoader(HUBBLOG_ARTICLE_LOADER, null, this);
        // todo:
        // listener.getSupportLoaderManager().initLoader(HUBBLOG_DEFAULT_TAGS_LOADER, null, this);
    }

    public long insertArticle(Article article) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();

        ContentValues cv = databaseHelper.buildContentValuesFromArticle(article);
        return db.insertOrThrow(DatabaseHelper.ArticleDataModel.TABLE_NAME, DatabaseHelper.ArticleDataModel.COLUMN_TITLE, cv);
    }

    public long insertTag(MetadataTag tag) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        ContentValues cv = databaseHelper.buildContentValuesFromMetadataTag(tag);
        return db.insertOrThrow(DatabaseHelper.MetadataTagDataModel.TABLE_NAME, DatabaseHelper.MetadataTagDataModel.COLUMN_TAG, cv);
    }

    public void update(Article article) {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();

        ContentValues cv = databaseHelper.buildContentValuesFromArticle(article);
        db.update(DatabaseHelper.ArticleDataModel.TABLE_NAME, cv, whereIdEquals + article.getId(), null);

        for (MetadataTag tag : article.getMetadataTags()) {
            ContentValues tagCv = databaseHelper.buildContentValuesFromMetadataTag(tag);
            db.update(DatabaseHelper.MetadataTagDataModel.TABLE_NAME, tagCv, whereIdEquals + tag.getTagId(), null);
        }
    }

    /* Data Loading */
    @Override
    public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle) {
        SQLiteCursorLoader cursorLoader;
        String query = "";

        switch (loaderID) {
            case HUBBLOG_ARTICLE_LOADER:
                query = DatabaseHelper.JOIN_ARTICLES_WITH_METADATA_TAGS;
                break;
        }

        cursorLoader = new SQLiteCursorLoader(
                listener.getContext(),
                databaseHelper,
                query,
                null
        );

        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
            case HUBBLOG_ARTICLE_LOADER:
                for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    int idx;

                    // Get or create Site
                    Site site = new Site(cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_SITE_NAME)));
                    idx = sites.indexOf(site);
                    if (idx == -1) {
                        sites.add(site);
                    } else {
                        site = sites.get(idx);
                    }

                    // Get or create Article
                    Article article = new Article();
                    article.setId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_ID)));
                    idx = site.getArticles().indexOf(article);
                    if (idx == -1) {
                        article = buildArticleFromCursor(cursor);
                        site.getArticles().add(article);
                    } else {
                        article = site.getArticles().get(idx);
                    }

                    // Get or create MetadataTag
                    MetadataTag tag = new MetadataTag();
                    tag.setTagId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.MetadataTagDataModel.COLUMN_TAG_ID_ALIAS)));
                    idx = article.getMetadataTags().indexOf(tag);
                    if (idx == -1) {
                        tag = buildMetadataTagFromCursor(cursor);
                        article.getMetadataTags().add(tag);
                    }
                }
                break;
        }

        centralDataProvider.setSites(sites);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        Log.w("HUBBLOG", "RESET");
    }

    /* Data Object Building */
    private Article buildArticleFromCursor(Cursor cursor) {
        String siteName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_SITE_NAME));

        Article article = new Article();
        article.setId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_ID)));
        article.setSiteName(siteName);
        article.setTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_TITLE)));
        article.setFileTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_FILE_TITLE)));

        int bool = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_IS_DRAFT));
        article.isDraft((bool == 1)? true : false);

        article.setContent(cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_CONTENT)));

        try{
            SimpleDateFormat format = new SimpleDateFormat(DatabaseHelper.ArticleDataModel.DATE_FORMAT);

            Date createdDate = format.parse(cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_CREATED_DATE)));
            article.setCreatedDate(createdDate);

            Date modifiedDate = format.parse(cursor.getString(cursor.getColumnIndex(DatabaseHelper.ArticleDataModel.COLUMN_LAST_MODIFIED_DATE)));
            article.setLastModifiedDate(modifiedDate);
        }
        catch (ParseException parseExp) {
            Log.e("HUBBLOG", "Parsing Date Exception");
        }

        return article;
    }

    private MetadataTag buildMetadataTagFromCursor(Cursor cursor) {
        MetadataTag metadataTag = new MetadataTag();
        metadataTag.setArticleId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.MetadataTagDataModel.COLUMN_ARTICLE_ID)));
        metadataTag.setTagId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.MetadataTagDataModel.COLUMN_TAG_ID_ALIAS)));
        metadataTag.setTag(cursor.getString(cursor.getColumnIndex(DatabaseHelper.MetadataTagDataModel.COLUMN_TAG)));
        return metadataTag;
    }
}




Java Source Code List

com.donskifarrell.Hubblog.BindingModule.java
com.donskifarrell.Hubblog.Activities.HubblogActivity.java
com.donskifarrell.Hubblog.Activities.LoginActivity.java
com.donskifarrell.Hubblog.Activities.StartupActivity.java
com.donskifarrell.Hubblog.Activities.Adapters.ArticleWebViewAdapter.java
com.donskifarrell.Hubblog.Activities.Adapters.ArticlesAdapter.java
com.donskifarrell.Hubblog.Activities.Adapters.MetadataAdapter.java
com.donskifarrell.Hubblog.Activities.Adapters.SidebarAdapter.java
com.donskifarrell.Hubblog.Activities.Adapters.TabsAdapter.java
com.donskifarrell.Hubblog.Activities.Dialogs.AboutDialogFragment.java
com.donskifarrell.Hubblog.Activities.Dialogs.AddSiteDialogFragment.java
com.donskifarrell.Hubblog.Activities.Dialogs.DeleteArticleDialogFragment.java
com.donskifarrell.Hubblog.Activities.Dialogs.EditArticleTitleDialogFragment.java
com.donskifarrell.Hubblog.Activities.Dialogs.LightAlertDialog.java
com.donskifarrell.Hubblog.Activities.Dialogs.LightProgressDialog.java
com.donskifarrell.Hubblog.Activities.Dialogs.NotificationDialogFragment.java
com.donskifarrell.Hubblog.Activities.Dialogs.SelectSiteDialogFragment.java
com.donskifarrell.Hubblog.Activities.Fragments.BasePageFragment.java
com.donskifarrell.Hubblog.Activities.Fragments.CommitArticleFragment.java
com.donskifarrell.Hubblog.Activities.Fragments.EditArticleFragment.java
com.donskifarrell.Hubblog.Activities.Fragments.EditMarkdownFragment.java
com.donskifarrell.Hubblog.Interfaces.ActivityDataListener.java
com.donskifarrell.Hubblog.Interfaces.ArticleWebViewJsInterface.java
com.donskifarrell.Hubblog.Interfaces.DataProvider.java
com.donskifarrell.Hubblog.Interfaces.DialogListener.java
com.donskifarrell.Hubblog.Interfaces.MetadataTagListener.java
com.donskifarrell.Hubblog.Interfaces.OnSidebarListItemSelected.java
com.donskifarrell.Hubblog.Providers.DatabaseHelper.java
com.donskifarrell.Hubblog.Providers.DatabaseProvider.java
com.donskifarrell.Hubblog.Providers.FileSystemProvider.java
com.donskifarrell.Hubblog.Providers.GitHubProvider.java
com.donskifarrell.Hubblog.Providers.HubblogDataProvider.java
com.donskifarrell.Hubblog.Providers.SharedPreferencesProvider.java
com.donskifarrell.Hubblog.Providers.Data.Account.java
com.donskifarrell.Hubblog.Providers.Data.Article.java
com.donskifarrell.Hubblog.Providers.Data.MetadataTag.java
com.donskifarrell.Hubblog.Providers.Data.Site.java
com.github.kevinsawicki.wishlist.ActivityUtils.java
com.github.kevinsawicki.wishlist.AsyncLoader.java
com.github.kevinsawicki.wishlist.CursorAdapter.java
com.github.kevinsawicki.wishlist.DatabaseHelper.java
com.github.kevinsawicki.wishlist.DecodeBitmapTask.java
com.github.kevinsawicki.wishlist.EditTextUtils.java
com.github.kevinsawicki.wishlist.Gestures.java
com.github.kevinsawicki.wishlist.ImageViewBitmapTask.java
com.github.kevinsawicki.wishlist.Keyboard.java
com.github.kevinsawicki.wishlist.LightDialog.java
com.github.kevinsawicki.wishlist.LocationUtils.java
com.github.kevinsawicki.wishlist.MultiTypeAdapter.java
com.github.kevinsawicki.wishlist.OnDoubleTapAdapter.java
com.github.kevinsawicki.wishlist.SectionFinder.java
com.github.kevinsawicki.wishlist.SectionMultiTypeAdapter.java
com.github.kevinsawicki.wishlist.SectionSingleTypeAdapter.java
com.github.kevinsawicki.wishlist.SingleTypeAdapter.java
com.github.kevinsawicki.wishlist.SingleTypeCursorAdapter.java
com.github.kevinsawicki.wishlist.Toaster.java
com.github.kevinsawicki.wishlist.TypeAdapter.java
com.github.kevinsawicki.wishlist.TypefaceUtils.java
com.github.kevinsawicki.wishlist.ViewFinder.java
com.github.kevinsawicki.wishlist.ViewUpdater.java
com.github.kevinsawicki.wishlist.ViewUtils.java
com.github.mobile.Accounts.AccountAuthenticatorService.java
com.github.mobile.Accounts.AccountAuthenticator.java
com.github.mobile.Accounts.AccountClient.java
com.github.mobile.Accounts.AccountConstants.java
com.github.mobile.Accounts.AccountScope.java
com.github.mobile.Accounts.AccountUtils.java
com.github.mobile.Accounts.AuthenticatedUserLoader.java
com.github.mobile.Accounts.AuthenticatedUserTask.java
com.github.mobile.Accounts.DefaultClient.java
com.github.mobile.Accounts.GitHubAccount.java
com.github.mobile.Accounts.ScopeBase.java
com.github.mobile.Accounts.TwoFactorAuthActivity.java
com.github.mobile.Accounts.TwoFactorAuthClient.java
com.github.mobile.Accounts.TwoFactorAuthException.java
com.github.mobile.Persistence.AccountDataManager.java
com.github.mobile.Persistence.CacheHelper.java
com.github.mobile.Persistence.DatabaseCache.java
com.github.mobile.Persistence.OrganizationLoader.java
com.github.mobile.Persistence.OrganizationRepositories.java
com.github.mobile.Persistence.Organizations.java
com.github.mobile.Persistence.PersistableResource.java
com.github.mobile.Requests.RequestCodes.java
com.github.mobile.Requests.RequestFuture.java
com.github.mobile.Requests.RequestReader.java
com.github.mobile.Requests.RequestWriter.java
com.github.mobile.Utils.IssueFilter.java
com.github.mobile.Utils.ToastUtils.java
com.github.mobile.Utils.UserComparator.java
com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java