Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.groupbuy.entity; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinTable; import javax.persistence.Lob; import javax.persistence.ManyToMany; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.Valid; import javax.validation.constraints.Digits; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import net.groupbuy.util.FreemarkerUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; import com.fasterxml.jackson.annotation.JsonProperty; import freemarker.template.TemplateException; /** * Entity - * * @author SHOP++ Team * @version 3.0 */ @Entity @Table(name = "xx_promotion") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_promotion_sequence") public class Promotion extends OrderEntity { private static final long serialVersionUID = 3536993535267962279L; /** ? */ private static final String PATH_PREFIX = "/promotion/content"; /** ? */ private static final String PATH_SUFFIX = ".jhtml"; /** ?? */ private String name; /** */ private String title; /** */ private Date beginDate; /** ? */ private Date endDate; /** ??? */ private Integer minimumQuantity; /** ?? */ private Integer maximumQuantity; /** ?? */ private BigDecimal minimumPrice; /** ? */ private BigDecimal maximumPrice; /** ?? */ private String priceExpression; /** ?? */ private String pointExpression; /** ??? */ private Boolean isFreeShipping; /** ?? */ private Boolean isCouponAllowed; /** ? */ private String introduction; /** ?? */ private Set<MemberRank> memberRanks = new HashSet<MemberRank>(); /** ??? */ private Set<ProductCategory> productCategories = new HashSet<ProductCategory>(); /** ??? */ private Set<Product> products = new HashSet<Product>(); /** ??? */ private Set<Brand> brands = new HashSet<Brand>(); /** ? */ private Set<Coupon> coupons = new HashSet<Coupon>(); /** ? */ private List<GiftItem> giftItems = new ArrayList<GiftItem>(); /** * ??? * * @return ?? */ @JsonProperty @NotEmpty @Length(max = 200) @Column(nullable = false) public String getName() { return name; } /** * ?? * * @param name * ?? */ public void setName(String name) { this.name = name; } /** * ? * * @return */ @JsonProperty @NotEmpty @Length(max = 200) @Column(nullable = false) public String getTitle() { return title; } /** * * * @param title * */ public void setTitle(String title) { this.title = title; } /** * ? * * @return */ @JsonProperty public Date getBeginDate() { return beginDate; } /** * * * @param beginDate * */ public void setBeginDate(Date beginDate) { this.beginDate = beginDate; } /** * ?? * * @return ? */ @JsonProperty public Date getEndDate() { return endDate; } /** * ? * * @param endDate * ? */ public void setEndDate(Date endDate) { this.endDate = endDate; } /** * ???? * * @return ??? */ @JsonProperty @Min(0) public Integer getMinimumQuantity() { return minimumQuantity; } /** * ??? * * @param minimumQuantity * ??? */ public void setMinimumQuantity(Integer minimumQuantity) { this.minimumQuantity = minimumQuantity; } /** * ??? * * @return ?? */ @JsonProperty @Min(0) public Integer getMaximumQuantity() { return maximumQuantity; } /** * ?? * * @param maximumQuantity * ?? */ public void setMaximumQuantity(Integer maximumQuantity) { this.maximumQuantity = maximumQuantity; } /** * ??? * * @return ?? */ @JsonProperty @Min(0) @Digits(integer = 12, fraction = 3) @Column(precision = 21, scale = 6) public BigDecimal getMinimumPrice() { return minimumPrice; } /** * ?? * * @param minimumPrice * ?? */ public void setMinimumPrice(BigDecimal minimumPrice) { this.minimumPrice = minimumPrice; } /** * ?? * * @return ? */ @JsonProperty @Min(0) @Digits(integer = 12, fraction = 3) @Column(precision = 21, scale = 6) public BigDecimal getMaximumPrice() { return maximumPrice; } /** * ? * * @param maximumPrice * ? */ public void setMaximumPrice(BigDecimal maximumPrice) { this.maximumPrice = maximumPrice; } /** * ??? * * @return ?? */ public String getPriceExpression() { return priceExpression; } /** * ?? * * @param priceExpression * ?? */ public void setPriceExpression(String priceExpression) { this.priceExpression = priceExpression; } /** * ??? * * @return ?? */ public String getPointExpression() { return pointExpression; } /** * ?? * * @param pointExpression * ?? */ public void setPointExpression(String pointExpression) { this.pointExpression = pointExpression; } /** * ???? * * @return ??? */ @NotNull @Column(nullable = false) public Boolean getIsFreeShipping() { return isFreeShipping; } /** * ??? * * @param isFreeShipping * ??? */ public void setIsFreeShipping(Boolean isFreeShipping) { this.isFreeShipping = isFreeShipping; } /** * ??? * * @return ?? */ @JsonProperty @NotNull @Column(nullable = false) public Boolean getIsCouponAllowed() { return isCouponAllowed; } /** * ?? * * @param isCouponAllowed * ?? */ public void setIsCouponAllowed(Boolean isCouponAllowed) { this.isCouponAllowed = isCouponAllowed; } /** * ?? * * @return ? */ @Lob public String getIntroduction() { return introduction; } /** * ? * * @param introduction * ? */ public void setIntroduction(String introduction) { this.introduction = introduction; } /** * ??? * * @return ?? */ @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_promotion_member_rank") public Set<MemberRank> getMemberRanks() { return memberRanks; } /** * ?? * * @param memberRanks * ?? */ public void setMemberRanks(Set<MemberRank> memberRanks) { this.memberRanks = memberRanks; } /** * ???? * * @return ??? */ @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_promotion_product_category") public Set<ProductCategory> getProductCategories() { return productCategories; } /** * ??? * * @param productCategories * ??? */ public void setProductCategories(Set<ProductCategory> productCategories) { this.productCategories = productCategories; } /** * ???? * * @return ??? */ @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_promotion_product") public Set<Product> getProducts() { return products; } /** * ??? * * @param products * ??? */ public void setProducts(Set<Product> products) { this.products = products; } /** * ???? * * @return ??? */ @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_promotion_brand") public Set<Brand> getBrands() { return brands; } /** * ??? * * @param brands * ??? */ public void setBrands(Set<Brand> brands) { this.brands = brands; } /** * ?? * * @return ? */ @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_promotion_coupon") public Set<Coupon> getCoupons() { return coupons; } /** * ? * * @param coupons * ? */ public void setCoupons(Set<Coupon> coupons) { this.coupons = coupons; } /** * ?? * * @return ? */ @Valid @OneToMany(mappedBy = "promotion", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) public List<GiftItem> getGiftItems() { return giftItems; } /** * ? * * @param giftItems * ? */ public void setGiftItems(List<GiftItem> giftItems) { this.giftItems = giftItems; } /** * ? * * @return ? */ @Transient public boolean hasBegun() { return getBeginDate() == null || new Date().after(getBeginDate()); } /** * ?? * * @return ?? */ @Transient public boolean hasEnded() { return getEndDate() != null && new Date().after(getEndDate()); } /** * ? * * @return */ @Transient public String getPath() { if (getId() != null) { return PATH_PREFIX + "/" + getId() + PATH_SUFFIX; } return null; } /** * * * @param quantity * ?? * @param price * ? * @return */ @Transient public BigDecimal calculatePrice(Integer quantity, BigDecimal price) { if (price == null || StringUtils.isEmpty(getPriceExpression())) { return price; } BigDecimal result = new BigDecimal(0); try { Map<String, Object> model = new HashMap<String, Object>(); model.put("quantity", quantity); model.put("price", price); result = new BigDecimal(FreemarkerUtils.process("#{(" + getPriceExpression() + ");M50}", model)); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } if (result.compareTo(price) > 0) { return price; } return result.compareTo(new BigDecimal(0)) > 0 ? result : new BigDecimal(0); } /** * ? * * @param quantity * ?? * @param point * ? * @return ? */ @Transient public Long calculatePoint(Integer quantity, Long point) { if (point == null || StringUtils.isEmpty(getPointExpression())) { return point; } Long result = 0L; try { Map<String, Object> model = new HashMap<String, Object>(); model.put("quantity", quantity); model.put("point", point); result = Double.valueOf(FreemarkerUtils.process("#{(" + getPointExpression() + ");M50}", model)) .longValue(); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } if (result < point) { return point; } return result > 0L ? result : 0L; } }