greyhat49

waterprice DS

Feb 1st, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.76 KB | None | 0 0
  1. package id.tukang.database.dao;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14.  
  15. import id.tukang.database.MySQLiteHelper;
  16. import id.tukang.database.model.WaterPrice;
  17.  
  18. public class WaterPricesDS {
  19.  
  20.     // table fields
  21.     private SQLiteDatabase database;
  22.     private MySQLiteHelper dbHelper;
  23.  
  24.     private String[] allColumnsByRankingParsing = {
  25.             MySQLiteHelper.COLUMN_RANKING
  26.             , MySQLiteHelper.COLUMN_ID
  27.             , MySQLiteHelper.COLUMN_VENDOR
  28.             , MySQLiteHelper.COLUMN_PRICE
  29.             , MySQLiteHelper.COLUMN_NAME_BRAND
  30.             , MySQLiteHelper.COLUMN_IMAGE_NAME
  31.     };
  32.     private String[] allColumnsByRanking = {
  33.             MySQLiteHelper.COLUMN_ID
  34.             , MySQLiteHelper.COLUMN_VENDOR
  35.             , MySQLiteHelper.COLUMN_JSON_PRODUCT_VENDOR
  36.     };
  37.     private String[] allColumns = {
  38.             MySQLiteHelper.COLUMN_ID
  39.             , MySQLiteHelper.COLUMN_PRICE
  40.             , MySQLiteHelper.COLUMN_NAME_BRAND
  41.             , MySQLiteHelper.COLUMN_IMAGE_NAME
  42.     };
  43.  
  44.     public WaterPricesDS(Context context) {
  45.         dbHelper = new MySQLiteHelper(context);
  46.     }
  47.  
  48.     public void open() throws SQLException {
  49.         database = dbHelper.getWritableDatabase();
  50.     }
  51.  
  52.     public void close() {
  53.         dbHelper.close();
  54.     }
  55.  
  56.     public void addPrice(int id, long price) {
  57.  
  58.         ContentValues values = new ContentValues();
  59.         values.put(MySQLiteHelper.COLUMN_ID, id);
  60.         values.put(MySQLiteHelper.COLUMN_PRICE, price);
  61.         database.insert(MySQLiteHelper.TABLE_WATER_PRICES, null,
  62.                 values);
  63.     }
  64.  
  65.     //Septian: override method
  66.     public void addPrice(int id, long price, String name) {
  67.  
  68.         ContentValues values = new ContentValues();
  69.         values.put(MySQLiteHelper.COLUMN_ID, id);
  70.         values.put(MySQLiteHelper.COLUMN_PRICE, price);
  71.         values.put(MySQLiteHelper.COLUMN_NAME_BRAND, name);
  72.         database.insert(MySQLiteHelper.TABLE_WATER_PRICES, null,
  73.                 values);
  74.     }
  75.  
  76.     //Septian: override method
  77.     public void addPrice(int id, long price, String name, String url) {
  78.         ContentValues values = new ContentValues();
  79.         values.put(MySQLiteHelper.COLUMN_ID, id);
  80.         values.put(MySQLiteHelper.COLUMN_PRICE, price);
  81.         values.put(MySQLiteHelper.COLUMN_NAME_BRAND, name);
  82.         values.put(MySQLiteHelper.COLUMN_IMAGE_NAME, url);
  83.         database.insert(MySQLiteHelper.TABLE_WATER_PRICES, null,
  84.                 values);
  85.     }
  86.  
  87.     //Septian: override method vendor ranking
  88.     public void addPrice(int id, String vendor, String jsonproduct) {
  89.         ContentValues values = new ContentValues();
  90.         values.put(MySQLiteHelper.COLUMN_ID, id);
  91.         values.put(MySQLiteHelper.COLUMN_VENDOR, vendor);
  92.         values.put(MySQLiteHelper.COLUMN_JSON_PRODUCT_VENDOR, jsonproduct);
  93.  
  94.         database.insert(MySQLiteHelper.TABLE_WATER_PRICE_BY_RANKING, null,
  95.                 values);
  96.     }
  97.  
  98.     //septian: vendor ranking parsing
  99.     public void addPrice(int ranking, String vendor, int id, long price, String name, String url) {
  100.         ContentValues values = new ContentValues();
  101.         values.put(MySQLiteHelper.COLUMN_RANKING, ranking);
  102.         values.put(MySQLiteHelper.COLUMN_ID, id);
  103.         values.put(MySQLiteHelper.COLUMN_VENDOR, vendor);
  104.         values.put(MySQLiteHelper.COLUMN_PRICE, price);
  105.         values.put(MySQLiteHelper.COLUMN_NAME_BRAND, name);
  106.         values.put(MySQLiteHelper.COLUMN_IMAGE_NAME, url);
  107.  
  108.         database.insert(MySQLiteHelper.TABLE_WATER_PRICE_BY_RANKING_PARSING, null,
  109.                 values);
  110.     }
  111.  
  112.     public void deleteAllPrice() {
  113.         database.delete(MySQLiteHelper.TABLE_WATER_PRICES, null, null);
  114.         database.delete(MySQLiteHelper.TABLE_WATER_PRICE_BY_RANKING_PARSING, null, null);
  115.     }
  116.  
  117. //    public List<WaterPrice> getAllPrices() {
  118. //        List<WaterPrice> waterPrices = new ArrayList<>();
  119. //
  120. //        Cursor cursor = database.query(MySQLiteHelper.TABLE_WATER_PRICES,
  121. //                allColumns, null, null, null, null, null);
  122. //
  123. //        cursor.moveToFirst();
  124. //        while (!cursor.isAfterLast()) {
  125. //          WaterPrice waterPrice = cursorToItem(cursor);
  126. //            waterPrices.add(waterPrice);
  127. //            cursor.moveToNext();
  128. //        }
  129. //        // make sure to close the cursor
  130. //        cursor.close();
  131. //        return waterPrices;
  132. //    }
  133.  
  134.     public WaterPrice getWaterPrice(int id) {
  135.         Cursor cursor = database.query(MySQLiteHelper.TABLE_WATER_PRICES,
  136.                 allColumns, " " + MySQLiteHelper.COLUMN_ID + " = ? ", new String[]{String.valueOf(id)}, null, null, null);
  137.  
  138.         WaterPrice waterPrice = null;
  139.         if (cursor.getCount() > 0) {
  140.             cursor.moveToFirst();
  141.             waterPrice = cursorToItem(cursor);
  142.             cursor.close();
  143.         }
  144.         return waterPrice;
  145.     }
  146.  
  147.     //Septian: get brand name from all vendor available
  148.     public String[] getBrandNameArr() {
  149.  
  150.         Cursor cursor = database.query(MySQLiteHelper.TABLE_WATER_PRICES,
  151.                 allColumns, null, null, null, null, null);
  152.  
  153.         String[] waterName = new String[cursor.getCount() + 1];
  154.         int i = 1;
  155.         if (cursor.getCount() > 0) {
  156.             cursor.moveToFirst();
  157.             if (cursor.moveToFirst()) {
  158.                 do {
  159.                     waterName[i] = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_BRAND));
  160.                     i++;
  161.                 } while (cursor.moveToNext());
  162.             }
  163.             cursor.close();
  164.         }
  165.  
  166.         return waterName;
  167.     }
  168.  
  169.     public String[] getBrandNameArrOnVendorRanking(String[] vendorInRanking) {
  170.         List vendorInRankingStr = Arrays.asList(vendorInRanking);
  171.         String ven = vendorInRankingStr.toString();
  172.         String[] venStr;
  173.  
  174.         //replace bracket with paranthesses
  175.         ven = ven.replace("[", "");
  176.         ven = ven.replace("]", "");
  177.  
  178.         venStr = ven.split(",");
  179.         for (int i = 0; i < venStr.length; i++) {
  180.             venStr[i] = "'"+venStr[i]+"'";
  181.         }
  182.  
  183.         List vendorInRankingStr2 = Arrays.asList(venStr);
  184.         String ven2 = vendorInRankingStr2.toString();
  185.  
  186.         ven2 = ven2.replace("[","(");
  187.         ven2 = ven2.replace("]", ")");
  188.  
  189.         String query = "select distinct(" + MySQLiteHelper.COLUMN_NAME_BRAND + ")"
  190.                 + " from " + MySQLiteHelper.TABLE_WATER_PRICE_BY_RANKING_PARSING
  191.                 + " where " + MySQLiteHelper.COLUMN_VENDOR + " in " + ven2;
  192.  
  193.         Cursor cursor = database.rawQuery(query, null);
  194.  
  195.         String[] brandName = new String[cursor.getCount() + 1];
  196.  
  197.         int i = 1;
  198.         if (cursor.getCount() > 0) {
  199.             cursor.moveToFirst();
  200.             if (cursor.moveToFirst()) {
  201.                 do {
  202.                     brandName[i] = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_BRAND));
  203.                     i++;
  204.                 } while (cursor.moveToNext());
  205.             }
  206.             cursor.close();
  207.         }
  208.         return brandName;
  209.     }
  210.  
  211.     public String[] getVendorNameArrOnVendorRanking(String brandName) {
  212.  
  213.         Cursor cursor = database.query(true
  214.                 , MySQLiteHelper.TABLE_WATER_PRICE_BY_RANKING_PARSING
  215.                 , allColumnsByRankingParsing
  216.                 , MySQLiteHelper.COLUMN_NAME_BRAND + " = ?"
  217.                 , new String[]{brandName}
  218.                 , null, null, null, null);
  219.  
  220.         String[] vendorName = new String[cursor.getCount()];
  221.         int i = 0;
  222.         if (cursor.getCount() > 0) {
  223.             cursor.moveToFirst();
  224.             if (cursor.moveToFirst()) {
  225.                 do {
  226.                     vendorName[i] = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_VENDOR));
  227.                     i++;
  228.                 } while (cursor.moveToNext());
  229.             }
  230.             cursor.close();
  231.         }
  232.         return vendorName;
  233.     }
  234.  
  235.     public int getPriceCount() {
  236.         String countQuery = "SELECT  " + MySQLiteHelper.COLUMN_ID + " FROM " + MySQLiteHelper.TABLE_WATER_PRICES;
  237.         //      SQLiteDatabase db = this.getReadableDatabase();
  238.         Cursor cursor = database.rawQuery(countQuery, null);
  239.         int count = cursor.getCount();
  240.         cursor.close();
  241.  
  242.         return count;
  243.     }
  244.  
  245.     //get item based in row in db sqlite
  246.     private WaterPrice cursorToItem(Cursor cursor) {
  247.         WaterPrice waterPrice = new WaterPrice();
  248.         waterPrice.setId(cursor.getInt(0));
  249.         waterPrice.setPrice(cursor.getLong(1));
  250.         waterPrice.setName(cursor.getString(2));
  251.         return waterPrice;
  252.     }
  253.  
  254.     //this gonna be help to query data sqlite based on name of product
  255.     //this is representing of 'product Id' not 'brand Id'
  256.     public int getBrandIdOnName(String brandName) {
  257.         String query = "SELECT "
  258.                 + MySQLiteHelper.COLUMN_ID + " FROM "
  259.                 + MySQLiteHelper.TABLE_WATER_PRICES + " WHERE "
  260.                 + MySQLiteHelper.COLUMN_NAME_BRAND + " = '" + brandName + "'";
  261.  
  262.         Cursor cursor = database.rawQuery(query, null);
  263.         int idProduct = -2;
  264.  
  265.         cursor.moveToFirst();
  266.         if (cursor.moveToFirst()) {
  267.             idProduct = cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
  268.         }
  269.         cursor.close();
  270.  
  271.         return idProduct;
  272.     }
  273.  
  274.     public String getNameBrandOnName(String brandName) {
  275.         String query = "SELECT "
  276.                 + MySQLiteHelper.COLUMN_NAME_BRAND + " FROM "
  277.                 + MySQLiteHelper.TABLE_WATER_PRICES + " WHERE "
  278.                 + MySQLiteHelper.COLUMN_NAME_BRAND + " = '" + brandName + "'";
  279.  
  280.         Cursor cursor = database.rawQuery(query, null);
  281.         String nameBrand = "";
  282.  
  283.         cursor.moveToFirst();
  284.         if (cursor.moveToFirst()) {
  285.             nameBrand = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_BRAND));
  286.         }
  287.         cursor.close();
  288.  
  289.         return nameBrand;
  290.     }
  291.  
  292.     public long getPriceBrandOnName(String brandName) {
  293.         String query = "SELECT "
  294.                 + MySQLiteHelper.COLUMN_PRICE + " FROM "
  295.                 + MySQLiteHelper.TABLE_WATER_PRICES + " WHERE "
  296.                 + MySQLiteHelper.COLUMN_NAME_BRAND + " = '" + brandName + "'";
  297.  
  298.         Cursor cursor = database.rawQuery(query, null);
  299.         long priceBrand = -2;
  300.  
  301.         cursor.moveToFirst();
  302.         if (cursor.moveToFirst()) {
  303.             priceBrand = cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.COLUMN_PRICE));
  304.         }
  305.         cursor.close();
  306.  
  307.         return priceBrand;
  308.     }
  309.  
  310.     public String getImageBrandOnName(String brandName) {
  311.         String query = "SELECT "
  312.                 + MySQLiteHelper.COLUMN_IMAGE_NAME + " FROM "
  313.                 + MySQLiteHelper.TABLE_WATER_PRICES + " WHERE "
  314.                 + MySQLiteHelper.COLUMN_NAME_BRAND + " = '" + brandName + "'";
  315.  
  316.         Cursor cursor = database.rawQuery(query, null);
  317.         String urlImage = "invalid_url";
  318.  
  319.         cursor.moveToFirst();
  320.         if (cursor.moveToFirst()) {
  321.             urlImage = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_IMAGE_NAME));
  322.         }
  323.         cursor.close();
  324.  
  325.         return urlImage;
  326.     }
  327.  
  328.     //Septian: get all value vendor product by ranking
  329.     public Map getAllProductVendorByRanking() {
  330.         Cursor cursor = database.query(MySQLiteHelper.TABLE_WATER_PRICE_BY_RANKING,
  331.                 allColumnsByRanking, null, null, null, null, null);
  332.  
  333.         Map<String, List<String>> mapJson = new HashMap<>();
  334.         List<String> listVendorProduct = new ArrayList<>();
  335.  
  336.         /*String[] vendor = new String[cursor.getCount() + 1];
  337.         String[] jsonProduct = new String[cursor.getCount() + 1];*/
  338.  
  339.         int i = 0;
  340.         if (cursor.getCount() > 0) {
  341.             cursor.moveToFirst();
  342.             if (cursor.moveToFirst()) {
  343.                 do {
  344.                     /*vendor[i] = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_VENDOR));
  345.                     jsonProduct[i] = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_JSON_PRODUCT_VENDOR));*/
  346.  
  347.                     listVendorProduct.add(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_VENDOR)));
  348.                     listVendorProduct.add(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_JSON_PRODUCT_VENDOR)));
  349.                     mapJson.put(String.valueOf(i), listVendorProduct);
  350.  
  351.                     i++;
  352.                 } while (cursor.moveToNext());
  353.             }
  354.             cursor.close();
  355.         }
  356.         return mapJson;
  357.     }
  358. }
Add Comment
Please, Sign In to add comment