@GetMapping("/pets/new") 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; }
/** * 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; }
@Override public void validate(Object obj, Errors errors) { Pet pet = (Pet) obj; String name = pet.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); } // birth date validation if (pet.getBirthDate() == null) { errors.rejectValue("birthDate", REQUIRED, REQUIRED); } }
public void addVisit(Visit visit) { getVisitsInternal().add(visit); visit.setPetId(this.getId()); }
/** * 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; }
public List<Visit> getVisits() { List<Visit> sortedVisits = new ArrayList<>(getVisitsInternal()); PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false)); return Collections.unmodifiableList(sortedVisits); }
@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}"; } }
@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}"; } }
public void addVisit(Visit visit) { getVisitsInternal().add(visit); visit.setPetId(this.id); }
@Override public void validate(Object obj, Errors errors) { Pet pet = (Pet) obj; String name = pet.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); } // birth date validation if (pet.getBirthDate() == null) { errors.rejectValue("birthDate", REQUIRED, REQUIRED); } }
/** * 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; }
public List<Visit> getVisits() { List<Visit> sortedVisits = new ArrayList<>(getVisitsInternal()); PropertyComparator.sort(sortedVisits, new MutableSortDefinition("date", false, false)); return Collections.unmodifiableList(sortedVisits); }
@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; }
/** * 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; }
@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}"; } }