| 2620 |
vikas |
1 |
package in.shop2020.web;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
| 2773 |
rajveer |
4 |
import java.util.Date;
|
| 2669 |
rajveer |
5 |
import java.util.HashMap;
|
|
|
6 |
import java.util.List;
|
|
|
7 |
import java.util.Map;
|
| 2620 |
vikas |
8 |
|
|
|
9 |
import javax.servlet.http.HttpServlet;
|
|
|
10 |
import javax.servlet.http.HttpServletRequest;
|
|
|
11 |
import javax.servlet.http.HttpServletResponse;
|
|
|
12 |
|
| 2669 |
rajveer |
13 |
import org.json.JSONObject;
|
|
|
14 |
|
| 2620 |
vikas |
15 |
import com.google.appengine.api.datastore.DatastoreService;
|
|
|
16 |
import com.google.appengine.api.datastore.DatastoreServiceFactory;
|
|
|
17 |
import com.google.appengine.api.datastore.Entity;
|
|
|
18 |
import com.google.appengine.api.datastore.PreparedQuery;
|
|
|
19 |
import com.google.appengine.api.datastore.Query;
|
|
|
20 |
|
|
|
21 |
public class ShowCompareProducts extends HttpServlet {
|
|
|
22 |
private static final long serialVersionUID = -8236918415987438049L;
|
|
|
23 |
|
|
|
24 |
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
|
| 2669 |
rajveer |
25 |
resp.setContentType("application/json");
|
| 2620 |
vikas |
26 |
|
|
|
27 |
DatastoreService datastore = DatastoreServiceFactory
|
|
|
28 |
.getDatastoreService();
|
|
|
29 |
|
| 2773 |
rajveer |
30 |
String fromDateString = req.getParameter("fromdate");
|
|
|
31 |
String toDateString = req.getParameter("todate");
|
|
|
32 |
Date fromDate = new Date(0);
|
|
|
33 |
Date toDate = new Date();
|
|
|
34 |
try{
|
|
|
35 |
if(fromDateString!=null){
|
|
|
36 |
fromDate = new Date(Long.parseLong(fromDateString));
|
|
|
37 |
}
|
|
|
38 |
if(toDateString!=null){
|
|
|
39 |
toDate = new Date(Long.parseLong(toDateString));
|
|
|
40 |
}
|
|
|
41 |
}catch (Exception e) {
|
|
|
42 |
System.out.println("Parse exception, using default date from 1970 to Today");
|
|
|
43 |
}
|
|
|
44 |
|
| 2620 |
vikas |
45 |
Query q = new Query("DataLog");
|
|
|
46 |
q.addFilter("eventType", Query.FilterOperator.EQUAL, "PRODUCT_COMPARE");
|
| 2773 |
rajveer |
47 |
q.addFilter("date", Query.FilterOperator.GREATER_THAN_OR_EQUAL, fromDate);
|
|
|
48 |
q.addFilter("date", Query.FilterOperator.LESS_THAN_OR_EQUAL, toDate);
|
|
|
49 |
|
| 2620 |
vikas |
50 |
PreparedQuery pq = datastore.prepare(q);
|
| 2669 |
rajveer |
51 |
Map<Long, Map<Long, Long>> comparisonStats = new HashMap<Long, Map<Long, Long>>();
|
|
|
52 |
|
| 2620 |
vikas |
53 |
try {
|
|
|
54 |
for (Entity result : pq.asIterable()) {
|
| 2670 |
rajveer |
55 |
List<Long> itemIds;
|
|
|
56 |
try{
|
|
|
57 |
itemIds = (List<Long>) result.getProperty("itemIds");
|
|
|
58 |
}catch (ClassCastException ce) {
|
|
|
59 |
System.out.println("Unable to cast itemIds to List");
|
|
|
60 |
continue;
|
|
|
61 |
}
|
| 2669 |
rajveer |
62 |
if(itemIds != null && !itemIds.isEmpty()){
|
|
|
63 |
for(int i=0; i<itemIds.size(); i++){
|
|
|
64 |
for(int j=0; j<i; j++){
|
|
|
65 |
Long itemOne = itemIds.get(i);
|
|
|
66 |
Long itemTwo = itemIds.get(j);
|
|
|
67 |
Map<Long, Long> comparedItems;
|
|
|
68 |
comparedItems = comparisonStats.get(itemOne);
|
|
|
69 |
if(comparedItems==null){
|
|
|
70 |
comparedItems = new HashMap<Long, Long>();
|
|
|
71 |
comparisonStats.put(itemOne, comparedItems);
|
|
|
72 |
}
|
|
|
73 |
if(comparedItems.containsKey(itemTwo)){
|
|
|
74 |
comparedItems.put(itemTwo, comparedItems.get(itemTwo)+1);
|
|
|
75 |
}else{
|
|
|
76 |
comparedItems.put(itemTwo, 1L);
|
|
|
77 |
}
|
|
|
78 |
|
| 2772 |
rajveer |
79 |
comparedItems = comparisonStats.get(itemTwo);
|
| 2669 |
rajveer |
80 |
if(comparedItems==null){
|
|
|
81 |
comparedItems = new HashMap<Long, Long>();
|
|
|
82 |
comparisonStats.put(itemTwo, comparedItems);
|
|
|
83 |
}
|
|
|
84 |
if(comparedItems.containsKey(itemOne)){
|
|
|
85 |
comparedItems.put(itemOne, comparedItems.get(itemOne)+1);
|
|
|
86 |
}else{
|
|
|
87 |
comparedItems.put(itemOne, 1L);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
| 2620 |
vikas |
92 |
}
|
| 2669 |
rajveer |
93 |
|
|
|
94 |
JSONObject comparisonStatsJson = new JSONObject(comparisonStats);
|
|
|
95 |
resp.getWriter().println(comparisonStatsJson);
|
|
|
96 |
|
| 2620 |
vikas |
97 |
} catch (IOException e) {
|
| 2670 |
rajveer |
98 |
System.out.println("Unable to write at output stream");
|
| 2620 |
vikas |
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
|
|
103 |
doPost(req, resp);
|
|
|
104 |
}
|
|
|
105 |
}
|