owner add age

This commit is contained in:
WickedSejin 2020-03-13 12:39:22 +09:00
parent ac3e64208e
commit 7bc27a916e

View file

@ -1,150 +1,160 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * https://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.samples.petclinic.owner; package org.springframework.samples.petclinic.owner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.Digits; import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator; import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator; import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person; import org.springframework.samples.petclinic.model.Person;
/** /**
* Simple JavaBean domain object representing an owner. * Simple JavaBean domain object representing an owner.
* *
* @author Ken Krebs * @author Ken Krebs
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen * @author Sam Brannen
* @author Michael Isvy * @author Michael Isvy
*/ */
@Entity @Entity
@Table(name = "owners") @Table(name = "owners")
public class Owner extends Person { public class Owner extends Person {
@Column(name = "address") @Column(name = "address")
@NotEmpty @NotEmpty
private String address; private String address;
@Column(name = "city") @Column(name = "city")
@NotEmpty @NotEmpty
private String city; private String city;
@Column(name = "telephone") @Column(name = "telephone")
@NotEmpty @NotEmpty
@Digits(fraction = 0, integer = 10) @Digits(fraction = 0, integer = 10)
private String telephone; private String telephone;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private Set<Pet> pets; private Set<Pet> pets;
public String getAddress() { private int age;
return this.address;
} public int getAge() {
return age;
public void setAddress(String address) { }
this.address = address;
} public void setAge(int age) {
this.age = age;
public String getCity() { }
return this.city;
} public String getAddress() {
return this.address;
public void setCity(String city) { }
this.city = city;
} public void setAddress(String address) {
this.address = address;
public String getTelephone() { }
return this.telephone;
} public String getCity() {
return this.city;
public void setTelephone(String telephone) { }
this.telephone = telephone;
} public void setCity(String city) {
this.city = city;
protected Set<Pet> getPetsInternal() { }
if (this.pets == null) {
this.pets = new HashSet<>(); public String getTelephone() {
} return this.telephone;
return this.pets; }
}
public void setTelephone(String telephone) {
protected void setPetsInternal(Set<Pet> pets) { this.telephone = telephone;
this.pets = pets; }
}
protected Set<Pet> getPetsInternal() {
public List<Pet> getPets() { if (this.pets == null) {
List<Pet> sortedPets = new ArrayList<>(getPetsInternal()); this.pets = new HashSet<>();
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); }
return Collections.unmodifiableList(sortedPets); return this.pets;
} }
public void addPet(Pet pet) { protected void setPetsInternal(Set<Pet> pets) {
if (pet.isNew()) { this.pets = pets;
getPetsInternal().add(pet); }
}
pet.setOwner(this); public List<Pet> getPets() {
} List<Pet> sortedPets = new ArrayList<>(getPetsInternal());
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
/** return Collections.unmodifiableList(sortedPets);
* Return the Pet with the given name, or null if none found for this Owner. }
* @param name to test
* @return true if pet name is already in use public void addPet(Pet pet) {
*/ if (pet.isNew()) {
public Pet getPet(String name) { getPetsInternal().add(pet);
return getPet(name, false); }
} pet.setOwner(this);
}
/**
* Return the Pet with the given name, or null if none found for this Owner. /**
* @param name to test * Return the Pet with the given name, or null if none found for this Owner.
* @return true if pet name is already in use * @param name to test
*/ * @return true if pet name is already in use
public Pet getPet(String name, boolean ignoreNew) { */
name = name.toLowerCase(); public Pet getPet(String name) {
for (Pet pet : getPetsInternal()) { return getPet(name, false);
if (!ignoreNew || !pet.isNew()) { }
String compName = pet.getName();
compName = compName.toLowerCase(); /**
if (compName.equals(name)) { * Return the Pet with the given name, or null if none found for this Owner.
return pet; * @param name to test
} * @return true if pet name is already in use
} */
} public Pet getPet(String name, boolean ignoreNew) {
return null; name = name.toLowerCase();
} for (Pet pet : getPetsInternal()) {
if (!ignoreNew || !pet.isNew()) {
@Override String compName = pet.getName();
public String toString() { compName = compName.toLowerCase();
return new ToStringCreator(this) if (compName.equals(name)) {
return pet;
.append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName()) }
.append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city) }
.append("telephone", this.telephone).toString(); }
} return null;
}
}
@Override
public String toString() {
return new ToStringCreator(this)
.append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName())
.append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city)
.append("telephone", this.telephone).toString();
}
}