This commit is contained in:
Samuel Teixeira 2014-06-02 00:42:05 +00:00
commit 151c1c430a
11 changed files with 195 additions and 1 deletions

View file

@ -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;
}

View file

@ -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 of<code>Pet</code> from the data store by name.
*
* @param name the name to search for
* @return the <code>Pet</code> if found
* @throws org.springframework.dao.DataRetrievalFailureException
* if not found
*/
Collection<Pet> findByName(String name) throws DataAccessException;
/**
* Save a <code>Pet</code> to the data store, either inserting or updating it.
*

View file

@ -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<Pet> findByName(String name) throws DataAccessException {
Map<String, Object> params = new HashMap<String, Object>();
params.put("name", "%" + name + "%");
List<Pet> 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;
}
}

View file

@ -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<Pet> 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<Pet> pets = new ArrayList<Pet>();
@SuppressWarnings("unchecked")
List<Object[]> 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;
}
}

View file

@ -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<Pet,
@Override
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
List<PetType> findPetTypes() throws DataAccessException;
@Override
@Query("SELECT id, name, birthDate, type, owner FROM Pet WHERE name like :name")
public List<Pet> findByName(@Param("name") String name);
}

View file

@ -37,6 +37,8 @@ public interface ClinicService {
Owner findOwnerById(int id) throws DataAccessException;
Pet findPetById(int id) throws DataAccessException;
Collection<Pet> findPetByName(String name) throws DataAccessException;
void savePet(Pet pet) throws DataAccessException;

View file

@ -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<Pet> findPetByName(String name) throws DataAccessException {
return petRepository.findByName(name);
}
@Override
@Transactional(readOnly = true)

View file

@ -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<String, Object> model) {
model.put("pet", new Pet());
return "pets/findPets";
}
@RequestMapping(value = "/pets", method = RequestMethod.GET)
public String searchPets(Pet pet, BindingResult result, Map<String, Object> model) {
if (pet.getName() == null) {
pet.setName(""); // empty string signifies broadest possible search
}
Collection<Pet> 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) {

View file

@ -11,6 +11,8 @@
Home</a></li>
<li style="width: 130px;"><a href="<spring:url value="/owners/find.html" htmlEscape="true" />"><i
class="icon-search"></i> Find owners</a></li>
<li style="width: 130px;"><a href="<spring:url value="/pets/find.html" htmlEscape="true" />"><i
class="icon-search"></i> Find Pets</a></li>
<li style="width: 140px;"><a href="<spring:url value="/vets.html" htmlEscape="true" />"><i
class="icon-th-list"></i> Veterinarians</a></li>
<li style="width: 90px;"><a href="<spring:url value="/oups.html" htmlEscape="true" />"

View file

@ -0,0 +1,41 @@
<!DOCTYPE html>
<%@ 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" %>
<html lang="en">
<jsp:include page="../fragments/headTag.jsp"/>
<body>
<div class="container">
<jsp:include page="../fragments/bodyHeader.jsp"/>
<h2>Find Pets</h2>
<spring:url value="/pets.html" var="formUrl"/>
<form:form modelAttribute="pet" action="${fn:escapeXml(formUrl)}" method="get" class="form-horizontal"
id="search-pet-form">
<fieldset>
<div class="control-group" id="lastName">
<label class="control-label">Name </label>
<form:input path="name" size="30" maxlength="80"/>
<span class="help-inline"><form:errors path="*"/></span>
</div>
<div class="form-actions">
<button type="submit">Find Pet</button>
</div>
</fieldset>
</form:form>
<br/>
<jsp:include page="../fragments/footer.jsp"/>
</div>
</body>
</html>

View file

@ -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" %>
<html lang="en">
<jsp:include page="../fragments/headTag.jsp"/>
<body>
<div class="container">
<jsp:include page="../fragments/bodyHeader.jsp"/>
<h2>Pets</h2>
<datatables:table id="pets" data="${selections}" cdn="true" row="pet" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" export="pdf">
<datatables:column title="Name" cssStyle="width: 150px;" display="html">
<spring:url value="/owners/{ownerId}/pets/{petId}/edit" var="petUrl">
<spring:param name="ownerId" value="${pet.owner.id}"/>
<spring:param name="petId" value="${pet.id}"/>
</spring:url>
<a href="${fn:escapeXml(petUrl)}"><c:out value="${pet.name}"/></a>
</datatables:column>
<datatables:column title="Name" display="pdf">
<c:out value="${pet.name}"/>
</datatables:column>
<datatables:column title="Birth Date">
<joda:format value="${pet.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="type" property="type.name"/>
<spring:url value="/owners/{ownerId}.html" var="ownerUrl">
<spring:param name="ownerId" value="${pet.owner.id}"/>
</spring:url>
<datatables:column title="owner" display="html">
<a href="${fn:escapeXml(ownerUrl)}"><c:out value="${pet.owner.firstName} ${pet.owner.lastName}"/></a>
</datatables:column>
<datatables:column title="owner" display="pdf">
<c:out value="${pet.owner.firstName} ${pet.owner.lastName}"/>
</datatables:column>
<datatables:export type="pdf" cssClass="btn btn-small" />
</datatables:table>
<jsp:include page="../fragments/footer.jsp"/>
</div>
</body>
</html>