Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

WORKSHOP - PR from Tosin Akinosho #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/
@Entity
@Table(name = "pets")
public class Pet extends NamedEntity {
public class MyPet extends NamedEntity {

@Column(name = "birth_date")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
Expand All @@ -61,7 +61,7 @@ public class Pet extends NamedEntity {
private Owner owner;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER)
private Set<Visit> visits;
private Set<Visit> visitsInternal;


public void setBirthDate(DateTime birthDate) {
Expand Down Expand Up @@ -89,14 +89,14 @@ public Owner getOwner() {
}

protected void setVisitsInternal(Set<Visit> visits) {
this.visits = visits;
this.visitsInternal = visits;
}

protected Set<Visit> getVisitsInternal() {
if (this.visits == null) {
this.visits = new HashSet<Visit>();
if (this.visitsInternal == null) {
this.visitsInternal = new HashSet<Visit>();
}
return this.visits;
return this.visitsInternal;
}

public List<Visit> getVisits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class Owner extends Person {
private String telephone;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private Set<Pet> pets;
private Set<MyPet> pets;


public String getAddress() {
Expand All @@ -85,24 +85,25 @@ public void setTelephone(String telephone) {
this.telephone = telephone;
}

protected void setPetsInternal(Set<Pet> pets) {
protected void setPetsInternal(Set<MyPet> pets) {
this.pets = pets;
}

protected Set<Pet> getPetsInternal() {
protected Set<MyPet> getPetsInternal() {
if (this.pets == null) {
this.pets = new HashSet<Pet>();
this.pets = new HashSet<MyPet>();
}
return this.pets;
}

public List<Pet> getPets() {
List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal());
public List<MyPet> getPets() {
List<MyPet> sortedPets = new ArrayList<MyPet>(getPetsInternal());
System.out.println(sortedPets);
PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedPets);
}

public void addPet(Pet pet) {
public void addPet(MyPet pet) {
getPetsInternal().add(pet);
pet.setOwner(this);
}
Expand All @@ -113,7 +114,7 @@ public void addPet(Pet pet) {
* @param name to test
* @return true if pet name is already in use
*/
public Pet getPet(String name) {
public MyPet getPet(String name) {
return getPet(name, false);
}

Expand All @@ -123,9 +124,9 @@ public Pet getPet(String name) {
* @param name to test
* @return true if pet name is already in use
*/
public Pet getPet(String name, boolean ignoreNew) {
public MyPet getPet(String name, boolean ignoreNew) {
name = name.toLowerCase();
for (Pet pet : getPetsInternal()) {
for (MyPet pet : getPetsInternal()) {
if (!ignoreNew || !pet.isNew()) {
String compName = pet.getName();
compName = compName.toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Visit extends BaseEntity {
*/
@ManyToOne
@JoinColumn(name = "pet_id")
private Pet pet;
private MyPet pet;


/**
Expand Down Expand Up @@ -107,7 +107,7 @@ public void setDescription(String description) {
*
* @return Value of property pet.
*/
public Pet getPet() {
public MyPet getPet() {
return this.pet;
}

Expand All @@ -116,7 +116,7 @@ public Pet getPet() {
*
* @param pet New value of property pet.
*/
public void setPet(Pet pet) {
public void setPet(MyPet pet) {
this.pet = pet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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.MyPet;
import org.springframework.samples.petclinic.model.PetType;

/**
Expand Down Expand Up @@ -48,14 +48,14 @@ public interface PetRepository {
* @throws org.springframework.dao.DataRetrievalFailureException
* if not found
*/
Pet findById(int id) throws DataAccessException;
MyPet findById(int id) throws DataAccessException;

/**
* Save a <code>Pet</code> to the data store, either inserting or updating it.
*
* @param pet the <code>Pet</code> to save
* @see BaseEntity#isNew
*/
void save(Pet pet) throws DataAccessException;
void save(MyPet pet) throws DataAccessException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
Expand Down Expand Up @@ -74,7 +74,7 @@ public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate

/**
* 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
* the given name; also loads the {@link MyPet Pets} and {@link Visit Visits} for the corresponding owners, if not
* already loaded.
*/
@Override
Expand All @@ -91,7 +91,7 @@ public Collection<Owner> findByLastName(String lastName) throws DataAccessExcept
}

/**
* Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
* Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link MyPet Pets} and {@link Visit Visits}
* for the corresponding owner, if not already loaded.
*/
@Override
Expand Down Expand Up @@ -151,7 +151,7 @@ public Collection<PetType> getPetTypes() throws DataAccessException {
}

/**
* Loads the {@link Pet} and {@link Visit} data for the supplied {@link List} of {@link Owner Owners}.
* Loads the {@link MyPet} 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package org.springframework.samples.petclinic.repository.jdbc;

import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.MyPet;

/**
* Subclass of Pet that carries temporary id properties which are only relevant for a JDBC implementation of the
* ClinicService.
*
* @author Juergen Hoeller
*/
class JdbcPet extends Pet {
class JdbcPet extends MyPet {

private int typeId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.OwnerRepository;
Expand Down Expand Up @@ -81,7 +81,7 @@ public List<PetType> findPetTypes() throws DataAccessException {
}

@Override
public Pet findById(int id) throws DataAccessException {
public MyPet findById(int id) throws DataAccessException {
JdbcPet pet;
try {
Map<String, Object> params = new HashMap<String, Object>();
Expand All @@ -91,7 +91,7 @@ public Pet findById(int id) throws DataAccessException {
params,
new JdbcPetRowMapper());
} catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
throw new ObjectRetrievalFailureException(MyPet.class, new Integer(id));
}
Owner owner = this.ownerRepository.findById(pet.getOwnerId());
owner.addPet(pet);
Expand All @@ -105,7 +105,7 @@ public Pet findById(int id) throws DataAccessException {
}

@Override
public void save(Pet pet) throws DataAccessException {
public void save(MyPet pet) throws DataAccessException {
if (pet.isNew()) {
Number newKey = this.insertPet.executeAndReturnKey(
createPetParameterSource(pet));
Expand All @@ -119,9 +119,9 @@ public void save(Pet pet) throws DataAccessException {
}

/**
* Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} instance.
* Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link MyPet} instance.
*/
private MapSqlParameterSource createPetParameterSource(Pet pet) {
private MapSqlParameterSource createPetParameterSource(MyPet pet) {
return new MapSqlParameterSource()
.addValue("id", pet.getId())
.addValue("name", pet.getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -47,12 +47,12 @@ public List<PetType> findPetTypes() {
}

@Override
public Pet findById(int id) {
return this.em.find(Pet.class, id);
public MyPet findById(int id) {
return this.em.find(MyPet.class, id);
}

@Override
public void save(Pet pet) {
public void save(MyPet pet) {
if (pet.getId() == null) {
this.em.persist(pet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.springframework.dao.DataAccessException;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.repository.PetRepository;

Expand All @@ -30,7 +30,7 @@
* @author Michael Isvy
* @since 15.1.2013
*/
public interface SpringDataPetRepository extends PetRepository, Repository<Pet, Integer> {
public interface SpringDataPetRepository extends PetRepository, Repository<MyPet, Integer> {

@Override
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

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.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Visit;
Expand All @@ -36,9 +36,9 @@ public interface ClinicService {

Owner findOwnerById(int id) throws DataAccessException;

Pet findPetById(int id) throws DataAccessException;
MyPet findPetById(int id) throws DataAccessException;

void savePet(Pet pet) throws DataAccessException;
void savePet(MyPet pet) throws DataAccessException;

void saveVisit(Visit visit) throws DataAccessException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.springframework.cache.annotation.Cacheable;
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.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.model.Visit;
Expand Down Expand Up @@ -88,13 +88,13 @@ public void saveVisit(Visit visit) throws DataAccessException {

@Override
@Transactional(readOnly = true)
public Pet findPetById(int id) throws DataAccessException {
public MyPet findPetById(int id) throws DataAccessException {
return petRepository.findById(id);
}

@Override
@Transactional
public void savePet(Pet pet) throws DataAccessException {
public void savePet(MyPet pet) throws DataAccessException {
petRepository.save(pet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.MyPet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -64,14 +64,14 @@ public void setAllowedFields(WebDataBinder dataBinder) {
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET)
public String initCreationForm(@PathVariable("ownerId") int ownerId, Map<String, Object> model) {
Owner owner = this.clinicService.findOwnerById(ownerId);
Pet pet = new Pet();
MyPet pet = new MyPet();
owner.addPet(pet);
model.put("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) {
public String processCreationForm(@ModelAttribute("pet") MyPet pet, BindingResult result, SessionStatus status) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) {
return "pets/createOrUpdatePetForm";
Expand All @@ -84,13 +84,13 @@ public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult

@RequestMapping(value = "/owners/*/pets/{petId}/edit", method = RequestMethod.GET)
public String initUpdateForm(@PathVariable("petId") int petId, Map<String, Object> model) {
Pet pet = this.clinicService.findPetById(petId);
MyPet pet = this.clinicService.findPetById(petId);
model.put("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) {
public String processUpdateForm(@ModelAttribute("pet") MyPet 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()) {
Expand Down
Loading