mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 20:25:50 +00:00
Merge pull request #152 from DevFactory/release/general-code-quality-fix-1
General code quality fix-1
This commit is contained in:
commit
05f7cf4e3f
5 changed files with 19 additions and 15 deletions
|
@ -41,7 +41,7 @@ public class BaseEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isNew() {
|
public boolean isNew() {
|
||||||
return (this.id == null);
|
return this.id == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ import org.springframework.web.servlet.ModelAndView;
|
||||||
@Controller
|
@Controller
|
||||||
public class OwnerController {
|
public class OwnerController {
|
||||||
|
|
||||||
|
private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm";
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,13 +60,13 @@ public class OwnerController {
|
||||||
public String initCreationForm(Map<String, Object> model) {
|
public String initCreationForm(Map<String, Object> model) {
|
||||||
Owner owner = new Owner();
|
Owner owner = new Owner();
|
||||||
model.put("owner", owner);
|
model.put("owner", owner);
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owners/new", method = RequestMethod.POST)
|
@RequestMapping(value = "/owners/new", method = RequestMethod.POST)
|
||||||
public String processCreationForm(@Valid Owner owner, BindingResult result) {
|
public String processCreationForm(@Valid Owner owner, BindingResult result) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
} else {
|
} else {
|
||||||
this.clinicService.saveOwner(owner);
|
this.clinicService.saveOwner(owner);
|
||||||
return "redirect:/owners/" + owner.getId();
|
return "redirect:/owners/" + owner.getId();
|
||||||
|
@ -107,13 +108,13 @@ public class OwnerController {
|
||||||
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
|
||||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
Owner owner = this.clinicService.findOwnerById(ownerId);
|
||||||
model.addAttribute(owner);
|
model.addAttribute(owner);
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST)
|
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST)
|
||||||
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) {
|
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return "owners/createOrUpdateOwnerForm";
|
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
|
||||||
} else {
|
} else {
|
||||||
owner.setId(ownerId);
|
owner.setId(ownerId);
|
||||||
this.clinicService.saveOwner(owner);
|
this.clinicService.saveOwner(owner);
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.springframework.web.bind.WebDataBinder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,6 +40,7 @@ import java.util.Collection;
|
||||||
@RequestMapping("/owners/{ownerId}")
|
@RequestMapping("/owners/{ownerId}")
|
||||||
public class PetController {
|
public class PetController {
|
||||||
|
|
||||||
|
private static final String VIEWS_PETS_CREATE_OR_UPDATE_FORM = "pets/createOrUpdatePetForm";
|
||||||
private final ClinicService clinicService;
|
private final ClinicService clinicService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -53,8 +55,7 @@ public class PetController {
|
||||||
|
|
||||||
@ModelAttribute("owner")
|
@ModelAttribute("owner")
|
||||||
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
||||||
Owner owner = this.clinicService.findOwnerById(ownerId);
|
return this.clinicService.findOwnerById(ownerId);
|
||||||
return owner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@InitBinder("owner")
|
@InitBinder("owner")
|
||||||
|
@ -72,7 +73,7 @@ public class PetController {
|
||||||
Pet pet = new Pet();
|
Pet pet = new Pet();
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return "pets/createOrUpdatePetForm";
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/pets/new", method = RequestMethod.POST)
|
@RequestMapping(value = "/pets/new", method = RequestMethod.POST)
|
||||||
|
@ -82,7 +83,7 @@ public class PetController {
|
||||||
}
|
}
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return "pets/createOrUpdatePetForm";
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
} else {
|
} else {
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
this.clinicService.savePet(pet);
|
this.clinicService.savePet(pet);
|
||||||
|
@ -94,14 +95,14 @@ public class PetController {
|
||||||
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
|
public String initUpdateForm(@PathVariable("petId") int petId, ModelMap model) {
|
||||||
Pet pet = this.clinicService.findPetById(petId);
|
Pet pet = this.clinicService.findPetById(petId);
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return "pets/createOrUpdatePetForm";
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST)
|
@RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST)
|
||||||
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
|
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
|
||||||
if (result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
model.put("pet", pet);
|
model.put("pet", pet);
|
||||||
return "pets/createOrUpdatePetForm";
|
return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
|
||||||
} else {
|
} else {
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
this.clinicService.savePet(pet);
|
this.clinicService.savePet(pet);
|
||||||
|
|
|
@ -31,23 +31,25 @@ import org.springframework.validation.Validator;
|
||||||
*/
|
*/
|
||||||
public class PetValidator implements Validator {
|
public class PetValidator implements Validator {
|
||||||
|
|
||||||
|
private static final String REQUIRED = "required";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validate(Object obj, Errors errors) {
|
public void validate(Object obj, Errors errors) {
|
||||||
Pet pet = (Pet) obj;
|
Pet pet = (Pet) obj;
|
||||||
String name = pet.getName();
|
String name = pet.getName();
|
||||||
// name validation
|
// name validation
|
||||||
if (!StringUtils.hasLength(name)) {
|
if (!StringUtils.hasLength(name)) {
|
||||||
errors.rejectValue("name", "required", "required");
|
errors.rejectValue("name", REQUIRED, REQUIRED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// type validation
|
// type validation
|
||||||
if (pet.isNew() && pet.getType() == null) {
|
if (pet.isNew() && pet.getType() == null) {
|
||||||
errors.rejectValue("type", "required", "required");
|
errors.rejectValue("type", REQUIRED, REQUIRED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// birth date validation
|
// birth date validation
|
||||||
if (pet.getBirthDate() == null) {
|
if (pet.getBirthDate() == null) {
|
||||||
errors.rejectValue("birthDate", "required", "required");
|
errors.rejectValue("birthDate", REQUIRED, REQUIRED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ public abstract class AbstractClinicServiceTests {
|
||||||
assertThat(owners.size()).isEqualTo(2);
|
assertThat(owners.size()).isEqualTo(2);
|
||||||
|
|
||||||
owners = this.clinicService.findOwnerByLastName("Daviss");
|
owners = this.clinicService.findOwnerByLastName("Daviss");
|
||||||
assertThat(owners.isEmpty());
|
assertThat(owners.isEmpty()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue