1 package org.hibernate.ce.auction.model; 2 3 import org.hibernate.ce.auction.exceptions.BusinessException; 4 5 import javax.persistence.*; 6 import java.io.Serializable ; 7 import java.util.*; 8 9 34 @Entity(access = AccessType.FIELD) 35 @Table(name = "USERS") 36 public class User implements Serializable , Comparable { 37 38 @Id(generate = GeneratorType.AUTO) 39 @Column(name = "USER_ID") 40 private Long id = null; 41 42 @Version 43 private int version = 0; 44 45 @Column(length = 255, nullable = false) 46 private String firstname; 47 48 @Column(length = 255, nullable = false) 49 private String lastname; 50 51 @Column(length = 16, nullable = false, unique = true, updatable = false) 52 private String username; 53 54 @Column(name = "'PASSWORD'", length = 12, nullable = false) 55 private String password; 56 57 @Column(length = 255, nullable = false) 58 private String email; 59 60 @Column(nullable = false) 61 private int ranking = 0; 62 63 @Column(name = "IS_ADMIN", nullable = false) 64 private boolean admin = false; 65 66 @Column(nullable = false) 67 private Date created = new Date(); 68 69 @Embedded 70 private Address address; 71 72 @OneToMany(mappedBy = "seller") 73 private Set<Item> items = new HashSet<Item>(); 74 75 @OneToMany(cascade = CascadeType.ALL, mappedBy = "user") 76 private Set<BillingDetails> billingDetails = new HashSet<BillingDetails>(); 77 78 @ManyToOne(fetch = FetchType.LAZY) 79 @JoinColumn(name="DEFAULT_BILLINGDETAILS_ID", nullable = true) 80 private BillingDetails defaultBillingDetails; 81 82 85 User() {} 86 87 90 public User(String firstname, String lastname, String username, 91 String password, String email, 92 Address address, Set<Item> items, 93 Set<BillingDetails> billingDetails) { 94 this.firstname = firstname; 95 this.lastname = lastname; 96 this.username = username; 97 this.password = password; 98 this.email = email; 99 this.address = address; 100 this.items = items; 101 this.billingDetails = billingDetails; 102 } 103 104 107 public User(String firstname, String lastname, 108 String username, String password, String email) { 109 this.firstname = firstname; 110 this.lastname = lastname; 111 this.username = username; 112 this.password = password; 113 this.email = email; 114 } 115 116 118 public Long getId() { return id; } 119 public int getVersion() { return version; } 120 121 public String getFirstname() { return firstname; } 122 public void setFirstname(String firstname) { this.firstname = firstname; } 123 124 public String getLastname() { return lastname; } 125 public void setLastname(String lastname) { this.lastname = lastname; } 126 127 public String getUsername() { return username; } 128 129 public String getPassword() { return password; } 130 public void setPassword(String password) { this.password = password; } 131 132 public String getEmail() { return email; } 133 public void setEmail(String email) { this.email = email; } 134 135 public int getRanking() { return ranking; } 136 public void setRanking(int ranking) { this.ranking = ranking; } 137 138 public Address getAddress() { return address; } 139 public void setAddress(Address address) { this.address = address; } 140 141 public Set getItems() { return items; } 142 public void addItem(Item item) { 143 if (item == null) 144 throw new IllegalArgumentException ("Can't add a null Item."); 145 this.getItems().add(item); 146 } 147 148 @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN) 149 public Set getBillingDetails() { return billingDetails; } 150 158 public void addBillingDetails(BillingDetails billingDetails) { 159 if (billingDetails == null) 160 throw new IllegalArgumentException ("Can't add a null BillingDetails."); 161 this.getBillingDetails().add(billingDetails); 162 163 if (getBillingDetails().size() == 1) { 164 setDefaultBillingDetails(billingDetails); 165 } 166 } 167 178 public void removeBillingDetails(BillingDetails billingDetails) 179 throws BusinessException { 180 if (billingDetails == null) 181 throw new IllegalArgumentException ("Can't add a null BillingDetails."); 182 183 if (getBillingDetails().size() >= 2) { 184 getBillingDetails().remove(billingDetails); 185 setDefaultBillingDetails((BillingDetails)getBillingDetails().iterator().next()); 186 } else { 187 throw new BusinessException("Please set new default BillingDetails first"); 188 } 189 } 190 191 public BillingDetails getDefaultBillingDetails() { return defaultBillingDetails; } 192 public void setDefaultBillingDetails(BillingDetails defaultBillingDetails) { 193 this.defaultBillingDetails = defaultBillingDetails; 194 } 195 196 public Date getCreated() { return created; } 197 198 public boolean isAdmin() { return admin; } 199 public void setAdmin(boolean admin) { this.admin = admin; } 200 201 203 public boolean equals(Object o) { 204 if (this == o) return true; 205 if (!(o instanceof User)) return false; 206 final User user = (User) o; 207 if (!username.equals(user.username)) return false; 208 return true; 209 } 210 211 public int hashCode() { 212 return username.hashCode(); 213 } 214 215 public String toString() { 216 return "User ('" + getId() + "'), " + 217 "Username: '" + getUsername() + "'"; 218 } 219 220 public int compareTo(Object o) { 221 if (o instanceof User) 222 return this.getCreated().compareTo( ((User)o).getCreated() ); 223 return 0; 224 } 225 226 228 public void increaseRanking() { 229 setRanking(getRanking() + 1); 230 } 231 232 } 233 | Popular Tags |