diff --git a/pom.xml b/pom.xml index 803ccc6e2..f02ba022f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,318 +1,962 @@ - 4.0.0 - org.springframework.samples - spring-petclinic - petclinic - war - 1.0.0-SNAPSHOT - - 3.2.1.RELEASE - 1.7.0 - 1.2.17 - 4.1.4.Final - 1.7.1 - 4.2.0.Final - 2.0 - 1.1.0.RELEASE - 4.10 - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + org.springframework.samples + spring-petclinic + 1.0.0-SNAPSHOT - - - org.springframework - spring-context - ${spring.version} - - - - org.apache.commons - commons-logging - - - - - org.springframework - spring-context-support - ${spring.version} - - - org.springframework - spring-orm - ${spring.version} - - - org.springframework - spring-oxm - ${spring.version} - - - org.springframework - spring-webmvc - ${spring.version} - - - - - org.springframework - spring-web - ${spring.version} - + petclinic + war - - - org.springframework - spring-jdbc - ${spring.version} - + + 2.7.7 + 1.7.1 + 1.4 + 3.1 + 1.6 + 2.6.3 + 1.3 + 1.0.1.Final + 4.3.0.Final + 4.1.9.Final + 2.2.9 + 3.1.0.CR1 + 3.17.1-GA + 1.6 + 2.2 + 2.2.7 + 2.2.7-b53 + 3.1.2.GA + 2.0.2 + 1.3 + 1.1.1 + 2.1 + 2.2 + 1.2 + 1.1 + 4.11 + 1.0.9 + 1.2.17 + 2.4 + 2.5 + 3.0 + 2.6 + 2.7 + 2.9 + 1.2 + 2.1.1 + 2.4 + 2.4 + 2.9 + 8.1.9.v20130131 + 1.0.0 + 3.2 + 2.6 + 2.4 + 2.6 + 3.2 + 2.2.1 + 2.13 + 2.4 + [2.2.1,3.1.0) + 2.3 + 5.1.22 + + UTF-8 + UTF-8 + 1.0 + 2.5 + 1.7.2 + 1.3.0.RELEASE + 3.2.1.RELEASE + 1.0.0.GA + 2.3.0 + 1.9.2 + 1.9.0 + - - - org.slf4j - jcl-over-slf4j - ${slf4j.version} - runtime - - - - org.slf4j - slf4j-api - ${slf4j.version} - runtime - - - - org.slf4j - slf4j-log4j12 - ${slf4j.version} - runtime - - - - log4j - log4j - ${log4j.version} - runtime - + + + + org.springframework + spring-context + + + org.springframework + spring-context-support + + + org.springframework + spring-orm + + + org.springframework + spring-oxm + + + org.springframework + spring-webmvc + + + + org.springframework + spring-web + + + + org.springframework + spring-jdbc + + + + org.slf4j + jcl-over-slf4j + + + org.slf4j + jul-to-slf4j + + + ch.qos.logback + logback-classic + - - - commons-dbcp - commons-dbcp - 1.4 - runtime - + + + commons-dbcp + commons-dbcp + + + org.hsqldb + hsqldb + + + + + + org.hibernate + hibernate-entitymanager + + + org.hibernate + hibernate-validator + + + net.sf.ehcache + ehcache-core + + + + org.springframework.data + spring-data-jpa + + + + javax.servlet + servlet-api + + + javax.servlet + jstl + + + + org.webjars + bootstrap + + + org.webjars + jquery-ui + + + + rome + rome + + + org.jdom + jdom + + + + com.sun.xml.bind + jaxb-impl + + + + + junit + junit + + + org.springframework + spring-test + + + javax.transaction + jta + + + joda-time + joda-time + + + org.jadira.usertype + usertype.core + + + joda-time + joda-time-jsptags + + + joda-time + joda-time-hibernate + + + + org.hamcrest + hamcrest-library + + + org.aspectj + aspectjweaver + + - - org.hsqldb - hsqldb - 2.2.8 - runtime - - - - - - - org.aspectj - aspectjrt - ${aspectj.version} - - - - org.aspectj - aspectjweaver - ${aspectj.version} - + + + install + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + + + + true + ${java.version} + ${java.version} + true + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*Tests.java + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + petclinic + + + + + org.apache.maven.plugins + maven-eclipse-plugin + ${maven-eclipse-plugin.version} + + true + true + 2.0 + + **/*.* + + + + org.springframework.ide.eclipse.core.springbuilder + + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.jdt.core.javanature + org.springframework.ide.eclipse.core.springnature + org.eclipse.m2e.core.maven2Nature + + + + + org.apache.maven.plugins + maven-dependency-plugin + ${maven-dependency-plugin.version} + + + install + install + + sources + + + + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + + org.apache.maven.plugins + maven-install-plugin + ${maven-install-plugin.version} + + + org.apache.maven.plugins + maven-release-plugin + ${maven-release-plugin.version} + + + org.apache.maven.plugins + maven-assembly-plugin + ${maven-assembly-plugin.version} + + + jar-with-dependencies + + + + + org.apache.maven.plugins + maven-clean-plugin + ${maven-clean-plugin.version} + + + + ${project.basedir} + + target + surefire* + + false + + + + + + org.mortbay.jetty + jetty-maven-plugin + ${maven-jetty-plugin.version} + + + /${project.name} + + + 9966 + shutterdown + + + logback.configurationFile + ./src/main/resources/logback.xml + + + com.sun.management.jmxremote + true + + + com.sun.management.jmxremote.port + 8050 + + + com.sun.management.jmxremote.ssl + false + + + com.sun.management.jmxremote.authenticate + false + + + + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + runtime + + + org.slf4j + slf4j-api + ${slf4j.version} + runtime + + + org.slf4j + jul-to-slf4j + ${slf4j.version} + runtime + + + ch.qos.logback + logback-classic + ${logback.version} + runtime + + + commons-dbcp + commons-dbcp + ${commons-dbcp.version} + runtime + + + commons-pool + commons-pool + ${commons-pool.version} + runtime + + + org.jdom + jdom + ${jdom.version} + runtime + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${maven-enforcer-plugin.version} + + + enforce-banned-dependencies + + enforce + + + + + + + + + true + + javassist + commons-logging + aspectj + asm + hsqldb + log4j + org.slf4j:1.5* + org.slf4j:1.6* + org.springframework:spring:2.* + org.springframework:spring*:3.0.* + org.springframework:spring*:3.1.* + + + + ${maven.version.range} + + + ${java.version} + + + true + + + + + + org.apache.maven.plugins + maven-resources-plugin + ${maven-resources-plugin.version} + + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-site-plugin + ${maven-site-plugin.version} + + + attach-descriptor + + attach-descriptor + + + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + ${maven-surefire-plugin.version} + + ${project.reporting.outputDirectory}/testresults + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + true + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + ${maven-project-info-reports-plugin.version} + + false + false + + + + en + + - - - - - org.hibernate - hibernate-entitymanager - ${hibernate.version} - - - - org.hibernate - hibernate-validator - ${hibernate.validator.version} - - - - - - org.springframework.data - spring-data-jpa - ${spring.data.jpa} - + - - - javax.servlet - servlet-api - 2.5 - provided - - - javax.servlet - jstl - 1.2 - - - - - org.webjars - bootstrap - 2.2.1 - - - - org.webjars - jquery-ui - 1.9.1 - - - - - rome - rome - 1.0 - - - - org.jdom - jdom - 2.0.2 - runtime - - - - - com.sun.xml.bind - jaxb-impl - 2.2.6 - provided - + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + [1.0.0,) + + enforce + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + [2.4,) + + unpack + sources + + + + + + + + + + + + + + + + + + + + org.aspectj + aspectjrt + ${aspectj.version} + + + org.aspectj + aspectjweaver + ${aspectj.version} + runtime + + + org.aspectj + aspectjtools + ${aspectj.version} + + + + org.hibernate + hibernate-core + ${hibernate.version} + + + asm + asm-attrs + + + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + org.hibernate.javax.persistence + hibernate-jpa-2.0-api + ${hibernate-jpa.version} + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + org.hibernate + hibernate-ehcache + ${hibernate.version} + + + net.sf.ehcache + ehcache-core + ${ehcache.version} + + + commons-logging + commons-logging + + + + + org.jadira.usertype + usertype.core + ${jadira-usertype-core.version} + + + javax.validation + validation-api + ${validation.version} + + + javax.servlet + jstl + ${jstl.version} + + + javax.servlet + servlet-api + ${servlet.version} + provided + + + javax.xml.bind + jaxb-api + ${jaxb-api.version} + provided + + + com.sun.xml.bind + jaxb-impl + ${jaxb-impl.version} + provided + + + + org.springframework.data + spring-data-jpa + ${spring-data-jpa.version} + + + org.springframework + spring-test + ${spring-framework.version} + test + + + org.springframework + spring-beans + ${spring-framework.version} + + + org.springframework + spring-context + ${spring-framework.version} + + + org.springframework + spring-jdbc + ${spring-framework.version} + + + org.springframework + spring-aop + ${spring-framework.version} + + + org.springframework + spring-web + ${spring-framework.version} + + + org.springframework + spring-webmvc + ${spring-framework.version} + + + org.springframework + spring-expression + ${spring-framework.version} + + + org.springframework + spring-tx + ${spring-framework.version} + + + org.springframework + spring-core + ${spring-framework.version} + + + commons-logging + commons-logging + + + + + org.springframework + spring-aspects + ${spring-framework.version} + + + org.springframework + spring-context-support + ${spring-framework.version} + + + quartz + quartz + + + + + org.springframework + spring-orm + ${spring-framework.version} + + + org.springframework + spring-oxm + ${spring-framework.version} + + + commons-lang + commons-lang + + + + + org.springframework + spring-jms + ${spring-framework.version} + + + javax.transaction + jta + ${jta.version} + + + org.javassist + javassist + ${javassist.version} + + + + + commons-dbcp + commons-dbcp + ${commons-dbcp.version} + runtime + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + commons-pool + commons-pool + ${commons-pool.version} + runtime + + + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + runtime + + + org.slf4j + slf4j-api + ${slf4j.version} + runtime + + + org.slf4j + jul-to-slf4j + ${slf4j.version} + runtime + + + ch.qos.logback + logback-classic + ${logback.version} + runtime + + + ch.qos.logback + logback-core + ${logback.version} + runtime + + + rome + rome + ${rome.version} + + + joda-time + joda-time + ${jodatime.version} + + + joda-time + joda-time-hibernate + ${jodatime-hibernate.version} + + + joda-time + joda-time-jsptags + ${jodatime-jsptags.version} + + + antlr + antlr + ${antlr.version} + + + + + mysql + mysql-connector-java + ${mysql.version} + + + org.hsqldb + hsqldb + ${hsqldb.version} + + + javax.servlet.jsp.jstl + jstl-api + ${jstl.version} + + + org.glassfish.web + jstl-impl + ${jstl.version} + + + javax.el + el-api + ${javax-el.version} + provided + + + javax.servlet.jsp + jsp-api + ${jsp.version} + provided + + + + org.webjars + bootstrap + ${webjars-bootstrap.version} + + + org.webjars + jquery-ui + ${webjars-jquery-ui.version} + + + org.webjars + jquery + ${webjars-jquery.version} + + + org.jboss.logging + jboss-logging + ${jboss-logging.version} + + + org.jdom + jdom + ${jdom.version} + runtime + + + + junit + junit + ${junit.version} + test + + + org.hamcrest + hamcrest-library + ${hamcrest.version} + test + + + org.hamcrest + hamcrest-core + ${hamcrest.version} + test + + + - - - junit - junit - ${junit.version} - test - - - org.springframework - spring-test - ${spring.version} - test - - - javax.transaction - jta - 1.1 - test - - - - net.sf.ehcache - ehcache - 2.5.2 - pom - - - - joda-time - joda-time - ${joda.version} - - - - org.jadira.usertype - usertype.core - 3.0.0.CR1 - - - joda-time - joda-time-jsptags - 1.1 - - - - - org.hamcrest - hamcrest-all - 1.3 - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - true - 1.6 - 1.6 - true - - - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*Tests.java - - - - - org.apache.maven.plugins - maven-war-plugin - - petclinic - - - - org.apache.maven.plugins - maven-eclipse-plugin - 2.8 - - true - true - 2.0 - - **/*.* - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - install - install - - sources - - - - - - \ No newline at end of file diff --git a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java index 9770ef7be..8dfbfe90b 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java @@ -21,28 +21,28 @@ import javax.persistence.Id; import javax.persistence.MappedSuperclass; /** - * Simple JavaBean domain object with an id property. - * Used as a base class for objects needing this property. + * Simple JavaBean domain object with an id property. Used as a base class for objects needing this property. * * @author Ken Krebs * @author Juergen Hoeller */ @MappedSuperclass public class BaseEntity { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - protected Integer id; - + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + protected Integer id; - public void setId(Integer id) { - this.id = id; - } - public Integer getId() { - return id; - } + public void setId(Integer id) { + this.id = id; + } - public boolean isNew() { - return (this.id == null); - } + public Integer getId() { + return id; + } + + public boolean isNew() { + return (this.id == null); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java index a5064eb93..cb36a6267 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java +++ b/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java @@ -20,8 +20,8 @@ import javax.persistence.MappedSuperclass; /** - * Simple JavaBean domain object adds a name property to BaseEntity. - * Used as a base class for objects needing these properties. + * Simple JavaBean domain object adds a name property to BaseEntity. Used as a base class for objects + * needing these properties. * * @author Ken Krebs * @author Juergen Hoeller @@ -29,21 +29,21 @@ import javax.persistence.MappedSuperclass; @MappedSuperclass public class NamedEntity extends BaseEntity { - @Column(name="name") - private String name; - + @Column(name = "name") + private String name; - public void setName(String name) { - this.name = name; - } - public String getName() { - return this.name; - } + public void setName(String name) { + this.name = name; + } - @Override - public String toString() { - return this.getName(); - } + public String getName() { + return this.name; + } + + @Override + public String toString() { + return this.getName(); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Owner.java b/src/main/java/org/springframework/samples/petclinic/model/Owner.java index 7b4d90b3f..c0efd4670 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Owner.java @@ -15,24 +15,15 @@ */ package org.springframework.samples.petclinic.model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.OneToMany; -import javax.persistence.Table; -import javax.validation.constraints.Digits; - import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.core.style.ToStringCreator; +import javax.persistence.*; +import javax.validation.constraints.Digits; +import java.util.*; + /** * Simple JavaBean domain object representing an owner. * @@ -41,111 +32,113 @@ import org.springframework.core.style.ToStringCreator; * @author Sam Brannen * @author Michael Isvy */ -@Entity @Table(name="owners") +@Entity +@Table(name = "owners") public class Owner extends Person { - @Column(name="address") - @NotEmpty - private String address; - - @Column(name="city") - @NotEmpty - private String city; + @Column(name = "address") + @NotEmpty + private String address; - @Column(name="telephone") - @NotEmpty @Digits(fraction = 0, integer = 10) - private String telephone; + @Column(name = "city") + @NotEmpty + private String city; - @OneToMany(cascade=CascadeType.ALL, mappedBy="owner") - private Set pets; + @Column(name = "telephone") + @NotEmpty + @Digits(fraction = 0, integer = 10) + private String telephone; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") + private Set pets; - public String getAddress() { - return this.address; - } + public String getAddress() { + return this.address; + } - public void setAddress(String address) { - this.address = address; - } + public void setAddress(String address) { + this.address = address; + } - public String getCity() { - return this.city; - } + public String getCity() { + return this.city; + } - public void setCity(String city) { - this.city = city; - } + public void setCity(String city) { + this.city = city; + } - public String getTelephone() { - return this.telephone; - } + public String getTelephone() { + return this.telephone; + } - public void setTelephone(String telephone) { - this.telephone = telephone; - } + public void setTelephone(String telephone) { + this.telephone = telephone; + } - protected void setPetsInternal(Set pets) { - this.pets = pets; - } + protected void setPetsInternal(Set pets) { + this.pets = pets; + } - protected Set getPetsInternal() { - if (this.pets == null) { - this.pets = new HashSet(); - } - return this.pets; - } + protected Set getPetsInternal() { + if (this.pets == null) { + this.pets = new HashSet(); + } + return this.pets; + } - public List getPets() { - List sortedPets = new ArrayList(getPetsInternal()); - PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); - return Collections.unmodifiableList(sortedPets); - } + public List getPets() { + List sortedPets = new ArrayList(getPetsInternal()); + PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); + return Collections.unmodifiableList(sortedPets); + } - public void addPet(Pet pet) { - getPetsInternal().add(pet); - pet.setOwner(this); - } + public void addPet(Pet pet) { + getPetsInternal().add(pet); + pet.setOwner(this); + } - /** - * 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 Pet getPet(String name) { - return getPet(name, false); - } + /** + * 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 Pet getPet(String name) { + return getPet(name, false); + } - /** - * 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 Pet getPet(String name, boolean ignoreNew) { - name = name.toLowerCase(); - for (Pet pet : getPetsInternal()) { - if (!ignoreNew || !pet.isNew()) { - String compName = pet.getName(); - compName = compName.toLowerCase(); - if (compName.equals(name)) { - return pet; - } - } - } - return null; - } + /** + * 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 Pet getPet(String name, boolean ignoreNew) { + name = name.toLowerCase(); + for (Pet pet : getPetsInternal()) { + if (!ignoreNew || !pet.isNew()) { + String compName = pet.getName(); + compName = compName.toLowerCase(); + if (compName.equals(name)) { + return pet; + } + } + } + return null; + } - @Override - public String toString() { - return new ToStringCreator(this) + @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(); - } + .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(); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Person.java b/src/main/java/org/springframework/samples/petclinic/model/Person.java index 97f1edbc5..5e8655d92 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Person.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Person.java @@ -15,11 +15,11 @@ */ package org.springframework.samples.petclinic.model; +import org.hibernate.validator.constraints.NotEmpty; + import javax.persistence.Column; import javax.persistence.MappedSuperclass; -import org.hibernate.validator.constraints.NotEmpty; - /** * Simple JavaBean domain object representing an person. * @@ -27,31 +27,30 @@ import org.hibernate.validator.constraints.NotEmpty; */ @MappedSuperclass public class Person extends BaseEntity { - - @Column(name="first_name") - @NotEmpty - protected String firstName; - @Column(name="last_name") - @NotEmpty - protected String lastName; + @Column(name = "first_name") + @NotEmpty + protected String firstName; - public String getFirstName() { - return this.firstName; - } + @Column(name = "last_name") + @NotEmpty + protected String lastName; - public void setFirstName(String firstName) { - this.firstName = firstName; - } + public String getFirstName() { + return this.firstName; + } - public String getLastName() { - return this.lastName; - } + public void setFirstName(String firstName) { + this.firstName = firstName; + } - public void setLastName(String lastName) { - this.lastName = lastName; - } + public String getLastName() { + return this.lastName; + } + public void setLastName(String lastName) { + this.lastName = lastName; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Pet.java b/src/main/java/org/springframework/samples/petclinic/model/Pet.java index f5f279a03..5b3c806f9 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -15,27 +15,15 @@ */ package org.springframework.samples.petclinic.model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; -import javax.persistence.Table; - import org.hibernate.annotations.Type; import org.joda.time.DateTime; import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; import org.springframework.format.annotation.DateTimeFormat; +import javax.persistence.*; +import java.util.*; + /** * Simple business object representing a pet. * @@ -43,70 +31,71 @@ import org.springframework.format.annotation.DateTimeFormat; * @author Juergen Hoeller * @author Sam Brannen */ -@Entity @Table(name="pets") +@Entity +@Table(name = "pets") public class Pet extends NamedEntity { - @Column(name="birth_date") - @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") - @DateTimeFormat(pattern="yyyy/MM/dd") - private DateTime birthDate; + @Column(name = "birth_date") + @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") + @DateTimeFormat(pattern = "yyyy/MM/dd") + private DateTime birthDate; - @ManyToOne + @ManyToOne @JoinColumn(name = "type_id") - private PetType type; - - @ManyToOne + private PetType type; + + @ManyToOne @JoinColumn(name = "owner_id") - private Owner owner; - - @OneToMany(cascade=CascadeType.ALL, mappedBy="pet", fetch=FetchType.EAGER) - private Set visits; + private Owner owner; + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER) + private Set visits; - public void setBirthDate(DateTime birthDate) { - this.birthDate = birthDate; - } + public void setBirthDate(DateTime birthDate) { + this.birthDate = birthDate; + } - public DateTime getBirthDate() { - return this.birthDate; - } + public DateTime getBirthDate() { + return this.birthDate; + } - public void setType(PetType type) { - this.type = type; - } + public void setType(PetType type) { + this.type = type; + } - public PetType getType() { - return this.type; - } + public PetType getType() { + return this.type; + } - protected void setOwner(Owner owner) { - this.owner = owner; - } + protected void setOwner(Owner owner) { + this.owner = owner; + } - public Owner getOwner() { - return this.owner; - } + public Owner getOwner() { + return this.owner; + } - protected void setVisitsInternal(Set visits) { - this.visits = visits; - } + protected void setVisitsInternal(Set visits) { + this.visits = visits; + } - protected Set getVisitsInternal() { - if (this.visits == null) { - this.visits = new HashSet(); - } - return this.visits; - } + protected Set getVisitsInternal() { + if (this.visits == null) { + this.visits = new HashSet(); + } + return this.visits; + } - public List getVisits() { - List sortedVisits = new ArrayList(getVisitsInternal()); - PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false)); - return Collections.unmodifiableList(sortedVisits); - } + public List getVisits() { + List sortedVisits = new ArrayList(getVisitsInternal()); + PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false)); + return Collections.unmodifiableList(sortedVisits); + } - public void addVisit(Visit visit) { - getVisitsInternal().add(visit); - visit.setPet(this); - } + public void addVisit(Visit visit) { + getVisitsInternal().add(visit); + visit.setPet(this); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/PetType.java b/src/main/java/org/springframework/samples/petclinic/model/PetType.java index 8b3f1c0f6..50cf0399a 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/PetType.java +++ b/src/main/java/org/springframework/samples/petclinic/model/PetType.java @@ -21,7 +21,8 @@ import javax.persistence.Table; /** * @author Juergen Hoeller */ -@Entity @Table(name="types") +@Entity +@Table(name = "types") public class PetType extends NamedEntity { } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Specialty.java b/src/main/java/org/springframework/samples/petclinic/model/Specialty.java index 35724a8e3..6ea209cd4 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Specialty.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Specialty.java @@ -20,10 +20,11 @@ import javax.persistence.Table; /** * Models a {@link Vet Vet's} specialty (for example, dentistry). - * + * * @author Juergen Hoeller */ -@Entity @Table(name="specialties") +@Entity +@Table(name = "specialties") public class Specialty extends NamedEntity { } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Vet.java b/src/main/java/org/springframework/samples/petclinic/model/Vet.java index 328b8c305..4d767e0f3 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Vet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Vet.java @@ -15,23 +15,13 @@ */ package org.springframework.samples.petclinic.model; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; -import javax.persistence.Table; -import javax.xml.bind.annotation.XmlElement; - import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.PropertyComparator; +import javax.persistence.*; +import javax.xml.bind.annotation.XmlElement; +import java.util.*; + /** * Simple JavaBean domain object representing a veterinarian. * @@ -40,39 +30,40 @@ import org.springframework.beans.support.PropertyComparator; * @author Sam Brannen * @author Arjen Poutsma */ -@Entity @Table(name="vets") +@Entity +@Table(name = "vets") public class Vet extends Person { - @ManyToMany(fetch=FetchType.EAGER) - @JoinTable (name="vet_specialties",joinColumns = @JoinColumn(name = "vet_id"), - inverseJoinColumns= @JoinColumn(name = "specialty_id")) - private Set specialties; + @ManyToMany(fetch = FetchType.EAGER) + @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), + inverseJoinColumns = @JoinColumn(name = "specialty_id")) + private Set specialties; - protected void setSpecialtiesInternal(Set specialties) { - this.specialties = specialties; - } + protected void setSpecialtiesInternal(Set specialties) { + this.specialties = specialties; + } - protected Set getSpecialtiesInternal() { - if (this.specialties == null) { - this.specialties = new HashSet(); - } - return this.specialties; - } + protected Set getSpecialtiesInternal() { + if (this.specialties == null) { + this.specialties = new HashSet(); + } + return this.specialties; + } - @XmlElement - public List getSpecialties() { - List sortedSpecs = new ArrayList(getSpecialtiesInternal()); - PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true)); - return Collections.unmodifiableList(sortedSpecs); - } + @XmlElement + public List getSpecialties() { + List sortedSpecs = new ArrayList(getSpecialtiesInternal()); + PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true)); + return Collections.unmodifiableList(sortedSpecs); + } - public int getNrOfSpecialties() { - return getSpecialtiesInternal().size(); - } + public int getNrOfSpecialties() { + return getSpecialtiesInternal().size(); + } - public void addSpecialty(Specialty specialty) { - getSpecialtiesInternal().add(specialty); - } + public void addSpecialty(Specialty specialty) { + getSpecialtiesInternal().add(specialty); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Vets.java b/src/main/java/org/springframework/samples/petclinic/model/Vets.java index 6882eabe7..48676e9a6 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Vets.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Vets.java @@ -16,29 +16,28 @@ */ package org.springframework.samples.petclinic.model; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - /** - * Simple domain object representing a list of veterinarians. Mostly here to be used for the 'vets' - * {@link org.springframework.web.servlet.view.xml.MarshallingView}. + * Simple domain object representing a list of veterinarians. Mostly here to be used for the 'vets' {@link + * org.springframework.web.servlet.view.xml.MarshallingView}. * * @author Arjen Poutsma */ @XmlRootElement public class Vets { - private List vets; + private List vets; - @XmlElement - public List getVetList() { - if (vets == null) { - vets = new ArrayList(); - } - return vets; - } + @XmlElement + public List getVetList() { + if (vets == null) { + vets = new ArrayList(); + } + return vets; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/model/Visit.java b/src/main/java/org/springframework/samples/petclinic/model/Visit.java index 704021d20..9ea0897f8 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Visit.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Visit.java @@ -15,88 +15,105 @@ */ package org.springframework.samples.petclinic.model; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; - import org.hibernate.annotations.Type; import org.hibernate.validator.constraints.NotEmpty; import org.joda.time.DateTime; import org.springframework.format.annotation.DateTimeFormat; +import javax.persistence.*; + /** * Simple JavaBean domain object representing a visit. * * @author Ken Krebs */ -@Entity @Table(name="visits") +@Entity +@Table(name = "visits") public class Visit extends BaseEntity { - /** Holds value of property date. */ - @Column(name="visit_date") - @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") - @DateTimeFormat(pattern="yyyy/MM/dd") - private DateTime date; + /** + * Holds value of property date. + */ + @Column(name = "visit_date") + @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime") + @DateTimeFormat(pattern = "yyyy/MM/dd") + private DateTime date; - /** Holds value of property description. */ - @NotEmpty - @Column(name="description") - private String description; + /** + * Holds value of property description. + */ + @NotEmpty + @Column(name = "description") + private String description; - /** Holds value of property pet. */ - @ManyToOne + /** + * Holds value of property pet. + */ + @ManyToOne @JoinColumn(name = "pet_id") - private Pet pet; + private Pet pet; - /** Creates a new instance of Visit for the current date */ - public Visit() { - this.date = new DateTime(); - } + /** + * Creates a new instance of Visit for the current date + */ + public Visit() { + this.date = new DateTime(); + } - /** Getter for property date. - * @return Value of property date. - */ - public DateTime getDate() { - return this.date; - } + /** + * Getter for property date. + * + * @return Value of property date. + */ + public DateTime getDate() { + return this.date; + } - /** Setter for property date. - * @param date New value of property date. - */ - public void setDate(DateTime date) { - this.date = date; - } + /** + * Setter for property date. + * + * @param date New value of property date. + */ + public void setDate(DateTime date) { + this.date = date; + } - /** Getter for property description. - * @return Value of property description. - */ - public String getDescription() { - return this.description; - } + /** + * Getter for property description. + * + * @return Value of property description. + */ + public String getDescription() { + return this.description; + } - /** Setter for property description. - * @param description New value of property description. - */ - public void setDescription(String description) { - this.description = description; - } + /** + * Setter for property description. + * + * @param description New value of property description. + */ + public void setDescription(String description) { + this.description = description; + } - /** Getter for property pet. - * @return Value of property pet. - */ - public Pet getPet() { - return this.pet; - } + /** + * Getter for property pet. + * + * @return Value of property pet. + */ + public Pet getPet() { + return this.pet; + } - /** Setter for property pet. - * @param pet New value of property pet. - */ - public void setPet(Pet pet) { - this.pet = pet; - } + /** + * Setter for property pet. + * + * @param pet New value of property pet. + */ + public void setPet(Pet pet) { + this.pet = pet; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java index 2046abe0e..e87aa5fbd 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/OwnerRepository.java @@ -30,49 +30,51 @@ */ package org.springframework.samples.petclinic.repository; -import java.util.Collection; - import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Owner; +import java.util.Collection; + /** - * Repository class for Owner domain objects - * All method names are compliant with Spring Data naming conventions so this interface can easily be - * extended for Spring Data - * See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation - * + * Repository class for Owner domain objects All method names are compliant with Spring Data naming + * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * * @author Ken Krebs * @author Juergen Hoeller * @author Sam Brannen * @author Michael Isvy - */ + */ public interface OwnerRepository { - /** - * Retrieve Owners from the data store by last name, - * returning all owners whose last name starts with the given name. - * @param lastName Value to search for - * @return a Collection of matching Owners - * (or an empty Collection if none found) - */ - Collection findByLastName(String lastName) throws DataAccessException; + /** + * Retrieve Owners from the data store by last name, returning all owners whose last name starts + * with the given name. + * + * @param lastName Value to search for + * @return a Collection of matching Owners (or an empty Collection if none + * found) + */ + Collection findByLastName(String lastName) throws DataAccessException; - /** - * Retrieve an Owner from the data store by id. - * @param id the id to search for - * @return the Owner if found - * @throws org.springframework.dao.DataRetrievalFailureException if not found - */ - Owner findById(int id) throws DataAccessException; + /** + * Retrieve an Owner from the data store by id. + * + * @param id the id to search for + * @return the Owner if found + * @throws org.springframework.dao.DataRetrievalFailureException + * if not found + */ + Owner findById(int id) throws DataAccessException; - /** - * Save an Owner to the data store, either inserting or updating it. - * @param owner the Owner to save - * @see BaseEntity#isNew - */ - void save(Owner owner) throws DataAccessException; + /** + * Save an Owner to the data store, either inserting or updating it. + * + * @param owner the Owner to save + * @see BaseEntity#isNew + */ + void save(Owner owner) throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java index 4cbd48d86..a075bc00a 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -15,18 +15,16 @@ */ package org.springframework.samples.petclinic.repository; -import java.util.List; - import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; +import java.util.List; + /** - * Repository class for Pet domain objects - * All method names are compliant with Spring Data naming conventions so this interface can easily be - * extended for Spring Data - * See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * Repository class for Pet domain objects All method names are compliant with Spring Data naming + * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation * * @author Ken Krebs * @author Juergen Hoeller @@ -35,25 +33,29 @@ import org.springframework.samples.petclinic.model.PetType; */ public interface PetRepository { - /** - * Retrieve all PetTypes from the data store. - * @return a Collection of PetTypes - */ - List findPetTypes() throws DataAccessException; + /** + * Retrieve all PetTypes from the data store. + * + * @return a Collection of PetTypes + */ + List findPetTypes() throws DataAccessException; - /** - * Retrieve a Pet from the data store by id. - * @param id the id to search for - * @return the Pet if found - * @throws org.springframework.dao.DataRetrievalFailureException if not found - */ - Pet findById(int id) throws DataAccessException; + /** + * Retrieve a Pet from the data store by id. + * + * @param id the id to search for + * @return the Pet if found + * @throws org.springframework.dao.DataRetrievalFailureException + * if not found + */ + Pet findById(int id) throws DataAccessException; - /** - * Save a Pet to the data store, either inserting or updating it. - * @param pet the Pet to save - * @see BaseEntity#isNew - */ - void save(Pet pet) throws DataAccessException; + /** + * Save a Pet to the data store, either inserting or updating it. + * + * @param pet the Pet to save + * @see BaseEntity#isNew + */ + void save(Pet pet) throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java index 183192e54..44f504d3a 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VetRepository.java @@ -15,16 +15,14 @@ */ package org.springframework.samples.petclinic.repository; -import java.util.Collection; - import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.Vet; +import java.util.Collection; + /** - * Repository class for Vet domain objects - * All method names are compliant with Spring Data naming conventions so this interface can easily be - * extended for Spring Data - * See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * Repository class for Vet domain objects All method names are compliant with Spring Data naming + * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation * * @author Ken Krebs * @author Juergen Hoeller @@ -33,11 +31,12 @@ import org.springframework.samples.petclinic.model.Vet; */ public interface VetRepository { - /** - * Retrieve all Vets from the data store. - * @return a Collection of Vets - */ - Collection findAll() throws DataAccessException; + /** + * Retrieve all Vets from the data store. + * + * @return a Collection of Vets + */ + Collection findAll() throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java index a78b10bc0..760f2001d 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/VisitRepository.java @@ -15,17 +15,15 @@ */ package org.springframework.samples.petclinic.repository; -import java.util.List; - import org.springframework.dao.DataAccessException; import org.springframework.samples.petclinic.model.BaseEntity; import org.springframework.samples.petclinic.model.Visit; +import java.util.List; + /** - * Repository class for Visit domain objects - * All method names are compliant with Spring Data naming conventions so this interface can easily be - * extended for Spring Data - * See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation + * Repository class for Visit domain objects All method names are compliant with Spring Data naming + * conventions so this interface can easily be extended for Spring Data See here: http://static.springsource.org/spring-data/jpa/docs/current/reference/html/jpa.repositories.html#jpa.query-methods.query-creation * * @author Ken Krebs * @author Juergen Hoeller @@ -34,13 +32,14 @@ import org.springframework.samples.petclinic.model.Visit; */ public interface VisitRepository { - /** - * Save a Visit to the data store, either inserting or updating it. - * @param visit the Visit to save - * @see BaseEntity#isNew - */ - void save(Visit visit) throws DataAccessException; + /** + * Save a Visit to the data store, either inserting or updating it. + * + * @param visit the Visit to save + * @see BaseEntity#isNew + */ + void save(Visit visit) throws DataAccessException; - List findByPetId(Integer petId); + List findByPetId(Integer petId); } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java index d186bb949..646378d6c 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java @@ -15,13 +15,6 @@ */ package org.springframework.samples.petclinic.repository.jdbc; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; @@ -39,10 +32,15 @@ import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.stereotype.Repository; +import javax.sql.DataSource; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * A simple JDBC-based implementation of the {@link OwnerRepository} interface. * - * * @author Ken Krebs * @author Juergen Hoeller * @author Rob Harrop @@ -53,119 +51,115 @@ import org.springframework.stereotype.Repository; @Repository public class JdbcOwnerRepositoryImpl implements OwnerRepository { - private VisitRepository visitRepository; - - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + private VisitRepository visitRepository; - private SimpleJdbcInsert insertOwner; + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; - @Autowired - public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate, - VisitRepository visitRepository) { + private SimpleJdbcInsert insertOwner; - this.insertOwner = new SimpleJdbcInsert(dataSource) - .withTableName("owners") - .usingGeneratedKeyColumns("id"); - - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); + @Autowired + public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate, + VisitRepository visitRepository) { - this.visitRepository = visitRepository; - } + this.insertOwner = new SimpleJdbcInsert(dataSource) + .withTableName("owners") + .usingGeneratedKeyColumns("id"); + + this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); + + this.visitRepository = visitRepository; + } - + /** + * Loads {@link Owner Owners} from the data store by last name, returning all owners whose last name starts with + * the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not + * already loaded. + */ + @Override + public Collection findByLastName(String lastName) throws DataAccessException { + Map params = new HashMap(); + params.put("lastName", lastName + "%"); + List owners = this.namedParameterJdbcTemplate.query( + "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName", + params, + ParameterizedBeanPropertyRowMapper.newInstance(Owner.class) + ); + loadOwnersPetsAndVisits(owners); + return owners; + } - /** - * Loads {@link Owner Owners} from the data store by last name, returning - * all owners whose last name starts with the given name; also loads - * the {@link Pet Pets} and {@link Visit Visits} for the corresponding - * owners, if not already loaded. - */ - public Collection findByLastName(String lastName) throws DataAccessException { - Map params = new HashMap(); - params.put("lastName", lastName+"%"); - List owners = this.namedParameterJdbcTemplate.query( - "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName", - params, - ParameterizedBeanPropertyRowMapper.newInstance(Owner.class) - ); - loadOwnersPetsAndVisits(owners); - return owners; - } + /** + * Loads the {@link Owner} with the supplied id; also loads the {@link Pet Pets} and {@link Visit Visits} + * for the corresponding owner, if not already loaded. + */ + @Override + public Owner findById(int id) throws DataAccessException { + Owner owner; + try { + Map params = new HashMap(); + params.put("id", id); + owner = this.namedParameterJdbcTemplate.queryForObject( + "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id", + params, + ParameterizedBeanPropertyRowMapper.newInstance(Owner.class) + ); + } catch (EmptyResultDataAccessException ex) { + throw new ObjectRetrievalFailureException(Owner.class, new Integer(id)); + } + loadPetsAndVisits(owner); + return owner; + } - /** - * Loads the {@link Owner} with the supplied id; also loads - * the {@link Pet Pets} and {@link Visit Visits} for the corresponding - * owner, if not already loaded. - */ - public Owner findById(int id) throws DataAccessException { - Owner owner; - try { - Map params = new HashMap(); - params.put("id", id); - owner = this.namedParameterJdbcTemplate.queryForObject( - "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id", - params, - ParameterizedBeanPropertyRowMapper.newInstance(Owner.class) - ); - } - catch (EmptyResultDataAccessException ex) { - throw new ObjectRetrievalFailureException(Owner.class, new Integer(id)); - } - loadPetsAndVisits(owner); - return owner; - } - - public void loadPetsAndVisits(final Owner owner) { - Map params = new HashMap(); - params.put("id", owner.getId().intValue()); - final List pets = this.namedParameterJdbcTemplate.query( - "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id", - params, - new JdbcPetRowMapper() - ); - for (JdbcPet pet : pets) { - owner.addPet(pet); - pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId())); - List visits = this.visitRepository.findByPetId(pet.getId()); - for (Visit visit : visits) { - pet.addVisit(visit); - } - } - } + public void loadPetsAndVisits(final Owner owner) { + Map params = new HashMap(); + params.put("id", owner.getId().intValue()); + final List pets = this.namedParameterJdbcTemplate.query( + "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id", + params, + new JdbcPetRowMapper() + ); + for (JdbcPet pet : pets) { + owner.addPet(pet); + pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId())); + List visits = this.visitRepository.findByPetId(pet.getId()); + for (Visit visit : visits) { + pet.addVisit(visit); + } + } + } - public void save(Owner owner) throws DataAccessException { - BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner); - if (owner.isNew()) { - Number newKey = this.insertOwner.executeAndReturnKey(parameterSource); - owner.setId(newKey.intValue()); - } - else { - this.namedParameterJdbcTemplate.update( - "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " + - "city=:city, telephone=:telephone WHERE id=:id", - parameterSource); - } - } + @Override + public void save(Owner owner) throws DataAccessException { + BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner); + if (owner.isNew()) { + Number newKey = this.insertOwner.executeAndReturnKey(parameterSource); + owner.setId(newKey.intValue()); + } else { + this.namedParameterJdbcTemplate.update( + "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " + + "city=:city, telephone=:telephone WHERE id=:id", + parameterSource); + } + } - public Collection getPetTypes() throws DataAccessException { - return this.namedParameterJdbcTemplate.query( - "SELECT id, name FROM types ORDER BY name", new HashMap(), - ParameterizedBeanPropertyRowMapper.newInstance(PetType.class)); - } + public Collection getPetTypes() throws DataAccessException { + return this.namedParameterJdbcTemplate.query( + "SELECT id, name FROM types ORDER BY name", new HashMap(), + ParameterizedBeanPropertyRowMapper.newInstance(PetType.class)); + } - /** - * Loads the {@link Pet} and {@link Visit} data for the supplied - * {@link List} of {@link Owner Owners}. - * - * @param owners the list of owners for whom the pet and visit data should be loaded - * @see #loadPetsAndVisits(Owner) - */ - private void loadOwnersPetsAndVisits(List owners) { - for (Owner owner : owners) { - loadPetsAndVisits(owner); - } - } + /** + * Loads the {@link Pet} and {@link Visit} data for the supplied {@link List} of {@link Owner Owners}. + * + * @param owners the list of owners for whom the pet and visit data should be loaded + * @see #loadPetsAndVisits(Owner) + */ + private void loadOwnersPetsAndVisits(List owners) { + for (Owner owner : owners) { + loadPetsAndVisits(owner); + } + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java index 25d93a3ca..b548629c7 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPet.java @@ -18,33 +18,33 @@ package org.springframework.samples.petclinic.repository.jdbc; import org.springframework.samples.petclinic.model.Pet; /** - * Subclass of Pet that carries temporary id properties which - * are only relevant for a JDBC implmentation of the ClinicService. + * Subclass of Pet that carries temporary id properties which are only relevant for a JDBC implmentation of the + * ClinicService. * * @author Juergen Hoeller * @see JdbcClinicImpl */ class JdbcPet extends Pet { - private int typeId; + private int typeId; - private int ownerId; + private int ownerId; - public void setTypeId(int typeId) { - this.typeId = typeId; - } + public void setTypeId(int typeId) { + this.typeId = typeId; + } - public int getTypeId() { - return this.typeId; - } + public int getTypeId() { + return this.typeId; + } - public void setOwnerId(int ownerId) { - this.ownerId = ownerId; - } + public void setOwnerId(int ownerId) { + this.ownerId = ownerId; + } - public int getOwnerId() { - return this.ownerId; - } + public int getOwnerId() { + return this.ownerId; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java index 693a77ea2..f11b5b05b 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java @@ -15,12 +15,6 @@ */ package org.springframework.samples.petclinic.repository.jdbc; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.sql.DataSource; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; @@ -39,8 +33,12 @@ import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.stereotype.Repository; +import javax.sql.DataSource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** - * * @author Ken Krebs * @author Juergen Hoeller * @author Rob Harrop @@ -51,84 +49,84 @@ import org.springframework.stereotype.Repository; @Repository public class JdbcPetRepositoryImpl implements PetRepository { - private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; - private SimpleJdbcInsert insertPet; - - private OwnerRepository ownerRepository; - - private VisitRepository visitRepository; - + private SimpleJdbcInsert insertPet; - @Autowired - public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) { - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); + private OwnerRepository ownerRepository; - this.insertPet = new SimpleJdbcInsert(dataSource) - .withTableName("pets") - .usingGeneratedKeyColumns("id"); - - this.ownerRepository = ownerRepository; - this.visitRepository = visitRepository; - } + private VisitRepository visitRepository; - public List findPetTypes() throws DataAccessException { - Map params = new HashMap(); - return this.namedParameterJdbcTemplate.query( - "SELECT id, name FROM types ORDER BY name", - params, - ParameterizedBeanPropertyRowMapper.newInstance(PetType.class)); - } - public Pet findById(int id) throws DataAccessException { - JdbcPet pet; - try { - Map params = new HashMap(); - params.put("id", id); - pet = this.namedParameterJdbcTemplate.queryForObject( - "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id", - params, - new JdbcPetRowMapper()); - } - catch (EmptyResultDataAccessException ex) { - throw new ObjectRetrievalFailureException(Pet.class, new Integer(id)); - } - Owner owner = this.ownerRepository.findById(pet.getOwnerId()); - owner.addPet(pet); - pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId())); - - List visits = this.visitRepository.findByPetId(pet.getId()); - for (Visit visit : visits) { - pet.addVisit(visit); - } - return pet; - } + @Autowired + public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) { + this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); - public void save(Pet pet) throws DataAccessException { - if (pet.isNew()) { - Number newKey = this.insertPet.executeAndReturnKey( - createPetParameterSource(pet)); - pet.setId(newKey.intValue()); - } - else { - this.namedParameterJdbcTemplate.update( - "UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " + - "owner_id=:owner_id WHERE id=:id", - createPetParameterSource(pet)); - } - } + this.insertPet = new SimpleJdbcInsert(dataSource) + .withTableName("pets") + .usingGeneratedKeyColumns("id"); - /** - * Creates a {@link MapSqlParameterSource} based on data values from the - * supplied {@link Pet} instance. - */ - private MapSqlParameterSource createPetParameterSource(Pet pet) { - return new MapSqlParameterSource() - .addValue("id", pet.getId()) - .addValue("name", pet.getName()) - .addValue("birth_date", pet.getBirthDate().toDate()) - .addValue("type_id", pet.getType().getId()) - .addValue("owner_id", pet.getOwner().getId()); - } + this.ownerRepository = ownerRepository; + this.visitRepository = visitRepository; + } + + @Override + public List findPetTypes() throws DataAccessException { + Map params = new HashMap(); + return this.namedParameterJdbcTemplate.query( + "SELECT id, name FROM types ORDER BY name", + params, + ParameterizedBeanPropertyRowMapper.newInstance(PetType.class)); + } + + @Override + public Pet findById(int id) throws DataAccessException { + JdbcPet pet; + try { + Map params = new HashMap(); + params.put("id", id); + pet = this.namedParameterJdbcTemplate.queryForObject( + "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id", + params, + new JdbcPetRowMapper()); + } catch (EmptyResultDataAccessException ex) { + throw new ObjectRetrievalFailureException(Pet.class, new Integer(id)); + } + Owner owner = this.ownerRepository.findById(pet.getOwnerId()); + owner.addPet(pet); + pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId())); + + List visits = this.visitRepository.findByPetId(pet.getId()); + for (Visit visit : visits) { + pet.addVisit(visit); + } + return pet; + } + + @Override + public void save(Pet pet) throws DataAccessException { + if (pet.isNew()) { + Number newKey = this.insertPet.executeAndReturnKey( + createPetParameterSource(pet)); + pet.setId(newKey.intValue()); + } else { + this.namedParameterJdbcTemplate.update( + "UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " + + "owner_id=:owner_id WHERE id=:id", + createPetParameterSource(pet)); + } + } + + /** + * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} instance. + */ + private MapSqlParameterSource createPetParameterSource(Pet pet) { + return new MapSqlParameterSource() + .addValue("id", pet.getId()) + .addValue("name", pet.getName()) + .addValue("birth_date", pet.getBirthDate().toDate()) + .addValue("type_id", pet.getType().getId()) + .addValue("owner_id", pet.getOwner().getId()); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java index f76c639cd..0193a8653 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRowMapper.java @@ -15,27 +15,28 @@ */ package org.springframework.samples.petclinic.repository.jdbc; +import org.joda.time.DateTime; +import org.springframework.jdbc.core.simple.ParameterizedRowMapper; + import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; -import org.joda.time.DateTime; -import org.springframework.jdbc.core.simple.ParameterizedRowMapper; - /** - * {@link ParameterizedRowMapper} implementation mapping data from a - * {@link ResultSet} to the corresponding properties of the {@link JdbcPet} class. + * {@link ParameterizedRowMapper} implementation mapping data from a {@link ResultSet} to the corresponding properties + * of the {@link JdbcPet} class. */ class JdbcPetRowMapper implements ParameterizedRowMapper { - public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException { - JdbcPet pet = new JdbcPet(); - pet.setId(rs.getInt("id")); - pet.setName(rs.getString("name")); - Date birthDate = rs.getDate("birth_date"); - pet.setBirthDate(new DateTime(birthDate)); - pet.setTypeId(rs.getInt("type_id")); - pet.setOwnerId(rs.getInt("owner_id")); - return pet; - } + @Override + public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException { + JdbcPet pet = new JdbcPet(); + pet.setId(rs.getInt("id")); + pet.setName(rs.getString("name")); + Date birthDate = rs.getDate("birth_date"); + pet.setBirthDate(new DateTime(birthDate)); + pet.setTypeId(rs.getInt("type_id")); + pet.setOwnerId(rs.getInt("owner_id")); + return pet; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java index 0783bd52a..a354c2a83 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVetRepositoryImpl.java @@ -15,12 +15,6 @@ */ package org.springframework.samples.petclinic.repository.jdbc; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.dao.DataAccessException; @@ -33,11 +27,16 @@ import org.springframework.samples.petclinic.repository.VetRepository; import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.stereotype.Repository; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + /** + * A simple JDBC-based implementation of the {@link VetRepository} interface. Uses @Cacheable to cache the result of the + * {@link findAll} method * - * A simple JDBC-based implementation of the {@link VetRepository} interface. - * Uses @Cacheable to cache the result of the {@link findAll} method - * * @author Ken Krebs * @author Juergen Hoeller * @author Rob Harrop @@ -49,44 +48,48 @@ import org.springframework.stereotype.Repository; @Repository public class JdbcVetRepositoryImpl implements VetRepository { - private JdbcTemplate jdbcTemplate; + private JdbcTemplate jdbcTemplate; - @Autowired - public JdbcVetRepositoryImpl(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } + @Autowired + public JdbcVetRepositoryImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } - /** - * Refresh the cache of Vets that the ClinicService is holding. - * @see org.springframework.samples.petclinic.model.service.ClinicService#findVets() - */ - @Cacheable(value="vets") - public Collection findAll() throws DataAccessException { - List vets = new ArrayList(); - // Retrieve the list of all vets. - vets.addAll(this.jdbcTemplate.query( - "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name", - ParameterizedBeanPropertyRowMapper.newInstance(Vet.class))); + /** + * Refresh the cache of Vets that the ClinicService is holding. + * + * @see org.springframework.samples.petclinic.model.service.ClinicService#findVets() + */ + @Override + @Cacheable(value = "vets") + public Collection findAll() throws DataAccessException { + List vets = new ArrayList(); + // Retrieve the list of all vets. + vets.addAll(this.jdbcTemplate.query( + "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name", + ParameterizedBeanPropertyRowMapper.newInstance(Vet.class))); - // Retrieve the list of all possible specialties. - final List specialties = this.jdbcTemplate.query( - "SELECT id, name FROM specialties", - ParameterizedBeanPropertyRowMapper.newInstance(Specialty.class)); + // Retrieve the list of all possible specialties. + final List specialties = this.jdbcTemplate.query( + "SELECT id, name FROM specialties", + ParameterizedBeanPropertyRowMapper.newInstance(Specialty.class)); - // Build each vet's list of specialties. - for (Vet vet : vets) { - final List vetSpecialtiesIds = this.jdbcTemplate.query( - "SELECT specialty_id FROM vet_specialties WHERE vet_id=?", - new ParameterizedRowMapper() { - public Integer mapRow(ResultSet rs, int row) throws SQLException { - return Integer.valueOf(rs.getInt(1)); - }}, - vet.getId().intValue()); - for (int specialtyId : vetSpecialtiesIds) { - Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId); - vet.addSpecialty(specialty); - } - } - return vets; - } + // Build each vet's list of specialties. + for (Vet vet : vets) { + final List vetSpecialtiesIds = this.jdbcTemplate.query( + "SELECT specialty_id FROM vet_specialties WHERE vet_id=?", + new ParameterizedRowMapper() { + @Override + public Integer mapRow(ResultSet rs, int row) throws SQLException { + return Integer.valueOf(rs.getInt(1)); + } + }, + vet.getId().intValue()); + for (int specialtyId : vetSpecialtiesIds) { + Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId); + vet.addSpecialty(specialty); + } + } + return vets; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java index e0f6eb422..e8081aaef 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java @@ -15,13 +15,6 @@ */ package org.springframework.samples.petclinic.repository.jdbc; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Date; -import java.util.List; - -import javax.sql.DataSource; - import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; @@ -33,10 +26,15 @@ import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Repository; +import javax.sql.DataSource; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Date; +import java.util.List; + /** * A simple JDBC-based implementation of the {@link VisitRepository} interface. * - * * @author Ken Krebs * @author Juergen Hoeller * @author Rob Harrop @@ -48,63 +46,64 @@ import org.springframework.stereotype.Repository; @Repository public class JdbcVisitRepositoryImpl implements VisitRepository { - private JdbcTemplate jdbcTemplate; + private JdbcTemplate jdbcTemplate; - private SimpleJdbcInsert insertVisit; + private SimpleJdbcInsert insertVisit; - @Autowired - public JdbcVisitRepositoryImpl(DataSource dataSource) { - this.jdbcTemplate = new JdbcTemplate(dataSource); + @Autowired + public JdbcVisitRepositoryImpl(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); - this.insertVisit = new SimpleJdbcInsert(dataSource) - .withTableName("visits") - .usingGeneratedKeyColumns("id"); - } + this.insertVisit = new SimpleJdbcInsert(dataSource) + .withTableName("visits") + .usingGeneratedKeyColumns("id"); + } - public void save(Visit visit) throws DataAccessException { - if (visit.isNew()) { - Number newKey = this.insertVisit.executeAndReturnKey( - createVisitParameterSource(visit)); - visit.setId(newKey.intValue()); - } - else { - throw new UnsupportedOperationException("Visit update not supported"); - } - } + @Override + public void save(Visit visit) throws DataAccessException { + if (visit.isNew()) { + Number newKey = this.insertVisit.executeAndReturnKey( + createVisitParameterSource(visit)); + visit.setId(newKey.intValue()); + } else { + throw new UnsupportedOperationException("Visit update not supported"); + } + } - public void deletePet(int id) throws DataAccessException { - this.jdbcTemplate.update("DELETE FROM pets WHERE id=?", id); - } + public void deletePet(int id) throws DataAccessException { + this.jdbcTemplate.update("DELETE FROM pets WHERE id=?", id); + } - /** - * Creates a {@link MapSqlParameterSource} based on data values from the - * supplied {@link Visit} instance. - */ - private MapSqlParameterSource createVisitParameterSource(Visit visit) { - return new MapSqlParameterSource() - .addValue("id", visit.getId()) - .addValue("visit_date", visit.getDate().toDate()) - .addValue("description", visit.getDescription()) - .addValue("pet_id", visit.getPet().getId()); - } + /** + * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Visit} instance. + */ + private MapSqlParameterSource createVisitParameterSource(Visit visit) { + return new MapSqlParameterSource() + .addValue("id", visit.getId()) + .addValue("visit_date", visit.getDate().toDate()) + .addValue("description", visit.getDescription()) + .addValue("pet_id", visit.getPet().getId()); + } - public List findByPetId(Integer petId) { - final List visits = this.jdbcTemplate.query( - "SELECT id, visit_date, description FROM visits WHERE pet_id=?", - new ParameterizedRowMapper() { - public Visit mapRow(ResultSet rs, int row) throws SQLException { - Visit visit = new Visit(); - visit.setId(rs.getInt("id")); - Date visitDate = rs.getDate("visit_date"); - visit.setDate(new DateTime(visitDate)); - visit.setDescription(rs.getString("description")); - return visit; - } - }, - petId); - return visits; - } + @Override + public List findByPetId(Integer petId) { + final List visits = this.jdbcTemplate.query( + "SELECT id, visit_date, description FROM visits WHERE pet_id=?", + new ParameterizedRowMapper() { + @Override + public Visit mapRow(ResultSet rs, int row) throws SQLException { + Visit visit = new Visit(); + visit.setId(rs.getInt("id")); + Date visitDate = rs.getDate("visit_date"); + visit.setDate(new DateTime(visitDate)); + visit.setDescription(rs.getString("description")); + return visit; + } + }, + petId); + return visits; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java index 898998056..3b85bfd8d 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaOwnerRepositoryImpl.java @@ -15,15 +15,14 @@ */ package org.springframework.samples.petclinic.repository.jpa; -import java.util.Collection; +import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.repository.OwnerRepository; +import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; - -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.repository.OwnerRepository; -import org.springframework.stereotype.Repository; +import java.util.Collection; /** * JPA implementation of the {@link OwnerRepository} interface. @@ -37,31 +36,34 @@ import org.springframework.stereotype.Repository; @Repository public class JpaOwnerRepositoryImpl implements OwnerRepository { - @PersistenceContext - private EntityManager em; - - - @SuppressWarnings("unchecked") - public Collection findByLastName(String lastName) { - // using 'join fetch' because a single query should load both owners and pets - // using 'left join fetch' because it might happen that an owner does not have pets yet - Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName"); - query.setParameter("lastName", lastName + "%"); - return query.getResultList(); - } - - public Owner findById(int id) { - // using 'join fetch' because a single query should load both owners and pets - // using 'left join fetch' because it might happen that an owner does not have pets yet - Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id"); - query.setParameter("id", id); - return (Owner) query.getSingleResult(); - } + @PersistenceContext + private EntityManager em; - public void save(Owner owner) { - this.em.merge(owner); + @Override + @SuppressWarnings("unchecked") + public Collection findByLastName(String lastName) { + // using 'join fetch' because a single query should load both owners and pets + // using 'left join fetch' because it might happen that an owner does not have pets yet + Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName"); + query.setParameter("lastName", lastName + "%"); + return query.getResultList(); + } - } + @Override + public Owner findById(int id) { + // using 'join fetch' because a single query should load both owners and pets + // using 'left join fetch' because it might happen that an owner does not have pets yet + Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id"); + query.setParameter("id", id); + return (Owner) query.getSingleResult(); + } + + + @Override + public void save(Owner owner) { + this.em.merge(owner); + + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java index c83fd6966..4073c9d86 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryImpl.java @@ -15,16 +15,15 @@ */ package org.springframework.samples.petclinic.repository.jpa; -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.repository.PetRepository; import org.springframework.stereotype.Repository; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + /** * JPA implementation of the {@link PetRepository} interface. * @@ -37,20 +36,23 @@ import org.springframework.stereotype.Repository; @Repository public class JpaPetRepositoryImpl implements PetRepository { - @PersistenceContext - private EntityManager em; + @PersistenceContext + private EntityManager em; - @SuppressWarnings("unchecked") - public List findPetTypes() { - return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList(); - } + @Override + @SuppressWarnings("unchecked") + public List findPetTypes() { + return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList(); + } - public Pet findById(int id) { - return this.em.find(Pet.class, id); - } + @Override + public Pet findById(int id) { + return this.em.find(Pet.class, id); + } - public void save(Pet pet) { - this.em.merge(pet); - } + @Override + public void save(Pet pet) { + this.em.merge(pet); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java index b32abc744..5ab58d3ec 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVetRepositoryImpl.java @@ -15,19 +15,18 @@ */ package org.springframework.samples.petclinic.repository.jpa; -import java.util.Collection; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - import org.springframework.cache.annotation.Cacheable; import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.repository.VetRepository; import org.springframework.stereotype.Repository; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.Collection; + /** -* JPA implementation of the {@link VetRepository} interface. - * + * JPA implementation of the {@link VetRepository} interface. + * * @author Mike Keith * @author Rod Johnson * @author Sam Brannen @@ -37,14 +36,15 @@ import org.springframework.stereotype.Repository; @Repository public class JpaVetRepositoryImpl implements VetRepository { - @PersistenceContext - private EntityManager em; + @PersistenceContext + private EntityManager em; - @Cacheable(value="vets") - @SuppressWarnings("unchecked") - public Collection findAll() { - return this.em.createQuery("SELECT vet FROM Vet vet join fetch vet.specialties ORDER BY vet.lastName, vet.firstName").getResultList(); - } + @Override + @Cacheable(value = "vets") + @SuppressWarnings("unchecked") + public Collection findAll() { + return this.em.createQuery("SELECT vet FROM Vet vet join fetch vet.specialties ORDER BY vet.lastName, vet.firstName").getResultList(); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java index 3500a437f..e9fbf07a8 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryImpl.java @@ -15,19 +15,18 @@ */ package org.springframework.samples.petclinic.repository.jpa; -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Repository; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import java.util.List; + /** * JPA implementation of the ClinicService interface using EntityManager. - * + *

*

The mappings are defined in "orm.xml" located in the META-INF directory. * * @author Mike Keith @@ -39,20 +38,22 @@ import org.springframework.stereotype.Repository; @Repository public class JpaVisitRepositoryImpl implements VisitRepository { - @PersistenceContext - private EntityManager em; + @PersistenceContext + private EntityManager em; - public void save(Visit visit) { - this.em.merge(visit); - } + @Override + public void save(Visit visit) { + this.em.merge(visit); + } - @SuppressWarnings("unchecked") - public List findByPetId(Integer petId) { - Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id"); - query.setParameter("id", petId); - return query.getResultList(); - } + @Override + @SuppressWarnings("unchecked") + public List findByPetId(Integer petId) { + Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id"); + query.setParameter("id", petId); + return query.getResultList(); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java index 6f28d798c..e6d050e59 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java @@ -15,51 +15,53 @@ */ package org.springframework.samples.petclinic.repository.springdatajpa; -import java.util.Collection; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.repository.OwnerRepository; import org.springframework.stereotype.Repository; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import java.util.Collection; + /** - * Using native JPA instead of Spring Data JPA here because of this query: - * "SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName" - * See https://jira.springsource.org/browse/DATAJPA-292 for more details. + * Using native JPA instead of Spring Data JPA here because of this query: "SELECT owner FROM Owner owner left join + * fetch owner.pets WHERE owner.lastName LIKE :lastName" See https://jira.springsource.org/browse/DATAJPA-292 for more + * details. * * @author Michael Isvy */ @Repository public class JpaOwnerRepositoryImpl implements OwnerRepository { - @PersistenceContext - private EntityManager em; - - - @SuppressWarnings("unchecked") - public Collection findByLastName(String lastName) { - // using 'join fetch' because a single query should load both owners and pets - // using 'left join fetch' because it might happen that an owner does not have pets yet - Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName"); - query.setParameter("lastName", lastName + "%"); - return query.getResultList(); - } - - public Owner findById(int id) { - // using 'join fetch' because a single query should load both owners and pets - // using 'left join fetch' because it might happen that an owner does not have pets yet - Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id"); - query.setParameter("id", id); - return (Owner) query.getSingleResult(); - } + @PersistenceContext + private EntityManager em; - public void save(Owner owner) { - this.em.merge(owner); + @Override + @SuppressWarnings("unchecked") + public Collection findByLastName(String lastName) { + // using 'join fetch' because a single query should load both owners and pets + // using 'left join fetch' because it might happen that an owner does not have pets yet + Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName"); + query.setParameter("lastName", lastName + "%"); + return query.getResultList(); + } - } + @Override + public Owner findById(int id) { + // using 'join fetch' because a single query should load both owners and pets + // using 'left join fetch' because it might happen that an owner does not have pets yet + Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id"); + query.setParameter("id", id); + return (Owner) query.getSingleResult(); + } + + + @Override + public void save(Owner owner) { + this.em.merge(owner); + + } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java index 4cdf1ac13..e510facd9 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetRepository.java @@ -15,8 +15,6 @@ */ package org.springframework.samples.petclinic.repository.springdatajpa; -import java.util.List; - import org.springframework.dao.DataAccessException; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; @@ -24,6 +22,8 @@ import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.repository.PetRepository; +import java.util.List; + /** * Spring Data JPA specialization of the {@link PetRepository} interface * @@ -31,7 +31,8 @@ import org.springframework.samples.petclinic.repository.PetRepository; * @since 15.1.2013 */ public interface SpringDataPetRepository extends PetRepository, Repository { - - @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") - List findPetTypes() throws DataAccessException; + + @Override + @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") + List findPetTypes() throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java index 93b0c2a1c..84740224b 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataVisitRepository.java @@ -18,7 +18,7 @@ package org.springframework.samples.petclinic.repository.springdatajpa; import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.VisitRepository; - + /** * Spring Data JPA specialization of the {@link VisitRepository} interface * diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java index 17440fb11..0ea3d7f16 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java @@ -15,14 +15,10 @@ */ package org.springframework.samples.petclinic.service; -import java.util.Collection; - import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.model.*; + +import java.util.Collection; /** @@ -31,21 +27,21 @@ import org.springframework.samples.petclinic.model.Visit; * @author Michael Isvy */ public interface ClinicService { - - public Collection findPetTypes() throws DataAccessException; - - public Owner findOwnerById(int id) throws DataAccessException; - - public Pet findPetById(int id) throws DataAccessException; - public void savePet(Pet pet) throws DataAccessException; + public Collection findPetTypes() throws DataAccessException; - public void saveVisit(Visit visit) throws DataAccessException; - - public Collection findVets() throws DataAccessException; + public Owner findOwnerById(int id) throws DataAccessException; - public void saveOwner(Owner owner) throws DataAccessException; + public Pet findPetById(int id) throws DataAccessException; - Collection findOwnerByLastName(String lastName) throws DataAccessException; + public void savePet(Pet pet) throws DataAccessException; + + public void saveVisit(Visit visit) throws DataAccessException; + + public Collection findVets() throws DataAccessException; + + public void saveOwner(Owner owner) throws DataAccessException; + + Collection findOwnerByLastName(String lastName) throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java index eeb40371c..e03c1eb2c 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -15,15 +15,9 @@ */ package org.springframework.samples.petclinic.service; -import java.util.Collection; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; -import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.PetType; -import org.springframework.samples.petclinic.model.Vet; -import org.springframework.samples.petclinic.model.Visit; +import org.springframework.samples.petclinic.model.*; import org.springframework.samples.petclinic.repository.OwnerRepository; import org.springframework.samples.petclinic.repository.PetRepository; import org.springframework.samples.petclinic.repository.VetRepository; @@ -31,6 +25,8 @@ import org.springframework.samples.petclinic.repository.VisitRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.Collection; + /** * Mostly used as a facade for all Petclinic controllers * @@ -38,70 +34,69 @@ import org.springframework.transaction.annotation.Transactional; */ @Service public class ClinicServiceImpl implements ClinicService { - - private PetRepository petRepository; - private VetRepository vetRepository; - private OwnerRepository ownerRepository; - private VisitRepository visitRepository; - @Autowired - public ClinicServiceImpl(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository, VisitRepository visitRepository) { - this.petRepository = petRepository; - this.vetRepository = vetRepository; - this.ownerRepository = ownerRepository; - this.visitRepository = visitRepository; - } + private PetRepository petRepository; + private VetRepository vetRepository; + private OwnerRepository ownerRepository; + private VisitRepository visitRepository; - @Transactional(readOnly=true) - public Collection findPetTypes() throws DataAccessException { - return petRepository.findPetTypes(); - } + @Autowired + public ClinicServiceImpl(PetRepository petRepository, VetRepository vetRepository, OwnerRepository ownerRepository, VisitRepository visitRepository) { + this.petRepository = petRepository; + this.vetRepository = vetRepository; + this.ownerRepository = ownerRepository; + this.visitRepository = visitRepository; + } - @Transactional(readOnly=true) - public Owner findOwnerById(int id) throws DataAccessException { - return ownerRepository.findById(id); - } - - @Transactional(readOnly=true) - public Collection findOwnerByLastName(String lastName) throws DataAccessException { - return ownerRepository.findByLastName(lastName); - } + @Override + @Transactional(readOnly = true) + public Collection findPetTypes() throws DataAccessException { + return petRepository.findPetTypes(); + } - @Transactional - public void saveOwner(Owner owner) throws DataAccessException { - ownerRepository.save(owner); - } - - - @Transactional - public void saveVisit(Visit visit) throws DataAccessException { - visitRepository.save(visit); - } - + @Override + @Transactional(readOnly = true) + public Owner findOwnerById(int id) throws DataAccessException { + return ownerRepository.findById(id); + } - @Transactional(readOnly=true) - public Pet findPetById(int id) throws DataAccessException { - return petRepository.findById(id); - } + @Override + @Transactional(readOnly = true) + public Collection findOwnerByLastName(String lastName) throws DataAccessException { + return ownerRepository.findByLastName(lastName); + } - @Transactional - public void savePet(Pet pet) throws DataAccessException { - petRepository.save(pet); - } + @Override + @Transactional + public void saveOwner(Owner owner) throws DataAccessException { + ownerRepository.save(owner); + } + + + @Override + @Transactional + public void saveVisit(Visit visit) throws DataAccessException { + visitRepository.save(visit); + } + + + @Override + @Transactional(readOnly = true) + public Pet findPetById(int id) throws DataAccessException { + return petRepository.findById(id); + } + + @Override + @Transactional + public void savePet(Pet pet) throws DataAccessException { + petRepository.save(pet); + } + + @Override + @Transactional(readOnly = true) + public Collection findVets() throws DataAccessException { + return vetRepository.findAll(); + } - @Transactional(readOnly=true) - public Collection findVets() throws DataAccessException { - return vetRepository.findAll(); - } - - - - - - - - - - } diff --git a/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java b/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java index 116e7f7aa..cad19ede0 100644 --- a/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java +++ b/src/main/java/org/springframework/samples/petclinic/util/CallMonitoringAspect.java @@ -24,8 +24,8 @@ import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.util.StopWatch; /** - * Simple aspect that monitors call count and call invocation time. - * It uses JMX annotations and therefore can be monitored using any JMX console such as the jConsole + * Simple aspect that monitors call count and call invocation time. It uses JMX annotations and therefore can be + * monitored using any JMX console such as the jConsole * * @author Rob Harrop * @author Juergen Hoeller @@ -36,61 +36,58 @@ import org.springframework.util.StopWatch; @Aspect public class CallMonitoringAspect { - private boolean isEnabled = true; + private boolean isEnabled = true; - private int callCount = 0; + private int callCount = 0; - private long accumulatedCallTime = 0; + private long accumulatedCallTime = 0; - @ManagedAttribute - public void setEnabled(boolean enabled) { - isEnabled = enabled; - } + @ManagedAttribute + public void setEnabled(boolean enabled) { + isEnabled = enabled; + } - @ManagedAttribute - public boolean isEnabled() { - return isEnabled; - } + @ManagedAttribute + public boolean isEnabled() { + return isEnabled; + } - @ManagedOperation - public void reset() { - this.callCount = 0; - this.accumulatedCallTime = 0; - } + @ManagedOperation + public void reset() { + this.callCount = 0; + this.accumulatedCallTime = 0; + } - @ManagedAttribute - public int getCallCount() { - return callCount; - } + @ManagedAttribute + public int getCallCount() { + return callCount; + } - @ManagedAttribute - public long getCallTime() { - return (this.callCount > 0 ? this.accumulatedCallTime / this.callCount : 0); - } + @ManagedAttribute + public long getCallTime() { + return (this.callCount > 0 ? this.accumulatedCallTime / this.callCount : 0); + } - @Around("within(@org.springframework.stereotype.Repository *)") - public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable { - if (this.isEnabled) { - StopWatch sw = new StopWatch(joinPoint.toShortString()); + @Around("within(@org.springframework.stereotype.Repository *)") + public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable { + if (this.isEnabled) { + StopWatch sw = new StopWatch(joinPoint.toShortString()); - sw.start("invoke"); - try { - return joinPoint.proceed(); - } - finally { - sw.stop(); - synchronized (this) { - this.callCount++; - this.accumulatedCallTime += sw.getTotalTimeMillis(); - } - } - } - - else { - return joinPoint.proceed(); - } - } + sw.start("invoke"); + try { + return joinPoint.proceed(); + } finally { + sw.stop(); + synchronized (this) { + this.callCount++; + this.accumulatedCallTime += sw.getTotalTimeMillis(); + } + } + } else { + return joinPoint.proceed(); + } + } } diff --git a/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java b/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java index 822fed9b1..5e538bdca 100644 --- a/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java +++ b/src/main/java/org/springframework/samples/petclinic/util/EntityUtils.java @@ -16,41 +16,40 @@ package org.springframework.samples.petclinic.util; -import java.util.Collection; - import org.springframework.orm.ObjectRetrievalFailureException; import org.springframework.samples.petclinic.model.BaseEntity; +import java.util.Collection; + /** - * Utility methods for handling entities. Separate from the BaseEntity class - * mainly because of dependency on the ORM-associated - * ObjectRetrievalFailureException. + * Utility methods for handling entities. Separate from the BaseEntity class mainly because of dependency on the + * ORM-associated ObjectRetrievalFailureException. * * @author Juergen Hoeller * @author Sam Brannen - * @since 29.10.2003 * @see org.springframework.samples.petclinic.model.BaseEntity + * @since 29.10.2003 */ public abstract class EntityUtils { - /** - * Look up the entity of the given class with the given id in the given - * collection. - * - * @param entities the collection to search - * @param entityClass the entity class to look up - * @param entityId the entity id to look up - * @return the found entity - * @throws ObjectRetrievalFailureException if the entity was not found - */ - public static T getById(Collection entities, Class entityClass, int entityId) - throws ObjectRetrievalFailureException { - for (T entity : entities) { - if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) { - return entity; - } - } - throw new ObjectRetrievalFailureException(entityClass, new Integer(entityId)); - } + /** + * Look up the entity of the given class with the given id in the given collection. + * + * @param entities the collection to search + * @param entityClass the entity class to look up + * @param entityId the entity id to look up + * @return the found entity + * @throws ObjectRetrievalFailureException + * if the entity was not found + */ + public static T getById(Collection entities, Class entityClass, int entityId) + throws ObjectRetrievalFailureException { + for (T entity : entities) { + if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) { + return entity; + } + } + throw new ObjectRetrievalFailureException(entityClass, new Integer(entityId)); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/CrashController.java b/src/main/java/org/springframework/samples/petclinic/web/CrashController.java index 6575b71da..e413f3f3b 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/CrashController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/CrashController.java @@ -21,20 +21,20 @@ import org.springframework.web.bind.annotation.RequestMethod; /** * Controller used to showcase what happens when an exception is thrown - * + * * @author Michael Isvy - * - * Also see how the bean of type 'SimpleMappingExceptionResolver' has been declared inside /WEB-INF/mvc-core-config.xml + *

+ * Also see how the bean of type 'SimpleMappingExceptionResolver' has been declared inside + * /WEB-INF/mvc-core-config.xml */ @Controller -public class CrashController { +public class CrashController { - @RequestMapping(value="/oups", method = RequestMethod.GET) - public String triggerException() { - throw new RuntimeException("Expected: controller used to showcase what " + - "happens when an exception is thrown"); - } + @RequestMapping(value = "/oups", method = RequestMethod.GET) + public String triggerException() { + throw new RuntimeException("Expected: controller used to showcase what " + + "happens when an exception is thrown"); + } - } diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java index 6edafdd39..b05ecbf86 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java @@ -15,10 +15,6 @@ */ package org.springframework.samples.petclinic.web; -import java.util.Collection; - -import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.service.ClinicService; @@ -26,16 +22,14 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.servlet.ModelAndView; +import javax.validation.Valid; +import java.util.Collection; + /** - * * @author Juergen Hoeller * @author Ken Krebs * @author Arjen Poutsma @@ -45,101 +39,98 @@ import org.springframework.web.servlet.ModelAndView; @SessionAttributes(types = Owner.class) public class OwnerController { - private final ClinicService clinicService; + private final ClinicService clinicService; - @Autowired - public OwnerController(ClinicService clinicService) { - this.clinicService = clinicService; - } + @Autowired + public OwnerController(ClinicService clinicService) { + this.clinicService = clinicService; + } - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } - @RequestMapping(value="/owners/new", method = RequestMethod.GET) - public String initCreationForm(Model model) { - Owner owner = new Owner(); - model.addAttribute(owner); - return "owners/createOrUpdateOwnerForm"; - } + @RequestMapping(value = "/owners/new", method = RequestMethod.GET) + public String initCreationForm(Model model) { + Owner owner = new Owner(); + model.addAttribute(owner); + return "owners/createOrUpdateOwnerForm"; + } - @RequestMapping(value="/owners/new", method = RequestMethod.POST) - public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { - if (result.hasErrors()) { - return "owners/createOrUpdateOwnerForm"; - } - else { - this.clinicService.saveOwner(owner); - status.setComplete(); - return "redirect:/owners/" + owner.getId(); - } - } + @RequestMapping(value = "/owners/new", method = RequestMethod.POST) + public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + if (result.hasErrors()) { + return "owners/createOrUpdateOwnerForm"; + } else { + this.clinicService.saveOwner(owner); + status.setComplete(); + return "redirect:/owners/" + owner.getId(); + } + } - @RequestMapping(value = "/owners/find", method = RequestMethod.GET) - public String initFindForm(Model model) { - model.addAttribute("owner", new Owner()); - return "owners/findOwners"; - } + @RequestMapping(value = "/owners/find", method = RequestMethod.GET) + public String initFindForm(Model model) { + model.addAttribute("owner", new Owner()); + return "owners/findOwners"; + } - @RequestMapping(value = "/owners", method = RequestMethod.GET) - public String processFindForm(Owner owner, BindingResult result, Model model) { + @RequestMapping(value = "/owners", method = RequestMethod.GET) + public String processFindForm(Owner owner, BindingResult result, Model model) { - // allow parameterless GET request for /owners to return all records - if (owner.getLastName() == null) { - owner.setLastName(""); // empty string signifies broadest possible search - } + // allow parameterless GET request for /owners to return all records + if (owner.getLastName() == null) { + owner.setLastName(""); // empty string signifies broadest possible search + } - // find owners by last name - Collection results = this.clinicService.findOwnerByLastName(owner.getLastName()); - if (results.size() < 1) { - // no owners found - result.rejectValue("lastName", "notFound", "not found"); - return "owners/findOwners"; - } - if (results.size() > 1) { - // multiple owners found - model.addAttribute("selections", results); - return "owners/ownersList"; - } - else { - // 1 owner found - owner = results.iterator().next(); - return "redirect:/owners/" + owner.getId(); - } - } + // find owners by last name + Collection results = this.clinicService.findOwnerByLastName(owner.getLastName()); + if (results.size() < 1) { + // no owners found + result.rejectValue("lastName", "notFound", "not found"); + return "owners/findOwners"; + } + if (results.size() > 1) { + // multiple owners found + model.addAttribute("selections", results); + return "owners/ownersList"; + } else { + // 1 owner found + owner = results.iterator().next(); + return "redirect:/owners/" + owner.getId(); + } + } - @RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.GET) - public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { - Owner owner = this.clinicService.findOwnerById(ownerId); - model.addAttribute(owner); - return "owners/createOrUpdateOwnerForm"; - } + @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET) + public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { + Owner owner = this.clinicService.findOwnerById(ownerId); + model.addAttribute(owner); + return "owners/createOrUpdateOwnerForm"; + } - @RequestMapping(value="/owners/{ownerId}/edit", method = RequestMethod.PUT) - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { - if (result.hasErrors()) { - return "owners/createOrUpdateOwnerForm"; - } - else { - this.clinicService.saveOwner(owner); - status.setComplete(); - return "redirect:/owners/{ownerId}"; - } - } + @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.PUT) + public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) { + if (result.hasErrors()) { + return "owners/createOrUpdateOwnerForm"; + } else { + this.clinicService.saveOwner(owner); + status.setComplete(); + return "redirect:/owners/{ownerId}"; + } + } - /** - * Custom handler for displaying an owner. - * - * @param ownerId the ID of the owner to display - * @return a ModelMap with the model attributes for the view - */ - @RequestMapping("/owners/{ownerId}") - public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { - ModelAndView mav = new ModelAndView("owners/ownerDetails"); - mav.addObject(this.clinicService.findOwnerById(ownerId)); - return mav; - } + /** + * Custom handler for displaying an owner. + * + * @param ownerId the ID of the owner to display + * @return a ModelMap with the model attributes for the view + */ + @RequestMapping("/owners/{ownerId}") + public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { + ModelAndView mav = new ModelAndView("owners/ownerDetails"); + mav.addObject(this.clinicService.findOwnerById(ownerId)); + return mav; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java index 2c0c0b73c..3161071d9 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -15,8 +15,6 @@ */ package org.springframework.samples.petclinic.web; -import java.util.Collection; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.model.Pet; @@ -26,16 +24,12 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.support.SessionStatus; +import java.util.Collection; + /** - * * @author Juergen Hoeller * @author Ken Krebs * @author Arjen Poutsma @@ -44,65 +38,63 @@ import org.springframework.web.bind.support.SessionStatus; @SessionAttributes("pet") public class PetController { - private final ClinicService clinicService; + private final ClinicService clinicService; - @Autowired - public PetController(ClinicService clinicService) { - this.clinicService = clinicService; - } + @Autowired + public PetController(ClinicService clinicService) { + this.clinicService = clinicService; + } - @ModelAttribute("types") - public Collection populatePetTypes() { - return this.clinicService.findPetTypes(); - } + @ModelAttribute("types") + public Collection populatePetTypes() { + return this.clinicService.findPetTypes(); + } - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } - @RequestMapping(value="/owners/{ownerId}/pets/new", method = RequestMethod.GET) - public String initCreationForm(@PathVariable("ownerId") int ownerId, Model model) { - Owner owner = this.clinicService.findOwnerById(ownerId); - Pet pet = new Pet(); - owner.addPet(pet); - model.addAttribute("pet", pet); - return "pets/createOrUpdatePetForm"; - } + @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET) + public String initCreationForm(@PathVariable("ownerId") int ownerId, Model model) { + Owner owner = this.clinicService.findOwnerById(ownerId); + Pet pet = new Pet(); + owner.addPet(pet); + model.addAttribute("pet", pet); + return "pets/createOrUpdatePetForm"; + } - @RequestMapping(value="/owners/{ownerId}/pets/new", method = RequestMethod.POST) - public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { - new PetValidator().validate(pet, result); - if (result.hasErrors()) { - return "pets/createOrUpdatePetForm"; - } - else { - this.clinicService.savePet(pet); - status.setComplete(); - return "redirect:/owners/{ownerId}"; - } - } + @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST) + public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { + new PetValidator().validate(pet, result); + if (result.hasErrors()) { + return "pets/createOrUpdatePetForm"; + } else { + this.clinicService.savePet(pet); + status.setComplete(); + return "redirect:/owners/{ownerId}"; + } + } - @RequestMapping(value="/owners/*/pets/{petId}/edit", method = RequestMethod.GET) - public String initUpdateForm(@PathVariable("petId") int petId, Model model) { - Pet pet = this.clinicService.findPetById(petId); - model.addAttribute("pet", pet); - return "pets/createOrUpdatePetForm"; - } + @RequestMapping(value = "/owners/*/pets/{petId}/edit", method = RequestMethod.GET) + public String initUpdateForm(@PathVariable("petId") int petId, Model model) { + Pet pet = this.clinicService.findPetById(petId); + model.addAttribute("pet", pet); + return "pets/createOrUpdatePetForm"; + } - @RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = { RequestMethod.PUT, RequestMethod.POST }) - public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { - // we're not using @Valid annotation here because it is easier to define such validation rule in Java - new PetValidator().validate(pet, result); - if (result.hasErrors()) { - return "pets/createOrUpdatePetForm"; - } - else { - this.clinicService.savePet(pet); - status.setComplete(); - return "redirect:/owners/{ownerId}"; - } - } + @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST}) + public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { + // we're not using @Valid annotation here because it is easier to define such validation rule in Java + new PetValidator().validate(pet, result); + if (result.hasErrors()) { + return "pets/createOrUpdatePetForm"; + } else { + this.clinicService.savePet(pet); + status.setComplete(); + return "redirect:/owners/{ownerId}"; + } + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java b/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java index 6217ba82f..725f32f30 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetTypeFormatter.java @@ -16,52 +16,51 @@ package org.springframework.samples.petclinic.web; -import java.text.ParseException; -import java.util.Collection; -import java.util.Locale; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.Formatter; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.service.ClinicService; +import java.text.ParseException; +import java.util.Collection; +import java.util.Locale; + /** - * Instructs Spring MVC on how to parse and print elements of type 'PetType'. - * Starting from Spring 3.0, Formatters have come as an improvement in comparison to legacy PropertyEditors. - * See the following links for more details: - * - The Spring ref doc: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#format-Formatter-SPI + * Instructs Spring MVC on how to parse and print elements of type 'PetType'. Starting from Spring 3.0, Formatters have + * come as an improvement in comparison to legacy PropertyEditors. See the following links for more details: - The + * Spring ref doc: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#format-Formatter-SPI * - A nice blog entry from Gordon Dickens: http://gordondickens.com/wordpress/2010/09/30/using-spring-3-0-custom-type-converter/ - * + *

* Also see how the bean 'conversionService' has been declared inside /WEB-INF/mvc-core-config.xml - * + * * @author Mark Fisher * @author Juergen Hoeller * @author Michael Isvy */ public class PetTypeFormatter implements Formatter { - private final ClinicService clinicService; + private final ClinicService clinicService; - @Autowired - public PetTypeFormatter(ClinicService clinicService) { - this.clinicService = clinicService; - } + @Autowired + public PetTypeFormatter(ClinicService clinicService) { + this.clinicService = clinicService; + } - @Override - public String print(PetType petType, Locale locale) { - return petType.getName(); - } + @Override + public String print(PetType petType, Locale locale) { + return petType.getName(); + } - @Override - public PetType parse(String text, Locale locale) throws ParseException { - Collection findPetTypes = this.clinicService.findPetTypes(); - for (PetType type : findPetTypes) { - if (type.getName().equals(text)) { - return type; - } - } - throw new ParseException("type not found: "+text, 0); - } + @Override + public PetType parse(String text, Locale locale) throws ParseException { + Collection findPetTypes = this.clinicService.findPetTypes(); + for (PetType type : findPetTypes) { + if (type.getName().equals(text)) { + return type; + } + } + throw new ParseException("type not found: " + text, 0); + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java index fbf080e87..7068dd878 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java @@ -27,14 +27,13 @@ import org.springframework.validation.Errors; */ public class PetValidator { - public void validate(Pet pet, Errors errors) { - String name = pet.getName(); - if (!StringUtils.hasLength(name)) { - errors.rejectValue("name", "required", "required"); - } - else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) { - errors.rejectValue("name", "duplicate", "already exists"); - } - } + public void validate(Pet pet, Errors errors) { + String name = pet.getName(); + if (!StringUtils.hasLength(name)) { + errors.rejectValue("name", "required", "required"); + } else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) { + errors.rejectValue("name", "duplicate", "already exists"); + } + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VetController.java b/src/main/java/org/springframework/samples/petclinic/web/VetController.java index dfbc216a7..0f5478556 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VetController.java @@ -23,7 +23,6 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** - * * @author Juergen Hoeller * @author Mark Fisher * @author Ken Krebs @@ -32,26 +31,23 @@ import org.springframework.web.bind.annotation.RequestMapping; @Controller public class VetController { - private final ClinicService clinicService; - - - @Autowired - public VetController(ClinicService clinicService) { - this.clinicService = clinicService; - } - - @RequestMapping("/vets") - public String showVetList(Model model) { - // Here we are returning an object of type 'Vets' rather than a collection of Vet objects - // so it is simpler for Object-Xml mapping - Vets vets = new Vets(); - vets.getVetList().addAll(this.clinicService.findVets()); - model.addAttribute("vets", vets); - return "vets/vetList"; - } + private final ClinicService clinicService; + @Autowired + public VetController(ClinicService clinicService) { + this.clinicService = clinicService; + } + @RequestMapping("/vets") + public String showVetList(Model model) { + // Here we are returning an object of type 'Vets' rather than a collection of Vet objects + // so it is simpler for Object-Xml mapping + Vets vets = new Vets(); + vets.getVetList().addAll(this.clinicService.findVets()); + model.addAttribute("vets", vets); + return "vets/vetList"; + } } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java b/src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java index 6df94372e..d379aebf1 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VetsAtomView.java @@ -15,60 +15,58 @@ */ package org.springframework.samples.petclinic.web; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import com.sun.syndication.feed.atom.Content; +import com.sun.syndication.feed.atom.Entry; +import com.sun.syndication.feed.atom.Feed; import org.springframework.samples.petclinic.model.Vet; import org.springframework.samples.petclinic.model.Vets; import org.springframework.web.servlet.view.feed.AbstractAtomFeedView; -import com.sun.syndication.feed.atom.Content; -import com.sun.syndication.feed.atom.Entry; -import com.sun.syndication.feed.atom.Feed; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** - * A view creating a Atom representation from a list of Visit objects. - * + * A view creating a Atom representation from a list of Visit objects. + * * @author Alef Arendsen * @author Arjen Poutsma */ public class VetsAtomView extends AbstractAtomFeedView { - @Override - protected void buildFeedMetadata(Map model, Feed feed, HttpServletRequest request) { - feed.setId("tag:springsource.org"); - feed.setTitle("Veterinarians"); - //feed.setUpdated(date); - } + @Override + protected void buildFeedMetadata(Map model, Feed feed, HttpServletRequest request) { + feed.setId("tag:springsource.org"); + feed.setTitle("Veterinarians"); + //feed.setUpdated(date); + } - @Override - protected List buildFeedEntries(Map model, - HttpServletRequest request, HttpServletResponse response) throws Exception { + @Override + protected List buildFeedEntries(Map model, + HttpServletRequest request, HttpServletResponse response) throws Exception { - Vets vets = (Vets) model.get("vets"); - List vetList = vets.getVetList(); - List entries = new ArrayList(vetList.size()); + Vets vets = (Vets) model.get("vets"); + List vetList = vets.getVetList(); + List entries = new ArrayList(vetList.size()); - for (Vet vet : vetList) { - Entry entry = new Entry(); - // see http://diveintomark.org/archives/2004/05/28/howto-atom-id#other - entry.setId(String.format("tag:springsource.org,%s", vet.getId())); - entry.setTitle(String.format("Vet: %s %s", vet.getFirstName(), vet.getLastName())); - //entry.setUpdated(visit.getDate().toDate()); + for (Vet vet : vetList) { + Entry entry = new Entry(); + // see http://diveintomark.org/archives/2004/05/28/howto-atom-id#other + entry.setId(String.format("tag:springsource.org,%s", vet.getId())); + entry.setTitle(String.format("Vet: %s %s", vet.getFirstName(), vet.getLastName())); + //entry.setUpdated(visit.getDate().toDate()); - Content summary = new Content(); - summary.setValue(vet.getSpecialties().toString()); - entry.setSummary(summary); + Content summary = new Content(); + summary.setValue(vet.getSpecialties().toString()); + entry.setSummary(summary); - entries.add(entry); - } - response.setContentType("blabla"); - return entries; + entries.add(entry); + } + response.setContentType("blabla"); + return entries; + + } - } - } diff --git a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java b/src/main/java/org/springframework/samples/petclinic/web/VisitController.java index b72e0cad3..7de966939 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/VisitController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/VisitController.java @@ -15,8 +15,6 @@ */ package org.springframework.samples.petclinic.web; -import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.Visit; @@ -25,16 +23,13 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.servlet.ModelAndView; +import javax.validation.Valid; + /** - * * @author Juergen Hoeller * @author Ken Krebs * @author Arjen Poutsma @@ -44,45 +39,44 @@ import org.springframework.web.servlet.ModelAndView; @SessionAttributes("visit") public class VisitController { - private final ClinicService clinicService; + private final ClinicService clinicService; - @Autowired - public VisitController(ClinicService clinicService) { - this.clinicService = clinicService; - } + @Autowired + public VisitController(ClinicService clinicService) { + this.clinicService = clinicService; + } - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } - @RequestMapping(value="/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET) - public String initNewVisitForm(@PathVariable("petId") int petId, Model model) { - Pet pet = this.clinicService.findPetById(petId); - Visit visit = new Visit(); - pet.addVisit(visit); - model.addAttribute("visit", visit); - return "pets/createOrUpdateVisitForm"; - } + @RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET) + public String initNewVisitForm(@PathVariable("petId") int petId, Model model) { + Pet pet = this.clinicService.findPetById(petId); + Visit visit = new Visit(); + pet.addVisit(visit); + model.addAttribute("visit", visit); + return "pets/createOrUpdateVisitForm"; + } - @RequestMapping(value="/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) - public String processNewVisitForm(@Valid Visit visit, BindingResult result, SessionStatus status) { - if (result.hasErrors()) { - return "pets/createOrUpdateVisitForm"; - } - else { - this.clinicService.saveVisit(visit); - status.setComplete(); - return "redirect:/owners/{ownerId}"; - } - } + @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) + public String processNewVisitForm(@Valid Visit visit, BindingResult result, SessionStatus status) { + if (result.hasErrors()) { + return "pets/createOrUpdateVisitForm"; + } else { + this.clinicService.saveVisit(visit); + status.setComplete(); + return "redirect:/owners/{ownerId}"; + } + } - @RequestMapping(value="/owners/*/pets/{petId}/visits", method=RequestMethod.GET) - public ModelAndView showVisits(@PathVariable int petId) { - ModelAndView mav = new ModelAndView("visitList"); - mav.addObject("visits", this.clinicService.findPetById(petId).getVisits()); - return mav; - } + @RequestMapping(value = "/owners/*/pets/{petId}/visits", method = RequestMethod.GET) + public ModelAndView showVisits(@PathVariable int petId) { + ModelAndView mav = new ModelAndView("visitList"); + mav.addObject("visits", this.clinicService.findPetById(petId).getVisits()); + return mav; + } } diff --git a/src/main/java/overview.html b/src/main/java/overview.html index 1eb7a2e8c..df4f4d6b7 100644 --- a/src/main/java/overview.html +++ b/src/main/java/overview.html @@ -1,7 +1,7 @@

-The Spring Data Binding framework, an internal library used by Spring Web Flow. + The Spring Data Binding framework, an internal library used by Spring Web Flow.

\ No newline at end of file diff --git a/src/main/resources/db/hsqldb/initDB.sql b/src/main/resources/db/hsqldb/initDB.sql index a76d662dd..a16c42dec 100644 --- a/src/main/resources/db/hsqldb/initDB.sql +++ b/src/main/resources/db/hsqldb/initDB.sql @@ -1,64 +1,64 @@ -drop table vet_specialties if exists; -drop table vets if exists; -drop table specialties if exists; -drop table visits if exists; -drop table pets if exists; -drop table types if exists; -drop table owners if exists; +DROP TABLE vet_specialties IF EXISTS; +DROP TABLE vets IF EXISTS; +DROP TABLE specialties IF EXISTS; +DROP TABLE visits IF EXISTS; +DROP TABLE pets IF EXISTS; +DROP TABLE types IF EXISTS; +DROP TABLE owners IF EXISTS; CREATE TABLE vets ( - id INTEGER IDENTITY PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR(30) + id INTEGER IDENTITY PRIMARY KEY, + first_name VARCHAR(30), + last_name VARCHAR(30) ); -CREATE INDEX vets_last_name ON vets(last_name); +CREATE INDEX vets_last_name ON vets (last_name); CREATE TABLE specialties ( - id INTEGER IDENTITY PRIMARY KEY, - name VARCHAR(80) + id INTEGER IDENTITY PRIMARY KEY, + name VARCHAR(80) ); -CREATE INDEX specialties_name ON specialties(name); +CREATE INDEX specialties_name ON specialties (name); CREATE TABLE vet_specialties ( - vet_id INTEGER NOT NULL, - specialty_id INTEGER NOT NULL + vet_id INTEGER NOT NULL, + specialty_id INTEGER NOT NULL ); -alter table vet_specialties add constraint fk_vet_specialties_vets foreign key (vet_id) references vets(id); -alter table vet_specialties add constraint fk_vet_specialties_specialties foreign key (specialty_id) references specialties(id); +ALTER TABLE vet_specialties ADD CONSTRAINT fk_vet_specialties_vets FOREIGN KEY (vet_id) REFERENCES vets (id); +ALTER TABLE vet_specialties ADD CONSTRAINT fk_vet_specialties_specialties FOREIGN KEY (specialty_id) REFERENCES specialties (id); CREATE TABLE types ( - id INTEGER IDENTITY PRIMARY KEY, - name VARCHAR(80) + id INTEGER IDENTITY PRIMARY KEY, + name VARCHAR(80) ); -CREATE INDEX types_name ON types(name); +CREATE INDEX types_name ON types (name); CREATE TABLE owners ( - id INTEGER IDENTITY PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR(30), - address VARCHAR(255), - city VARCHAR(80), - telephone VARCHAR(20) + id INTEGER IDENTITY PRIMARY KEY, + first_name VARCHAR(30), + last_name VARCHAR(30), + address VARCHAR(255), + city VARCHAR(80), + telephone VARCHAR(20) ); -CREATE INDEX owners_last_name ON owners(last_name); +CREATE INDEX owners_last_name ON owners (last_name); CREATE TABLE pets ( - id INTEGER IDENTITY PRIMARY KEY, - name VARCHAR(30), - birth_date DATE, - type_id INTEGER NOT NULL, - owner_id INTEGER NOT NULL + id INTEGER IDENTITY PRIMARY KEY, + name VARCHAR(30), + birth_date DATE, + type_id INTEGER NOT NULL, + owner_id INTEGER NOT NULL ); -alter table pets add constraint fk_pets_owners foreign key (owner_id) references owners(id); -alter table pets add constraint fk_pets_types foreign key (type_id) references types(id); -CREATE INDEX pets_name ON pets(name); +ALTER TABLE pets ADD CONSTRAINT fk_pets_owners FOREIGN KEY (owner_id) REFERENCES owners (id); +ALTER TABLE pets ADD CONSTRAINT fk_pets_types FOREIGN KEY (type_id) REFERENCES types (id); +CREATE INDEX pets_name ON pets (name); CREATE TABLE visits ( - id INTEGER IDENTITY PRIMARY KEY, - pet_id INTEGER NOT NULL, - visit_date DATE, - description VARCHAR(255) + id INTEGER IDENTITY PRIMARY KEY, + pet_id INTEGER NOT NULL, + visit_date DATE, + description VARCHAR(255) ); -alter table visits add constraint fk_visits_pets foreign key (pet_id) references pets(id); -CREATE INDEX visits_pet_id ON visits(pet_id); +ALTER TABLE visits ADD CONSTRAINT fk_visits_pets FOREIGN KEY (pet_id) REFERENCES pets (id); +CREATE INDEX visits_pet_id ON visits (pet_id); diff --git a/src/main/resources/ehcache.xml b/src/main/resources/ehcache.xml index 8167066b1..32e509953 100644 --- a/src/main/resources/ehcache.xml +++ b/src/main/resources/ehcache.xml @@ -1,17 +1,17 @@ + xsi:noNamespaceSchemaLocation="ehcache.xsd" + updateCheck="false"> - + - + timeToLiveSeconds="60" + maxElementsInMemory="100" + eternal="false" + overflowToDisk="false" + maxElementsOnDisk="10000000" + diskPersistent="false" + diskExpiryThreadIntervalSeconds="1" + memoryStoreEvictionPolicy="LRU"/> + diff --git a/src/main/resources/ehcache.xsd b/src/main/resources/ehcache.xsd index 2a539199f..bfc19ddb1 100644 --- a/src/main/resources/ehcache.xsd +++ b/src/main/resources/ehcache.xsd @@ -227,13 +227,13 @@ - - - - - - - + + + + + + + @@ -371,26 +371,27 @@ - + - - + + - - - - - - - - - - - - + + + + + + + + + + + + diff --git a/src/main/resources/log4j.dtd b/src/main/resources/log4j.dtd deleted file mode 100644 index d92a6e7bc..000000000 --- a/src/main/resources/log4j.dtd +++ /dev/null @@ -1,166 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml deleted file mode 100755 index f52e0fcec..000000000 --- a/src/main/resources/log4j.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 000000000..3213b0262 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,22 @@ + + + + + true + + + + + + + + %-5level %logger{0} - %msg%n + + + + + + + + + diff --git a/src/main/resources/spring/dao-config.xml b/src/main/resources/spring/dao-config.xml index d4a411057..c29a9bd4c 100644 --- a/src/main/resources/spring/dao-config.xml +++ b/src/main/resources/spring/dao-config.xml @@ -1,101 +1,107 @@ - + - + - - + + - - - + + + - + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - + + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/spring/datasource-config.xml b/src/main/resources/spring/datasource-config.xml index d803c0a2f..2efe12010 100644 --- a/src/main/resources/spring/datasource-config.xml +++ b/src/main/resources/spring/datasource-config.xml @@ -1,37 +1,39 @@ - - + xmlns:p="http://www.springframework.org/schema/p" + xmlns:context="http://www.springframework.org/schema/context" + xmlns:jdbc="http://www.springframework.org/schema/jdbc" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context.xsd + http://www.springframework.org/schema/jdbc + http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> - - - + - - - - - + + + - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/spring/jdbc.properties b/src/main/resources/spring/jdbc.properties index 214778bdc..21d1560b4 100644 --- a/src/main/resources/spring/jdbc.properties +++ b/src/main/resources/spring/jdbc.properties @@ -4,9 +4,6 @@ # various application context XML files (e.g., "applicationContext-*.xml"). # Targeted at system administrators, to avoid touching the context XML files. - - - #------------------------------------------------------------------------------- # HSQL Settings diff --git a/src/main/resources/spring/mvc-core-config.xml b/src/main/resources/spring/mvc-core-config.xml index d814c256d..8aa5d61f0 100644 --- a/src/main/resources/spring/mvc-core-config.xml +++ b/src/main/resources/spring/mvc-core-config.xml @@ -1,57 +1,63 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/spring/mvc-view-config.xml b/src/main/resources/spring/mvc-view-config.xml index 053fa3fdb..8425754ac 100644 --- a/src/main/resources/spring/mvc-view-config.xml +++ b/src/main/resources/spring/mvc-view-config.xml @@ -2,52 +2,50 @@ - + + + + + + + + + + - - - - + + + + + + - - - - - + + - - - - - - - - - + + - - + + + + - - - - - - - - - + + + + diff --git a/src/main/resources/spring/tools-config.xml b/src/main/resources/spring/tools-config.xml index cf204ba2a..e64c18931 100644 --- a/src/main/resources/spring/tools-config.xml +++ b/src/main/resources/spring/tools-config.xml @@ -2,44 +2,48 @@ - + - - - - + + + + - - + + - - - - - - - - - - - - + + + + + + + + - - - - + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/exception.jsp b/src/main/webapp/WEB-INF/jsp/exception.jsp index 42c7e29a2..86416dbaa 100644 --- a/src/main/webapp/WEB-INF/jsp/exception.jsp +++ b/src/main/webapp/WEB-INF/jsp/exception.jsp @@ -1,27 +1,29 @@ <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> -
- - - -

Something happened...

-

${exception.message}

- - - - - -
+ + + + diff --git a/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp b/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp index f8f4645c7..76c184417 100644 --- a/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp +++ b/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp @@ -2,16 +2,23 @@ <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> - -