View Javadoc

1   package org.springframework.samples.petclinic;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Set;
8   
9   import org.springframework.beans.support.MutableSortDefinition;
10  import org.springframework.beans.support.PropertyComparator;
11  import org.springframework.core.style.ToStringCreator;
12  
13  /**
14   * Simple JavaBean domain object representing an owner.
15   *
16   * @author Ken Krebs
17   * @author Juergen Hoeller
18   * @author Sam Brannen
19   */
20  public class Owner extends Person {
21  
22  	private String address;
23  
24  	private String city;
25  
26  	private String telephone;
27  
28  	private Set<Pet> pets;
29  
30  
31  	public String getAddress() {
32  		return this.address;
33  	}
34  
35  	public void setAddress(String address) {
36  		this.address = address;
37  	}
38  
39  	public String getCity() {
40  		return this.city;
41  	}
42  
43  	public void setCity(String city) {
44  		this.city = city;
45  	}
46  
47  	public String getTelephone() {
48  		return this.telephone;
49  	}
50  
51  	public void setTelephone(String telephone) {
52  		this.telephone = telephone;
53  	}
54  
55  	protected void setPetsInternal(Set<Pet> pets) {
56  		this.pets = pets;
57  	}
58  
59  	protected Set<Pet> getPetsInternal() {
60  		if (this.pets == null) {
61  			this.pets = new HashSet<Pet>();
62  		}
63  		return this.pets;
64  	}
65  
66  	public List<Pet> getPets() {
67  		List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal());
68  		PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
69  		return Collections.unmodifiableList(sortedPets);
70  	}
71  
72  	public void addPet(Pet pet) {
73  		getPetsInternal().add(pet);
74  		pet.setOwner(this);
75  	}
76  
77  	/**
78  	 * Return the Pet with the given name, or null if none found for this Owner.
79  	 *
80  	 * @param name to test
81  	 * @return true if pet name is already in use
82  	 */
83  	public Pet getPet(String name) {
84  		return getPet(name, false);
85  	}
86  
87  	/**
88  	 * Return the Pet with the given name, or null if none found for this Owner.
89  	 *
90  	 * @param name to test
91  	 * @return true if pet name is already in use
92  	 */
93  	public Pet getPet(String name, boolean ignoreNew) {
94  		name = name.toLowerCase();
95  		for (Pet pet : getPetsInternal()) {
96  			if (!ignoreNew || !pet.isNew()) {
97  				String compName = pet.getName();
98  				compName = compName.toLowerCase();
99  				if (compName.equals(name)) {
100 					return pet;
101 				}
102 			}
103 		}
104 		return null;
105 	}
106 
107 	@Override
108 	public String toString() {
109 		return new ToStringCreator(this)
110 
111 		.append("id", this.getId())
112 
113 		.append("new", this.isNew())
114 
115 		.append("lastName", this.getLastName())
116 
117 		.append("firstName", this.getFirstName())
118 
119 		.append("address", this.address)
120 
121 		.append("city", this.city)
122 
123 		.append("telephone", this.telephone)
124 
125 		.toString();
126 	}
127 }