Rev 7897 | 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.CatalogService;import in.shop2020.model.v1.catalog.Category;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.thrift.clients.HelperClient;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.apache.thrift.TException;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;public Long category;public Long categoryId;private List<ProductNotificationRequest> notificationRequests;private List<ProductNotificationRequestCount> notificationRequestCounts;private static List<Category> parentCategories;static {try {CatalogService.Client csc = new CatalogClient().getClient();parentCategories = csc.getAllParentCategories();} catch (Exception e) {logger.error("Unable to get all parent categories in ProductNotificationController", e);}}public String index() {try {CatalogClient csc = new CatalogClient();in.shop2020.model.v1.catalog.CatalogService.Client catalogClient= csc.getClient();Date startDate;if (request.getParameter("sp") != null) {startDate = getStartDate(request.getParameter("sp"));} else {startDate = getStartDate("d");}//logger.info("", startDate);notificationRequestCounts = catalogClient.getProductNotificationRequestCount(startDate.getTime(), (categoryId==null)?0L:categoryId);//logger.info("", notificationRequestCounts);} catch (TException e) {logger.error("", e);}return "index";}public HttpHeaders create() {try {HelperClient hsc = new HelperClient("helper_service_server_host_prod", "helper_service_server_port_prod");in.shop2020.utils.HelperService.Client helperClient = hsc.getClient();Date startDate = dateFormat.parse(request.getParameter("startDate"));Date endDate = dateFormat.parse(request.getParameter("endDate"));logger.info("" + startDate + "\t" + endDate);List<String> notificationEmails = helperClient.getEmailsForNotificationsSent(startDate.getTime(), endDate.getTime());logger.info(notificationEmails.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(getEmailsCSV(notificationEmails).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 getEmailsCSV(List<String> emails) {StringBuilder builder = new StringBuilder();for (String email: emails) {builder.append(email);builder.append("\n");}return builder.toString();}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);}public Long getCategoryId() {return categoryId;}public void setCategoryId(Long categoryId) {this.categoryId = categoryId;}public static List<Category> getParentCategories() {return parentCategories;}}