com.wit.android.support.database.loremipsum.BaseLoremIpsumEntity.java Source code

Java tutorial

Introduction

Here is the source code for com.wit.android.support.database.loremipsum.BaseLoremIpsumEntity.java

Source

/*
 * =================================================================================================
 *                    Copyright (C) 2014 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License 
 * you may obtain at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written within this file but as it 
 * is described in the License, the software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.wit.android.support.database.loremipsum;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;

import com.wit.android.support.database.LoaderOptions;
import com.wit.android.support.database.annotation.Column;
import com.wit.android.support.database.annotation.PrimaryColumn;
import com.wit.android.support.database.entity.EditableEntity;
import com.wit.android.support.database.entity.EditableTransactions;
import com.wit.android.support.database.entity.EntityColumnType;
import com.wit.android.support.database.IdSelection;

/**
 * <h3>Class Overview</h3>
 * todo: description
 *
 * @author Martin Albedinsky
 */
public class BaseLoremIpsumEntity extends EditableEntity<LoremIpsum, EditableTransactions<LoremIpsum>> {

    /**
     * Interface ===================================================================================
     */

    /**
     * Constants ===================================================================================
     */

    /**
     * Log TAG.
     */
    // private static final String TAG = "BaseLoremIpsumEntity";

    /**
     * Flag indicating whether the output trough log-cat is enabled or not.
     */
    // private static final boolean LOG_ENABLED = true;

    /**
     * Flag indicating whether the debug output trough log-cat is enabled or not.
     */
    // private static final boolean DEBUG_ENABLED = true;

    /**
     * todo
     */
    public static final String ENTITY_NAME = "lorem_ipsum";

    /**
     * Columns =====================================================================================
     */

    @PrimaryColumn
    public static final String ID = PrimaryColumn.COLUMN_NAME;

    @Column
    public static final String TITLE = "title";

    @Column
    public static final String TEXT = "text";

    @Column
    public static final String NAME = "name";

    @Column
    public static final String NOTE = "note";

    @Column
    public static final String DESCRIPTION = "description";

    @Column(defaultValue = "0000-00-00 00:00:00")
    public static final String DATE = "date";

    @Column(type = EntityColumnType.INTEGER)
    public static final String COUNT = "count";

    @Column(type = EntityColumnType.BOOLEAN)
    public static final String ENABLED = "enabled";

    @Column(type = EntityColumnType.BOOLEAN)
    public static final String EMPTY = "empty";

    @Column(type = EntityColumnType.BOOLEAN)
    public static final String EDITABLE = "editable";

    /**
     * Access points ===============================================================================
     */

    /**
     * todo
     */
    public static final String ACCESS_POINT_COLLECTION = "createCollectionLoader";

    /**
     * todo
     */
    public static final String ACCESS_POINT_DETAIL = "createDetailLoader";

    /**
     * Static members ==============================================================================
     */

    /**
     * Members =====================================================================================
     */

    /**
     * todo
     */
    private int mDefaultContentSize = 100;

    /**
     * Constructors ================================================================================
     */

    /**
     * todo
     */
    public BaseLoremIpsumEntity() {
        super();
    }

    /**
     * todo
     *
     * @param databaseAuthority todo
     */
    protected BaseLoremIpsumEntity(@NonNull String databaseAuthority) {
        super(ENTITY_NAME, databaseAuthority, LoremIpsum.class);
    }

    /**
     * Methods =====================================================================================
     */

    /**
     * Public --------------------------------------------------------------------------------------
     */

    /**
     * <h3>Projection:</h3>
     * [{@link #ID}, {@link #TITLE}, {@link #NOTE}, {@link #DATE}, {@link #COUNT}, {@link #ENABLED},
     * {@link #EMPTY}]
     *
     * @return todo
     */
    @NonNull
    public Loader<Cursor> createCollectionLoader() {
        return LoaderOptions.createLoader(getContext(), getContentUri(),
                new String[] { ID, TITLE, NOTE, DATE, COUNT, ENABLED, EMPTY });
    }

    /**
     * <h3>Projection:</h3>
     * [{@link #ID}, {@link #TITLE}, {@link #NOTE}, {@link #DATE}, {@link #COUNT}, {@link #ENABLED},
     * {@link #EMPTY}]
     *
     * @param params todo
     * @return todo
     */
    @NonNull
    public Loader<Cursor> createDetailLoader(@Nullable Bundle params) {
        // Prepare selection.
        final IdSelection selection = new IdSelection(ID, params != null ? params.getLong(ID) : 0);
        return LoaderOptions.createLoader(getContext(), getContentUri(),
                new String[] { ID, TITLE, NAME, DESCRIPTION, DATE, EDITABLE, EMPTY }, selection.build(),
                selection.buildArgs());
    }

    /**
     */
    @Override
    public boolean requiresDefaultContent() {
        return true;
    }

    /**
     * Getters + Setters ---------------------------------------------------------------------------
     */

    /**
     * todo
     *
     * @param size todo
     */
    public void setDefaultContentSize(int size) {
        if (size >= 0) {
            this.mDefaultContentSize = size;
        }
    }

    /**
     * Protected -----------------------------------------------------------------------------------
     */

    /**
     */
    @Override
    protected void onCreateDefaultContent(@NonNull SQLiteDatabase db, @NonNull Context context) {
        for (int i = 0; i < mDefaultContentSize; i++) {
            db.insert(ENTITY_NAME, null, new LoremIpsum(i).toContentValues());
        }
    }

    /**
     * Private -------------------------------------------------------------------------------------
     */

    /**
     * Inner classes ===============================================================================
     */
}