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 4bc2b92f7..ebd231c88 100644 --- a/src/main/java/org/springframework/samples/petclinic/model/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/model/Pet.java @@ -80,7 +80,7 @@ public class Pet extends NamedEntity { return this.type; } - protected void setOwner(Owner owner) { + public void setOwner(Owner owner) { this.owner = owner; } 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 693b2e5e9..b74a9597c 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/PetRepository.java @@ -15,6 +15,7 @@ */ package org.springframework.samples.petclinic.repository; +import java.util.Collection; import java.util.List; import org.springframework.dao.DataAccessException; @@ -50,6 +51,16 @@ public interface PetRepository { */ Pet findById(int id) throws DataAccessException; + /** + * Retrieve a list ofPet from the data store by name. + * + * @param name the name to search for + * @return the Pet if found + * @throws org.springframework.dao.DataRetrievalFailureException + * if not found + */ + Collection findByName(String name) throws DataAccessException; + /** * Save a Pet to the data store, either inserting or updating it. * 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 c594ead1b..5bfc7999f 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,6 +15,7 @@ */ package org.springframework.samples.petclinic.repository.jdbc; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -129,5 +130,24 @@ public class JdbcPetRepositoryImpl implements PetRepository { .addValue("type_id", pet.getType().getId()) .addValue("owner_id", pet.getOwner().getId()); } + + @Override + public Collection findByName(String name) throws DataAccessException { + + Map params = new HashMap(); + params.put("name", "%" + name + "%"); + List pets = this.namedParameterJdbcTemplate.query( + "SELECT id, birth_date, type_id, owner_id FROM pets WHERE name like :name", + params, + ParameterizedBeanPropertyRowMapper.newInstance(Pet.class) + ); + for (Pet pet : pets) { + if(pet.getName().equals(null)){ + new Exception(); + } + } + return pets; + + } } 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 84d564da4..56c1aede7 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,11 +15,16 @@ */ package org.springframework.samples.petclinic.repository.jpa; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import org.joda.time.DateTime; +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.repository.PetRepository; @@ -60,5 +65,25 @@ public class JpaPetRepositoryImpl implements PetRepository { this.em.merge(pet); } } + + @Override + public Collection findByName(String name) { + Query query = this.em.createQuery("SELECT pet.id, pet.name,pet.birthDate,pet.type,pet.owner FROM Pet pet inner join pet.owner as owner WHERE pet.name like :name"); + query.setParameter("name", "%"+ name + "%"); + Collection pets = new ArrayList(); + @SuppressWarnings("unchecked") + List resultList = query.getResultList(); + for (Object[] obj : resultList) { + Pet pet = new Pet(); + pet.setId((Integer)obj[0]); + pet.setName((String)obj[1]); + pet.setBirthDate((DateTime)obj[2]); + pet.setType((PetType)obj[3]); + pet.setOwner((Owner)obj[4]); + pets.add(pet); + } + return pets; + + } } 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 56a413147..928be848a 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 @@ -20,6 +20,7 @@ import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.repository.PetRepository; @@ -35,4 +36,8 @@ public interface SpringDataPetRepository extends PetRepository, Repository findPetTypes() throws DataAccessException; + + @Override + @Query("SELECT id, name, birthDate, type, owner FROM Pet WHERE name like :name") + public List findByName(@Param("name") String name); } 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 e72b5c10f..b16faed09 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicService.java @@ -37,6 +37,8 @@ public interface ClinicService { Owner findOwnerById(int id) throws DataAccessException; Pet findPetById(int id) throws DataAccessException; + + Collection findPetByName(String name) throws DataAccessException; void savePet(Pet pet) 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 0d7ff4d0c..1b5446507 100644 --- a/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/service/ClinicServiceImpl.java @@ -65,6 +65,12 @@ public class ClinicServiceImpl implements ClinicService { public Owner findOwnerById(int id) throws DataAccessException { return ownerRepository.findById(id); } + + @Override + @Transactional(readOnly = true) + public Collection findPetByName(String name) throws DataAccessException { + return petRepository.findByName(name); + } @Override @Transactional(readOnly = true) 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 ea8aeaaa8..a7ede5a53 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -69,6 +69,35 @@ public class PetController { model.put("pet", pet); return "pets/createOrUpdatePetForm"; } + + @RequestMapping(value = "/pets/find", method = RequestMethod.GET) + public String initFindForm(Map model) { + model.put("pet", new Pet()); + return "pets/findPets"; + } + + @RequestMapping(value = "/pets", method = RequestMethod.GET) + public String searchPets(Pet pet, BindingResult result, Map model) { + if (pet.getName() == null) { + pet.setName(""); // empty string signifies broadest possible search + } + + + Collection results = this.clinicService.findPetByName(pet.getName()); + + if (results.size() < 1) { + // no pets found + result.rejectValue("name", "notFound", "not found"); + return "pets/findPets"; + } + if (results.size() >= 1) { + // multiple pets found + model.put("selections", results); + return "pets/petsList"; + } + + return "pets/findPets"; + } @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST) public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { diff --git a/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp b/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp index 76c184417..3a4fae1bc 100644 --- a/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp +++ b/src/main/webapp/WEB-INF/jsp/fragments/bodyHeader.jsp @@ -11,6 +11,8 @@ Home
  • "> Find owners
  • +
  • "> Find Pets
  • "> Veterinarians
  • " diff --git a/src/main/webapp/WEB-INF/jsp/pets/findPets.jsp b/src/main/webapp/WEB-INF/jsp/pets/findPets.jsp new file mode 100644 index 000000000..9089c54d6 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/pets/findPets.jsp @@ -0,0 +1,41 @@ + + +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> + + + + + + +
    + + +

    Find Pets

    + + + +
    +
    + + + +
    +
    + +
    +
    +
    + +
    + + + + +
    + + + diff --git a/src/main/webapp/WEB-INF/jsp/pets/petsList.jsp b/src/main/webapp/WEB-INF/jsp/pets/petsList.jsp new file mode 100644 index 000000000..505abc172 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/pets/petsList.jsp @@ -0,0 +1,53 @@ +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %> +<%@ taglib prefix="datatables" uri="http://github.com/dandelion/datatables" %> + + + + + + +
    + +

    Pets

    + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + +
    + + + \ No newline at end of file