Subversion Repositories SmartDukaan

Rev

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