|
15 | 15 | */ |
16 | 16 | package org.springframework.samples.petclinic.model; |
17 | 17 |
|
18 | | -import java.util.ArrayList; |
19 | | -import java.util.Collections; |
20 | | -import java.util.HashSet; |
21 | | -import java.util.List; |
22 | | -import java.util.Set; |
23 | | - |
24 | | -import javax.persistence.CascadeType; |
25 | | -import javax.persistence.Column; |
26 | | -import javax.persistence.Entity; |
27 | | -import javax.persistence.OneToMany; |
28 | | -import javax.persistence.Table; |
29 | | -import javax.validation.constraints.Digits; |
30 | | - |
31 | 18 | import org.hibernate.validator.constraints.NotEmpty; |
32 | 19 | import org.springframework.beans.support.MutableSortDefinition; |
33 | 20 | import org.springframework.beans.support.PropertyComparator; |
34 | 21 | import org.springframework.core.style.ToStringCreator; |
35 | 22 |
|
| 23 | +import javax.persistence.*; |
| 24 | +import javax.validation.constraints.Digits; |
| 25 | +import java.util.*; |
| 26 | + |
36 | 27 | /** |
37 | 28 | * Simple JavaBean domain object representing an owner. |
38 | 29 | * |
|
41 | 32 | * @author Sam Brannen |
42 | 33 | * @author Michael Isvy |
43 | 34 | */ |
44 | | -@Entity @Table(name="owners") |
| 35 | +@Entity |
| 36 | +@Table(name = "owners") |
45 | 37 | public class Owner extends Person { |
46 | | - @Column(name="address") |
47 | | - @NotEmpty |
48 | | - private String address; |
49 | | - |
50 | | - @Column(name="city") |
51 | | - @NotEmpty |
52 | | - private String city; |
53 | | - |
54 | | - @Column(name="telephone") |
55 | | - @NotEmpty @Digits(fraction = 0, integer = 10) |
56 | | - private String telephone; |
57 | | - |
58 | | - @OneToMany(cascade=CascadeType.ALL, mappedBy="owner") |
59 | | - private Set<Pet> pets; |
60 | | - |
61 | | - |
62 | | - public String getAddress() { |
63 | | - return this.address; |
64 | | - } |
65 | | - |
66 | | - public void setAddress(String address) { |
67 | | - this.address = address; |
68 | | - } |
69 | | - |
70 | | - public String getCity() { |
71 | | - return this.city; |
72 | | - } |
73 | | - |
74 | | - public void setCity(String city) { |
75 | | - this.city = city; |
76 | | - } |
77 | | - |
78 | | - public String getTelephone() { |
79 | | - return this.telephone; |
80 | | - } |
81 | | - |
82 | | - public void setTelephone(String telephone) { |
83 | | - this.telephone = telephone; |
84 | | - } |
85 | | - |
86 | | - protected void setPetsInternal(Set<Pet> pets) { |
87 | | - this.pets = pets; |
88 | | - } |
89 | | - |
90 | | - protected Set<Pet> getPetsInternal() { |
91 | | - if (this.pets == null) { |
92 | | - this.pets = new HashSet<Pet>(); |
93 | | - } |
94 | | - return this.pets; |
95 | | - } |
96 | | - |
97 | | - public List<Pet> getPets() { |
98 | | - List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal()); |
99 | | - PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); |
100 | | - return Collections.unmodifiableList(sortedPets); |
101 | | - } |
102 | | - |
103 | | - public void addPet(Pet pet) { |
104 | | - getPetsInternal().add(pet); |
105 | | - pet.setOwner(this); |
106 | | - } |
107 | | - |
108 | | - /** |
109 | | - * Return the Pet with the given name, or null if none found for this Owner. |
110 | | - * |
111 | | - * @param name to test |
112 | | - * @return true if pet name is already in use |
113 | | - */ |
114 | | - public Pet getPet(String name) { |
115 | | - return getPet(name, false); |
116 | | - } |
117 | | - |
118 | | - /** |
119 | | - * Return the Pet with the given name, or null if none found for this Owner. |
120 | | - * |
121 | | - * @param name to test |
122 | | - * @return true if pet name is already in use |
123 | | - */ |
124 | | - public Pet getPet(String name, boolean ignoreNew) { |
125 | | - name = name.toLowerCase(); |
126 | | - for (Pet pet : getPetsInternal()) { |
127 | | - if (!ignoreNew || !pet.isNew()) { |
128 | | - String compName = pet.getName(); |
129 | | - compName = compName.toLowerCase(); |
130 | | - if (compName.equals(name)) { |
131 | | - return pet; |
132 | | - } |
133 | | - } |
134 | | - } |
135 | | - return null; |
136 | | - } |
137 | | - |
138 | | - @Override |
139 | | - public String toString() { |
140 | | - return new ToStringCreator(this) |
141 | | - |
142 | | - .append("id", this.getId()) |
143 | | - .append("new", this.isNew()) |
144 | | - .append("lastName", this.getLastName()) |
145 | | - .append("firstName", this.getFirstName()) |
146 | | - .append("address", this.address) |
147 | | - .append("city", this.city) |
148 | | - .append("telephone", this.telephone) |
149 | | - .toString(); |
150 | | - } |
| 38 | + @Column(name = "address") |
| 39 | + @NotEmpty |
| 40 | + private String address; |
| 41 | + |
| 42 | + @Column(name = "city") |
| 43 | + @NotEmpty |
| 44 | + private String city; |
| 45 | + |
| 46 | + @Column(name = "telephone") |
| 47 | + @NotEmpty |
| 48 | + @Digits(fraction = 0, integer = 10) |
| 49 | + private String telephone; |
| 50 | + |
| 51 | + @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") |
| 52 | + private Set<Pet> pets; |
| 53 | + |
| 54 | + |
| 55 | + public String getAddress() { |
| 56 | + return this.address; |
| 57 | + } |
| 58 | + |
| 59 | + public void setAddress(String address) { |
| 60 | + this.address = address; |
| 61 | + } |
| 62 | + |
| 63 | + public String getCity() { |
| 64 | + return this.city; |
| 65 | + } |
| 66 | + |
| 67 | + public void setCity(String city) { |
| 68 | + this.city = city; |
| 69 | + } |
| 70 | + |
| 71 | + public String getTelephone() { |
| 72 | + return this.telephone; |
| 73 | + } |
| 74 | + |
| 75 | + public void setTelephone(String telephone) { |
| 76 | + this.telephone = telephone; |
| 77 | + } |
| 78 | + |
| 79 | + protected void setPetsInternal(Set<Pet> pets) { |
| 80 | + this.pets = pets; |
| 81 | + } |
| 82 | + |
| 83 | + protected Set<Pet> getPetsInternal() { |
| 84 | + if (this.pets == null) { |
| 85 | + this.pets = new HashSet<Pet>(); |
| 86 | + } |
| 87 | + return this.pets; |
| 88 | + } |
| 89 | + |
| 90 | + public List<Pet> getPets() { |
| 91 | + List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal()); |
| 92 | + PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); |
| 93 | + return Collections.unmodifiableList(sortedPets); |
| 94 | + } |
| 95 | + |
| 96 | + public void addPet(Pet pet) { |
| 97 | + getPetsInternal().add(pet); |
| 98 | + pet.setOwner(this); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Return the Pet with the given name, or null if none found for this Owner. |
| 103 | + * |
| 104 | + * @param name to test |
| 105 | + * @return true if pet name is already in use |
| 106 | + */ |
| 107 | + public Pet getPet(String name) { |
| 108 | + return getPet(name, false); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Return the Pet with the given name, or null if none found for this Owner. |
| 113 | + * |
| 114 | + * @param name to test |
| 115 | + * @return true if pet name is already in use |
| 116 | + */ |
| 117 | + public Pet getPet(String name, boolean ignoreNew) { |
| 118 | + name = name.toLowerCase(); |
| 119 | + for (Pet pet : getPetsInternal()) { |
| 120 | + if (!ignoreNew || !pet.isNew()) { |
| 121 | + String compName = pet.getName(); |
| 122 | + compName = compName.toLowerCase(); |
| 123 | + if (compName.equals(name)) { |
| 124 | + return pet; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String toString() { |
| 133 | + return new ToStringCreator(this) |
| 134 | + |
| 135 | + .append("id", this.getId()) |
| 136 | + .append("new", this.isNew()) |
| 137 | + .append("lastName", this.getLastName()) |
| 138 | + .append("firstName", this.getFirstName()) |
| 139 | + .append("address", this.address) |
| 140 | + .append("city", this.city) |
| 141 | + .append("telephone", this.telephone) |
| 142 | + .toString(); |
| 143 | + } |
151 | 144 | } |
0 commit comments