From 03b0759065790e3260c898ebdf3d0b2ff5ceb717 Mon Sep 17 00:00:00 2001 From: Sasank Vishnubhatla Date: Wed, 7 Jun 2017 12:51:51 -0400 Subject: [PATCH] full refactoring --- ...ication.java => KidClinicApplication.java} | 6 +- .../{vet/Vet.java => doctor/Doctor.java} | 8 +- .../DoctorController.java} | 36 ++--- .../DoctorRepository.java} | 14 +- .../{vet/Vets.java => doctor/Doctors.java} | 16 +-- .../kidclinic/{vet => doctor}/Specialty.java | 4 +- .../kidclinic/owner/OwnerController.java | 136 ------------------ .../kidclinic/owner/PetController.java | 116 --------------- .../{owner/Pet.java => parent/Kid.java} | 34 ++--- .../kidclinic/parent/KidController.java | 116 +++++++++++++++ .../PetType.java => parent/KidGender.java} | 6 +- .../KidGenderFormatter.java} | 28 ++-- .../KidRepository.java} | 26 ++-- .../KidValidator.java} | 22 +-- .../{owner/Owner.java => parent/Parent.java} | 62 ++++---- .../kidclinic/parent/ParentController.java | 136 ++++++++++++++++++ .../ParentRepository.java} | 28 ++-- .../{owner => parent}/VisitController.java | 38 ++--- .../samples/kidclinic/system/CacheConfig.java | 2 +- .../kidclinic/system/CrashController.java | 2 +- .../samples/kidclinic/visit/Visit.java | 20 +-- .../kidclinic/visit/VisitRepository.java | 2 +- src/main/resources/db/hsqldb/data.sql | 76 +++++----- src/main/resources/db/hsqldb/schema.sql | 46 +++--- .../templates/doctors/doctorList.html | 28 ++++ src/main/resources/templates/error.html | 4 +- .../resources/templates/fragments/layout.html | 10 +- .../createOrUpdateKidForm.html} | 16 +-- .../createOrUpdateVisitForm.html | 18 +-- .../createOrUpdateParentForm.html} | 6 +- .../findParents.html} | 8 +- .../parentDetails.html} | 22 +-- .../parentsList.html} | 16 +-- src/main/resources/templates/reviews.html | 10 ++ .../resources/templates/vets/vetList.html | 28 ---- 35 files changed, 579 insertions(+), 567 deletions(-) rename src/main/java/org/springframework/samples/kidclinic/{PetClinicApplication.java => KidClinicApplication.java} (86%) rename src/main/java/org/springframework/samples/kidclinic/{vet/Vet.java => doctor/Doctor.java} (89%) rename src/main/java/org/springframework/samples/kidclinic/{vet/VetController.java => doctor/DoctorController.java} (54%) rename src/main/java/org/springframework/samples/kidclinic/{vet/VetRepository.java => doctor/DoctorRepository.java} (73%) rename src/main/java/org/springframework/samples/kidclinic/{vet/Vets.java => doctor/Doctors.java} (72%) rename src/main/java/org/springframework/samples/kidclinic/{vet => doctor}/Specialty.java (88%) delete mode 100644 src/main/java/org/springframework/samples/kidclinic/owner/OwnerController.java delete mode 100644 src/main/java/org/springframework/samples/kidclinic/owner/PetController.java rename src/main/java/org/springframework/samples/kidclinic/{owner/Pet.java => parent/Kid.java} (81%) create mode 100644 src/main/java/org/springframework/samples/kidclinic/parent/KidController.java rename src/main/java/org/springframework/samples/kidclinic/{owner/PetType.java => parent/KidGender.java} (87%) rename src/main/java/org/springframework/samples/kidclinic/{owner/PetTypeFormatter.java => parent/KidGenderFormatter.java} (67%) rename src/main/java/org/springframework/samples/kidclinic/{owner/PetRepository.java => parent/KidRepository.java} (67%) rename src/main/java/org/springframework/samples/kidclinic/{owner/PetValidator.java => parent/KidValidator.java} (73%) rename src/main/java/org/springframework/samples/kidclinic/{owner/Owner.java => parent/Parent.java} (67%) create mode 100644 src/main/java/org/springframework/samples/kidclinic/parent/ParentController.java rename src/main/java/org/springframework/samples/kidclinic/{owner/OwnerRepository.java => parent/ParentRepository.java} (59%) rename src/main/java/org/springframework/samples/kidclinic/{owner => parent}/VisitController.java (68%) create mode 100644 src/main/resources/templates/doctors/doctorList.html rename src/main/resources/templates/{pets/createOrUpdatePetForm.html => kids/createOrUpdateKidForm.html} (64%) rename src/main/resources/templates/{pets => kids}/createOrUpdateVisitForm.html (71%) rename src/main/resources/templates/{owners/createOrUpdateOwnerForm.html => parents/createOrUpdateParentForm.html} (78%) rename src/main/resources/templates/{owners/findOwners.html => parents/findParents.html} (75%) rename src/main/resources/templates/{owners/ownerDetails.html => parents/parentDetails.html} (66%) rename src/main/resources/templates/{owners/ownersList.html => parents/parentsList.html} (50%) create mode 100644 src/main/resources/templates/reviews.html delete mode 100644 src/main/resources/templates/vets/vetList.html diff --git a/src/main/java/org/springframework/samples/kidclinic/PetClinicApplication.java b/src/main/java/org/springframework/samples/kidclinic/KidClinicApplication.java similarity index 86% rename from src/main/java/org/springframework/samples/kidclinic/PetClinicApplication.java rename to src/main/java/org/springframework/samples/kidclinic/KidClinicApplication.java index f15ab087b..6541ec2d9 100644 --- a/src/main/java/org/springframework/samples/kidclinic/PetClinicApplication.java +++ b/src/main/java/org/springframework/samples/kidclinic/KidClinicApplication.java @@ -20,16 +20,16 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** - * PetClinic Spring Boot Application. + * KidClinic Spring Boot Application. * * @author Dave Syer * */ @SpringBootApplication -public class PetClinicApplication { +public class KidClinicApplication { public static void main(String[] args) throws Exception { - SpringApplication.run(PetClinicApplication.class, args); + SpringApplication.run(KidClinicApplication.class, args); } } diff --git a/src/main/java/org/springframework/samples/kidclinic/vet/Vet.java b/src/main/java/org/springframework/samples/kidclinic/doctor/Doctor.java similarity index 89% rename from src/main/java/org/springframework/samples/kidclinic/vet/Vet.java rename to src/main/java/org/springframework/samples/kidclinic/doctor/Doctor.java index 26b459021..2965f9505 100644 --- a/src/main/java/org/springframework/samples/kidclinic/vet/Vet.java +++ b/src/main/java/org/springframework/samples/kidclinic/doctor/Doctor.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.vet; +package org.springframework.samples.kidclinic.doctor; import java.util.ArrayList; import java.util.Collections; @@ -42,11 +42,11 @@ import org.springframework.samples.kidclinic.model.Person; * @author Arjen Poutsma */ @Entity -@Table(name = "vets") -public class Vet extends Person { +@Table(name = "doctors") +public class Doctor extends Person { @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) + @JoinTable(name = "doctor_specialties", joinColumns = @JoinColumn(name = "doctor_id"), inverseJoinColumns = @JoinColumn(name = "specialty_id")) private Set specialties; protected Set getSpecialtiesInternal() { diff --git a/src/main/java/org/springframework/samples/kidclinic/vet/VetController.java b/src/main/java/org/springframework/samples/kidclinic/doctor/DoctorController.java similarity index 54% rename from src/main/java/org/springframework/samples/kidclinic/vet/VetController.java rename to src/main/java/org/springframework/samples/kidclinic/doctor/DoctorController.java index 4ee84da8a..7fa5adb6a 100644 --- a/src/main/java/org/springframework/samples/kidclinic/vet/VetController.java +++ b/src/main/java/org/springframework/samples/kidclinic/doctor/DoctorController.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.vet; +package org.springframework.samples.kidclinic.doctor; import java.util.Map; @@ -29,32 +29,32 @@ import org.springframework.web.bind.annotation.ResponseBody; * @author Arjen Poutsma */ @Controller -class VetController { +class DoctorController { - private final VetRepository vets; + private final DoctorRepository doctors; @Autowired - public VetController(VetRepository clinicService) { - this.vets = clinicService; + public DoctorController(DoctorRepository clinicService) { + this.doctors = clinicService; } - @RequestMapping(value = { "/vets.html" }) - public String showVetList(Map model) { - // Here we are returning an object of type 'Vets' rather than a collection of Vet + @RequestMapping(value = { "/doctors.html" }) + public String showDoctorList(Map model) { + // Here we are returning an object of type 'Doctors' rather than a collection of Doctor // objects so it is simpler for Object-Xml mapping - Vets vets = new Vets(); - vets.getVetList().addAll(this.vets.findAll()); - model.put("vets", vets); - return "vets/vetList"; + Doctors doctors = new Doctors(); + doctors.getDoctorList().addAll(this.doctors.findAll()); + model.put("doctors", doctors); + return "doctors/doctorList"; } - @RequestMapping(value = { "/vets.json", "/vets.xml" }) - public @ResponseBody Vets showResourcesVetList() { - // Here we are returning an object of type 'Vets' rather than a collection of Vet + @RequestMapping(value = { "/doctors.json", "/doctors.xml" }) + public @ResponseBody Doctors showResourcesVetList() { + // Here we are returning an object of type 'Doctors' rather than a collection of Doctor // objects so it is simpler for JSon/Object mapping - Vets vets = new Vets(); - vets.getVetList().addAll(this.vets.findAll()); - return vets; + Doctors doctors = new Doctors(); + doctors.getDoctorList().addAll(this.doctors.findAll()); + return doctors; } } diff --git a/src/main/java/org/springframework/samples/kidclinic/vet/VetRepository.java b/src/main/java/org/springframework/samples/kidclinic/doctor/DoctorRepository.java similarity index 73% rename from src/main/java/org/springframework/samples/kidclinic/vet/VetRepository.java rename to src/main/java/org/springframework/samples/kidclinic/doctor/DoctorRepository.java index 8d623f369..a250efd83 100644 --- a/src/main/java/org/springframework/samples/kidclinic/vet/VetRepository.java +++ b/src/main/java/org/springframework/samples/kidclinic/doctor/DoctorRepository.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.vet; +package org.springframework.samples.kidclinic.doctor; import java.util.Collection; @@ -23,7 +23,7 @@ import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; /** - * Repository class for Vet domain objects All method names are compliant with Spring Data naming + * Repository class for Doctor 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 @@ -31,16 +31,16 @@ import org.springframework.transaction.annotation.Transactional; * @author Sam Brannen * @author Michael Isvy */ -public interface VetRepository extends Repository { +public interface DoctorRepository extends Repository { /** - * Retrieve all Vets from the data store. + * Retrieve all Doctors from the data store. * - * @return a Collection of Vets + * @return a Collection of Doctors */ @Transactional(readOnly = true) - @Cacheable("vets") - Collection findAll() throws DataAccessException; + @Cacheable("doctors") + Collection findAll() throws DataAccessException; } diff --git a/src/main/java/org/springframework/samples/kidclinic/vet/Vets.java b/src/main/java/org/springframework/samples/kidclinic/doctor/Doctors.java similarity index 72% rename from src/main/java/org/springframework/samples/kidclinic/vet/Vets.java rename to src/main/java/org/springframework/samples/kidclinic/doctor/Doctors.java index d86a025d0..9f3f9f900 100644 --- a/src/main/java/org/springframework/samples/kidclinic/vet/Vets.java +++ b/src/main/java/org/springframework/samples/kidclinic/doctor/Doctors.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.vet; +package org.springframework.samples.kidclinic.doctor; import java.util.ArrayList; import java.util.List; @@ -22,22 +22,22 @@ 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 + * Simple domain object representing a list of doctors. Mostly here to be used for the 'doctors' {@link * org.springframework.web.servlet.view.xml.MarshallingView}. * * @author Arjen Poutsma */ @XmlRootElement -public class Vets { +public class Doctors { - private List vets; + private List doctors; @XmlElement - public List getVetList() { - if (vets == null) { - vets = new ArrayList<>(); + public List getDoctorList() { + if (doctors == null) { + doctors = new ArrayList<>(); } - return vets; + return doctors; } } diff --git a/src/main/java/org/springframework/samples/kidclinic/vet/Specialty.java b/src/main/java/org/springframework/samples/kidclinic/doctor/Specialty.java similarity index 88% rename from src/main/java/org/springframework/samples/kidclinic/vet/Specialty.java rename to src/main/java/org/springframework/samples/kidclinic/doctor/Specialty.java index 718f9c843..6da0cfe67 100644 --- a/src/main/java/org/springframework/samples/kidclinic/vet/Specialty.java +++ b/src/main/java/org/springframework/samples/kidclinic/doctor/Specialty.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.vet; +package org.springframework.samples.kidclinic.doctor; import java.io.Serializable; @@ -23,7 +23,7 @@ import javax.persistence.Table; import org.springframework.samples.kidclinic.model.NamedEntity; /** - * Models a {@link Vet Vet's} specialty (for example, dentistry). + * Models a {@link Doctor Doctor's} specialty (for example, dentistry). * * @author Juergen Hoeller */ diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/kidclinic/owner/OwnerController.java deleted file mode 100644 index de4d19256..000000000 --- a/src/main/java/org/springframework/samples/kidclinic/owner/OwnerController.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.kidclinic.owner; - -import java.util.Collection; -import java.util.Map; - -import javax.validation.Valid; - -import org.springframework.beans.factory.annotation.Autowired; -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.servlet.ModelAndView; - -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - * @author Michael Isvy - */ -@Controller -class OwnerController { - - private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; - private final OwnerRepository owners; - - - @Autowired - public OwnerController(OwnerRepository clinicService) { - this.owners = clinicService; - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @RequestMapping(value = "/owners/new", method = RequestMethod.GET) - public String initCreationForm(Map model) { - Owner owner = new Owner(); - model.put("owner", owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - - @RequestMapping(value = "/owners/new", method = RequestMethod.POST) - public String processCreationForm(@Valid Owner owner, BindingResult result) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } else { - this.owners.save(owner); - return "redirect:/owners/" + owner.getId(); - } - } - - @RequestMapping(value = "/owners/find", method = RequestMethod.GET) - public String initFindForm(Map model) { - model.put("owner", new Owner()); - return "owners/findOwners"; - } - - @RequestMapping(value = "/owners", method = RequestMethod.GET) - public String processFindForm(Owner owner, BindingResult result, Map model) { - - // 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.owners.findByLastName(owner.getLastName()); - if (results.isEmpty()) { - // no owners found - result.rejectValue("lastName", "notFound", "not found"); - return "owners/findOwners"; - } else if (results.size() == 1) { - // 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.owners.findById(ownerId); - model.addAttribute(owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - - @RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST) - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } else { - owner.setId(ownerId); - this.owners.save(owner); - 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.owners.findById(ownerId)); - return mav; - } - -} diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/PetController.java b/src/main/java/org/springframework/samples/kidclinic/owner/PetController.java deleted file mode 100644 index b39507049..000000000 --- a/src/main/java/org/springframework/samples/kidclinic/owner/PetController.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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. - */ -package org.springframework.samples.kidclinic.owner; - -import java.util.Collection; - -import javax.validation.Valid; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.ui.ModelMap; -import org.springframework.util.StringUtils; -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; - -/** - * @author Juergen Hoeller - * @author Ken Krebs - * @author Arjen Poutsma - */ -@Controller -@RequestMapping("/owners/{ownerId}") -class PetController { - - private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm"; - private final PetRepository pets; - private final OwnerRepository owners; - - @Autowired - public PetController(PetRepository pets, OwnerRepository owners) { - this.pets = pets; - this.owners = owners; - } - - @ModelAttribute("types") - public Collection populatePetTypes() { - return this.pets.findPetTypes(); - } - - @ModelAttribute("owner") - public Owner findOwner(@PathVariable("ownerId") int ownerId) { - return this.owners.findById(ownerId); - } - - @InitBinder("owner") - public void initOwnerBinder(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @InitBinder("pet") - public void initPetBinder(WebDataBinder dataBinder) { - dataBinder.setValidator(new PetValidator()); - } - - @RequestMapping(value = "/pets/new", method = RequestMethod.GET) - public String initCreationForm(Owner owner, ModelMap model) { - Pet pet = new Pet(); - owner.addPet(pet); - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } - - @RequestMapping(value = "/pets/new", method = RequestMethod.POST) - public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, ModelMap model) { - if (StringUtils.hasLength(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null){ - result.rejectValue("name", "duplicate", "already exists"); - } - if (result.hasErrors()) { - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } else { - owner.addPet(pet); - this.pets.save(pet); - return "redirect:/owners/{ownerId}"; - } - } - - @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.GET) - public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) { - Pet pet = this.pets.findById(petId); - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } - - @RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST) - public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) { - if (result.hasErrors()) { - pet.setOwner(owner); - model.put("pet", pet); - return VIEWS_PETS_CREATE_OR_UPDATE_FORM; - } else { - owner.addPet(pet); - this.pets.save(pet); - return "redirect:/owners/{ownerId}"; - } - } - -} diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/Pet.java b/src/main/java/org/springframework/samples/kidclinic/parent/Kid.java similarity index 81% rename from src/main/java/org/springframework/samples/kidclinic/owner/Pet.java rename to src/main/java/org/springframework/samples/kidclinic/parent/Kid.java index 2aa10e2d9..d7a1f6ca0 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/Pet.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/Kid.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import java.util.ArrayList; import java.util.Collections; @@ -48,8 +48,8 @@ import org.springframework.samples.kidclinic.visit.Visit; * @author Sam Brannen */ @Entity -@Table(name = "pets") -public class Pet extends NamedEntity { +@Table(name = "kids") +public class Kid extends NamedEntity { @Column(name = "birth_date") @Temporal(TemporalType.DATE) @@ -57,14 +57,14 @@ public class Pet extends NamedEntity { private Date birthDate; @ManyToOne - @JoinColumn(name = "type_id") - private PetType type; + @JoinColumn(name = "gender_id") + private KidGender gender; @ManyToOne - @JoinColumn(name = "owner_id") - private Owner owner; + @JoinColumn(name = "parent_id") + private Parent parent; - @OneToMany(cascade = CascadeType.ALL, mappedBy = "petId", fetch = FetchType.EAGER) + @OneToMany(cascade = CascadeType.ALL, mappedBy = "kidId", fetch = FetchType.EAGER) private Set visits = new LinkedHashSet<>(); public void setBirthDate(Date birthDate) { @@ -75,20 +75,20 @@ public class Pet extends NamedEntity { return this.birthDate; } - public PetType getType() { - return this.type; + public KidGender getGender() { + return this.gender; } - public void setType(PetType type) { - this.type = type; + public void setGender(KidGender gender) { + this.gender = gender; } - public Owner getOwner() { - return this.owner; + public Parent getParent() { + return this.parent; } - protected void setOwner(Owner owner) { - this.owner = owner; + protected void setParent(Parent parent) { + this.parent = parent; } protected Set getVisitsInternal() { @@ -111,7 +111,7 @@ public class Pet extends NamedEntity { public void addVisit(Visit visit) { getVisitsInternal().add(visit); - visit.setPetId(this.getId()); + visit.setKidId(this.getId()); } } diff --git a/src/main/java/org/springframework/samples/kidclinic/parent/KidController.java b/src/main/java/org/springframework/samples/kidclinic/parent/KidController.java new file mode 100644 index 000000000..ed2349395 --- /dev/null +++ b/src/main/java/org/springframework/samples/kidclinic/parent/KidController.java @@ -0,0 +1,116 @@ +/* + * 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. + */ +package org.springframework.samples.kidclinic.parent; + +import java.util.Collection; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.util.StringUtils; +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; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + */ +@Controller +@RequestMapping("/parents/{parentId}") +class KidController { + + private static final String VIEWS_KIDS_CREATE_OR_UPDATE_FORM = "kids/createOrUpdateKidForm"; + private final KidRepository kids; + private final ParentRepository parents; + + @Autowired + public KidController(KidRepository kids, ParentRepository parents) { + this.kids = kids; + this.parents = parents; + } + + @ModelAttribute("gender") + public Collection populateKidGenders() { + return this.kids.findKidGenders(); + } + + @ModelAttribute("parent") + public Parent findParent(@PathVariable("parentId") int parentId) { + return this.parents.findById(parentId); + } + + @InitBinder("parent") + public void initParentBinder(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @InitBinder("kid") + public void initKidBinder(WebDataBinder dataBinder) { + dataBinder.setValidator(new KidValidator()); + } + + @RequestMapping(value = "/kids/new", method = RequestMethod.GET) + public String initCreationForm(Parent parent, ModelMap model) { + Kid kid = new Kid(); + parent.addKid(kid); + model.put("kid", kid); + return VIEWS_KIDS_CREATE_OR_UPDATE_FORM; + } + + @RequestMapping(value = "/kids/new", method = RequestMethod.POST) + public String processCreationForm(Parent parent, @Valid Kid kid, BindingResult result, ModelMap model) { + if (StringUtils.hasLength(kid.getName()) && kid.isNew() && parent.getKid(kid.getName(), true) != null){ + result.rejectValue("name", "duplicate", "already exists"); + } + if (result.hasErrors()) { + model.put("kid", kid); + return VIEWS_KIDS_CREATE_OR_UPDATE_FORM; + } else { + parent.addKid(kid); + this.kids.save(kid); + return "redirect:/parents/{parentId}"; + } + } + + @RequestMapping(value = "/kids/{kidId}/edit", method = RequestMethod.GET) + public String initUpdateForm(@PathVariable("kidId") int kidId, ModelMap model) { + Kid kid = this.kids.findById(kidId); + model.put("kid", kid); + return VIEWS_KIDS_CREATE_OR_UPDATE_FORM; + } + + @RequestMapping(value = "/kids/{kidsId}/edit", method = RequestMethod.POST) + public String processUpdateForm(@Valid Kid kid, BindingResult result, Parent parent, ModelMap model) { + if (result.hasErrors()) { + kid.setParent(parent); + model.put("kid", kid); + return VIEWS_KIDS_CREATE_OR_UPDATE_FORM; + } else { + parent.addKid(kid); + this.kids.save(kid); + return "redirect:/parents/{parentId}"; + } + } + +} diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/PetType.java b/src/main/java/org/springframework/samples/kidclinic/parent/KidGender.java similarity index 87% rename from src/main/java/org/springframework/samples/kidclinic/owner/PetType.java rename to src/main/java/org/springframework/samples/kidclinic/parent/KidGender.java index c17443482..a5c40ec53 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/PetType.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/KidGender.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import javax.persistence.Entity; import javax.persistence.Table; @@ -25,7 +25,7 @@ import org.springframework.samples.kidclinic.model.NamedEntity; * Can be Cat, Dog, Hamster... */ @Entity -@Table(name = "types") -public class PetType extends NamedEntity { +@Table(name = "gender") +public class KidGender extends NamedEntity { } diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/PetTypeFormatter.java b/src/main/java/org/springframework/samples/kidclinic/parent/KidGenderFormatter.java similarity index 67% rename from src/main/java/org/springframework/samples/kidclinic/owner/PetTypeFormatter.java rename to src/main/java/org/springframework/samples/kidclinic/parent/KidGenderFormatter.java index 7e582ed71..7511c4a1d 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/PetTypeFormatter.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/KidGenderFormatter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import java.text.ParseException; @@ -25,7 +25,7 @@ import org.springframework.format.Formatter; import org.springframework.stereotype.Component; /** - * Instructs Spring MVC on how to parse and print elements of type 'PetType'. Starting from Spring 3.0, Formatters have + * Instructs Spring MVC on how to parse and print elements of type 'KidGender'. 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/ @@ -36,30 +36,30 @@ import org.springframework.stereotype.Component; * @author Michael Isvy */ @Component -public class PetTypeFormatter implements Formatter { +public class KidGenderFormatter implements Formatter { - private final PetRepository pets; + private final KidRepository kids; @Autowired - public PetTypeFormatter(PetRepository pets) { - this.pets = pets; + public KidGenderFormatter(KidRepository kids) { + this.kids = kids; } @Override - public String print(PetType petType, Locale locale) { - return petType.getName(); + public String print(KidGender kidGender, Locale locale) { + return kidGender.getName(); } @Override - public PetType parse(String text, Locale locale) throws ParseException { - Collection findPetTypes = this.pets.findPetTypes(); - for (PetType type : findPetTypes) { - if (type.getName().equals(text)) { - return type; + public KidGender parse(String text, Locale locale) throws ParseException { + Collection findKidGenders = this.kids.findKidGenders(); + for (KidGender gender : findKidGenders) { + if (gender.getName().equals(text)) { + return gender; } } - throw new ParseException("type not found: " + text, 0); + throw new ParseException("gender not found: " + text, 0); } } diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/PetRepository.java b/src/main/java/org/springframework/samples/kidclinic/parent/KidRepository.java similarity index 67% rename from src/main/java/org/springframework/samples/kidclinic/owner/PetRepository.java rename to src/main/java/org/springframework/samples/kidclinic/parent/KidRepository.java index ae731bca3..3102ec7ce 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/PetRepository.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/KidRepository.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import java.util.List; @@ -22,7 +22,7 @@ import org.springframework.data.repository.Repository; import org.springframework.transaction.annotation.Transactional; /** - * Repository class for Pet domain objects All method names are compliant with Spring Data naming + * Repository class for Kid 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 @@ -30,29 +30,29 @@ import org.springframework.transaction.annotation.Transactional; * @author Sam Brannen * @author Michael Isvy */ -public interface PetRepository extends Repository { +public interface KidRepository extends Repository { /** - * Retrieve all {@link PetType}s from the data store. - * @return a Collection of {@link PetType}s. + * Retrieve all {@link KidGender}s from the data store. + * @return a Collection of {@link KidGender}s. */ - @Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name") + @Query("SELECT ptype FROM KidGender ptype ORDER BY ptype.name") @Transactional(readOnly = true) - List findPetTypes(); + List findKidGenders(); /** - * Retrieve a {@link Pet} from the data store by id. + * Retrieve a {@link Kid} from the data store by id. * @param id the id to search for - * @return the {@link Pet} if found + * @return the {@link Kid} if found */ @Transactional(readOnly = true) - Pet findById(Integer id); + Kid findById(Integer id); /** - * Save a {@link Pet} to the data store, either inserting or updating it. - * @param pet the {@link Pet} to save + * Save a {@link Kid} to the data store, either inserting or updating it. + * @param kid the {@link Kid} to save */ - void save(Pet pet); + void save(Kid kid); } diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/PetValidator.java b/src/main/java/org/springframework/samples/kidclinic/parent/KidValidator.java similarity index 73% rename from src/main/java/org/springframework/samples/kidclinic/owner/PetValidator.java rename to src/main/java/org/springframework/samples/kidclinic/parent/KidValidator.java index 27af51e22..4a65f4021 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/PetValidator.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/KidValidator.java @@ -13,14 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import org.springframework.util.StringUtils; import org.springframework.validation.Errors; import org.springframework.validation.Validator; /** - * Validator for Pet forms. + * Validator for Kid forms. *

* We're not using Bean Validation annotations here because it is easier to define such validation rule in Java. *

@@ -28,36 +28,36 @@ import org.springframework.validation.Validator; * @author Ken Krebs * @author Juergen Hoeller */ -public class PetValidator implements Validator { +public class KidValidator implements Validator { private static final String REQUIRED = "required"; @Override public void validate(Object obj, Errors errors) { - Pet pet = (Pet) obj; - String name = pet.getName(); + Kid kid = (Kid) obj; + String name = kid.getName(); // name validation if (!StringUtils.hasLength(name)) { errors.rejectValue("name", REQUIRED, REQUIRED); } - // type validation - if (pet.isNew() && pet.getType() == null) { - errors.rejectValue("type", REQUIRED, REQUIRED); + // gender validation + if (kid.isNew() && kid.getGender() == null) { + errors.rejectValue("gender", REQUIRED, REQUIRED); } // birth date validation - if (pet.getBirthDate() == null) { + if (kid.getBirthDate() == null) { errors.rejectValue("birthDate", REQUIRED, REQUIRED); } } /** - * This Validator validates *just* Pet instances + * This Validator validates *just* Kid instances */ @Override public boolean supports(Class clazz) { - return Pet.class.isAssignableFrom(clazz); + return Kid.class.isAssignableFrom(clazz); } diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/Owner.java b/src/main/java/org/springframework/samples/kidclinic/parent/Parent.java similarity index 67% rename from src/main/java/org/springframework/samples/kidclinic/owner/Owner.java rename to src/main/java/org/springframework/samples/kidclinic/parent/Parent.java index b13087a28..56f15963e 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/Parent.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import java.util.ArrayList; import java.util.Collections; @@ -35,7 +35,7 @@ import org.springframework.core.style.ToStringCreator; import org.springframework.samples.kidclinic.model.Person; /** - * Simple JavaBean domain object representing an owner. + * Simple JavaBean domain object representing an parent. * * @author Ken Krebs * @author Juergen Hoeller @@ -43,8 +43,8 @@ import org.springframework.samples.kidclinic.model.Person; * @author Michael Isvy */ @Entity -@Table(name = "owners") -public class Owner extends Person { +@Table(name = "parents") +public class Parent extends Person { @Column(name = "address") @NotEmpty private String address; @@ -58,8 +58,8 @@ public class Owner extends Person { @Digits(fraction = 0, integer = 10) private String telephone; - @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner") - private Set pets; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent") + private Set kids; public String getAddress() { @@ -86,54 +86,54 @@ public class Owner extends Person { this.telephone = telephone; } - protected Set getPetsInternal() { - if (this.pets == null) { - this.pets = new HashSet<>(); + protected Set getKidsInternal() { + if (this.kids == null) { + this.kids = new HashSet<>(); } - return this.pets; + return this.kids; } - protected void setPetsInternal(Set pets) { - this.pets = pets; + protected void setKidsInternal(Set kids) { + this.kids = kids; } - public List getPets() { - List sortedPets = new ArrayList<>(getPetsInternal()); - PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true)); - return Collections.unmodifiableList(sortedPets); + public List getKids() { + List sortedKids = new ArrayList<>(getKidsInternal()); + PropertyComparator.sort(sortedKids, new MutableSortDefinition("name", true, true)); + return Collections.unmodifiableList(sortedKids); } - public void addPet(Pet pet) { - if (pet.isNew()) { - getPetsInternal().add(pet); + public void addKid(Kid kid) { + if (kid.isNew()) { + getKidsInternal().add(kid); } - pet.setOwner(this); + kid.setParent(this); } /** - * Return the Pet with the given name, or null if none found for this Owner. + * Return the Kid with the given name, or null if none found for this Parent. * * @param name to test - * @return true if pet name is already in use + * @return true if kid name is already in use */ - public Pet getPet(String name) { - return getPet(name, false); + public Kid getKid(String name) { + return getKid(name, false); } /** - * Return the Pet with the given name, or null if none found for this Owner. + * Return the Kid with the given name, or null if none found for this Kid. * * @param name to test - * @return true if pet name is already in use + * @return true if kid name is already in use */ - public Pet getPet(String name, boolean ignoreNew) { + public Kid getKid(String name, boolean ignoreNew) { name = name.toLowerCase(); - for (Pet pet : getPetsInternal()) { - if (!ignoreNew || !pet.isNew()) { - String compName = pet.getName(); + for (Kid kid : getKidsInternal()) { + if (!ignoreNew || !kid.isNew()) { + String compName = kid.getName(); compName = compName.toLowerCase(); if (compName.equals(name)) { - return pet; + return kid; } } } diff --git a/src/main/java/org/springframework/samples/kidclinic/parent/ParentController.java b/src/main/java/org/springframework/samples/kidclinic/parent/ParentController.java new file mode 100644 index 000000000..8770d2977 --- /dev/null +++ b/src/main/java/org/springframework/samples/kidclinic/parent/ParentController.java @@ -0,0 +1,136 @@ +/* + * 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. + */ +package org.springframework.samples.kidclinic.parent; + +import java.util.Collection; +import java.util.Map; + +import javax.validation.Valid; + +import org.springframework.beans.factory.annotation.Autowired; +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.servlet.ModelAndView; + +/** + * @author Juergen Hoeller + * @author Ken Krebs + * @author Arjen Poutsma + * @author Michael Isvy + */ +@Controller +class ParentController { + + private static final String VIEWS_PARENT_CREATE_OR_UPDATE_FORM = "parents/createOrUpdateParentForm"; + private final ParentRepository parents; + + + @Autowired + public ParentController(ParentRepository clinicService) { + this.parents = clinicService; + } + + @InitBinder + public void setAllowedFields(WebDataBinder dataBinder) { + dataBinder.setDisallowedFields("id"); + } + + @RequestMapping(value = "/parents/new", method = RequestMethod.GET) + public String initCreationForm(Map model) { + Parent parent = new Parent(); + model.put("parent", parent); + return VIEWS_PARENT_CREATE_OR_UPDATE_FORM; + } + + @RequestMapping(value = "/parents/new", method = RequestMethod.POST) + public String processCreationForm(@Valid Parent parent, BindingResult result) { + if (result.hasErrors()) { + return VIEWS_PARENT_CREATE_OR_UPDATE_FORM; + } else { + this.parents.save(parent); + return "redirect:/parent/" + parent.getId(); + } + } + + @RequestMapping(value = "/parents/find", method = RequestMethod.GET) + public String initFindForm(Map model) { + model.put("parent", new Parent()); + return "parents/findParents"; + } + + @RequestMapping(value = "/parents", method = RequestMethod.GET) + public String processFindForm(Parent parent, BindingResult result, Map model) { + + // allow parameterless GET request for /parents to return all records + if (parent.getLastName() == null) { + parent.setLastName(""); // empty string signifies broadest possible search + } + + // find parents by last name + Collection results = this.parents.findByLastName(parent.getLastName()); + if (results.isEmpty()) { + // no parents found + result.rejectValue("lastName", "notFound", "not found"); + return "parents/findParents"; + } else if (results.size() == 1) { + // 1 parent found + parent = results.iterator().next(); + return "redirect:/parents/" + parent.getId(); + } else { + // multiple parents found + model.put("selections", results); + return "parents/parentsList"; + } + } + + @RequestMapping(value = "/parents/{parentId}/edit", method = RequestMethod.GET) + public String initUpdateParentForm(@PathVariable("parentId") int parentId, Model model) { + Parent parent = this.parents.findById(parentId); + model.addAttribute(parent); + return VIEWS_PARENT_CREATE_OR_UPDATE_FORM; + } + + @RequestMapping(value = "/parents/{parentId}/edit", method = RequestMethod.POST) + public String processUpdateParentForm(@Valid Parent parent, BindingResult result, @PathVariable("parentId") int parentId) { + if (result.hasErrors()) { + return VIEWS_PARENT_CREATE_OR_UPDATE_FORM; + } else { + parent.setId(parentId); + this.parents.save(parent); + return "redirect:/parents/{parentId}"; + } + } + + /** + * Custom handler for displaying an parent. + * + * @param parentId the ID of the parent to display + * @return a ModelMap with the model attributes for the view + */ + @RequestMapping("/parents/{parentId}") + public ModelAndView showParent(@PathVariable("parentId") int parentId) { + ModelAndView mav = new ModelAndView("parents/parentDetails"); + mav.addObject(this.parents.findById(parentId)); + return mav; + } + +} diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/kidclinic/parent/ParentRepository.java similarity index 59% rename from src/main/java/org/springframework/samples/kidclinic/owner/OwnerRepository.java rename to src/main/java/org/springframework/samples/kidclinic/parent/ParentRepository.java index becd59de6..431abba96 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/ParentRepository.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import java.util.Collection; @@ -23,7 +23,7 @@ import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; /** - * Repository class for Owner domain objects All method names are compliant with Spring Data naming + * Repository class for Parent 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 @@ -31,33 +31,33 @@ import org.springframework.transaction.annotation.Transactional; * @author Sam Brannen * @author Michael Isvy */ -public interface OwnerRepository extends Repository { +public interface ParentRepository extends Repository { /** - * Retrieve {@link Owner}s from the data store by last name, returning all owners + * Retrieve {@link Parent}s from the data store by last name, returning all parents * whose last name starts with the given name. * @param lastName Value to search for - * @return a Collection of matching {@link Owner}s (or an empty Collection if none + * @return a Collection of matching {@link Parent}s (or an empty Collection if none * found) */ - @Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") + @Query("SELECT DISTINCT parent FROM Parent parent left join fetch parent.kids WHERE parent.lastName LIKE :lastName%") @Transactional(readOnly = true) - Collection findByLastName(@Param("lastName") String lastName); + Collection findByLastName(@Param("lastName") String lastName); /** - * Retrieve an {@link Owner} from the data store by id. + * Retrieve an {@link Parent} from the data store by id. * @param id the id to search for - * @return the {@link Owner} if found + * @return the {@link Parent} if found */ - @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id") + @Query("SELECT parent FROM Parent parent left join fetch parent.kids WHERE parent.id =:id") @Transactional(readOnly = true) - Owner findById(@Param("id") Integer id); + Parent findById(@Param("id") Integer id); /** - * Save an {@link Owner} to the data store, either inserting or updating it. - * @param owner the {@link Owner} to save + * Save an {@link Parent} to the data store, either inserting or updating it. + * @param owner the {@link Parent} to save */ - void save(Owner owner); + void save(Parent parent); } diff --git a/src/main/java/org/springframework/samples/kidclinic/owner/VisitController.java b/src/main/java/org/springframework/samples/kidclinic/parent/VisitController.java similarity index 68% rename from src/main/java/org/springframework/samples/kidclinic/owner/VisitController.java rename to src/main/java/org/springframework/samples/kidclinic/parent/VisitController.java index c0d4378a3..f48d8ecaf 100644 --- a/src/main/java/org/springframework/samples/kidclinic/owner/VisitController.java +++ b/src/main/java/org/springframework/samples/kidclinic/parent/VisitController.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.samples.kidclinic.owner; +package org.springframework.samples.kidclinic.parent; import java.util.Map; @@ -42,13 +42,13 @@ import org.springframework.web.bind.annotation.RequestMethod; class VisitController { private final VisitRepository visits; - private final PetRepository pets; + private final KidRepository kids; @Autowired - public VisitController(VisitRepository visits, PetRepository pets) { + public VisitController(VisitRepository visits, KidRepository kids) { this.visits = visits; - this.pets = pets; + this.kids = kids; } @InitBinder @@ -60,35 +60,35 @@ class VisitController { * 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 Kid object always has an id * (Even though id is not part of the form fields) * - * @param petId - * @return Pet + * @param kidId + * @return Kid */ @ModelAttribute("visit") - public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map model) { - Pet pet = this.pets.findById(petId); - model.put("pet", pet); + public Visit loadKidWithVisit(@PathVariable("kidId") int kidId, Map model) { + Kid kid = this.kids.findById(kidId); + model.put("kid", kid); Visit visit = new Visit(); - pet.addVisit(visit); + kid.addVisit(visit); return visit; } - // 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 model) { - return "pets/createOrUpdateVisitForm"; + // Spring MVC calls method loadKidWithVisit(...) before initNewVisitForm is called + @RequestMapping(value = "/parents/*/kids/{kidId}/visits/new", method = RequestMethod.GET) + public String initNewVisitForm(@PathVariable("kidId") int kidId, Map model) { + return "kids/createOrUpdateVisitForm"; } - // Spring MVC calls method loadPetWithVisit(...) before processNewVisitForm is called - @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/visits/new", method = RequestMethod.POST) + // Spring MVC calls method loadKidWithVisit(...) before processNewVisitForm is called + @RequestMapping(value = "/parents/{parentId}/kids/{kidId}/visits/new", method = RequestMethod.POST) public String processNewVisitForm(@Valid Visit visit, BindingResult result) { if (result.hasErrors()) { - return "pets/createOrUpdateVisitForm"; + return "kids/createOrUpdateVisitForm"; } else { this.visits.save(visit); - return "redirect:/owners/{ownerId}"; + return "redirect:/parents/{parentId}"; } } diff --git a/src/main/java/org/springframework/samples/kidclinic/system/CacheConfig.java b/src/main/java/org/springframework/samples/kidclinic/system/CacheConfig.java index 516168388..a719197bd 100755 --- a/src/main/java/org/springframework/samples/kidclinic/system/CacheConfig.java +++ b/src/main/java/org/springframework/samples/kidclinic/system/CacheConfig.java @@ -20,7 +20,7 @@ class CacheConfig { public JCacheManagerCustomizer cacheManagerCustomizer() { return cm -> { Configuration cacheConfiguration = createCacheConfiguration(); - cm.createCache("vets", cacheConfiguration); + cm.createCache("doctors", cacheConfiguration); }; } diff --git a/src/main/java/org/springframework/samples/kidclinic/system/CrashController.java b/src/main/java/org/springframework/samples/kidclinic/system/CrashController.java index 6cf3bf4b1..d1c7589fd 100644 --- a/src/main/java/org/springframework/samples/kidclinic/system/CrashController.java +++ b/src/main/java/org/springframework/samples/kidclinic/system/CrashController.java @@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.RequestMethod; @Controller class CrashController { - @RequestMapping(value = "/oups", method = RequestMethod.GET) + @RequestMapping(value = "/oops", 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/kidclinic/visit/Visit.java b/src/main/java/org/springframework/samples/kidclinic/visit/Visit.java index 425dfe12f..1619c928b 100755 --- a/src/main/java/org/springframework/samples/kidclinic/visit/Visit.java +++ b/src/main/java/org/springframework/samples/kidclinic/visit/Visit.java @@ -55,8 +55,8 @@ public class Visit extends BaseEntity { /** * Holds value of property pet. */ - @Column(name = "pet_id") - private Integer petId; + @Column(name = "kid_id") + private Integer kidId; /** @@ -104,21 +104,21 @@ public class Visit extends BaseEntity { } /** - * Getter for property pet id. + * Getter for property kid id. * - * @return Value of property pet id. + * @return Value of property kid id. */ - public Integer getPetId() { - return this.petId; + public Integer getKidId() { + return this.kidId; } /** - * Setter for property pet id. + * Setter for property kid id. * - * @param petId New value of property pet id. + * @param petId New value of property kid id. */ - public void setPetId(Integer petId) { - this.petId = petId; + public void setKidId(Integer kidId) { + this.kidId = kidId; } } diff --git a/src/main/java/org/springframework/samples/kidclinic/visit/VisitRepository.java b/src/main/java/org/springframework/samples/kidclinic/visit/VisitRepository.java index 7d0aa724f..b7e97b37f 100644 --- a/src/main/java/org/springframework/samples/kidclinic/visit/VisitRepository.java +++ b/src/main/java/org/springframework/samples/kidclinic/visit/VisitRepository.java @@ -40,6 +40,6 @@ public interface VisitRepository extends Repository { */ void save(Visit visit) throws DataAccessException; - List findByPetId(Integer petId); + List findByKidId(Integer kidId); } diff --git a/src/main/resources/db/hsqldb/data.sql b/src/main/resources/db/hsqldb/data.sql index 340465fe5..931424f88 100644 --- a/src/main/resources/db/hsqldb/data.sql +++ b/src/main/resources/db/hsqldb/data.sql @@ -1,50 +1,50 @@ -INSERT INTO vets VALUES (1, 'James', 'Carter'); -INSERT INTO vets VALUES (2, 'Helen', 'Leary'); -INSERT INTO vets VALUES (3, 'Linda', 'Douglas'); -INSERT INTO vets VALUES (4, 'Rafael', 'Ortega'); -INSERT INTO vets VALUES (5, 'Henry', 'Stevens'); -INSERT INTO vets VALUES (6, 'Sharon', 'Jenkins'); +INSERT INTO doctors VALUES (1, 'James', 'Carter'); +INSERT INTO doctors VALUES (2, 'Helen', 'Leary'); +INSERT INTO doctors VALUES (3, 'Linda', 'Douglas'); +INSERT INTO doctors VALUES (4, 'Rafael', 'Ortega'); +INSERT INTO doctors VALUES (5, 'Henry', 'Stevens'); +INSERT INTO doctors VALUES (6, 'Sharon', 'Jenkins'); INSERT INTO specialties VALUES (1, 'radiology'); INSERT INTO specialties VALUES (2, 'surgery'); INSERT INTO specialties VALUES (3, 'dentistry'); -INSERT INTO vet_specialties VALUES (2, 1); -INSERT INTO vet_specialties VALUES (3, 2); -INSERT INTO vet_specialties VALUES (3, 3); -INSERT INTO vet_specialties VALUES (4, 2); -INSERT INTO vet_specialties VALUES (5, 1); +INSERT INTO doctor_specialties VALUES (2, 1); +INSERT INTO doctor_specialties VALUES (3, 2); +INSERT INTO doctor_specialties VALUES (3, 3); +INSERT INTO doctor_specialties VALUES (4, 2); +INSERT INTO doctor_specialties VALUES (5, 1); -INSERT INTO types VALUES (1, 'male'); -INSERT INTO types VALUES (2, 'female'); +INSERT INTO gender VALUES (1, 'male'); +INSERT INTO gender VALUES (2, 'female'); -INSERT INTO owners VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023'); -INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749'); -INSERT INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763'); -INSERT INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198'); -INSERT INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765'); -INSERT INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654'); -INSERT INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387'); -INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683'); -INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435'); -INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); +INSERT INTO parents VALUES (1, 'George', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023'); +INSERT INTO parents VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749'); +INSERT INTO parents VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763'); +INSERT INTO parents VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198'); +INSERT INTO parents VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765'); +INSERT INTO parents VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654'); +INSERT INTO parents VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387'); +INSERT INTO parents VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683'); +INSERT INTO parents VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435'); +INSERT INTO parents VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); -INSERT INTO pets VALUES (1, 'Alyssa', '2000-09-07', 2, 1); -INSERT INTO pets VALUES (2, 'Joe', '2002-08-06', 1, 2); -INSERT INTO pets VALUES (3, 'Lauren', '2001-04-17', 2, 3); -INSERT INTO pets VALUES (4, 'Nicole', '2000-03-07', 2, 3); -INSERT INTO pets VALUES (5, 'Thomas', '2000-11-30', 1, 4); -INSERT INTO pets VALUES (6, 'Samantha', '2000-01-20', 2, 5); -INSERT INTO pets VALUES (7, 'George', '1995-09-04', 1, 6); -INSERT INTO pets VALUES (8, 'Max', '1995-09-04', 1, 6); -INSERT INTO pets VALUES (9, 'Brendan', '1999-08-06', 1, 7); -INSERT INTO pets VALUES (10, 'Elizabeth', '1997-02-24', 2, 8); -INSERT INTO pets VALUES (11, 'Lucy', '2000-03-09', 2, 9); -INSERT INTO pets VALUES (12, 'Sunny', '2000-06-24', 2, 10); -INSERT INTO pets VALUES (13, 'Conner', '2002-06-08', 1, 10); +INSERT INTO kids VALUES (1, 'Alyssa', '2000-09-07', 2, 1); +INSERT INTO kids VALUES (2, 'Joe', '2002-08-06', 1, 2); +INSERT INTO kids VALUES (3, 'Lauren', '2001-04-17', 2, 3); +INSERT INTO kids VALUES (4, 'Nicole', '2000-03-07', 2, 3); +INSERT INTO kids VALUES (5, 'Thomas', '2000-11-30', 1, 4); +INSERT INTO kids VALUES (6, 'Samantha', '2000-01-20', 2, 5); +INSERT INTO kids VALUES (7, 'George', '1995-09-04', 1, 6); +INSERT INTO kids VALUES (8, 'Max', '1995-09-04', 1, 6); +INSERT INTO kids VALUES (9, 'Brendan', '1999-08-06', 1, 7); +INSERT INTO kids VALUES (10, 'Elizabeth', '1997-02-24', 2, 8); +INSERT INTO kids VALUES (11, 'Lucy', '2000-03-09', 2, 9); +INSERT INTO kids VALUES (12, 'Sunny', '2000-06-24', 2, 10); +INSERT INTO kids VALUES (13, 'Conner', '2002-06-08', 1, 10); INSERT INTO visits VALUES (1, 7, '2013-01-01', 'rabies shot'); INSERT INTO visits VALUES (2, 8, '2013-01-02', 'rabies shot'); -INSERT INTO visits VALUES (3, 8, '2013-01-03', 'neutered'); -INSERT INTO visits VALUES (4, 7, '2013-01-04', 'spayed'); +INSERT INTO visits VALUES (3, 8, '2013-01-03', 'cold'); +INSERT INTO visits VALUES (4, 7, '2013-01-04', 'flu'); diff --git a/src/main/resources/db/hsqldb/schema.sql b/src/main/resources/db/hsqldb/schema.sql index f3c6947b7..ae02638a7 100644 --- a/src/main/resources/db/hsqldb/schema.sql +++ b/src/main/resources/db/hsqldb/schema.sql @@ -1,18 +1,18 @@ -DROP TABLE vet_specialties IF EXISTS; -DROP TABLE vets IF EXISTS; +DROP TABLE doctor_specialties IF EXISTS; +DROP TABLE doctors 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 kids IF EXISTS; +DROP TABLE gender IF EXISTS; +DROP TABLE parents IF EXISTS; -CREATE TABLE vets ( +CREATE TABLE doctors ( id INTEGER IDENTITY PRIMARY KEY, first_name VARCHAR(30), last_name VARCHAR(30) ); -CREATE INDEX vets_last_name ON vets (last_name); +CREATE INDEX doctors_last_name ON doctors (last_name); CREATE TABLE specialties ( id INTEGER IDENTITY PRIMARY KEY, @@ -20,20 +20,20 @@ CREATE TABLE specialties ( ); CREATE INDEX specialties_name ON specialties (name); -CREATE TABLE vet_specialties ( - vet_id INTEGER NOT NULL, +CREATE TABLE doctor_specialties ( + doctor_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 doctor_specialties ADD CONSTRAINT fk_doctor_specialties_doctors FOREIGN KEY (doctor_id) REFERENCES doctors (id); +ALTER TABLE doctor_specialties ADD CONSTRAINT fk_doctor_specialties_specialties FOREIGN KEY (specialty_id) REFERENCES specialties (id); -CREATE TABLE types ( +CREATE TABLE gender ( id INTEGER IDENTITY PRIMARY KEY, name VARCHAR(80) ); -CREATE INDEX types_name ON types (name); +CREATE INDEX gender_name ON gender (name); -CREATE TABLE owners ( +CREATE TABLE parents ( id INTEGER IDENTITY PRIMARY KEY, first_name VARCHAR(30), last_name VARCHAR_IGNORECASE(30), @@ -41,24 +41,24 @@ CREATE TABLE owners ( city VARCHAR(80), telephone VARCHAR(20) ); -CREATE INDEX owners_last_name ON owners (last_name); +CREATE INDEX parents_last_name ON parents (last_name); -CREATE TABLE pets ( +CREATE TABLE kids ( id INTEGER IDENTITY PRIMARY KEY, name VARCHAR(30), birth_date DATE, type_id INTEGER NOT NULL, - owner_id INTEGER NOT NULL + parent_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 kids ADD CONSTRAINT fk_kids_parents FOREIGN KEY (parent_id) REFERENCES parents (id); +ALTER TABLE kids ADD CONSTRAINT fk_kids_gender FOREIGN KEY (type_id) REFERENCES gender (id); +CREATE INDEX kids_name ON kids (name); CREATE TABLE visits ( id INTEGER IDENTITY PRIMARY KEY, - pet_id INTEGER NOT NULL, + kid_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_kids FOREIGN KEY (kid_id) REFERENCES kids (id); +CREATE INDEX visits_kid_id ON visits (kid_id); diff --git a/src/main/resources/templates/doctors/doctorList.html b/src/main/resources/templates/doctors/doctorList.html new file mode 100644 index 000000000..54e8e82ff --- /dev/null +++ b/src/main/resources/templates/doctors/doctorList.html @@ -0,0 +1,28 @@ + + + + + + +

Pediatricians

+ + + + + + + + + + + + + + +
NameSpecialties
none
+ + + diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index c1f6c9693..6bb77100e 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -3,8 +3,8 @@ -

Reviews

-

We are currently curating our review.

+

Errors

+

Exception message

diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index e1655b38f..0e2cce319 100755 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -10,7 +10,7 @@ - PetClinic :: a Spring Framework demonstration + KidClinic :: a Spring Framework demonstration