cc2k

Untitled

Dec 19th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. package com.moyoweb.winescanner;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import android.app.Application;
  12. import android.content.Context;
  13. import android.os.Parcel;
  14. import android.os.Parcelable;
  15.  
  16. public class Wine implements Parcelable {
  17.  
  18.     private String barcode;
  19.     private String barcodeType;
  20.     private String name;
  21.     private int year;
  22.     private String country;
  23.     private String taste;
  24.     private String color;
  25.     private String description;
  26.     private boolean sparkling;
  27.     private boolean biologic;
  28.     private int wineImage;
  29.     private static Context context;
  30.  
  31.     public Wine() {
  32.     }
  33.  
  34.     public Wine(String barcode, String barcodeType, String name, int year,
  35.             String country, String taste, String color, String description,
  36.             Boolean sparkling, Boolean biologic) {
  37.         this.barcode = barcode;
  38.         this.barcodeType = barcodeType;
  39.         this.name = name;
  40.         this.year = year;
  41.         this.country = country;
  42.         this.taste = taste;
  43.         this.color = color;
  44.         this.description = description;
  45.         this.sparkling = sparkling;
  46.         this.biologic = biologic;
  47.         Wine.context = context.getApplicationContext();
  48.     }
  49.    
  50.     public static ArrayList<Wine> fromJSON(ArrayList<Wine> wineListFromWSRArrayList) {
  51.        
  52.         return wineListFromWSRArrayList;
  53.     }
  54.  
  55.     public static Wine fromJSON(JSONObject jObject) {
  56.         Wine wine = new Wine();
  57.         JSONObject jOb = jObject;
  58.  
  59.         try {
  60.             wine.setBarcode(jOb.getString("barcode"));
  61.             //wine.setBarcodeType(jOb.getString("barcodeType"));
  62.             wine.setName(jOb.getString("name"));
  63.             wine.setYear(jOb.getInt("year"));
  64.             wine.setCountry(jOb.getString("country"));
  65.             wine.setTaste(jOb.getString("taste"));
  66.             wine.setColor(jOb.getString("color"));
  67.             //wine.setDescription(jOb.getString("description"));
  68.             //wine.setSparkling(parseToBool(jOb.getInt("sparkling")));
  69.             //wine.setBiologic(parseToBool(jOb.getInt("biologic")));
  70.             wine.setWineImage(0);//wanneer er een link is kunnen we deze gebruiken.
  71.  
  72.             return wine;
  73.  
  74.         } catch (JSONException e) {
  75.             // TODO Auto-generated catch block
  76.             e.printStackTrace();
  77.         }
  78.         return wine;
  79.     }
  80.    
  81.     public static List<Wine> fromJSONURL(JSONArray jsonAr) {
  82.         List<Wine> wines = new ArrayList<Wine>();
  83.  
  84.         for (int i = 0; i < jsonAr.length(); i++) {
  85.             try {
  86.                 Wine wine = new Wine();
  87.                 JSONObject jWine = jsonAr.getJSONObject(i);
  88.                 JSONArray jsarDescription = jWine.getJSONArray("descriptions");
  89.  
  90.                 wine.setBarcode(jWine.getString("uuid"));//testdoeleindes, dit is uniq id van de wijnen!
  91.                 //wine.setBarcodeType(jWine.getString("barcodeType"));
  92.                 wine.setName(jWine.getString("title"));
  93.                 wine.setCountry(jWine.getString("country"));
  94.                 //wine.setBiologic(parseToBool(jWine.getInt("biologic")));
  95.                 wine.setColor(jWine.getString("color"));
  96.                
  97.                 String descriptions="";
  98.                 int j =0;
  99.                 Random randDescript = new Random();
  100.                 j = randDescript.nextInt(jsarDescription.length());
  101.  
  102.  
  103. //dante here is the error
  104.                
  105.                     JSONObject obDesc = jsarDescription.getJSONObject(j);
  106.  
  107.                     descriptions = descriptions + context.getResources().getString(com.moyoweb.winescanner.R.string.this_is_what_from_description)  
  108.                             +obDesc.getJSONObject("store").getString("title")+ ""
  109.                             +  context.getResources().getString(com.moyoweb.winescanner.R.string.write_about_wine_from_description)  
  110.                             +obDesc.getString("description");
  111.                    
  112.                
  113.                
  114.                
  115.                 wine.setDescription(descriptions);//aanpassen zodat we alle descriptions krijgen.
  116.                
  117.                
  118.                
  119.                 //wine.setSparkling(parseToBool(jWine.getInt("sparkling")));
  120.                 wine.setTaste(jWine.getString("taste"));
  121.                 if(!jWine.getString("year").equalsIgnoreCase("")){
  122.                 wine.setYear(Integer.parseInt(jWine.getString("year")));
  123.                 }
  124.                 if(!jWine.has("image")){
  125.                     wine.setWineImage(com.moyoweb.winescanner.R.drawable.image_not_available);//wanneer er een link is kunnen we deze gebruiken.
  126.                    
  127.                 }else if(!jWine.getString("image").equalsIgnoreCase("")){
  128.                 wine.setWineImage(com.moyoweb.winescanner.R.drawable.image_not_available);//wanneer er een link is kunnen we deze gebruiken.
  129.                 }else{
  130.                     wine.setWineImage(com.moyoweb.winescanner.R.drawable.marques_de_arienzo_crianza_rode_spanje);//nep, kijken hoe we deze opvangen
  131.                 }
  132.                
  133.                 wines.add(wine);
  134.  
  135.             } catch (JSONException e) {
  136.                 // TODO Auto-generated catch block
  137.                 e.printStackTrace();
  138.             }
  139.         }
  140.         return wines;
  141.     }
  142.  
  143.     public static Boolean parseToBool(int int1) {
  144.         if (int1 == 1) {
  145.             return true;
  146.         } else if (int1 == 0) {
  147.             return false;
  148.         }
  149.         return null;
  150.     }
  151.    
  152.     private static int BoolparseToInt(Boolean bool) {
  153.         if (bool) {
  154.             return 1;
  155.         } else if (!bool) {
  156.             return 0;
  157.         }
  158.         return -1;
  159.     }
  160.  
  161.     public Boolean getBiologic() {
  162.         return biologic;
  163.     }
  164.  
  165.     public Boolean getSparkling() {
  166.         return sparkling;
  167.     }
  168.  
  169.     public String getDescription() {
  170.         return description;
  171.     }
  172.  
  173.     public String getColor() {
  174.         return color;
  175.     }
  176.  
  177.     public String getTaste() {
  178.         return taste;
  179.     }
  180.  
  181.     public String getCountry() {
  182.         return country;
  183.     }
  184.  
  185.     public int getYear() {
  186.         return year;
  187.     }
  188.  
  189.     public String getName() {
  190.         return name;
  191.     }
  192.  
  193.     public String getBarcodeType() {
  194.         return barcodeType;
  195.     }
  196.  
  197.     public String getBarcode() {
  198.         return barcode;
  199.     }
  200.  
  201.     public int getWineImage() {
  202.         return wineImage;
  203.     }
  204.  
  205.     public void setWineImage(int imageNotAvailable) {
  206.         this.wineImage = imageNotAvailable;
  207.     }
  208.  
  209.     public void setBiologic(Boolean biologic) {
  210.         this.biologic = biologic;
  211.     }
  212.  
  213.     public void setSparkling(Boolean sparkling) {
  214.         this.sparkling = sparkling;
  215.     }
  216.  
  217.     public void setDescription(String description) {
  218.         this.description = description;
  219.     }
  220.  
  221.     public void setColor(String color) {
  222.         this.color = color;
  223.     }
  224.  
  225.     public void setTaste(String taste) {
  226.         this.taste = taste;
  227.     }
  228.  
  229.     public void setCountry(String country) {
  230.         this.country = country;
  231.     }
  232.  
  233.     public void setYear(int year) {
  234.         this.year = year;
  235.     }
  236.  
  237.     public void setName(String name) {
  238.         this.name = name;
  239.     }
  240.  
  241.     public void setBarcodeType(String barcodeType) {
  242.         this.barcodeType = barcodeType;
  243.     }
  244.  
  245.     public void setBarcode(String barcode) {
  246.         this.barcode = barcode;
  247.     }
  248.  
  249. //  // Parcelling part
  250.     private Wine(Parcel in) {
  251.         this.barcode = in.readString();
  252.         this.barcodeType = in.readString();
  253.         this.name = in.readString();
  254.         this.year = in.readInt();
  255.         this.country = in.readString();
  256.         this.taste = in.readString();
  257.         this.color =in.readString();
  258.         this.description = in.readString();
  259.         this.sparkling = parseToBool(in.readInt());
  260.         this.biologic = parseToBool(in.readInt());
  261.     }
  262.  
  263.     @Override
  264.     public int describeContents() {
  265.         // TODO Auto-generated method stub
  266.         return 0;
  267.     }
  268.  
  269.     @Override
  270.     public void writeToParcel(Parcel dest, int flags) {
  271.         dest.writeString(barcode);
  272.         dest.writeString(barcodeType);
  273.         dest.writeString(name);
  274.         dest.writeInt(year);
  275.         dest.writeString(country);
  276.         dest.writeString(taste);
  277.         dest.writeString(color);
  278.         dest.writeString(description);
  279.         dest.writeInt(BoolparseToInt(sparkling));
  280.         dest.writeInt(BoolparseToInt(biologic));
  281.     }
  282.    
  283.     public static final Parcelable.Creator<Wine> CREATOR = new Parcelable.Creator<Wine>() {
  284.          
  285.         @Override
  286.         public Wine createFromParcel(Parcel source) {
  287.             return new Wine(source);
  288.         }
  289.  
  290.         @Override
  291.         public Wine[] newArray(int size) {
  292.             return new Wine[size];
  293.         }
  294.     };
  295. }
Advertisement
Add Comment
Please, Sign In to add comment