Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package net.shopxx.entity; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.OrderBy; 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 net.shopxx.entity.MerchantAttribute.Type; import net.shopxx.interceptor.MerchantInterceptor; import net.shopxx.util.JsonUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; /** * Entity - * * @author SHOP++ Team * @version 3.0 */ @Entity @Table(name = "xx_merchant") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "xx_merchant_sequence") public class Merchant extends BaseEntity { private static final long serialVersionUID = 1533130686714725835L; /** 0? 1? */ private Integer type; /** * */ public enum Gender { /** */ male, /** */ female } /** "?"??? */ public static final String PRINCIPAL_ATTRIBUTE_NAME = MerchantInterceptor.class.getName() + ".PRINCIPAL"; /** "??"Cookie?? */ public static final String USERNAME_COOKIE_NAME = "username"; /** */ public static final int ATTRIBUTE_VALUE_PROPERTY_COUNT = 10; /** ??? */ public static final String ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX = "attributeValue"; /** ?? */ public static final Integer MAX_FAVORITE_COUNT = 10; /** ?? */ private String username; /** ? */ private String password; /** E-mail */ private String email; /** */ private Long point; /** ? */ private BigDecimal amount; /** ? */ private BigDecimal balance; /** ?? */ private Boolean isEnabled; /** ?? */ private Boolean isLocked; /** */ private Integer loginFailureCount; /** ? */ private Date lockedDate; /** IP */ private String registerIp; /** ?IP */ private String loginIp; /** ? */ private Date loginDate; /** ?? */ private String name; /** */ private Gender gender; /** */ private Date birth; /** ? */ private String address; /** */ private String zipCode; /** ? */ private String phone; /** */ private String mobile; /** 0 */ private String attributeValue0; /** 1 */ private String attributeValue1; /** 2 */ private String attributeValue2; /** 3 */ private String attributeValue3; /** 4 */ private String attributeValue4; /** 5 */ private String attributeValue5; /** 6 */ private String attributeValue6; /** 7 */ private String attributeValue7; /** 8 */ private String attributeValue8; /** 9 */ private String attributeValue9; /** */ private SafeKey safeKey; /** */ private Area area; /** */ private MerchantRank merchantRank; // /** */ // private Cart cart; /** ? */ // private Set<Order> orders = new HashSet<Order>(); /** */ // private Set<Deposit> deposits = new HashSet<Deposit>(); /** ? */ // private Set<Payment> payments = new HashSet<Payment>(); /** ? */ // private Set<CouponCode> couponCodes = new HashSet<CouponCode>(); /** ? */ // private Set<Receiver> receivers = new HashSet<Receiver>(); /** */ // private Set<Review> reviews = new HashSet<Review>(); /** */ // private Set<Consultation> consultations = new HashSet<Consultation>(); /** ?? */ // private Set<Product> favoriteProducts = new HashSet<Product>(); /** */ // private Set<ProductNotify> productNotifies = new HashSet<ProductNotify>(); // // /** ? */ // private Set<Message> inMessages = new HashSet<Message>(); // // /** ??? */ // private Set<Message> outMessages = new HashSet<Message>(); /** * ? * * @return */ @NotEmpty @Column(nullable = false) public Integer getType() { return type; } /** * * * @return */ public void setType(Integer type) { this.type = type; } /** * ??? * * @return ?? */ @NotEmpty(groups = Save.class) @Pattern(regexp = "^[0-9a-z_A-Z\\u4e00-\\u9fa5]+$") @Column(nullable = false, updatable = false, unique = true, length = 100) public String getUsername() { return username; } /** * ?? * * @param username * ?? */ public void setUsername(String username) { this.username = username; } /** * ?? * * @return ? */ @NotEmpty(groups = Save.class) @Pattern(regexp = "^[^\\s&\"<>]+$") @Column(nullable = false) public String getPassword() { return password; } /** * ? * * @param password * ? */ public void setPassword(String password) { this.password = password; } /** * ?E-mail * * @return E-mail */ @NotEmpty @Email @Length(max = 200) @Column(nullable = false) public String getEmail() { return email; } /** * E-mail * * @param email * E-mail */ public void setEmail(String email) { this.email = email; } /** * ? * * @return */ @NotNull(groups = Save.class) @Min(0) @Column(nullable = false) public Long getPoint() { return point; } /** * * * @param point * */ public void setPoint(Long point) { this.point = point; } /** * ?? * * @return ? */ @Column(nullable = false, precision = 27, scale = 12) public BigDecimal getAmount() { return amount; } /** * ? * * @param amount * ? */ public void setAmount(BigDecimal amount) { this.amount = amount; } /** * ?? * * @return ? */ @NotNull(groups = Save.class) @Min(0) @Digits(integer = 12, fraction = 3) @Column(nullable = false, precision = 27, scale = 12) public BigDecimal getBalance() { return balance; } /** * ? * * @param balance * ? */ public void setBalance(BigDecimal balance) { this.balance = balance; } /** * ??? * * @return ?? */ @NotNull @Column(nullable = false) public Boolean getIsEnabled() { return isEnabled; } /** * ?? * * @param isEnabled * ?? */ public void setIsEnabled(Boolean isEnabled) { this.isEnabled = isEnabled; } /** * ??? * * @return ?? */ @Column(nullable = false) public Boolean getIsLocked() { return isLocked; } /** * ?? * * @param isLocked * ?? */ public void setIsLocked(Boolean isLocked) { this.isLocked = isLocked; } /** * ? * * @return */ @Column(nullable = false) public Integer getLoginFailureCount() { return loginFailureCount; } /** * * * @param loginFailureCount * */ public void setLoginFailureCount(Integer loginFailureCount) { this.loginFailureCount = loginFailureCount; } /** * ?? * * @return ? */ public Date getLockedDate() { return lockedDate; } /** * ? * * @param lockedDate * ? */ public void setLockedDate(Date lockedDate) { this.lockedDate = lockedDate; } /** * ?IP * * @return IP */ @Column(nullable = false, updatable = false) public String getRegisterIp() { return registerIp; } /** * IP * * @param registerIp * IP */ public void setRegisterIp(String registerIp) { this.registerIp = registerIp; } /** * ??IP * * @return ?IP */ public String getLoginIp() { return loginIp; } /** * ?IP * * @param loginIp * ?IP */ public void setLoginIp(String loginIp) { this.loginIp = loginIp; } /** * ?? * * @return ? */ public Date getLoginDate() { return loginDate; } /** * ? * * @param loginDate * ? */ public void setLoginDate(Date loginDate) { this.loginDate = loginDate; } /** * ??? * * @return ?? */ @Length(max = 200) public String getName() { return name; } /** * ?? * * @param name * ?? */ public void setName(String name) { this.name = name; } /** * ? * * @return */ public Gender getGender() { return gender; } /** * * * @param gender * */ public void setGender(Gender gender) { this.gender = gender; } /** * ? * * @return */ public Date getBirth() { return birth; } /** * * * @param birth * */ public void setBirth(Date birth) { this.birth = birth; } /** * ?? * * @return ? */ @Length(max = 200) public String getAddress() { return address; } /** * ? * * @param address * ? */ public void setAddress(String address) { this.address = address; } /** * ? * * @return */ @Length(max = 200) public String getZipCode() { return zipCode; } /** * * * @param zipCode * */ public void setZipCode(String zipCode) { this.zipCode = zipCode; } /** * ?? * * @return ? */ @Length(max = 200) public String getPhone() { return phone; } /** * ? * * @param phone * ? */ public void setPhone(String phone) { this.phone = phone; } /** * ? * * @return */ @Length(max = 200) public String getMobile() { return mobile; } /** * * * @param mobile * */ public void setMobile(String mobile) { this.mobile = mobile; } /** * ?0 * * @return 0 */ @Length(max = 200) public String getAttributeValue0() { return attributeValue0; } /** * 0 * * @param attributeValue0 * 0 */ public void setAttributeValue0(String attributeValue0) { this.attributeValue0 = attributeValue0; } /** * ?1 * * @return 1 */ @Length(max = 200) public String getAttributeValue1() { return attributeValue1; } /** * 1 * * @param attributeValue1 * 1 */ public void setAttributeValue1(String attributeValue1) { this.attributeValue1 = attributeValue1; } /** * ?2 * * @return 2 */ @Length(max = 200) public String getAttributeValue2() { return attributeValue2; } /** * 2 * * @param attributeValue2 * 2 */ public void setAttributeValue2(String attributeValue2) { this.attributeValue2 = attributeValue2; } /** * ?3 * * @return 3 */ @Length(max = 200) public String getAttributeValue3() { return attributeValue3; } /** * 3 * * @param attributeValue3 * 3 */ public void setAttributeValue3(String attributeValue3) { this.attributeValue3 = attributeValue3; } /** * ?4 * * @return 4 */ @Length(max = 200) public String getAttributeValue4() { return attributeValue4; } /** * 4 * * @param attributeValue4 * 4 */ public void setAttributeValue4(String attributeValue4) { this.attributeValue4 = attributeValue4; } /** * ?5 * * @return 5 */ @Length(max = 200) public String getAttributeValue5() { return attributeValue5; } /** * 5 * * @param attributeValue5 * 5 */ public void setAttributeValue5(String attributeValue5) { this.attributeValue5 = attributeValue5; } /** * ?6 * * @return 6 */ @Length(max = 200) public String getAttributeValue6() { return attributeValue6; } /** * 6 * * @param attributeValue6 * 6 */ public void setAttributeValue6(String attributeValue6) { this.attributeValue6 = attributeValue6; } /** * ?7 * * @return 7 */ @Length(max = 200) public String getAttributeValue7() { return attributeValue7; } /** * 7 * * @param attributeValue7 * 7 */ public void setAttributeValue7(String attributeValue7) { this.attributeValue7 = attributeValue7; } /** * ?8 * * @return 8 */ @Length(max = 200) public String getAttributeValue8() { return attributeValue8; } /** * 8 * * @param attributeValue8 * 8 */ public void setAttributeValue8(String attributeValue8) { this.attributeValue8 = attributeValue8; } /** * ?9 * * @return 9 */ @Length(max = 200) public String getAttributeValue9() { return attributeValue9; } /** * 9 * * @param attributeValue9 * 9 */ public void setAttributeValue9(String attributeValue9) { this.attributeValue9 = attributeValue9; } /** * ? * * @return */ @Embedded public SafeKey getSafeKey() { return safeKey; } /** * * * @param safeKey * */ public void setSafeKey(SafeKey safeKey) { this.safeKey = safeKey; } /** * ? * * @return */ @ManyToOne(fetch = FetchType.LAZY) public Area getArea() { return area; } /** * * * @param area * */ public void setArea(Area area) { this.area = area; } /** * ? * * @return */ @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(nullable = false) public MerchantRank getMerchantRank() { return merchantRank; } /** * * * @param merchantRank * */ public void setMerchantRank(MerchantRank merchantRank) { this.merchantRank = merchantRank; } // /** // * ? // * // * @return // */ // @OneToOne(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Cart getCart() { // return cart; // } // // /** // * // * // * @param cart // * // */ // public void setCart(Cart cart) { // this.cart = cart; // } /** * ?? * * @return ? */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<Order> getOrders() { // return orders; // } // // /** // * ? // * // * @param orders // * ? // */ // public void setOrders(Set<Order> orders) { // this.orders = orders; // } /** * ? * * @return */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<Deposit> getDeposits() { // return deposits; // } // // /** // * // * // * @param deposits // * // */ // public void setDeposits(Set<Deposit> deposits) { // this.deposits = deposits; // } // /** // * ?? // * // * @return ? // */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<Payment> getPayments() { // return payments; // } // // /** // * ? // * // * @param payments // * ? // */ // public void setPayments(Set<Payment> payments) { // this.payments = payments; // } // /** // * ?? // * // * @return ? // */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<CouponCode> getCouponCodes() { // return couponCodes; // } // // /** // * ? // * // * @param couponCodes // * ? // */ // public void setCouponCodes(Set<CouponCode> couponCodes) { // this.couponCodes = couponCodes; // } /** * ?? * * @return ? */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // @OrderBy("isDefault desc, createDate desc") // public Set<Receiver> getReceivers() { // return receivers; // } // // /** // * ? // * // * @param receivers // * ? // */ // public void setReceivers(Set<Receiver> receivers) { // this.receivers = receivers; // } /** * ? * * @return */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // @OrderBy("createDate desc") // public Set<Review> getReviews() { // return reviews; // } // // /** // * // * // * @param reviews // * // */ // public void setReviews(Set<Review> reviews) { // this.reviews = reviews; // } // // /** // * ? // * // * @return // */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // @OrderBy("createDate desc") // public Set<Consultation> getConsultations() { // return consultations; // } // // /** // * // * // * @param consultations // * // */ // public void setConsultations(Set<Consultation> consultations) { // this.consultations = consultations; // } /** * ??? * * @return ?? */ // @ManyToMany(fetch = FetchType.LAZY) // @JoinTable(name = "xx_merchant_favorite_product") // @OrderBy("createDate desc") // public Set<Product> getFavoriteProducts() { // return favoriteProducts; // } // // /** // * ?? // * // * @param favoriteProducts // * ?? // */ // public void setFavoriteProducts(Set<Product> favoriteProducts) { // this.favoriteProducts = favoriteProducts; // } // /** // * ? // * // * @return // */ // @OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<ProductNotify> getProductNotifies() { // return productNotifies; // } // // /** // * // * // * @param productNotifies // * // */ // public void setProductNotifies(Set<ProductNotify> productNotifies) { // this.productNotifies = productNotifies; // } /** * ?? * * @return ? */ // @OneToMany(mappedBy = "receiver", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<Message> getInMessages() { // return inMessages; // } // // /** // * ? // * // * @param inMessages // * ? // */ // public void setInMessages(Set<Message> inMessages) { // this.inMessages = inMessages; // } /** * ???? * * @return ??? */ // @OneToMany(mappedBy = "sender", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) // public Set<Message> getOutMessages() { // return outMessages; // } // // /** // * ??? // * // * @param outMessages // * ??? // */ // public void setOutMessages(Set<Message> outMessages) { // this.outMessages = outMessages; // } /** * ? * * @param merchantAttribute * * @return */ @Transient public Object getAttributeValue(MerchantAttribute merchantAttribute) { if (merchantAttribute != null) { if (merchantAttribute.getType() == Type.name) { return getName(); } else if (merchantAttribute.getType() == Type.gender) { return getGender(); } else if (merchantAttribute.getType() == Type.birth) { return getBirth(); } else if (merchantAttribute.getType() == Type.area) { return getArea(); } else if (merchantAttribute.getType() == Type.address) { return getAddress(); } else if (merchantAttribute.getType() == Type.zipCode) { return getZipCode(); } else if (merchantAttribute.getType() == Type.phone) { return getPhone(); } else if (merchantAttribute.getType() == Type.mobile) { return getMobile(); } else if (merchantAttribute.getType() == Type.checkbox) { if (merchantAttribute.getPropertyIndex() != null) { try { String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + merchantAttribute.getPropertyIndex(); String propertyValue = (String) PropertyUtils.getProperty(this, propertyName); if (propertyValue != null) { return JsonUtils.toObject(propertyValue, List.class); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } else { if (merchantAttribute.getPropertyIndex() != null) { try { String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + merchantAttribute.getPropertyIndex(); return (String) PropertyUtils.getProperty(this, propertyName); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } } return null; } /** * * * @param merchantAttribute * * @param attributeValue * */ @Transient public void setAttributeValue(MerchantAttribute merchantAttribute, Object attributeValue) { if (merchantAttribute != null) { if (attributeValue instanceof String && StringUtils.isEmpty((String) attributeValue)) { attributeValue = null; } if (merchantAttribute.getType() == Type.name && (attributeValue instanceof String || attributeValue == null)) { setName((String) attributeValue); } else if (merchantAttribute.getType() == Type.gender && (attributeValue instanceof Gender || attributeValue == null)) { setGender((Gender) attributeValue); } else if (merchantAttribute.getType() == Type.birth && (attributeValue instanceof Date || attributeValue == null)) { setBirth((Date) attributeValue); } else if (merchantAttribute.getType() == Type.area && (attributeValue instanceof Area || attributeValue == null)) { setArea((Area) attributeValue); } else if (merchantAttribute.getType() == Type.address && (attributeValue instanceof String || attributeValue == null)) { setAddress((String) attributeValue); } else if (merchantAttribute.getType() == Type.zipCode && (attributeValue instanceof String || attributeValue == null)) { setZipCode((String) attributeValue); } else if (merchantAttribute.getType() == Type.phone && (attributeValue instanceof String || attributeValue == null)) { setPhone((String) attributeValue); } else if (merchantAttribute.getType() == Type.mobile && (attributeValue instanceof String || attributeValue == null)) { setMobile((String) attributeValue); } else if (merchantAttribute.getType() == Type.checkbox && (attributeValue instanceof List || attributeValue == null)) { if (merchantAttribute.getPropertyIndex() != null) { if (attributeValue == null || (merchantAttribute.getOptions() != null && merchantAttribute.getOptions().containsAll((List<?>) attributeValue))) { try { String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + merchantAttribute.getPropertyIndex(); PropertyUtils.setProperty(this, propertyName, JsonUtils.toJson(attributeValue)); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } } else { if (merchantAttribute.getPropertyIndex() != null) { try { String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + merchantAttribute.getPropertyIndex(); PropertyUtils.setProperty(this, propertyName, attributeValue); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } } } /** * */ @Transient public void removeAttributeValue() { setName(null); setGender(null); setBirth(null); setArea(null); setAddress(null); setZipCode(null); setPhone(null); setMobile(null); for (int i = 0; i < ATTRIBUTE_VALUE_PROPERTY_COUNT; i++) { String propertyName = ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + i; try { PropertyUtils.setProperty(this, propertyName, null); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } }