Android Open Source - sqlite-provider Firework Reader






From Project

Back to project page sqlite-provider.

License

The source code is released under:

Apache License

If you think the Android project sqlite-provider 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.novoda.sqliteprovider.demo.persistance;
//from   w w w.  jav a  2  s. c  om
import android.database.Cursor;

import com.novoda.sqliteprovider.demo.domain.Firework;
import com.novoda.sqliteprovider.demo.domain.Groups;
import com.novoda.sqliteprovider.demo.domain.Groups.Group;
import com.novoda.sqliteprovider.demo.persistance.DatabaseConstants.Fireworks;
import com.novoda.sqliteprovider.demo.util.Log;

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

import static com.novoda.sqliteprovider.demo.persistance.DatabaseConstants.TBL_FIREWORKS;
import static com.novoda.sqliteprovider.demo.persistance.DatabaseConstants.TBL_SHOPS;

public class FireworkReader {

    private final DatabaseReader databaseReader;

    public FireworkReader(DatabaseReader databaseReader) {
        this.databaseReader = databaseReader;
    }

    public Firework getFirework(int primaryKey) {
        Cursor cursor = databaseReader.getFrom(TBL_FIREWORKS, primaryKey);

        if (cursor.moveToFirst()) {
            Firework firework = getFirework(cursor);

            cursor.close();

            return firework;
        }
        Log.e("No data in the cursor. Returning null safe firework.");
        return Firework.getNullSafeFirework();
    }

    public List<Firework> getFireworksForShop(int primaryKey) {
        Cursor cursor = databaseReader.getChildren(TBL_SHOPS, TBL_FIREWORKS, primaryKey);

        List<Firework> fireworks = populateListWith(cursor);

        cursor.close();

        return fireworks;
    }

    public List<Firework> getLimitedNumberOfFireworks(int limit) {
        Cursor cursor = databaseReader.getLimited(TBL_FIREWORKS, limit);

        List<Firework> fireworks = populateListWith(cursor);

        cursor.close();

        return fireworks;
    }

    public List<Firework> getUniqueFireworks() {
        String[] selection = {Fireworks.COL_NAME, Fireworks.COL_TYPE, Fireworks.COL_COLOR, Fireworks.COL_NOISE, Fireworks.COL_PRICE};

        Cursor cursor = databaseReader.getDistinct(TBL_FIREWORKS, selection);

        List<Firework> fireworks = populateListWith(cursor);

        cursor.close();

        return fireworks;
    }

    public List<Firework> getAll() {
        Cursor cursor = databaseReader.getAllFrom(TBL_FIREWORKS);

        List<Firework> fireworks = populateListWith(cursor);

        cursor.close();

        return fireworks;
    }

    private List<Firework> populateListWith(Cursor cursor) {
        List<Firework> data = new ArrayList<Firework>();
        if (cursor.moveToFirst()) {
            do {
                Firework firework = getFirework(cursor);
                data.add(firework);
            } while (cursor.moveToNext());
        } else {
            Log.e("No data in the cursor.");
        }
        return data;
    }

    private Firework getFirework(Cursor cursor) {
        String name = cursor.getString(cursor.getColumnIndexOrThrow(Fireworks.COL_NAME));
        String color = cursor.getString(cursor.getColumnIndexOrThrow(Fireworks.COL_COLOR));
        String type = cursor.getString(cursor.getColumnIndexOrThrow(Fireworks.COL_NOISE));
        String noise = cursor.getString(cursor.getColumnIndexOrThrow(Fireworks.COL_TYPE));
        double price = cursor.getDouble(cursor.getColumnIndexOrThrow(Fireworks.COL_PRICE));

        return new Firework(name, color, type, noise, price);
    }

    public Groups getShopsWithFireworkPricesAddingUpToOverForty() {
        String group = Fireworks.COL_SHOP;
        String having = "SUM (" + Fireworks.COL_PRICE + ") > 40";
        String[] selection = {Fireworks.COL_SHOP, "SUM (" + Fireworks.COL_PRICE + ") as total"};
        Cursor cursor = databaseReader.getGroupedByAndHaving(TBL_FIREWORKS, group, having, selection);

        List<Group> data = populateGroupListWith(cursor);

        cursor.close();

        return new Groups(data);
    }

    private List<Group> populateGroupListWith(Cursor cursor) {
        List<Group> data = new ArrayList<Group>();
        if (cursor.moveToFirst()) {
            do {
                double count = cursor.getDouble(cursor.getColumnIndexOrThrow("total"));
                int shopId = cursor.getInt(cursor.getColumnIndexOrThrow(Fireworks.COL_SHOP));
                data.add(new Group(count, shopId));
            } while (cursor.moveToNext());
        } else {
            Log.e("No data in the cursor.");
        }
        return data;
    }
}




Java Source Code List

com.novoda.sqliteprovider.demo.NovodaApplication.java
com.novoda.sqliteprovider.demo.domain.Firework.java
com.novoda.sqliteprovider.demo.domain.Groups.java
com.novoda.sqliteprovider.demo.domain.Shop.java
com.novoda.sqliteprovider.demo.domain.UseCaseFactory.java
com.novoda.sqliteprovider.demo.domain.UseCaseInfo.java
com.novoda.sqliteprovider.demo.loader.FireworkLoader.java
com.novoda.sqliteprovider.demo.loader.FireworkSaver.java
com.novoda.sqliteprovider.demo.persistance.DatabaseConstants.java
com.novoda.sqliteprovider.demo.persistance.DatabaseReader.java
com.novoda.sqliteprovider.demo.persistance.DatabaseWriter.java
com.novoda.sqliteprovider.demo.persistance.FireworkReader.java
com.novoda.sqliteprovider.demo.persistance.FireworkWriter.java
com.novoda.sqliteprovider.demo.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.provider.FireworkUriConstants.java
com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.simple.ui.MainActivity.java
com.novoda.sqliteprovider.demo.simple.ui.MainFragment.java
com.novoda.sqliteprovider.demo.ui.AddFireworkActivity.java
com.novoda.sqliteprovider.demo.ui.FindDistinctFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.FindFireworkWithPkActivity.java
com.novoda.sqliteprovider.demo.ui.FindFireworksFromOneShopActivity.java
com.novoda.sqliteprovider.demo.ui.FindThreeFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.FireworkActivity.java
com.novoda.sqliteprovider.demo.ui.MainActivity.java
com.novoda.sqliteprovider.demo.ui.ViewAllFireworksActivity.java
com.novoda.sqliteprovider.demo.ui.ViewShopActivity.java
com.novoda.sqliteprovider.demo.ui.WellStockedShopActivity.java
com.novoda.sqliteprovider.demo.ui.adapter.FireworkAdapter.java
com.novoda.sqliteprovider.demo.ui.base.NovodaActivity.java
com.novoda.sqliteprovider.demo.ui.fragment.AddFireworkFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.FindFireworkWithPkFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.FindFireworksFromOneShopFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.ShopFragment.java
com.novoda.sqliteprovider.demo.ui.fragment.UriSqlFragment.java
com.novoda.sqliteprovider.demo.ui.input.DemoMenu.java
com.novoda.sqliteprovider.demo.ui.input.OnPrimaryKeyInputError.java
com.novoda.sqliteprovider.demo.ui.util.FromXML.java
com.novoda.sqliteprovider.demo.ui.widget.UriSqlView.java
com.novoda.sqliteprovider.demo.util.Log.java
novoda.lib.sqliteprovider.ContentProviderTest.java
novoda.lib.sqliteprovider.cursor.EmptyCursor.java
novoda.lib.sqliteprovider.migration.Migrations.java
novoda.lib.sqliteprovider.provider.ImplLogger.java
novoda.lib.sqliteprovider.provider.LoggedContentProvider.java
novoda.lib.sqliteprovider.provider.SQLiteContentProviderImpl.java
novoda.lib.sqliteprovider.provider.SQLiteContentProvider.java
novoda.lib.sqliteprovider.provider.action.InsertHelper.java
novoda.lib.sqliteprovider.sqlite.AssetHelperTest.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelperTest.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteOpenHelper.java
novoda.lib.sqliteprovider.sqlite.ExtendedSQLiteQueryBuilder.java
novoda.lib.sqliteprovider.sqlite.IDatabaseMetaInfo.java
novoda.lib.sqliteprovider.sqlite.InsertHelperTest.java
novoda.lib.sqliteprovider.sqlite.SQLiteUri.java
novoda.lib.sqliteprovider.testproviders.TestProvider.java
novoda.lib.sqliteprovider.util.DBUtilsTest.java
novoda.lib.sqliteprovider.util.DBUtils.java
novoda.lib.sqliteprovider.util.DatabaseUtils.java
novoda.lib.sqliteprovider.util.Log.java
novoda.lib.sqliteprovider.util.SQLFile.java
novoda.lib.sqliteprovider.util.UriInspector.java
novoda.lib.sqliteprovider.util.UriToSqlAttributes.java
novoda.lib.sqliteprovider.util.UriUtils.java
novoda.rest.database.ModularSQLiteOpenHelper.java
novoda.rest.database.Persister.java
novoda.rest.database.SQLiteConflictClause.java
novoda.rest.database.SQLiteFileParser.java
novoda.rest.database.SQLiteInserter.java
novoda.rest.database.SQLiteTableCreatorWrapper.java
novoda.rest.database.SQLiteTableCreator.java
novoda.rest.database.SQLiteType.java
novoda.rest.database.SQLiteUtil.java
novoda.rest.database.UriQueryBuilder.java
novoda.rest.database.UriTableCreator.java
novoda.rest.database.listener.NodeInsertListener.java
novoda.rest.database.listener.SQLiteInsertListener.java
novoda.rest.database.listener.SQLiteTableCreateListener.java