From 456a13430edcb5cc63deb7f84af085ff868e0c3b Mon Sep 17 00:00:00 2001 From: sarika_deshmukh Date: Tue, 17 Jun 2025 23:45:55 +0530 Subject: [PATCH] Assignment Changes added for same. Assignment completed to committed the changes. --- .../samples/petclinic/owner/Pet.java | 25 +++++ .../samples/petclinic/owner/PetAttribute.java | 60 ++++++++++++ .../petclinic/owner/PetController.java | 6 +- .../petclinic/owner/PetViewController.java | 92 +++++++++++++++++++ .../petclinic/owner/PetViewRepository.java | 18 ++++ src/main/resources/db/h2/data.sql | 5 + src/main/resources/db/h2/schema.sql | 11 +++ .../resources/messages/messages.properties | 6 ++ .../resources/templates/fragments/layout.html | 5 + .../templates/owners/ownerDetails.html | 6 ++ .../templates/pets/createOrUpdatePetForm.html | 3 + .../resources/templates/pets/findPets.html | 35 +++++++ .../resources/templates/pets/petDetails.html | 68 ++++++++++++++ .../resources/templates/pets/petsList.html | 63 +++++++++++++ 14 files changed, 402 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java create mode 100644 src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java create mode 100644 src/main/resources/templates/pets/findPets.html create mode 100644 src/main/resources/templates/pets/petDetails.html create mode 100644 src/main/resources/templates/pets/petsList.html diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java index 1945f9b67..99ca428dc 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Pet.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Pet.java @@ -30,6 +30,7 @@ import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; +import jakarta.persistence.OneToOne; import jakarta.persistence.OrderBy; import jakarta.persistence.Table; @@ -57,6 +58,21 @@ public class Pet extends NamedEntity { @JoinColumn(name = "pet_id") @OrderBy("date ASC") private final Set visits = new LinkedHashSet<>(); + + @OneToOne(mappedBy ="pet",cascade = CascadeType.ALL) + private PetAttribute petAttribute; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="owner_id") + private Owner owner; + + public Owner getOwner() { + return owner; + } + + public void setOwner(Owner owner) { + this.owner = owner; + } public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; @@ -82,4 +98,13 @@ public class Pet extends NamedEntity { getVisits().add(visit); } + public PetAttribute getPetAttribute() { + return petAttribute; + } + + public void setPetAttribute(PetAttribute petAttribute) { + this.petAttribute = petAttribute; + } + + } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java b/src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java new file mode 100644 index 000000000..b6a82a38d --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetAttribute.java @@ -0,0 +1,60 @@ +package org.springframework.samples.petclinic.owner; + +import org.springframework.samples.petclinic.model.BaseEntity; + +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; + +@Entity +@Table (name="pet_attributes") +public class PetAttribute extends BaseEntity{ + + + @OneToOne + @JoinColumn(name="pet_id") + private Pet pet; + + //@NotBlank(message="Temperament is required") + private String temperament; + + private Double length; + + private Double weight; + + public Pet getPet() { + return pet; + } + + public void setPet(Pet pet) { + this.pet = pet; + } + + public String getTemperament() { + return temperament; + } + + public void setTemperament(String temperament) { + this.temperament = temperament; + } + + public Double getLength() { + return length; + } + + public void setLength(Double length) { + this.length = length; + } + + public Double getWeight() { + return weight; + } + + public void setWeight(Double weight) { + this.weight = weight; + } + + + +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 56707d99f..f1cd4ca78 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -95,12 +95,15 @@ class PetController { @GetMapping("/pets/new") public String initCreationForm(Owner owner, ModelMap model) { Pet pet = new Pet(); + PetAttribute petAttribute= new PetAttribute(); + pet.setPetAttribute(petAttribute); + pet.getPetAttribute().setPet(pet); owner.addPet(pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } @PostMapping("/pets/new") - public String processCreationForm(Owner owner, @Valid Pet pet, BindingResult result, + public String processCreationForm(Owner owner, @Valid Pet pet,BindingResult result, RedirectAttributes redirectAttributes) { if (StringUtils.hasText(pet.getName()) && pet.isNew() && owner.getPet(pet.getName(), true) != null) @@ -116,6 +119,7 @@ class PetController { } owner.addPet(pet); + pet.getPetAttribute().setPet(pet); this.owners.save(owner); redirectAttributes.addFlashAttribute("message", "New Pet has been Added"); return "redirect:/owners/{ownerId}"; diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java new file mode 100644 index 000000000..aa29574c9 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetViewController.java @@ -0,0 +1,92 @@ +package org.springframework.samples.petclinic.owner; + +import java.util.List; +import java.util.Optional; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class PetViewController { + + private final PetViewRepository petViewRepository; + + public PetViewController(PetViewRepository petViewRepository) { + this.petViewRepository = petViewRepository; + } + + @ModelAttribute("pet") + public Pet findPet(@PathVariable(name = "petId", required = false) Integer petId) { + return petId == null ? new Pet() + : this.petViewRepository.findById(petId) + .orElseThrow(() -> new IllegalArgumentException("Pet not found with id: " + petId + + ". Please ensure the ID is correct " + "and the pet exists in the database.")); + } + + @GetMapping("/pets/find") + public String initFindForm() { + return "pets/findPets"; + } + + @GetMapping("/pets") + public String processFindForm(@RequestParam(defaultValue = "1") int page, Pet pet, BindingResult result, + Model model) { + // allow parameterless GET request for /pets to return all records + if (pet.getName() == null) { + pet.setName(""); // empty string signifies broadest possible search + } + + // find owners by last name + Page petsResults = findPaginatedForPetsName(page, pet.getName()); + if (petsResults.isEmpty()) { + // no owners found + result.rejectValue("Name", "notFound", "not found"); + return "pets/findPets"; + } + + if (petsResults.getTotalElements() == 1) { + // 1 owner found + pet = petsResults.iterator().next(); + return "redirect:/pets/" + pet.getId(); + } + + // multiple owners found + return addPaginationModel(page, model, petsResults); + } + + private String addPaginationModel(int page, Model model, Page paginated) { + List listPets = paginated.getContent(); + model.addAttribute("currentPage", page); + model.addAttribute("totalPages", paginated.getTotalPages()); + model.addAttribute("totalItems", paginated.getTotalElements()); + model.addAttribute("listPets", listPets); + return "pets/petsList"; + } + private Page findPaginatedForPetsName(int page, String name) { + int pageSize = 5; + Pageable pageable = PageRequest.of(page - 1, pageSize); + return petViewRepository.findByNameStartingWith(name, pageable); + } + + @GetMapping("/pets/{petId}") + public ModelAndView showPet(@PathVariable("petId") int petId) { + ModelAndView mav = new ModelAndView("pets/petDetails"); + Optional optionalPet = this.petViewRepository.findPetByWithOwnerAndAttribute(petId); + Pet pet = optionalPet.orElseThrow(() -> new IllegalArgumentException( + "Pet not found with id: " + petId + ". Please ensure the ID is correct ")); + + mav.addObject(pet); + mav.addObject(pet.getOwner()); + //mav.addObject(pet.getPetAttribute()); + return mav; + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java new file mode 100644 index 000000000..2068fb175 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetViewRepository.java @@ -0,0 +1,18 @@ +package org.springframework.samples.petclinic.owner; + +import java.util.Optional; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface PetViewRepository extends JpaRepository{ + + Page findByNameStartingWith(String name, Pageable pageable); + + + @Query("SELECT p from Pet p LEFT JOIN FETCH p.owner LEFT JOIN FETCH p.petAttribute WHERE p.id= :id") + Optional findPetByWithOwnerAndAttribute(@Param("id") int id); +} diff --git a/src/main/resources/db/h2/data.sql b/src/main/resources/db/h2/data.sql index f232b1361..8aa135879 100644 --- a/src/main/resources/db/h2/data.sql +++ b/src/main/resources/db/h2/data.sql @@ -51,3 +51,8 @@ INSERT INTO visits VALUES (default, 7, '2013-01-01', 'rabies shot'); INSERT INTO visits VALUES (default, 8, '2013-01-02', 'rabies shot'); INSERT INTO visits VALUES (default, 8, '2013-01-03', 'neutered'); INSERT INTO visits VALUES (default, 7, '2013-01-04', 'spayed'); + +INSERT INTO pet_attributes VALUES (default,2,'Friendly',35.5,12.0); +INSERT INTO pet_attributes VALUES (default,9,'social',25.5,10.0); + + diff --git a/src/main/resources/db/h2/schema.sql b/src/main/resources/db/h2/schema.sql index 4a6c322cb..a3a6c0b52 100644 --- a/src/main/resources/db/h2/schema.sql +++ b/src/main/resources/db/h2/schema.sql @@ -5,6 +5,7 @@ DROP TABLE visits IF EXISTS; DROP TABLE pets IF EXISTS; DROP TABLE types IF EXISTS; DROP TABLE owners IF EXISTS; +DROP TABLE pet_attributes IF EXISTS; CREATE TABLE vets ( @@ -62,3 +63,13 @@ CREATE TABLE visits ( ); ALTER TABLE visits ADD CONSTRAINT fk_visits_pets FOREIGN KEY (pet_id) REFERENCES pets (id); CREATE INDEX visits_pet_id ON visits (pet_id); + +CREATE TABLE pet_attributes( + id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + pet_id INTEGER, + temperament VARCHAR(255), + length DOUBLE, + weight DOUBLE +); +ALTER TABLE pet_attributes ADD CONSTRAINT fk_pets_attribute FOREIGN KEY (pet_id) REFERENCES pets (id); + diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 7b8b5dfd8..b747bec1c 100644 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -16,6 +16,8 @@ owners=Owners addOwner=Add Owner findOwner=Find Owner findOwners=Find Owners +findPet=Find Pet +findPets=Find Pets updateOwner=Update Owner vets=Veterinarians name=Name @@ -42,7 +44,11 @@ new=New addVisit=Add Visit editPet=Edit Pet ownerInformation=Owner Information +petInformation=Pet Information visitDate=Visit Date editOwner=Edit Owner addNewPet=Add New Pet petsAndVisits=Pets and Visits +petAttributeWeight=Weight +petAttributeLength=Length +petAttributeTemp=Temperament diff --git a/src/main/resources/templates/fragments/layout.html b/src/main/resources/templates/fragments/layout.html index 7c5cd0d86..2a74f5d6f 100644 --- a/src/main/resources/templates/fragments/layout.html +++ b/src/main/resources/templates/fragments/layout.html @@ -48,6 +48,11 @@ Find owners +
  • + + Find pets +
  • +
  • Veterinarians diff --git a/src/main/resources/templates/owners/ownerDetails.html b/src/main/resources/templates/owners/ownerDetails.html index cc175cd13..c7c2c781f 100644 --- a/src/main/resources/templates/owners/ownerDetails.html +++ b/src/main/resources/templates/owners/ownerDetails.html @@ -54,6 +54,12 @@
    Type
    +
    Temperament
    +
    +
    Length
    +
    +
    Weight
    +
    diff --git a/src/main/resources/templates/pets/createOrUpdatePetForm.html b/src/main/resources/templates/pets/createOrUpdatePetForm.html index 396963d41..51318d8c5 100644 --- a/src/main/resources/templates/pets/createOrUpdatePetForm.html +++ b/src/main/resources/templates/pets/createOrUpdatePetForm.html @@ -20,6 +20,9 @@ + + +
    diff --git a/src/main/resources/templates/pets/findPets.html b/src/main/resources/templates/pets/findPets.html new file mode 100644 index 000000000..1625022d0 --- /dev/null +++ b/src/main/resources/templates/pets/findPets.html @@ -0,0 +1,35 @@ + + + + + + +

    Find Pets

    + +
    +
    +
    + +
    + + +
    +

    Error

    +
    +
    +
    +
    +
    +
    +
    + +
    +
    + + +
    + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/pets/petDetails.html b/src/main/resources/templates/pets/petDetails.html new file mode 100644 index 000000000..2dfeba5ae --- /dev/null +++ b/src/main/resources/templates/pets/petDetails.html @@ -0,0 +1,68 @@ + + + + + + +

    Pet Information

    + +
    + +
    + +
    + +
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Name
    Birth Date
    Type
    Temperament
    Length
    Weight
    Owner
    + +
    +
    + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/pets/petsList.html b/src/main/resources/templates/pets/petsList.html new file mode 100644 index 000000000..bf69978eb --- /dev/null +++ b/src/main/resources/templates/pets/petsList.html @@ -0,0 +1,63 @@ + + + + + + +

    Pets

    + + + + + + + + + + + + + + + + + +
    NameBirth DateTypeTemperamentLengthWeight
    + + + + + + +
    +
    + Pages: + [ + + [[${i}]] + [[${i}]] + + + + + + + + + + + + + + + + + + +
    + + + \ No newline at end of file