Subversion Repositories SmartDukaan

Rev

Rev 4295 | Rev 5055 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4295 varun.gupt 1
package in.shop2020.support.controllers;
2
 
3
import java.io.IOException;
4
import java.text.DateFormat;
5
import java.text.SimpleDateFormat;
6
import java.util.Calendar;
7
import java.util.Date;
8
import java.util.List;
9
 
10
import in.shop2020.model.v1.catalog.Item;
11
import in.shop2020.model.v1.catalog.ProductNotificationRequest;
12
import in.shop2020.model.v1.catalog.ProductNotificationRequestCount;
13
import in.shop2020.thrift.clients.CatalogClient;
14
import in.shop2020.utils.ModelUtils;
15
 
16
import javax.servlet.ServletOutputStream;
17
import javax.servlet.http.HttpServletRequest;
18
import javax.servlet.http.HttpServletResponse;
19
 
4305 varun.gupt 20
import org.apache.struts2.convention.annotation.InterceptorRef;
21
import org.apache.struts2.convention.annotation.InterceptorRefs;
22
import org.apache.struts2.convention.annotation.Result;
23
import org.apache.struts2.convention.annotation.Results;
4295 varun.gupt 24
import org.apache.struts2.interceptor.ServletRequestAware;
25
import org.apache.struts2.interceptor.ServletResponseAware;
26
import org.apache.struts2.rest.DefaultHttpHeaders;
27
import org.apache.struts2.rest.HttpHeaders;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30
 
31
/**
32
 * @author Varun Gupta
33
 */
34
 
4305 varun.gupt 35
@InterceptorRefs({
36
    @InterceptorRef("defaultStack"),
37
    @InterceptorRef("login")
38
})
39
@Results({
40
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
41
})
4295 varun.gupt 42
public class ProductNotificationsController implements ServletRequestAware, ServletResponseAware {
43
 
44
	private static Logger logger = LoggerFactory.getLogger(ProductNotificationsController.class);
45
	private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
46
	private HttpServletRequest request;
47
	private HttpServletResponse response;
48
 
49
	private List<ProductNotificationRequest> notificationRequests;
50
	private List<ProductNotificationRequestCount> notificationRequestCounts;
51
 
52
	public String index()	{
53
		try	{
54
			CatalogClient csc = new CatalogClient();
55
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
56
			Date startDate;
57
 
58
			if (request.getParameter("sp") != null)	{
59
				startDate = getStartDate(request.getParameter("sp"));
60
			} else	{
61
				startDate = getStartDate("d");
62
			}
63
			System.out.println(startDate);
64
			notificationRequestCounts = catalogClient.getProductNotificationRequestCount(startDate.getTime());
65
			System.out.println(notificationRequestCounts);
66
		} catch (Exception e) {
67
			logger.error(e.toString());
68
		}
69
		return "index";
70
	}
71
 
72
	public HttpHeaders create() {
73
		try	{
74
			CatalogClient csc = new CatalogClient();
75
			in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
76
			Date startDate = getStartDate(request.getParameter("np"));
77
			notificationRequests = catalogClient.getProductNotifications(startDate.getTime());
78
 
79
			logger.info(notificationRequests.toString());
80
 
81
			response.setContentType("text/tab-separated-values");
82
			response.setHeader("Content-disposition", "inline; filename=product-notifications-since-" + Long.toString(startDate.getTime()) + ".tsv");
83
 
84
			ServletOutputStream sos;
85
			try {
86
				sos = response.getOutputStream();
87
				sos.write(getNotificationsTSV().getBytes());
88
				sos.flush();
89
			} catch (IOException e) {
90
				logger.error("Error while streaming the hotspot reconciliation report", e);
91
			}
92
		} catch (Exception e) {
93
			e.printStackTrace();
94
		}
95
		return new DefaultHttpHeaders("report");
96
	}
97
 
98
	@Override
99
	public void setServletRequest(HttpServletRequest request) {
100
		this.request = request;
101
	}
102
 
103
	@Override
104
	public void setServletResponse(HttpServletResponse response) {
105
		this.response = response;
106
	}
107
 
108
	private String getNotificationsTSV()	{
109
		StringBuilder builder = new StringBuilder();
110
 
111
		for (ProductNotificationRequest notificationRequest: notificationRequests)	{
112
			Item item = notificationRequest.getItem();
113
			builder.append(ModelUtils.extractProductNameFromItem(item));
114
			builder.append("\t");
115
			builder.append(notificationRequest.getEmail());
116
			builder.append("\t");
117
			builder.append(dateFormat.format(new Date(notificationRequest.getAddedOn())));
118
			builder.append("\n");
119
		}
120
		return builder.toString();
121
	}
122
 
123
	private Date getStartDate(String selectedPeriodOption)	{
124
		System.out.println("Selected Period Option: " + selectedPeriodOption);
125
 
126
		Calendar now = Calendar.getInstance();
127
		System.out.println(now);
128
 
129
		if(selectedPeriodOption.equals("d"))	{
130
			now.add(Calendar.DAY_OF_MONTH, -1);
131
		}
132
		else if (selectedPeriodOption.equals("2d"))	{
133
			now.add(Calendar.DAY_OF_MONTH, -2);
134
		}
135
		else if (selectedPeriodOption.equals("3d"))	{
136
			now.add(Calendar.DAY_OF_MONTH, -3);
137
		}
138
		else if (selectedPeriodOption.equals("w"))	{
139
			now.add(Calendar.WEEK_OF_MONTH, -1);
140
		}
141
		else if (selectedPeriodOption.equals("f"))	{
142
			now.add(Calendar.WEEK_OF_MONTH, -2);
143
		}
144
		else if (selectedPeriodOption.equals("m"))	{
145
			now.add(Calendar.MONTH, -1);
146
		}
147
		else if (selectedPeriodOption.equals("3m"))	{
148
			now.add(Calendar.MONTH, -3);
149
		}
150
		else if (selectedPeriodOption.equals("6m"))	{
151
			now.add(Calendar.MONTH, -6);
152
		}
153
		else	{
154
			return new Date(0);
155
		}
156
		return now.getTime();
157
	}
158
 
159
	public List<ProductNotificationRequestCount> getNotificationRequestCounts()	{
160
		return notificationRequestCounts;
161
	}
162
 
163
	public String getSelectedPeriod()	{
164
		if (request.getParameter("sp") != null)	{
165
			return request.getParameter("sp");
166
		} else	{
167
			return "d";
168
		}
169
	}
170
 
171
	public String getProductNameFromItem(Item item)	{
172
		return ModelUtils.extractProductNameFromItem(item);
173
	}
174
}