Rev 2799 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.ui.util;import in.shop2020.metamodel.core.EntityState;import in.shop2020.metamodel.util.CreationUtils;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.Serializable;import java.net.URL;import java.net.URLConnection;import java.util.Calendar;import java.util.Comparator;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;import java.util.Map.Entry;import java.util.TreeMap;import org.json.JSONException;import org.json.JSONObject;public class ComparisonStatsFetcher implements Serializable{private static final long serialVersionUID = -326425176723602170L;public static final String GOOGLE_COMPARE_DATA_URL = "http://saholic-datastore.appspot.com/show-comparisons";class ValueComparator implements Comparator, Serializable {private static final long serialVersionUID = 6060119992669086920L;Map base;public ValueComparator(Map base) {this.base = base;}public int compare(Object a, Object b) {if((Long)base.get(a) < (Long)base.get(b)) {return 1;} else {return -1;}}}public void fetchAndStoreComparisonStats(){String data = ComparisonStatsFetcher.sendGetRequest(GOOGLE_COMPARE_DATA_URL, null);try {JSONObject json = new JSONObject(data);Map<Long, Map<Long, Long>> comparisonStats = new HashMap<Long, Map<Long, Long>>();for (String itemId : JSONObject.getNames(json)) {Map<Long, Long> freq = new HashMap<Long, Long>();JSONObject items = (JSONObject) json.get(itemId);for (String comparedItemId : JSONObject.getNames(items)) {Integer frequency = (Integer) items.get(comparedItemId);freq.put(Long.parseLong(comparedItemId), frequency.longValue());}comparisonStats.put(Long.parseLong(itemId), freq);}if(comparisonStats != null){Map<Long, Map<Long, Long>> sortedComparisonStats = new HashMap<Long, Map<Long, Long>>();for(Entry<Long, Map<Long, Long>> entry: comparisonStats.entrySet()){ValueComparator bvc = new ValueComparator(entry.getValue());TreeMap<Long, Long> sortedMap = new TreeMap(bvc);sortedMap.putAll(entry.getValue());sortedComparisonStats.put(entry.getKey(), sortedMap);}CreationUtils.storeComparisonStats(sortedComparisonStats);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static Map<String, Long> getMostComparedPhones(Map<Long, Map<Long, Long>> comparisonStats){Map<String, Long> allCounts = new HashMap<String, Long>();for(Long key: comparisonStats.keySet()){for(Long key1: comparisonStats.get(key).keySet()){String newKey;if(key<key1){newKey = key + " : " + key;//newKey = key + "\t" + ComparisonStatsFetcher.getProductName(es) + " : " + key1 + "\t" + ComparisonStatsFetcher.getProductName(es1);}else{//newKey = key1 + "\t" + ComparisonStatsFetcher.getProductName(es1) + " : " + key + "\t" + ComparisonStatsFetcher.getProductName(es);newKey = key1 + " : " + key;}allCounts.put(newKey, comparisonStats.get(key).get(key1));}}return allCounts;}private static Map<Long, Long> getTopComparedPhones(Map<Long, Map<Long, Long>> comparisonStats){Map<Long, Long> mostCompared = new LinkedHashMap<Long, Long>();for(Long key: comparisonStats.keySet()){long count = 0;for(Long key1: comparisonStats.get(key).keySet()){count += comparisonStats.get(key).get(key1);}mostCompared.put(key, new Long(count));}return mostCompared;}public static void main(String[] args) throws Exception{Calendar cal = Calendar.getInstance();//int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);//if(dayOfWeek == Calendar.SUNDAY){long toDate = cal.getTime().getTime();cal.add(Calendar.MONTH, -1);long fromDate = cal.getTime().getTime();ComparisonStatsFetcher csf = new ComparisonStatsFetcher();csf.fetchAndStoreComparisonStats();}/**** @param endpoint* @param requestParameters* @return*/public static String sendGetRequest(String endpoint, String requestParameters){String result = null;try{String urlStr = endpoint;if (requestParameters != null && requestParameters.length () > 0){urlStr += "?" + requestParameters;}URL url = new URL(urlStr);URLConnection conn = url.openConnection ();// Get the responseBufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));StringBuffer sb = new StringBuffer();String line;while ((line = rd.readLine()) != null){sb.append(line);}rd.close();result = sb.toString();} catch (Exception e){e.printStackTrace();}return result;}/*** Get the name of the product from entity. It considers null values also* @param EntityState* @return*/public static String getProductName(EntityState EntityState){//FIXME Use stringbuilderString productName = ((EntityState.getBrand() != null) ? EntityState.getBrand().trim() + " " : "")+ ((EntityState.getModelName() != null) ? EntityState.getModelName().trim() + " " : "")+ (( EntityState.getModelNumber() != null ) ? EntityState.getModelNumber().trim() : "" );return productName;}}