org.springframework.samples.petclinic.model.Pet Java Examples

The following examples show how to use org.springframework.samples.petclinic.model.Pet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PetValidator.java    From DevOps-for-Web-Development with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #2
Source File: PetValidator.java    From audit4j-demo with Apache License 2.0 6 votes vote down vote up
public void validate(Pet pet, Errors errors) {
    String name = pet.getName();
    // name validaation
    if (!StringUtils.hasLength(name)) {
        errors.rejectValue("name", "required", "required");
    } else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) {
        errors.rejectValue("name", "duplicate", "already exists");
    }
    
    // type valication
    if (pet.isNew() && pet.getType() == null) {
        errors.rejectValue("type", "required", "required");
    }
    
 // type valication
    if (pet.getBirthDate()==null) {
        errors.rejectValue("birthDate", "required", "required");
    }
}
 
Example #3
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 6 votes vote down vote up
@Test
@Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() {
    Owner owner6 = this.clinicService.findOwnerById(6);
    int found = owner6.getPets().size();

    Pet pet = new Pet();
    pet.setName("bowser");
    Collection<PetType> types = this.clinicService.findPetTypes();
    pet.setType(EntityUtils.getById(types, PetType.class, 2));
    pet.setBirthDate(new Date());
    owner6.addPet(pet);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);

    this.clinicService.savePet(pet);
    this.clinicService.saveOwner(owner6);

    owner6 = this.clinicService.findOwnerById(6);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);
    // checks that id has been generated
    assertThat(pet.getId()).isNotNull();
}
 
Example #4
Source File: PetValidator.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
public void validate(Pet pet, Errors errors) {
    String name = pet.getName();
    // name validaation
    if (!StringUtils.hasLength(name)) {
        errors.rejectValue("name", "required", "required");
    } else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) {
        errors.rejectValue("name", "duplicate", "already exists");
    }
    
    // type valication
    if (pet.isNew() && pet.getType() == null) {
        errors.rejectValue("type", "required", "required");
    }
    
 // type valication
    if (pet.getBirthDate()==null) {
        errors.rejectValue("birthDate", "required", "required");
    }
}
 
Example #5
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
@Test
@Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() {
    Owner owner6 = this.clinicService.findOwnerById(6);
    int found = owner6.getPets().size();
    
    Pet pet = new Pet();
    pet.setName("bowser");
    Collection<PetType> types = this.clinicService.findPetTypes();
    pet.setType(EntityUtils.getById(types, PetType.class, 2));
    pet.setBirthDate(new DateTime());
    owner6.addPet(pet);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);
    
    this.clinicService.savePet(pet);
    this.clinicService.saveOwner(owner6);
    
    owner6 = this.clinicService.findOwnerById(6);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);
    // checks that id has been generated
    assertThat(pet.getId()).isNotNull();
}
 
Example #6
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 6 votes vote down vote up
@Test
@Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() {
    Owner owner6 = this.clinicService.findOwnerById(6);
    int found = owner6.getPets().size();

    Pet pet = new Pet();
    pet.setName("bowser");
    Collection<PetType> types = this.clinicService.findPetTypes();
    pet.setType(EntityUtils.getById(types, PetType.class, 2));
    pet.setBirthDate(new LocalDate());
    owner6.addPet(pet);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);

    this.clinicService.savePet(pet);
    this.clinicService.saveOwner(owner6);

    owner6 = this.clinicService.findOwnerById(6);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);
    // checks that id has been generated
    assertThat(pet.getId()).isNotNull();
}
 
Example #7
Source File: PetValidator.java    From DevOps-for-Web-Development with MIT License 6 votes vote down vote up
@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");
    }
}
 
Example #8
Source File: JdbcPetRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 6 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        pet = this.namedParameterJdbcTemplate.queryForObject(
            "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
            params,
            new JdbcPetRowMapper());
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, id);
    }
    Owner owner = this.ownerRepository.findById(pet.getOwnerId());
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

    List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
    return pet;
}
 
Example #9
Source File: JdbcPetRepositoryImpl.java    From audit4j-demo with Apache License 2.0 6 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        pet = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
                params,
                new JdbcPetRowMapper());
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
    }
    Owner owner = this.ownerRepository.findById(pet.getOwnerId());
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

    List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
    return pet;
}
 
Example #10
Source File: JdbcPetRepositoryImpl.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    JdbcPet pet;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        pet = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
                params,
                new JdbcPetRowMapper());
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
    }
    Owner owner = this.ownerRepository.findById(pet.getOwnerId());
    owner.addPet(pet);
    pet.setType(EntityUtils.getById(findPetTypes(), PetType.class, pet.getTypeId()));

    List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
    for (Visit visit : visits) {
        pet.addVisit(visit);
    }
    return pet;
}
 
Example #11
Source File: AbstractClinicServiceTests.java    From audit4j-demo with Apache License 2.0 6 votes vote down vote up
@Test
@Transactional
public void shouldInsertPetIntoDatabaseAndGenerateId() {
    Owner owner6 = this.clinicService.findOwnerById(6);
    int found = owner6.getPets().size();
    
    Pet pet = new Pet();
    pet.setName("bowser");
    Collection<PetType> types = this.clinicService.findPetTypes();
    pet.setType(EntityUtils.getById(types, PetType.class, 2));
    pet.setBirthDate(new DateTime());
    owner6.addPet(pet);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);
    
    this.clinicService.savePet(pet);
    this.clinicService.saveOwner(owner6);
    
    owner6 = this.clinicService.findOwnerById(6);
    assertThat(owner6.getPets().size()).isEqualTo(found + 1);
    // checks that id has been generated
    assertThat(pet.getId()).isNotNull();
}
 
Example #12
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldFindPetWithCorrectId() {
    Pet pet7 = this.clinicService.findPetById(7);
    assertThat(pet7.getName()).startsWith("Samantha");
    assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");
    
}
 
Example #13
Source File: PetController.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
    // we're not using @Valid annotation here because it is easier to define such validation rule in Java
    new PetValidator().validate(pet, result);
    if (result.hasErrors()) {
        return "pets/createOrUpdatePetForm";
    } else {
        this.clinicService.savePet(pet);
        status.setComplete();
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #14
Source File: JpaPetRepositoryImpl.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@Override
public void save(Pet pet) {
	if (pet.getId() == null) {
		this.em.persist(pet);     		
	}
	else {
		this.em.merge(pet);    
	}
}
 
Example #15
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
@Transactional
public void shouldAddNewVisitForPet() {
    Pet pet7 = this.clinicService.findPetById(7);
    int found = pet7.getVisits().size();
    Visit visit = new Visit();
    pet7.addVisit(visit);
    visit.setDescription("test");	    
    this.clinicService.saveVisit(visit);
    this.clinicService.savePet(pet7);

    pet7 = this.clinicService.findPetById(7);
    assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
    assertThat(visit.getId()).isNotNull();
}
 
Example #16
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
@Transactional
public void sholdUpdatePetName() throws Exception {
    Pet pet7 = this.clinicService.findPetById(7);
    String oldName = pet7.getName();
    
    String newName = oldName + "X";
	pet7.setName(newName);
    this.clinicService.savePet(pet7);

    pet7 = this.clinicService.findPetById(7);
    assertThat(pet7.getName()).isEqualTo(newName);
}
 
Example #17
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
public void shouldFindPetWithCorrectId() {
    Pet pet7 = this.clinicService.findPetById(7);
    assertThat(pet7.getName()).startsWith("Samantha");
    assertThat(pet7.getOwner().getFirstName()).isEqualTo("Jean");

}
 
Example #18
Source File: PetController.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
    // we're not using @Valid annotation here because it is easier to define such validation rule in Java
    new PetValidator().validate(pet, result);
    if (result.hasErrors()) {
        return "pets/createOrUpdatePetForm";
    } else {
        this.clinicService.savePet(pet);
        status.setComplete();
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #19
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
@Transactional
public void shouldAddNewVisitForPet() {
    Pet pet7 = this.clinicService.findPetById(7);
    int found = pet7.getVisits().size();
    Visit visit = new Visit();
    pet7.addVisit(visit);
    visit.setDescription("test");
    this.clinicService.saveVisit(visit);
    this.clinicService.savePet(pet7);

    pet7 = this.clinicService.findPetById(7);
    assertThat(pet7.getVisits().size()).isEqualTo(found + 1);
    assertThat(visit.getId()).isNotNull();
}
 
Example #20
Source File: PetController.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST)
public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
    new PetValidator().validate(pet, result);
    if (result.hasErrors()) {
        return "pets/createOrUpdatePetForm";
    } else {
        this.clinicService.savePet(pet);
        status.setComplete();
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #21
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
@Transactional
public void shouldUpdatePetName() throws Exception {
    Pet pet7 = this.clinicService.findPetById(7);
    String oldName = pet7.getName();

    String newName = oldName + "X";
    pet7.setName(newName);
    this.clinicService.savePet(pet7);

    pet7 = this.clinicService.findPetById(7);
    assertThat(pet7.getName()).isEqualTo(newName);
}
 
Example #22
Source File: JdbcPetRepositoryImpl.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
/**
 * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} instance.
 */
private MapSqlParameterSource createPetParameterSource(Pet pet) {
    return new MapSqlParameterSource()
            .addValue("id", pet.getId())
            .addValue("name", pet.getName())
            .addValue("birth_date", pet.getBirthDate().toDate())
            .addValue("type_id", pet.getType().getId())
            .addValue("owner_id", pet.getOwner().getId());
}
 
Example #23
Source File: PetController.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@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;
}
 
Example #24
Source File: PetControllerTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Before
public void setup() {
    this.mockMvc = MockMvcBuilders
        .standaloneSetup(petController)
        .setConversionService(formattingConversionServiceFactoryBean.getObject())
        .build();

    PetType cat = new PetType();
    cat.setId(3);
    cat.setName("hamster");
    given(this.clinicService.findPetTypes()).willReturn(Lists.newArrayList(cat));
    given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(new Owner());
    given(this.clinicService.findPetById(TEST_PET_ID)).willReturn(new Pet());
}
 
Example #25
Source File: JdbcPetRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
/**
 * Creates a {@link MapSqlParameterSource} based on data values from the supplied {@link Pet} instance.
 */
private MapSqlParameterSource createPetParameterSource(Pet pet) {
    return new MapSqlParameterSource()
        .addValue("id", pet.getId())
        .addValue("name", pet.getName())
        .addValue("birth_date", pet.getBirthDate())
        .addValue("type_id", pet.getType().getId())
        .addValue("owner_id", pet.getOwner().getId());
}
 
Example #26
Source File: JdbcPetRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Override
public Pet findById(int id) throws DataAccessException {
    Integer ownerId;
    try {
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        ownerId = this.namedParameterJdbcTemplate.queryForObject("SELECT owner_id FROM pets WHERE id=:id", params, Integer.class);
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Pet.class, id);
    }
    Owner owner = this.ownerRepository.findById(ownerId);
    return EntityUtils.getById(owner.getPets(), Pet.class, id);
}
 
Example #27
Source File: JpaPetRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Override
public void save(Pet pet) {
    if (pet.getId() == null) {
        this.em.persist(pet);
    } else {
        this.em.merge(pet);
    }
}
 
Example #28
Source File: PetController.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@RequestMapping(value = "/pets/{petId}/edit", method = RequestMethod.POST)
public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owner, ModelMap model) {
    if (result.hasErrors()) {
        model.put("pet", pet);
        return VIEWS_PETS_CREATE_OR_UPDATE_FORM;
    } else {
        owner.addPet(pet);
        this.clinicService.savePet(pet);
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #29
Source File: PetController.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@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.clinicService.savePet(pet);
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #30
Source File: JdbcPetRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Override
public void save(Pet pet) throws DataAccessException {
    if (pet.isNew()) {
        Number newKey = this.insertPet.executeAndReturnKey(
            createPetParameterSource(pet));
        pet.setId(newKey.intValue());
    } else {
        this.namedParameterJdbcTemplate.update(
            "UPDATE pets SET name=:name, birth_date=:birth_date, type_id=:type_id, " +
                "owner_id=:owner_id WHERE id=:id",
            createPetParameterSource(pet));
    }
}