Subversion Repositories SmartDukaan

Rev

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