Subversion Repositories SmartDukaan

Rev

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