com.savedollars.ProductStockDisplay.java Source code

Java tutorial

Introduction

Here is the source code for com.savedollars.ProductStockDisplay.java

Source

/**************************************************************************************
SaveDollars  An open source Android application that helps users to compare prices 
of a product across different ecommerce sites and make a decision about purchase.
    
Copyright (C) 2013 Smita Kundargi and Jeanne Betcy Victor
    
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 3 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, see http://www.gnu.org/licenses/.
    
Following is the link for the repository: https://github.com/SmitaBetcy/SaveDollars
    
Please, see the file license in this distribution for license terms. Link is
https://github.com/SmitaBetcy/SaveDollars/blob/master/License
    
References:
https://developers.google.com/shopping-search/v1/reference-response-format
https://developers.google.com/shopping-search/v1/getting_started
https://code.google.com/p/zxing/wiki/ScanningViaIntent
http://stackoverflow.com/questions/8632529/listview-with-multiple-strings
http://sweetclipart.com/
    
    
Author - Smita Kundargi and Jeanne Betcy Victor
email: ksmita@pdx.edu and jbv3@pdx.edu
    
 ******************************************************************************************/

package com.savedollars;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.adapter.ListViewAdapter;
import com.example.savedollars.R;

/*******************************************************************************************************
** ProductStockDisplay is used to parse the JSON Data which is retrieved calling the google 
** shopping API to display the availability of the product for different merchants and it 
** allows the user to visit the home page of the merchant by one click.
*********************************************************************************************************/

public class ProductStockDisplay extends ListActivity {

    private String JSONData = "";
    private Map merchantMap = new HashMap();
    private Map availabilityMap = new HashMap();
    private Map<Object, Object> sortedMap = new LinkedHashMap<Object, Object>();
    private int totalCount = 0;
    private String[][] PDT_INFO;
    private String[] merchantNames;
    public String pdtName;
    public String merchantPage;
    private Map merchantLinkMap = new HashMap();
    private static final String LOG_TAG = "ProductStockDisplay";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
                .detectNetwork().penaltyLog().build());

        JSONData = (getIntent().getStringExtra("JsonData"));

        if (JSONData != null) {
            parseJsonData(JSONData);
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.pdtstockview);
        // Setting Product Name
        TextView productName = (TextView) findViewById(R.id.pdtNameTextView);
        productName.setText(pdtName);

        Iterator it = sortedMap.keySet().iterator();
        int rowIndex = 0;

        PDT_INFO = new String[totalCount][2];
        while (it.hasNext()) {

            String key = ProductTotalPriceDisplay.merchantNames[rowIndex];
            String availability = (String) availabilityMap.get(key);
            PDT_INFO[rowIndex][0] = key;
            PDT_INFO[rowIndex][1] = availability;
            rowIndex++;
            it.next();
        }

        ListViewAdapter listv = new ListViewAdapter(this, PDT_INFO);

        setListAdapter(listv);

        final ListView lv = getListView();

        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                lv.getItemAtPosition(position);
                String pdtKey = PDT_INFO[position][0];
                String merchantLink = (String) merchantLinkMap.get(pdtKey);

                Uri uri = Uri.parse(merchantLink);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /*
     * Method name : parseJsonData Arguments : String Description : Parses the
     * Json Data for availabilty of product Returns : void
     */

    private void parseJsonData(String data) {
        try {
            JSONObject jsonResponse = new JSONObject(data);
            // Get the names
            //         JSONArray arr = jsonResponse.names();
            if (jsonResponse.has("items")) {

                JSONArray parsedItems = jsonResponse.getJSONArray("items");
                JSONObject inventory = null;

                for (int j = 0; j < parsedItems.length(); j++) {

                    inventory = parsedItems.getJSONObject(j);

                    JSONObject objPrice = inventory.getJSONObject("product");
                    JSONObject merchant = objPrice.getJSONObject("author");
                    String merchantName = merchant.getString("name");

                    JSONArray invObj = objPrice.getJSONArray("inventories");

                    for (int z = 0; z < invObj.length(); z++) {
                        JSONObject price = invObj.getJSONObject(z);
                        String productPrice = price.getString("price");
                        String shipping = "0.0";
                        if (price.has("shipping")) {
                            shipping = price.getString("shipping");
                        }
                        String availability = price.getString("availability");
                        float finalPrice = Float.parseFloat(productPrice) + Float.parseFloat(shipping);

                        merchantMap.put(merchantName, finalPrice);
                        availabilityMap.put(merchantName, availability);

                    }
                    pdtName = objPrice.getString("title");
                    // retrieve merchant page
                    merchantPage = objPrice.getString("link");

                    merchantLinkMap.put(merchantName, merchantPage);
                }

                sortMerchantPrices();

            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, "Cannot process JSON results", e);
        }

    }

    /*
     * Method : sortMerchantPrices Argument: none Description : Sorts the
     * merchants and availability by price Returns : None
     */
    private void sortMerchantPrices() {
        List objList = new LinkedList(merchantMap.entrySet());

        Collections.sort(objList, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
            }
        });

        for (Iterator it = objList.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            sortedMap.put(entry.getKey(), entry.getValue());
            totalCount++;
        }

    }

    /* Code for the buttons */

    public void pdttotalpriceview(View v) {
        Intent searchIntent = new Intent(ProductStockDisplay.this, ProductTotalPriceDisplay.class);
        searchIntent.putExtra("JsonData", JSONData);
        startActivity(searchIntent);
    }

    public void pdtpriceview(View v) {
        Intent searchIntent = new Intent(ProductStockDisplay.this, ProductPriceDisplay.class);
        searchIntent.putExtra("JsonData", JSONData);
        startActivity(searchIntent);
    }

    public void pdtshippingpriceview(View v) {
        Intent searchIntent = new Intent(ProductStockDisplay.this, ProductShippingPriceDisplay.class);
        searchIntent.putExtra("JsonData", JSONData);
        startActivity(searchIntent);
    }

    public void pdtstockview(View v) {
        Intent searchIntent = new Intent(ProductStockDisplay.this, ProductStockDisplay.class);
        searchIntent.putExtra("JsonData", JSONData);
        startActivity(searchIntent);
    }

    public void activity_main(View v) {
        Intent searchIntent = new Intent(ProductStockDisplay.this, MainActivity.class);
        startActivity(searchIntent);
    }

}