private long createCoupon( String title, String startDate, String endDate, int amount, CouponType type, String massage, double price) { System.out.println("creating coupon : " + title); Coupon coupon = new Coupon(); try { coupon.setStartDate(Utils.string2Date(startDate)); coupon.setEndDate(Utils.string2Date(endDate)); coupon.setTitle(title); coupon.setAmount(amount); coupon.setPrice(price); coupon.setType(type); coupon.setMassage(massage); String image = title + ".img"; coupon.setImage(image); compFacade.createCoupon(coupon); return coupon.getId(); } catch (Exception e) { logger.error("create coupons " + title + " failed : " + e.toString()); return 0; } }
private void expireCoupon(long couponId) { System.out.println("expiering coupon with id : " + couponId); try { Coupon coupon = compFacade.getCoupon(couponId); coupon.setEndDate(new Date()); // the DailyCouponExpirationTask will delete this coupon compFacade.updateCoupon(coupon); } catch (Exception e) { logger.error("expireCoupon failed : " + e.toString()); } }
private void updateCoupon(long couponId) { System.out.println("updating coupon with id : " + couponId); try { Coupon coupon = compFacade.getCoupon(couponId); coupon.setAmount(100); coupon.setPrice(coupon.getPrice() / 2); compFacade.updateCoupon(coupon); } catch (Exception e) { logger.error("updateCoupon failed : " + e.toString()); } }
@Override public void run() { logger.info("DailyCouponExpirationTask starting"); CouponDAO couponDAO = new CouponDBDAO(); Collection<Coupon> coupons; while (!quit) { try { coupons = couponDAO.getAllCoupons(); // long millis = System.currentTimeMillis(); // Date currentDate = new Date(millis); int nRemoved = 0; for (Coupon coupon : coupons) { // if (coupon.getEndDate().before(currentDate)) { if (coupon.isExpired()) { try { logger.info("removing expierd coupon : " + coupon); couponDAO.removeCoupon(coupon); nRemoved++; } catch (Exception e) { logger.error("remove expired coupon failed"); } } } logger.info("DailyCouponExpirationTask removed " + nRemoved + " expierd coupons"); } catch (SQLException e) { logger.error("DailyCouponExpirationTask run failed : " + e.toString()); } try { // sleep(24 * Utils.houer); // sleep(15 * Utils.minute); sleep(sleepTime); } catch (InterruptedException e) { logger.error("DailyCouponExpirationTask interrupted"); } } logger.info("DailyCouponExpirationTask finished"); }