fr.eoit.activity.loader.InvestItemsLoader.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.loader.InvestItemsLoader.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.activity.loader;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;

import fr.eoit.EOITConst;
import fr.eoit.activity.util.PricesUtils;
import fr.eoit.bean.ItemBeanWithMaterials;
import fr.eoit.db.bean.Blueprint;
import fr.eoit.db.bean.Item;
import fr.eoit.db.bean.ItemMaterials;
import fr.eoit.db.bean.Prices;
import fr.piconsoft.db.util.DbUtil;
import fr.eoit.util.SparseItemBeanArray;

/**
 * @author picon.software
 *
 */
public class InvestItemsLoader extends AsyncTaskLoader<SparseItemBeanArray> {

    private Activity activity;
    private SparseItemBeanArray data;
    private int itemId;

    public InvestItemsLoader(Activity activity, int itemid) {
        super(activity);
        this.itemId = itemid;
        this.activity = activity;
    }

    @Override
    public SparseItemBeanArray loadInBackground() {
        data = new SparseItemBeanArray();
        data.putAll(getInvestItems(activity, itemId));
        data.putAll(getInvestParentBlueprint(activity, itemId));
        return data;
    }

    @Override
    protected void onStartLoading() {
        if (data == null) {
            forceLoad();
        } else {
            deliverResult(data);
        }
    }

    private static SparseItemBeanArray getInvestItems(Context context, long itemId) {
        SparseItemBeanArray items = new SparseItemBeanArray();

        final Cursor cursor = context.getContentResolver().query(
                ContentUris.withAppendedId(Item.CONTENT_ID_URI_INVEST, itemId),
                new String[] { Item._ID, Item.COLUMN_NAME_NAME, Item.COLUMN_NAME_CHOSEN_PRICE_ID,
                        Blueprint.COLUMN_NAME_PRODUCE_ITEM_ID, Prices.COLUMN_NAME_OWN_PRICE,
                        Prices.COLUMN_NAME_BUY_PRICE, Prices.COLUMN_NAME_SELL_PRICE,
                        Prices.COLUMN_NAME_PRODUCE_PRICE },
                null, null, null);

        try {
            if (DbUtil.hasAtLeastOneRow(cursor)) {

                while (!cursor.isAfterLast()) {

                    long producedItemId = cursor
                            .getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_PRODUCE_ITEM_ID));
                    ItemBeanWithMaterials item = new ItemBeanWithMaterials();
                    item.id = cursor.getInt(cursor.getColumnIndexOrThrow(Item._ID));
                    item.name = cursor.getString(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_NAME));
                    item.price = PricesUtils.getPriceOrNaN(cursor);
                    item.quantity = 1;
                    item.chosenPriceId = cursor
                            .getShort(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_CHOSEN_PRICE_ID));
                    item.volume = Double.NaN;

                    items.append(item);
                    items.putAll(getInvestItems(context, producedItemId));

                    cursor.moveToNext();
                }
            }
        } finally {
            cursor.close();
        }

        return items;
    }

    private static SparseItemBeanArray getInvestParentBlueprint(Context context, long itemId) {
        SparseItemBeanArray items = new SparseItemBeanArray();

        final Cursor cursor = context.getContentResolver()
                .query(ContentUris.withAppendedId(Blueprint.CONTENT_ITEM_ID_URI_BASE, itemId),
                        new String[] { Blueprint._ID, Blueprint.COLUMN_NAME_NAME,
                                Blueprint.COLUMN_NAME_PARENT_TYPE_ID, Blueprint.COLUMN_NAME_TECH_LEVEL,
                                Blueprint.COLUMN_NAME_DECRYPTOR_ID },
                        null, null, null);

        try {
            if (DbUtil.hasAtLeastOneRow(cursor)) {

                ItemBeanWithMaterials item = new ItemBeanWithMaterials();
                item.id = cursor.getInt(cursor.getColumnIndexOrThrow(Blueprint._ID));
                item.name = cursor.getString(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_NAME));
                //item.price = PricesUtils.getPriceOrNaN(cursor);
                item.quantity = 1;
                //item.chosenPriceId = cursor.getShort(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_CHOSEN_PRICE_ID));
                item.volume = Double.NaN;

                items.append(item);

                int techLevel = cursor.getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_TECH_LEVEL));
                int parentTypeId = cursor
                        .getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_PARENT_TYPE_ID));
                int decryptorId = cursor.getInt(cursor.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_DECRYPTOR_ID));

                if (techLevel == 2) {
                    final Cursor cursorDataInterface = context.getContentResolver().query(
                            ContentUris.withAppendedId(Blueprint.CONTENT_ID_URI_INVENTION_BASE, parentTypeId),
                            new String[] { "i." + Item._ID + " AS " + Item._ID, ItemMaterials.COLUMN_NAME_QUANTITY,
                                    Item.COLUMN_NAME_CHOSEN_PRICE_ID, Item.COLUMN_NAME_NAME,
                                    Prices.COLUMN_NAME_BUY_PRICE, Prices.COLUMN_NAME_SELL_PRICE,
                                    Prices.COLUMN_NAME_OWN_PRICE, Prices.COLUMN_NAME_PRODUCE_PRICE },
                            null, new String[] { decryptorId == 0 ? null : String.valueOf(decryptorId) },
                            "i." + Item._ID + " ASC");

                    try {
                        if (DbUtil.hasAtLeastOneRow(cursorDataInterface)) {
                            while (!cursorDataInterface.isAfterLast()) {

                                item = new ItemBeanWithMaterials();
                                item.id = cursorDataInterface
                                        .getInt(cursorDataInterface.getColumnIndexOrThrow(Item._ID));
                                item.name = cursorDataInterface.getString(
                                        cursorDataInterface.getColumnIndexOrThrow(Item.COLUMN_NAME_NAME));
                                item.price = PricesUtils.getPriceOrNaN(cursorDataInterface);
                                item.quantity = 1;
                                item.chosenPriceId = cursorDataInterface.getShort(cursorDataInterface
                                        .getColumnIndexOrThrow(Item.COLUMN_NAME_CHOSEN_PRICE_ID));
                                item.volume = Double.NaN;

                                if (EOITConst.Invention.DATA_INTERFACE_IDS.contains((long) item.id))
                                    items.append(item);

                                cursorDataInterface.moveToNext();
                            }
                        }
                    } finally {
                        cursorDataInterface.close();
                    }
                }
            }
        } finally {
            cursor.close();
        }

        return items;
    }

    public static class InvestItemsLoaderCallBacks implements LoaderCallbacks<SparseItemBeanArray> {

        private Activity activity;
        private int itemId;
        private OnLoadFinishedListener listener;

        public InvestItemsLoaderCallBacks(FragmentActivity activity, OnLoadFinishedListener listener, int itemId) {
            this.activity = activity;
            this.itemId = itemId;
            this.listener = listener;
        }

        @Override
        public Loader<SparseItemBeanArray> onCreateLoader(int id, Bundle args) {
            return new InvestItemsLoader(activity, itemId);
        }

        @Override
        public void onLoadFinished(Loader<SparseItemBeanArray> loader, SparseItemBeanArray data) {
            listener.onLoadFinishedInvestItems(data);
        }

        @Override
        public void onLoaderReset(Loader<SparseItemBeanArray> loader) {
        }
    }

    public interface OnLoadFinishedListener {
        public void onLoadFinishedInvestItems(SparseItemBeanArray data);
    }
}