/** * Return the Pet with the given name, or null if none found for this Owner. * * @param name to test * @return true if pet name is already in use */ public Pet getPet(String name) { return getPet(name, false); }
@Override public String print(PetType petType, Locale locale) { return petType.getName(); }
public void addPet(Pet pet) { if (pet.isNew()) { getPetsInternal().add(pet); } pet.setOwner(this); }
@PostMapping("/pets/new") 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"); } owner.addPet(pet); if (result.hasErrors()) { model.put("pet", pet); return VIEWS_PETS_CREATE_OR_UPDATE_FORM; } else { this.pets.save(pet); return "redirect:/owners/{ownerId}"; } }
@Override public String toString() { return new ToStringCreator(this) .append("id", this.getId()).append("new", this.isNew()) .append("lastName", this.getLastName()) .append("firstName", this.getFirstName()).append("address", this.address) .append("city", this.city).append("telephone", this.telephone).toString(); } }
/** * Return the Pet with the given name, or null if none found for this Owner. * * @param name to test * @return true if pet name is already in use */ public Pet getPet(String name, boolean ignoreNew) { name = name.toLowerCase(); for (Pet pet : getPetsInternal()) { if (!ignoreNew || !pet.isNew()) { String compName = pet.getName(); compName = compName.toLowerCase(); if (compName.equals(name)) { return pet; } } } return null; }
@PostMapping("/pets/{petId}/edit") 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}"; } }
@Override public PetType parse(String text, Locale locale) throws ParseException { Collection<PetType> findPetTypes = this.pets.findPetTypes(); for (PetType type : findPetTypes) { if (type.getName().equals(text)) { return type; } } throw new ParseException("type not found: " + text, 0); }
@PostMapping("/owners/{ownerId}/edit") 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}"; } }
/** * 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 * (Even though id is not part of the form fields) * * @param petId * @return Pet */ @ModelAttribute("visit") public Visit loadPetWithVisit(@PathVariable("petId") int petId, Map<String, Object> model) { Pet pet = this.pets.findById(petId); model.put("pet", pet); Visit visit = new Visit(); pet.addVisit(visit); return visit; }
@ModelAttribute("owner") public Owner findOwner(@PathVariable("ownerId") int ownerId) { return this.owners.findById(ownerId); }
public void addVisit(Visit visit) { getVisitsInternal().add(visit); visit.setPetId(this.getId()); }
@GetMapping("/owners/new") public String initCreationForm(Map<String, Object> model) { Owner owner = new Owner(); model.put("owner", owner); return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; }
@ModelAttribute("types") public Collection<PetType> populatePetTypes() { return this.pets.findPetTypes(); }
@GetMapping("/pets/{petId}/edit") 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; }
@InitBinder("pet") public void initPetBinder(WebDataBinder dataBinder) { dataBinder.setValidator(new PetValidator()); }
@GetMapping("/owners/{ownerId}/edit") 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; }
@GetMapping("/owners/find") public String initFindForm(Map<String, Object> model) { model.put("owner", new Owner()); return "owners/findOwners"; }