Rev 4295 | Rev 5623 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import java.io.IOException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.List;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.ProductNotificationRequest;import in.shop2020.model.v1.catalog.ProductNotificationRequestCount;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.utils.ModelUtils;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/*** @author Varun Gupta*/@InterceptorRefs({@InterceptorRef("defaultStack"),@InterceptorRef("login")})@Results({@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})})public class ProductNotificationsController implements ServletRequestAware, ServletResponseAware {private static Logger logger = LoggerFactory.getLogger(ProductNotificationsController.class);private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");private HttpServletRequest request;private HttpServletResponse response;private List<ProductNotificationRequest> notificationRequests;private List<ProductNotificationRequestCount> notificationRequestCounts;public String index() {try {CatalogClient csc = new CatalogClient();in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();Date startDate;if (request.getParameter("sp") != null) {startDate = getStartDate(request.getParameter("sp"));} else {startDate = getStartDate("d");}System.out.println(startDate);notificationRequestCounts = catalogClient.getProductNotificationRequestCount(startDate.getTime());System.out.println(notificationRequestCounts);} catch (Exception e) {logger.error(e.toString());}return "index";}public HttpHeaders create() {try {CatalogClient csc = new CatalogClient();in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();Date startDate = getStartDate(request.getParameter("np"));notificationRequests = catalogClient.getProductNotifications(startDate.getTime());logger.info(notificationRequests.toString());response.setContentType("text/tab-separated-values");response.setHeader("Content-disposition", "inline; filename=product-notifications-since-" + Long.toString(startDate.getTime()) + ".tsv");ServletOutputStream sos;try {sos = response.getOutputStream();sos.write(getNotificationsTSV().getBytes());sos.flush();} catch (IOException e) {logger.error("Error while streaming the hotspot reconciliation report", e);}} catch (Exception e) {e.printStackTrace();}return new DefaultHttpHeaders("report");}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}private String getNotificationsTSV() {StringBuilder builder = new StringBuilder();for (ProductNotificationRequest notificationRequest: notificationRequests) {Item item = notificationRequest.getItem();builder.append(ModelUtils.extractProductNameFromItem(item));builder.append("\t");builder.append(notificationRequest.getEmail());builder.append("\t");builder.append(dateFormat.format(new Date(notificationRequest.getAddedOn())));builder.append("\n");}return builder.toString();}private Date getStartDate(String selectedPeriodOption) {System.out.println("Selected Period Option: " + selectedPeriodOption);Calendar now = Calendar.getInstance();System.out.println(now);if(selectedPeriodOption.equals("d")) {now.add(Calendar.DAY_OF_MONTH, -1);}else if (selectedPeriodOption.equals("2d")) {now.add(Calendar.DAY_OF_MONTH, -2);}else if (selectedPeriodOption.equals("3d")) {now.add(Calendar.DAY_OF_MONTH, -3);}else if (selectedPeriodOption.equals("w")) {now.add(Calendar.WEEK_OF_MONTH, -1);}else if (selectedPeriodOption.equals("f")) {now.add(Calendar.WEEK_OF_MONTH, -2);}else if (selectedPeriodOption.equals("m")) {now.add(Calendar.MONTH, -1);}else if (selectedPeriodOption.equals("3m")) {now.add(Calendar.MONTH, -3);}else if (selectedPeriodOption.equals("6m")) {now.add(Calendar.MONTH, -6);}else {return new Date(0);}return now.getTime();}public List<ProductNotificationRequestCount> getNotificationRequestCounts() {return notificationRequestCounts;}public String getSelectedPeriod() {if (request.getParameter("sp") != null) {return request.getParameter("sp");} else {return "d";}}public String getProductNameFromItem(Item item) {return ModelUtils.extractProductNameFromItem(item);}}