Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.model;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.MapKey;
- import javax.persistence.OneToMany;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- import org.springframework.beans.factory.annotation.Required;
- import com.google.common.base.Strings;
- @Entity
- @Table(name = "customer")
- public class Customer extends BaseObject {
- private static final long serialVersionUID = -6998276217573300147L;
- private Long id;
- private Date createdTimestamp = new Date();
- private String customerNumber;
- private String lastName;
- private String firstName;
- private EmailContact emailContact;
- private Map<PhoneType, PhoneContact> phoneContacts;
- public Customer() {
- super();
- }
- public Customer(String customerNumber) {
- super();
- this.customerNumber = customerNumber;
- }
- public void addPhoneContact(PhoneContact phoneContact) {
- if (phoneContact != null) {
- getPhoneContacts().put( phoneContact.getPhoneType(),
- phoneContact);
- }
- }
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- @Column(name = "created_timestamp")
- @Temporal(TemporalType.TIMESTAMP)
- @Required
- public Date getCreatedTimestamp() {
- return createdTimestamp;
- }
- public void setCreatedTimestamp(Date createdTimestamp) {
- this.createdTimestamp = createdTimestamp;
- }
- @Column(name = "customer_number", length = 20, unique = true)
- @Required
- public String getCustomerNumber() {
- return customerNumber;
- }
- public void setCustomerNumber(String customerNumber) {
- this.customerNumber = customerNumber;
- }
- @Column(name = "last_name", length = 100)
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- @Column(name = "first_name", length = 100)
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- @OneToOne(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
- public EmailContact getEmailContact() {
- return emailContact;
- }
- public void setEmailContact(EmailContact emailContact) {
- this.emailContact = emailContact;
- }
- @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
- @MapKey(name = "phoneType")
- public Map<PhoneType, PhoneContact> getPhoneContacts() {
- if (phoneContacts == null) {
- phoneContacts = new HashMap<PhoneType, PhoneContact>();
- }
- return phoneContacts;
- }
- public void setPhoneContacts(Map<PhoneType, PhoneContact> phoneContacts) {
- this.phoneContacts = phoneContacts;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((customerNumber == null) ? 0 : customerNumber.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Customer other = (Customer) obj;
- if (customerNumber == null) {
- if (other.customerNumber != null)
- return false;
- } else if (!customerNumber.equals(other.customerNumber))
- return false;
- return true;
- }
- @Override
- public String toString() {
- String toString = "Customer [id=" + (id == null ? "null" : id.toString()) + ", createdTimestamp=" + createdTimestamp
- + ", customerNumber=" + customerNumber + ", lastName=" + lastName + ", firstName=" + firstName + ", emailContact="
- + (emailContact == null ? "null" : emailContact.getEmailAddress()) + ", phoneContacts=" + toString(phoneContacts)
- + "]";
- System.out.println(toString);
- return toString;
- }
- private String toString(Map<PhoneType, PhoneContact> phoneContacts) {
- StringBuilder builder = new StringBuilder();
- if (phoneContacts != null) {
- builder.append("[");
- for (Entry<PhoneType, PhoneContact> entry : phoneContacts.entrySet()) {
- if (builder.length() > 1) {
- builder.append(", ");
- }
- builder.append(entry.getKey()).append("=");
- if (entry.getValue() != null) {
- builder.append(entry.getValue().getPhoneNumber());
- if (!Strings.isNullOrEmpty(entry.getValue().getPhoneExtention())) {
- builder.append("x").append(entry.getValue().getPhoneExtention());
- }
- }
- }
- builder.append("]");
- }
- return builder.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement