Java tutorial
/* * Copyright 2013-2015 cetvision.com. All rights reserved. * Support: http://www.cetvision.com * License: http://www.cetvision.com/license */ package com.dp2345.entity; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.Lob; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.constraints.Digits; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import org.apache.commons.lang.StringUtils; import org.dom4j.io.SAXReader; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.FieldBridge; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.NumericField; import org.hibernate.search.annotations.Store; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.core.io.ClassPathResource; import com.dp2345.BigDecimalNumericFieldBridge; import com.dp2345.CommonAttributes; import com.dp2345.util.FreemarkerUtils; import com.fasterxml.jackson.annotation.JsonProperty; import freemarker.template.TemplateException; /** * Entity - ?? * * @author CETVISION CORP * @version 2.0.3 */ @Entity @Table(name = "xx_set_product") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_set_product_sequence") public class SetProduct extends OrderEntity { private static final long serialVersionUID = 3536993535267962279L; /** ?? */ private static String staticPath; /** * ? */ public enum OrderType { /** ?? */ topDesc, /** ?? */ priceAsc, /** ?? */ priceDesc, /** ??? */ salesDesc, /** ?? */ scoreDesc, /** ?? */ dateDesc } /** ? */ private String sn; /** ?? */ private String name; /** */ private String title; /** */ private BigDecimal price; /** ? */ private Long point; /** */ private String image; /** */ private Date beginDate; /** ? */ private Date endDate; /** ?? */ private String pointExpression; /** ??? */ private Boolean isFreeShipping; /** ?? */ private Boolean isCouponAllowed; /** ? */ private String introduction; /** ?? */ private Set<SetProductItem> setProductItems = new HashSet<SetProductItem>(); /** */ private Shop shop; /** ? */ private Set<Coupon> coupons = new HashSet<Coupon>(); /** */ private Map<MemberRank, BigDecimal> memberPrice = new HashMap<MemberRank, BigDecimal>(); /** ?? */ private String unit; static { try { File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile(); org.dom4j.Document document = new SAXReader().read(dp2345XmlFile); org.dom4j.Element element = (org.dom4j.Element) document .selectSingleNode("/dp2345/template[@id='setProductContent']"); staticPath = element.attributeValue("staticPath"); } catch (Exception e) { e.printStackTrace(); } } /** * ??? * * @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 ?? */ 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 ? */ @OneToMany(mappedBy = "set_product", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) public Set<SetProductItem> getSetProductItems() { return setProductItems; } /** * ??? * * @param products * ??? */ public void setSetProductItems(Set<SetProductItem> setProductItems) { this.setProductItems = setProductItems; } /** * ? * * @return */ @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(nullable = false) public Shop getShop() { return shop; } /** * * * @param shop * */ public void setShop(Shop shop) { this.shop = shop; } /** * ?? * * @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 ? */ @JsonProperty @Field(store = Store.YES, index = Index.UN_TOKENIZED) @Pattern(regexp = "^[0-9a-zA-Z_-]+$") @Length(max = 100) @Column(nullable = false, unique = true, length = 100) public String getSn() { return sn; } /** * ? * * @param sn * ? */ public void setSn(String sn) { this.sn = sn; } /** * ?? * * @return ? */ @Field(store = Store.YES, index = Index.NO) @Min(0) @Column(nullable = false) public Long getPoint() { return point; } /** * ? * * @param point * ? */ public void setPoint(Long point) { this.point = point; } /** * ? * * @return */ @JsonProperty @Field(store = Store.YES, index = Index.UN_TOKENIZED) @NumericField @FieldBridge(impl = BigDecimalNumericFieldBridge.class) @NotNull @Min(0) @Digits(integer = 12, fraction = 3) @Column(nullable = false, precision = 21, scale = 6) public BigDecimal getPrice() { return price; } /** * * * @param price * */ public void setPrice(BigDecimal price) { this.price = price; } /** * ? * * @return */ @JsonProperty @Field(store = Store.YES, index = Index.NO) @Length(max = 200) public String getImage() { return image; } /** * * * @param image * */ public void setImage(String image) { this.image = image; } /** * ??? * * @return ?? */ @JsonProperty @Field(store = Store.YES, index = Index.NO) @Length(max = 200) public String getUnit() { return unit; } /** * ?? * * @param unit * ?? */ public void setUnit(String unit) { this.unit = unit; } /** * ? * * @return */ @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "xx_product_member_price") public Map<MemberRank, BigDecimal> getMemberPrice() { return memberPrice; } /** * * * @param memberPrice * */ public void setMemberPrice(Map<MemberRank, BigDecimal> memberPrice) { this.memberPrice = memberPrice; } // ?? /** * ? * * @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() { Map<String, Object> model = new HashMap<String, Object>(); model.put("id", getId()); model.put("createDate", getCreateDate()); model.put("modifyDate", getModifyDate()); model.put("sn", getSn()); model.put("name", getName()); model.put("fullName", getTitle()); try { return FreemarkerUtils.process(staticPath, model); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } return null; } /** * ???? * * @return ???? */ @Transient public Integer getWeight() { Integer result = 0; Set<SetProductItem> items = getSetProductItems(); for (SetProductItem item : items) { result += item.getWeight(); } return result; } /** * ?? * * @return ?? */ @Transient public Long calculatePoint() { Long point = 0L; if (getSetProductItems() != null) { for (SetProductItem item : getSetProductItems()) { point += item.getSubPoint(); } } if (StringUtils.isEmpty(getPointExpression())) { return point; } Long result = 0L; try { Map<String, Object> model = new HashMap<String, Object>(); model.put("quantity", 1); 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; } /** * * * @return ? */ @Transient public BigDecimal calculatePrice() { BigDecimal price = new BigDecimal(0); if (getSetProductItems() != null) { for (SetProductItem item : getSetProductItems()) { if (item != null && item.getSubPrice() != null) { price = price.add(item.getSubPrice()); } } } return price; } /** * ?? * * @return ? */ @Transient public BigDecimal getCost() { BigDecimal cost = new BigDecimal(0); if (getSetProductItems() != null) { for (SetProductItem item : getSetProductItems()) { if (item != null && item.getSubCost() != null) { cost = cost.add(item.getSubCost()); } } } return cost; } /** * ?? * * @return ? */ @Transient public Integer getAvailableStock() { Integer tempStock = null; Integer availableStock = 10000; if (getSetProductItems() != null) { for (SetProductItem item : getSetProductItems()) { tempStock = (item.getProduct().getStock() - item.getProduct().getAllocatedStock()) / item.getQuantity(); if (availableStock > tempStock) availableStock = tempStock; } } return availableStock < 0 ? 0 : availableStock; } /** * ?? * * @return ? */ @Transient public Boolean getIsOutOfStock() { boolean isOutOfStock = false; if (getSetProductItems() != null) { for (SetProductItem item : getSetProductItems()) { Product product = item.getProduct(); if (product.getStock() == null || product.getAllocatedStock() >= product.getStock()) isOutOfStock = true; } } return isOutOfStock; } }