Advertisement
Guest User

Customer.java

a guest
Apr 30th, 2013
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.84 KB | None | 0 0
  1. package com.example.model;
  2.  
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. import javax.persistence.CascadeType;
  9. import javax.persistence.Column;
  10. import javax.persistence.Entity;
  11. import javax.persistence.FetchType;
  12. import javax.persistence.GeneratedValue;
  13. import javax.persistence.GenerationType;
  14. import javax.persistence.Id;
  15. import javax.persistence.MapKey;
  16. import javax.persistence.OneToMany;
  17. import javax.persistence.OneToOne;
  18. import javax.persistence.Table;
  19. import javax.persistence.Temporal;
  20. import javax.persistence.TemporalType;
  21.  
  22. import org.springframework.beans.factory.annotation.Required;
  23.  
  24. import com.google.common.base.Strings;
  25.  
  26. @Entity
  27. @Table(name = "customer")
  28. public class Customer extends BaseObject {
  29.  
  30.     private static final long serialVersionUID = -6998276217573300147L;
  31.  
  32.     private Long id;
  33.  
  34.     private Date createdTimestamp = new Date();
  35.  
  36.     private String customerNumber;
  37.     private String lastName;
  38.     private String firstName;
  39.     private EmailContact emailContact;
  40.     private Map<PhoneType, PhoneContact> phoneContacts;
  41.  
  42.     public Customer() {
  43.         super();
  44.     }
  45.  
  46.     public Customer(String customerNumber) {
  47.         super();
  48.         this.customerNumber = customerNumber;
  49.     }
  50.  
  51.     public void addPhoneContact(PhoneContact phoneContact) {
  52.         if (phoneContact != null) {
  53.             getPhoneContacts().put( phoneContact.getPhoneType(),
  54.                                     phoneContact);
  55.         }
  56.     }
  57.  
  58.     @Id
  59.     @GeneratedValue(strategy = GenerationType.AUTO)
  60.     public Long getId() {
  61.         return id;
  62.     }
  63.  
  64.     public void setId(Long id) {
  65.         this.id = id;
  66.     }
  67.  
  68.     @Column(name = "created_timestamp")
  69.     @Temporal(TemporalType.TIMESTAMP)
  70.     @Required
  71.     public Date getCreatedTimestamp() {
  72.         return createdTimestamp;
  73.     }
  74.  
  75.     public void setCreatedTimestamp(Date createdTimestamp) {
  76.         this.createdTimestamp = createdTimestamp;
  77.     }
  78.  
  79.     @Column(name = "customer_number", length = 20, unique = true)
  80.     @Required
  81.     public String getCustomerNumber() {
  82.         return customerNumber;
  83.     }
  84.  
  85.     public void setCustomerNumber(String customerNumber) {
  86.         this.customerNumber = customerNumber;
  87.     }
  88.  
  89.     @Column(name = "last_name", length = 100)
  90.     public String getLastName() {
  91.         return lastName;
  92.     }
  93.  
  94.     public void setLastName(String lastName) {
  95.         this.lastName = lastName;
  96.     }
  97.  
  98.     @Column(name = "first_name", length = 100)
  99.     public String getFirstName() {
  100.         return firstName;
  101.     }
  102.  
  103.     public void setFirstName(String firstName) {
  104.         this.firstName = firstName;
  105.     }
  106.  
  107.     @OneToOne(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  108.     public EmailContact getEmailContact() {
  109.         return emailContact;
  110.     }
  111.  
  112.     public void setEmailContact(EmailContact emailContact) {
  113.         this.emailContact = emailContact;
  114.     }
  115.  
  116.     @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  117.     @MapKey(name = "phoneType")
  118.     public Map<PhoneType, PhoneContact> getPhoneContacts() {
  119.         if (phoneContacts == null) {
  120.             phoneContacts = new HashMap<PhoneType, PhoneContact>();
  121.         }
  122.         return phoneContacts;
  123.     }
  124.  
  125.     public void setPhoneContacts(Map<PhoneType, PhoneContact> phoneContacts) {
  126.         this.phoneContacts = phoneContacts;
  127.     }
  128.  
  129.     @Override
  130.     public int hashCode() {
  131.         final int prime = 31;
  132.         int result = 1;
  133.         result = prime * result + ((customerNumber == null) ? 0 : customerNumber.hashCode());
  134.         return result;
  135.     }
  136.  
  137.     @Override
  138.     public boolean equals(Object obj) {
  139.         if (this == obj)
  140.             return true;
  141.         if (obj == null)
  142.             return false;
  143.         if (getClass() != obj.getClass())
  144.             return false;
  145.         Customer other = (Customer) obj;
  146.         if (customerNumber == null) {
  147.             if (other.customerNumber != null)
  148.                 return false;
  149.         } else if (!customerNumber.equals(other.customerNumber))
  150.             return false;
  151.         return true;
  152.     }
  153.  
  154.     @Override
  155.     public String toString() {
  156.         String toString = "Customer [id=" + (id == null ? "null" : id.toString()) + ", createdTimestamp=" + createdTimestamp
  157.                             + ", customerNumber=" + customerNumber + ", lastName=" + lastName + ", firstName=" + firstName + ", emailContact="
  158.                             + (emailContact == null ? "null" : emailContact.getEmailAddress()) + ", phoneContacts=" + toString(phoneContacts)
  159.                             + "]";
  160.         System.out.println(toString);
  161.         return toString;
  162.     }
  163.  
  164.     private String toString(Map<PhoneType, PhoneContact> phoneContacts) {
  165.         StringBuilder builder = new StringBuilder();
  166.         if (phoneContacts != null) {
  167.             builder.append("[");
  168.             for (Entry<PhoneType, PhoneContact> entry : phoneContacts.entrySet()) {
  169.                 if (builder.length() > 1) {
  170.                     builder.append(", ");
  171.                 }
  172.  
  173.                 builder.append(entry.getKey()).append("=");
  174.                 if (entry.getValue() != null) {
  175.                     builder.append(entry.getValue().getPhoneNumber());
  176.                     if (!Strings.isNullOrEmpty(entry.getValue().getPhoneExtention())) {
  177.                         builder.append("x").append(entry.getValue().getPhoneExtention());
  178.                     }
  179.                 }
  180.             }
  181.             builder.append("]");
  182.         }
  183.         return builder.toString();
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement