mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Minor fixes - indentation mostly
This commit is contained in:
parent
06be7eb5be
commit
66227140aa
33 changed files with 117 additions and 169 deletions
|
@ -32,7 +32,6 @@ public class BaseEntity {
|
|||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
protected Integer id;
|
||||
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.samples.petclinic.model;
|
|||
import javax.persistence.Column;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
|
||||
/**
|
||||
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as a base class for objects
|
||||
* needing these properties.
|
||||
|
@ -32,7 +31,6 @@ public class NamedEntity extends BaseEntity {
|
|||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
|
|
@ -60,7 +60,6 @@ public class Owner extends Person {
|
|||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
|
||||
private Set<Pet> pets;
|
||||
|
||||
|
||||
public String getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
@ -141,13 +140,13 @@ public class Owner extends Person {
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,5 +52,4 @@ public class Person extends BaseEntity {
|
|||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -63,7 +63,6 @@ public class Pet extends NamedEntity {
|
|||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER)
|
||||
private Set<Visit> visits;
|
||||
|
||||
|
||||
public void setBirthDate(DateTime birthDate) {
|
||||
this.birthDate = birthDate;
|
||||
}
|
||||
|
|
|
@ -46,10 +46,9 @@ public class Vet extends Person {
|
|||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
|
||||
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
|
||||
private Set<Specialty> specialties;
|
||||
|
||||
|
||||
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
||||
this.specialties = specialties;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
|
|
|
@ -57,7 +57,6 @@ public class Visit extends BaseEntity {
|
|||
@JoinColumn(name = "pet_id")
|
||||
private Pet pet;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of Visit for the current date
|
||||
*/
|
||||
|
@ -65,7 +64,6 @@ public class Visit extends BaseEntity {
|
|||
this.date = new DateTime();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Getter for property date.
|
||||
*
|
||||
|
|
|
@ -1,18 +1,3 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
|
|
|
@ -38,5 +38,4 @@ public interface VetRepository {
|
|||
*/
|
||||
Collection<Vet> findAll() throws DataAccessException;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -58,14 +58,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
|
||||
|
||||
this.insertOwner = new SimpleJdbcInsert(dataSource)
|
||||
.withTableName("owners")
|
||||
.usingGeneratedKeyColumns("id");
|
||||
.withTableName("owners")
|
||||
.usingGeneratedKeyColumns("id");
|
||||
|
||||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads {@link Owner Owners} from the data store by last name, returning all owners whose last name <i>starts</i> with
|
||||
* the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not
|
||||
|
@ -76,10 +75,10 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("lastName", lastName + "%");
|
||||
List<Owner> owners = this.namedParameterJdbcTemplate.query(
|
||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
|
||||
params,
|
||||
BeanPropertyRowMapper.newInstance(Owner.class)
|
||||
);
|
||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
|
||||
params,
|
||||
BeanPropertyRowMapper.newInstance(Owner.class)
|
||||
);
|
||||
loadOwnersPetsAndVisits(owners);
|
||||
return owners;
|
||||
}
|
||||
|
@ -95,10 +94,10 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
Map<String, Object> 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,
|
||||
BeanPropertyRowMapper.newInstance(Owner.class)
|
||||
);
|
||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
|
||||
params,
|
||||
BeanPropertyRowMapper.newInstance(Owner.class)
|
||||
);
|
||||
} catch (EmptyResultDataAccessException ex) {
|
||||
throw new ObjectRetrievalFailureException(Owner.class, id);
|
||||
}
|
||||
|
@ -109,11 +108,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
public void loadPetsAndVisits(final Owner owner) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("id", owner.getId());
|
||||
final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
|
||||
"SELECT pets.id, name, birth_date, type_id, owner_id, visits.id as visit_id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON pets.id = pet_id WHERE owner_id=:id",
|
||||
params,
|
||||
new JdbcPetVisitExtractor()
|
||||
);
|
||||
final List<JdbcPet> pets =
|
||||
this.namedParameterJdbcTemplate
|
||||
.query(
|
||||
"SELECT pets.id, name, birth_date, type_id, owner_id, visits.id as visit_id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON pets.id = pet_id WHERE owner_id=:id",
|
||||
params,
|
||||
new JdbcPetVisitExtractor()
|
||||
);
|
||||
for (JdbcPet pet : pets) {
|
||||
owner.addPet(pet);
|
||||
}
|
||||
|
@ -127,16 +128,16 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
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);
|
||||
"UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " +
|
||||
"city=:city, telephone=:telephone WHERE id=:id",
|
||||
parameterSource);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<PetType> getPetTypes() throws DataAccessException {
|
||||
return this.namedParameterJdbcTemplate.query(
|
||||
"SELECT id, name FROM types ORDER BY name", new HashMap<String, Object>(),
|
||||
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||
"SELECT id, name FROM types ORDER BY name", new HashMap<String, Object>(),
|
||||
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,5 +152,4 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@ class JdbcPet extends Pet {
|
|||
|
||||
private int ownerId;
|
||||
|
||||
|
||||
public void setTypeId(int typeId) {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
|
|
@ -58,14 +58,13 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
|
||||
private VisitRepository visitRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) {
|
||||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||
|
||||
this.insertPet = new SimpleJdbcInsert(dataSource)
|
||||
.withTableName("pets")
|
||||
.usingGeneratedKeyColumns("id");
|
||||
.withTableName("pets")
|
||||
.usingGeneratedKeyColumns("id");
|
||||
|
||||
this.ownerRepository = ownerRepository;
|
||||
this.visitRepository = visitRepository;
|
||||
|
@ -75,9 +74,9 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
public List<PetType> findPetTypes() throws DataAccessException {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
return this.namedParameterJdbcTemplate.query(
|
||||
"SELECT id, name FROM types ORDER BY name",
|
||||
params,
|
||||
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||
"SELECT id, name FROM types ORDER BY name",
|
||||
params,
|
||||
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -87,9 +86,9 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
Map<String, Object> 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());
|
||||
"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, id);
|
||||
}
|
||||
|
@ -108,13 +107,13 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
public void save(Pet pet) throws DataAccessException {
|
||||
if (pet.isNew()) {
|
||||
Number newKey = this.insertPet.executeAndReturnKey(
|
||||
createPetParameterSource(pet));
|
||||
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));
|
||||
"UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " +
|
||||
"owner_id=:owner_id WHERE id=:id",
|
||||
createPetParameterSource(pet));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -123,11 +122,11 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
*/
|
||||
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());
|
||||
.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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ import java.sql.SQLException;
|
|||
* {@link OneToManyResultSetExtractor} of Spring Data Core JDBC Extensions.
|
||||
*/
|
||||
public class JdbcPetVisitExtractor extends
|
||||
OneToManyResultSetExtractor<JdbcPet, Visit, Integer> {
|
||||
OneToManyResultSetExtractor<JdbcPet, Visit, Integer> {
|
||||
|
||||
public JdbcPetVisitExtractor() {
|
||||
super(new JdbcPetRowMapper(), new JdbcVisitRowMapper());
|
||||
|
|
|
@ -60,25 +60,25 @@ public class JdbcVetRepositoryImpl implements VetRepository {
|
|||
List<Vet> 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",
|
||||
BeanPropertyRowMapper.newInstance(Vet.class)));
|
||||
"SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
|
||||
BeanPropertyRowMapper.newInstance(Vet.class)));
|
||||
|
||||
// Retrieve the list of all possible specialties.
|
||||
final List<Specialty> specialties = this.jdbcTemplate.query(
|
||||
"SELECT id, name FROM specialties",
|
||||
BeanPropertyRowMapper.newInstance(Specialty.class));
|
||||
"SELECT id, name FROM specialties",
|
||||
BeanPropertyRowMapper.newInstance(Specialty.class));
|
||||
|
||||
// Build each vet's list of specialties.
|
||||
for (Vet vet : vets) {
|
||||
final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
|
||||
"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
|
||||
new BeanPropertyRowMapper<Integer>() {
|
||||
@Override
|
||||
public Integer mapRow(ResultSet rs, int row) throws SQLException {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
},
|
||||
vet.getId());
|
||||
"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
|
||||
new BeanPropertyRowMapper<Integer>() {
|
||||
@Override
|
||||
public Integer mapRow(ResultSet rs, int row) throws SQLException {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
},
|
||||
vet.getId());
|
||||
for (int specialtyId : vetSpecialtiesIds) {
|
||||
Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
|
||||
vet.addSpecialty(specialty);
|
||||
|
|
|
@ -50,39 +50,37 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
|
|||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
|
||||
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
||||
.withTableName("visits")
|
||||
.usingGeneratedKeyColumns("id");
|
||||
.withTableName("visits")
|
||||
.usingGeneratedKeyColumns("id");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Visit visit) throws DataAccessException {
|
||||
if (visit.isNew()) {
|
||||
Number newKey = this.insertVisit.executeAndReturnKey(
|
||||
createVisitParameterSource(visit));
|
||||
createVisitParameterSource(visit));
|
||||
visit.setId(newKey.intValue());
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Visit update not supported");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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());
|
||||
.addValue("id", visit.getId())
|
||||
.addValue("visit_date", visit.getDate().toDate())
|
||||
.addValue("description", visit.getDescription())
|
||||
.addValue("pet_id", visit.getPet().getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Visit> findByPetId(Integer petId) {
|
||||
return this.jdbcTemplate.query(
|
||||
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
|
||||
new JdbcVisitRowMapper(), petId);
|
||||
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
|
||||
new JdbcVisitRowMapper(), petId);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.repository.jdbc;
|
||||
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.samples.petclinic.model.Visit;
|
||||
|
|
|
@ -41,9 +41,8 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
|
|||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
|
||||
/**
|
||||
* Important: in the current version of this method, we load Owners with all their Pets and Visits while
|
||||
* Important: in the current version of this method, we load Owners with all their Pets and Visits while
|
||||
* we do not need Visits at all and we only need one property from the Pet objects (the 'name' property).
|
||||
* There are some ways to improve it such as:
|
||||
* - creating a Ligtweight class (example here: https://community.jboss.org/wiki/LightweightClass)
|
||||
|
@ -67,15 +66,14 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
|
|||
return (Owner) query.getSingleResult();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Owner owner) {
|
||||
if (owner.getId() == null) {
|
||||
this.em.persist(owner);
|
||||
}
|
||||
else {
|
||||
this.em.merge(owner);
|
||||
}
|
||||
if (owner.getId() == null) {
|
||||
this.em.persist(owner);
|
||||
}
|
||||
else {
|
||||
this.em.merge(owner);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -53,12 +53,12 @@ public class JpaPetRepositoryImpl implements PetRepository {
|
|||
|
||||
@Override
|
||||
public void save(Pet pet) {
|
||||
if (pet.getId() == null) {
|
||||
this.em.persist(pet);
|
||||
}
|
||||
else {
|
||||
this.em.merge(pet);
|
||||
}
|
||||
if (pet.getId() == null) {
|
||||
this.em.persist(pet);
|
||||
}
|
||||
else {
|
||||
this.em.merge(pet);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ public class JpaVetRepositoryImpl implements VetRepository {
|
|||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
|
||||
@Override
|
||||
@Cacheable(value = "vets")
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
|
@ -42,18 +42,16 @@ public class JpaVisitRepositoryImpl implements VisitRepository {
|
|||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Visit visit) {
|
||||
if (visit.getId() == null) {
|
||||
this.em.persist(visit);
|
||||
}
|
||||
else {
|
||||
this.em.merge(visit);
|
||||
}
|
||||
if (visit.getId() == null) {
|
||||
this.em.persist(visit);
|
||||
}
|
||||
else {
|
||||
this.em.merge(visit);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Visit> findByPetId(Integer petId) {
|
||||
|
|
|
@ -30,12 +30,12 @@ import org.springframework.samples.petclinic.repository.OwnerRepository;
|
|||
* @since 15.1.2013
|
||||
*/
|
||||
public interface SpringDataOwnerRepository extends OwnerRepository, Repository<Owner, Integer> {
|
||||
|
||||
@Override
|
||||
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
||||
public Collection<Owner> findByLastName(@Param("lastName") String lastName);
|
||||
|
||||
@Override
|
||||
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
||||
public Owner findById(@Param("id") int id);
|
||||
|
||||
@Override
|
||||
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
||||
public Collection<Owner> findByLastName(@Param("lastName") String lastName);
|
||||
|
||||
@Override
|
||||
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
||||
public Owner findById(@Param("id") int id);
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ import org.springframework.samples.petclinic.model.PetType;
|
|||
import org.springframework.samples.petclinic.model.Vet;
|
||||
import org.springframework.samples.petclinic.model.Visit;
|
||||
|
||||
|
||||
/**
|
||||
* Mostly used as a facade so all controllers have a single point of entry
|
||||
*
|
||||
|
|
|
@ -78,14 +78,12 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
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 {
|
||||
|
@ -105,5 +103,4 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
return vetRepository.findAll();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -42,10 +42,9 @@ public class CallMonitoringAspect {
|
|||
|
||||
private long accumulatedCallTime = 0;
|
||||
|
||||
|
||||
@ManagedAttribute
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
|
@ -66,13 +65,12 @@ public class CallMonitoringAspect {
|
|||
|
||||
@ManagedAttribute
|
||||
public long getCallTime() {
|
||||
if (this.callCount > 0)
|
||||
return this.accumulatedCallTime / this.callCount;
|
||||
else
|
||||
return 0;
|
||||
if (this.callCount > 0)
|
||||
return this.accumulatedCallTime / this.callCount;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@Around("within(@org.springframework.stereotype.Repository *)")
|
||||
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
if (this.enabled) {
|
||||
|
|
|
@ -43,7 +43,7 @@ public abstract class EntityUtils {
|
|||
* if the entity was not found
|
||||
*/
|
||||
public static <T extends BaseEntity> T getById(Collection<T> entities, Class<T> entityClass, int entityId)
|
||||
throws ObjectRetrievalFailureException {
|
||||
throws ObjectRetrievalFailureException {
|
||||
for (T entity : entities) {
|
||||
if (entity.getId() == entityId && entityClass.isInstance(entity)) {
|
||||
return entity;
|
||||
|
|
|
@ -33,8 +33,7 @@ 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");
|
||||
"happens when an exception is thrown");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -47,7 +47,6 @@ public class OwnerController {
|
|||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public OwnerController(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
|
@ -98,17 +97,16 @@ public class OwnerController {
|
|||
return "owners/findOwners";
|
||||
}
|
||||
else if (results.size() == 1) {
|
||||
// 1 owner found
|
||||
owner = results.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
else {
|
||||
// 1 owner found
|
||||
owner = results.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
} else {
|
||||
// multiple owners found
|
||||
model.put("selections", results);
|
||||
return "owners/ownersList";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.GET)
|
||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||
|
|
|
@ -47,7 +47,6 @@ public class PetController {
|
|||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public PetController(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
|
@ -41,7 +40,6 @@ public class PetTypeFormatter implements Formatter<PetType> {
|
|||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public PetTypeFormatter(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
|
|
|
@ -41,12 +41,12 @@ public class PetValidator implements Validator {
|
|||
} else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) {
|
||||
errors.rejectValue("name", "duplicate", "already exists");
|
||||
}
|
||||
|
||||
|
||||
// type validation
|
||||
if (pet.isNew() && pet.getType() == null) {
|
||||
errors.rejectValue("type", "required", "required");
|
||||
}
|
||||
|
||||
|
||||
// birth date validation
|
||||
if (pet.getBirthDate()==null) {
|
||||
errors.rejectValue("birthDate", "required", "required");
|
||||
|
@ -61,5 +61,4 @@ public class PetValidator implements Validator {
|
|||
return Pet.class.equals(clazz);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -35,7 +35,6 @@ public class VetController {
|
|||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public VetController(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
|
@ -50,7 +49,7 @@ public class VetController {
|
|||
model.put("vets", vets);
|
||||
return "vets/vetList";
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/vets.json")
|
||||
public @ResponseBody Vets showResourcesVetList() {
|
||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet objects
|
||||
|
@ -60,5 +59,4 @@ public class VetController {
|
|||
return vets;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -43,7 +43,6 @@ public class VisitController {
|
|||
|
||||
private final ClinicService clinicService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public VisitController(ClinicService clinicService) {
|
||||
this.clinicService = clinicService;
|
||||
|
@ -53,12 +52,12 @@ public class VisitController {
|
|||
public void setAllowedFields(WebDataBinder dataBinder) {
|
||||
dataBinder.setDisallowedFields("id");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called before each and every @RequestMapping annotated method.
|
||||
* 2 goals:
|
||||
* - Make sure we always have fresh data
|
||||
* - Since we do not use the session scope, make sure that Pet object always has an id
|
||||
* - Since we do not use the session scope, make sure that Pet object always has an id
|
||||
* (Even though id is not part of the form fields)
|
||||
* @param petId
|
||||
* @return Pet
|
||||
|
@ -67,17 +66,17 @@ public class VisitController {
|
|||
public Visit loadPetWithVisit(@PathVariable("petId") int petId) {
|
||||
Pet pet = this.clinicService.findPetById(petId);
|
||||
Visit visit = new Visit();
|
||||
pet.addVisit(visit);
|
||||
pet.addVisit(visit);
|
||||
return visit;
|
||||
}
|
||||
|
||||
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||
// Spring MVC calls method loadPetWithVisit(...) before initNewVisitForm is called
|
||||
@RequestMapping(value = "/owners/*/pets/{petId}/visits/new", method = RequestMethod.GET)
|
||||
public String initNewVisitForm(@PathVariable("petId") int petId, Map<String, Object> model) {
|
||||
return "pets/createOrUpdateVisitForm";
|
||||
}
|
||||
|
||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||
// Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called
|
||||
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST)
|
||||
public String processNewVisitForm(@Valid Visit visit, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
|
|
Loading…
Reference in a new issue