Subversion Repositories SmartDukaan

Rev

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