Rev 3389 | Rev 5149 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import java.util.ArrayList;import java.util.List;import in.shop2020.model.v1.user.UserContextService;import in.shop2020.model.v1.user.PromotionService.Client;import in.shop2020.model.v1.user.Coupon;import in.shop2020.support.utils.ReportsUtils;import in.shop2020.thrift.clients.PromotionClient;import in.shop2020.thrift.clients.UserClient;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;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.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@InterceptorRefs({@InterceptorRef("defaultStack"),@InterceptorRef("login")})@Results({@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})})public class PromotionsController implements ServletRequestAware {private static Logger log = LoggerFactory.getLogger(PromotionsController.class);private HttpServletRequest request;private HttpSession session;private Client promotionClient;private UserContextService.Client userClient;private List<CouponInfo> coupons;@Overridepublic void setServletRequest(HttpServletRequest req) {this.request = req;this.session = req.getSession();}public String index() {if (!ReportsUtils.canAccessReport((Long) session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {return "authfail";}try {PromotionClient promotionServiceClient = new PromotionClient();promotionClient = promotionServiceClient.getClient();UserClient userServiceClient = new UserClient();userClient = userServiceClient.getClient();coupons = new ArrayList<CouponInfo>();CouponInfo couponInfo;for (Coupon coupon: promotionClient.getActiveCoupons()) {couponInfo = new CouponInfo(coupon.getCouponCode());couponInfo.setPaymentsCount(promotionClient.getSuccessfulPaymentCountForCoupon(coupon.getCouponCode()));couponInfo.setCartsCount(userClient.getCartsWithCouponCount(coupon.getCouponCode()));String args = coupon.getArguments();String description;if(args == null) {description = promotionClient.getRuleDocString(coupon.getPromotion().getRuleExecutionSrc());couponInfo.setDescription("<pre>" + description + "</pre>");} else {JSONObject argsJSON = new JSONObject(args);description = "Rs." + argsJSON.getString("discount");description += " off on " + argsJSON.getString("handset_display_name");description += " (Max " + argsJSON.getString("usage_limit") + " uses)";couponInfo.setDescription("<i>" + description + "</i>");}coupons.add(couponInfo);}} catch (Exception e) {log.error(e.getStackTrace().toString());}return "index";}public List<CouponInfo> getActiveCoupons() {return coupons;}public static class CouponInfo {private String code;private long paymentsCount;private long cartsCount;private String description;public CouponInfo(String code) {this.code = code;paymentsCount = -1;cartsCount = -1;description = null;}public void setPaymentsCount(long paymentsCount) {this.paymentsCount = paymentsCount;}public void setCartsCount(long cartsCount) {this.cartsCount = cartsCount;}public void setDescription(String desc) {description = desc;}public String getCode() {return this.code;}public long getPaymentsCount() {return this.paymentsCount;}public long getCartsCount() {return this.cartsCount;}public String getDescription() {return description;}public String toString() {return code + " " + paymentsCount + " " + cartsCount + " " + description;}}}