Subversion Repositories SmartDukaan

Rev

Rev 2773 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2773 Rev 3506
Line 1... Line 1...
1
package in.shop2020.web;
1
package in.shop2020.web;
2
 
2
 
-
 
3
import in.shop2020.model.ComparisonStats;
-
 
4
import in.shop2020.server.ComparisonStatsRepository;
-
 
5
 
3
import java.io.IOException;
6
import java.io.IOException;
-
 
7
import java.text.SimpleDateFormat;
4
import java.util.Date;
8
import java.util.Date;
5
import java.util.HashMap;
-
 
6
import java.util.List;
-
 
7
import java.util.Map;
-
 
-
 
9
 
8
 
10
 
9
import javax.servlet.http.HttpServlet;
11
import javax.servlet.http.HttpServlet;
10
import javax.servlet.http.HttpServletRequest;
12
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
13
import javax.servlet.http.HttpServletResponse;
12
 
14
 
13
import org.json.JSONObject;
-
 
14
 
-
 
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
 
15
 
21
public class ShowCompareProducts extends HttpServlet {
16
public class ShowCompareProducts extends HttpServlet {
22
    private static final long serialVersionUID = -8236918415987438049L;
17
    private static final long serialVersionUID = -8236918415987438049L;
23
 
18
 
24
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
19
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
25
        resp.setContentType("application/json");
20
        resp.setContentType("application/json");
26
        
-
 
27
        DatastoreService datastore = DatastoreServiceFactory
-
 
28
                .getDatastoreService();
-
 
29
 
-
 
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
        
-
 
45
        Query q = new Query("DataLog");
-
 
46
        q.addFilter("eventType", Query.FilterOperator.EQUAL, "PRODUCT_COMPARE");
-
 
47
        q.addFilter("date", Query.FilterOperator.GREATER_THAN_OR_EQUAL, fromDate);
-
 
48
        q.addFilter("date", Query.FilterOperator.LESS_THAN_OR_EQUAL, toDate);
-
 
49
 
-
 
50
        PreparedQuery pq = datastore.prepare(q);
-
 
51
        Map<Long, Map<Long, Long>> comparisonStats = new HashMap<Long, Map<Long, Long>>();
-
 
52
        
-
 
53
        try {
21
        try {
54
            for (Entity result : pq.asIterable()) {
-
 
55
            	List<Long> itemIds;
-
 
56
            	try{
22
        	
57
            		itemIds =  (List<Long>) result.getProperty("itemIds");
-
 
58
            	}catch (ClassCastException ce) {
-
 
59
					System.out.println("Unable to cast itemIds to List");
-
 
60
					continue;
-
 
61
				}
-
 
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);
23
        	Date toDate = new Date();
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
                			
24
            
79
                			comparedItems = comparisonStats.get(itemTwo);
-
 
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);
25
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
86
                			}else{
-
 
87
                				comparedItems.put(itemOne, 1L);
26
            String id = sdf.format(toDate);
88
                			}
-
 
89
                		}
-
 
90
                	}
-
 
91
                }
-
 
92
            }
-
 
93
            
27
            
94
            JSONObject comparisonStatsJson = new JSONObject(comparisonStats);
28
            ComparisonStatsRepository repo = new ComparisonStatsRepository();
-
 
29
        	ComparisonStats comparisonStats = repo.getById(id);
-
 
30
        	
95
            resp.getWriter().println(comparisonStatsJson);
31
            resp.getWriter().println(comparisonStats.getComparisonStatsJSON());
96
            	
32
            	
97
        } catch (IOException e) {
33
        } catch (IOException e) {
98
            System.out.println("Unable to write at output stream");
34
            System.out.println("Unable to write at output stream");
99
        }
35
        }
100
    }
36
    }