Subversion Repositories SmartDukaan

Rev

Rev 2793 | Rev 2797 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2729 rajveer 1
package in.shop2020.ui.util;
2
 
3
import in.shop2020.metamodel.core.EntityState;
4
import in.shop2020.metamodel.util.CreationUtils;
5
 
6
import java.io.BufferedReader;
7
import java.io.InputStreamReader;
2737 rajveer 8
import java.io.Serializable;
2729 rajveer 9
import java.net.URL;
10
import java.net.URLConnection;
11
import java.util.Comparator;
12
import java.util.HashMap;
13
import java.util.LinkedHashMap;
14
import java.util.Map;
15
import java.util.Map.Entry;
16
import java.util.TreeMap;
17
 
18
import org.json.JSONException;
19
import org.json.JSONObject;
20
 
2739 rajveer 21
public class ComparisonStatsFetcher implements Serializable{
2793 rajveer 22
	private static final long serialVersionUID = 1L;
2729 rajveer 23
	public static final String GOOGLE_COMPARE_DATA_URL = "http://saholic-datastore.appspot.com/show-comparisons";
24
 
2737 rajveer 25
	class ValueComparator implements Comparator, Serializable {
2795 rajveer 26
		private static final long serialVersionUID = 1L;
27
	    Map base;
28
        public ValueComparator(Map base) {
29
	    this.base = base;
30
	  }
31
	  public int compare(Object a, Object b) {
32
	    if((Long)base.get(a) < (Long)base.get(b)) {
33
	      return 1;
34
	    } else {
35
	      return -1;
2729 rajveer 36
	    }
2795 rajveer 37
	  }
38
	}
2729 rajveer 39
 
40
 
2775 rajveer 41
	public void fetchAndStoreComparisonStats(long fromDate, long toDate){
42
		String requestParameters = "fromdate=" +  fromDate + "&todate=" + toDate;
43
		String data = ComparisonStatsFetcher.sendGetRequest(GOOGLE_COMPARE_DATA_URL, requestParameters);
2729 rajveer 44
		try {
45
			JSONObject json = new JSONObject(data);
46
			Map<Long, Map<Long, Long>> comparisonStats = new HashMap<Long, Map<Long, Long>>();
47
 
48
			for (String itemId : JSONObject.getNames(json)) {
49
				Map<Long, Long> freq = new HashMap<Long, Long>();
50
				JSONObject items = (JSONObject) json.get(itemId);
51
				for (String comparedItemId : JSONObject.getNames(items)) {
52
					Integer frequency = (Integer) items.get(comparedItemId);
53
					freq.put(Long.parseLong(comparedItemId), frequency.longValue());
54
				}
55
				comparisonStats.put(Long.parseLong(itemId), freq);
56
			}
57
 
58
			if(comparisonStats != null){
59
				Map<Long, Map<Long, Long>> sortedComparisonStats = new HashMap<Long, Map<Long, Long>>();
60
				for(Entry<Long, Map<Long, Long>> entry: comparisonStats.entrySet()){
61
					ValueComparator bvc =  new ValueComparator(entry.getValue());
62
					TreeMap<Long, Long> sortedMap = new TreeMap(bvc);
63
					sortedMap.putAll(entry.getValue());
64
					sortedComparisonStats.put(entry.getKey(), sortedMap);
65
				}
66
				CreationUtils.storeComparisonStats(sortedComparisonStats);
67
			}
68
		} catch (JSONException e) {
69
			// TODO Auto-generated catch block
70
			e.printStackTrace();
71
		} catch (Exception e) {
72
			// TODO Auto-generated catch block
73
			e.printStackTrace();
74
		}
75
	}
76
 
77
	private static Map<String, Long> getMostComparedPhones(Map<Long, Map<Long, Long>> comparisonStats){
78
		Map<String, Long> allCounts = new HashMap<String, Long>(); 
79
		for(Long key: comparisonStats.keySet()){
80
			for(Long key1: comparisonStats.get(key).keySet()){
81
				String newKey;
82
				if(key<key1){
83
					newKey = key + " : " + key;
84
					//newKey = key + "\t" + ComparisonStatsFetcher.getProductName(es) + " : " + key1 + "\t" + ComparisonStatsFetcher.getProductName(es1);   
85
				}else{
86
					//newKey = key1 + "\t" + ComparisonStatsFetcher.getProductName(es1) + " : " + key + "\t" + ComparisonStatsFetcher.getProductName(es);
87
					newKey = key1 + " : " + key;
88
				}
89
				allCounts.put(newKey, comparisonStats.get(key).get(key1));
90
			}
91
		}
92
		return allCounts;
93
	}
94
 
95
	private static Map<Long, Long> getTopComparedPhones(Map<Long, Map<Long, Long>> comparisonStats){
96
		Map<Long, Long> mostCompared = new LinkedHashMap<Long, Long>(); 
97
		for(Long key: comparisonStats.keySet()){
98
			long count = 0;
99
			for(Long key1: comparisonStats.get(key).keySet()){
100
				count += comparisonStats.get(key).get(key1);
101
			}
102
			mostCompared.put(key, new Long(count));
103
		}
104
		return mostCompared;
105
	}
106
 
107
	public static void main(String[] args) throws Exception{
108
//		String data = FileUtils.readFileToString(new File("/home/rajveer/Desktop/comparisonStats.json"));
109
//		JSONObject json = new JSONObject(data);
110
	}
111
 
112
 
113
	/**
114
	 * 
115
	 * @param endpoint
116
	 * @param requestParameters
117
	 * @return
118
	 */
119
	 public static String sendGetRequest(String endpoint, String requestParameters)
120
     {
121
             String result = null;
122
             try
123
             {
124
                     String urlStr = endpoint;
125
                     if (requestParameters != null && requestParameters.length () > 0){
126
                             urlStr += "?" + requestParameters;
127
                     }
128
                     URL url = new URL(urlStr);
129
                     URLConnection conn = url.openConnection ();
130
 
131
                     // Get the response
132
                     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
133
                     StringBuffer sb = new StringBuffer();
134
                     String line;
135
                     while ((line = rd.readLine()) != null)
136
                     {
137
                             sb.append(line);
138
                     }
139
                     rd.close();
140
                     result = sb.toString();
141
             } catch (Exception e){
142
                     e.printStackTrace();
143
             }
144
 
145
             return result;
146
     }
147
 
148
		/**
149
	     * Get the name of the product from entity. It considers null values also
150
	     * @param EntityState
151
	     * @return
152
	     */
153
		public static String getProductName(EntityState EntityState){
154
			//FIXME Use stringbuilder
155
			String productName = ((EntityState.getBrand() != null) ? EntityState.getBrand().trim() + " " : "")
156
			+ ((EntityState.getModelName() != null) ? EntityState.getModelName().trim() + " " : "")
157
			+ (( EntityState.getModelNumber() != null ) ? EntityState.getModelNumber().trim() : "" );	
158
			return productName;
159
		}
160
}