01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.sample.petclinic;
18:
19: import java.util.ArrayList;
20: import java.util.Collections;
21: import java.util.HashSet;
22: import java.util.Iterator;
23: import java.util.List;
24: import java.util.Set;
25:
26: import org.springframework.beans.support.MutableSortDefinition;
27: import org.springframework.beans.support.PropertyComparator;
28:
29: /**
30: * Simple JavaBean domain object representing an owner.
31: *
32: * @author Ken Krebs
33: * @author Juergen Hoeller
34: */
35:
36: public class Owner extends Person {
37:
38: private Set pets;
39:
40: protected void setPetsInternal(Set pets) {
41: this .pets = pets;
42: }
43:
44: protected Set getPetsInternal() {
45: if (this .pets == null) {
46: this .pets = new HashSet();
47: }
48: return this .pets;
49: }
50:
51: public List getPets() {
52: List sortedPets = new ArrayList(getPetsInternal());
53: PropertyComparator.sort(sortedPets, new MutableSortDefinition(
54: "name", true, true));
55: return Collections.unmodifiableList(sortedPets);
56: }
57:
58: public void addPet(Pet pet) {
59: getPetsInternal().add(pet);
60: pet.setOwner(this );
61: }
62:
63: /**
64: * Return the Pet with the given name, or null if none found for this Owner.
65: *
66: * @param name
67: * to test
68: * @return true if pet name is already in use
69: */
70: public Pet getPet(String name) {
71: return getPet(name, false);
72: }
73:
74: /**
75: * Return the Pet with the given name, or null if none found for this Owner.
76: *
77: * @param name
78: * to test
79: * @return true if pet name is already in use
80: */
81: public Pet getPet(String name, boolean ignoreNew) {
82: name = name.toLowerCase();
83: for (Iterator it = getPetsInternal().iterator(); it.hasNext();) {
84: Pet pet = (Pet) it.next();
85: if (!ignoreNew || !pet.isNew()) {
86: String compName = pet.getName();
87: compName = compName.toLowerCase();
88: if (compName.equals(name)) {
89: return pet;
90: }
91: }
92: }
93: return null;
94: }
95: }
|