Subversion Repositories SmartDukaan

Rev

Rev 2799 | 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;
3516 rajveer 11
import java.util.Calendar;
2729 rajveer 12
import java.util.Comparator;
13
import java.util.HashMap;
14
import java.util.LinkedHashMap;
15
import java.util.Map;
16
import java.util.Map.Entry;
17
import java.util.TreeMap;
18
 
19
import org.json.JSONException;
20
import org.json.JSONObject;
21
 
2739 rajveer 22
public class ComparisonStatsFetcher implements Serializable{
2799 rajveer 23
	private static final long serialVersionUID = -326425176723602170L;
2729 rajveer 24
	public static final String GOOGLE_COMPARE_DATA_URL = "http://saholic-datastore.appspot.com/show-comparisons";
25
 
2737 rajveer 26
	class ValueComparator implements Comparator, Serializable {
2797 rajveer 27
		private static final long serialVersionUID = 6060119992669086920L;
2795 rajveer 28
	    Map base;
29
        public ValueComparator(Map base) {
30
	    this.base = base;
31
	  }
32
	  public int compare(Object a, Object b) {
33
	    if((Long)base.get(a) < (Long)base.get(b)) {
34
	      return 1;
35
	    } else {
36
	      return -1;
2729 rajveer 37
	    }
2795 rajveer 38
	  }
39
	}
2729 rajveer 40
 
41
 
3516 rajveer 42
	public void fetchAndStoreComparisonStats(){
43
		String data = ComparisonStatsFetcher.sendGetRequest(GOOGLE_COMPARE_DATA_URL, null);
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{
3516 rajveer 108
		Calendar cal = Calendar.getInstance();
109
        //int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
110
        //if(dayOfWeek == Calendar.SUNDAY){
111
    		long toDate = cal.getTime().getTime();
112
    		cal.add(Calendar.MONTH, -1);
113
    		long fromDate = cal.getTime().getTime();
114
        	ComparisonStatsFetcher csf = new ComparisonStatsFetcher();
115
        	csf.fetchAndStoreComparisonStats();
2729 rajveer 116
	}
117
 
118
 
119
	/**
120
	 * 
121
	 * @param endpoint
122
	 * @param requestParameters
123
	 * @return
124
	 */
125
	 public static String sendGetRequest(String endpoint, String requestParameters)
126
     {
127
             String result = null;
128
             try
129
             {
130
                     String urlStr = endpoint;
131
                     if (requestParameters != null && requestParameters.length () > 0){
132
                             urlStr += "?" + requestParameters;
133
                     }
134
                     URL url = new URL(urlStr);
135
                     URLConnection conn = url.openConnection ();
136
 
137
                     // Get the response
138
                     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
139
                     StringBuffer sb = new StringBuffer();
140
                     String line;
141
                     while ((line = rd.readLine()) != null)
142
                     {
143
                             sb.append(line);
144
                     }
145
                     rd.close();
146
                     result = sb.toString();
147
             } catch (Exception e){
148
                     e.printStackTrace();
149
             }
150
 
151
             return result;
152
     }
153
 
154
		/**
155
	     * Get the name of the product from entity. It considers null values also
156
	     * @param EntityState
157
	     * @return
158
	     */
159
		public static String getProductName(EntityState EntityState){
160
			//FIXME Use stringbuilder
161
			String productName = ((EntityState.getBrand() != null) ? EntityState.getBrand().trim() + " " : "")
162
			+ ((EntityState.getModelName() != null) ? EntityState.getModelName().trim() + " " : "")
163
			+ (( EntityState.getModelNumber() != null ) ? EntityState.getModelNumber().trim() : "" );	
164
			return productName;
165
		}
166
}