Subversion Repositories SmartDukaan

Rev

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