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

The following examples show how to use org.springframework.samples.petclinic.model.Owner. 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: 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 #2
Source File: OwnerController.java    From audit4j-demo with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {

    // allow parameterless GET request for /owners to return all records
    if (owner.getLastName() == null) {
        owner.setLastName(""); // empty string signifies broadest possible search
    }

    // find owners by last name
    Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
    if (results.isEmpty()) {
        // no owners found
        result.rejectValue("lastName", "notFound", "not found");
        return "owners/findOwners";
    }
    else if (results.size() == 1) {
	// 1 owner found
	owner = results.iterator().next();
	return "redirect:/owners/" + owner.getId();
    }
    else {
        // multiple owners found
        model.put("selections", results);
        return "owners/ownersList";
    }
}
 
Example #3
Source File: JdbcOwnerRepositoryImpl.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
 * for the corresponding owner, if not already loaded.
 */
@Override
public Owner findById(int id) throws DataAccessException {
    Owner owner;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        owner = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
                params,
                BeanPropertyRowMapper.newInstance(Owner.class)
        );
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Owner.class, id);
    }
    loadPetsAndVisits(owner);
    return owner;
}
 
Example #4
Source File: OwnerController.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {

    // allow parameterless GET request for /owners to return all records
    if (owner.getLastName() == null) {
        owner.setLastName(""); // empty string signifies broadest possible search
    }

    // find owners by last name
    Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
    if (results.isEmpty()) {
        // no owners found
        result.rejectValue("lastName", "notFound", "not found");
        return "owners/findOwners";
    }
    else if (results.size() == 1) {
	// 1 owner found
	owner = results.iterator().next();
	return "redirect:/owners/" + owner.getId();
    }
    else {
        // multiple owners found
        model.put("selections", results);
        return "owners/ownersList";
    }
}
 
Example #5
Source File: JdbcOwnerRepositoryImpl.java    From docker-workflow-plugin with MIT License 6 votes vote down vote up
public void loadPetsAndVisits(final Owner owner) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("id", owner.getId().intValue());
    final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
            "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id",
            params,
            new JdbcPetRowMapper()
    );
    for (JdbcPet pet : pets) {
        owner.addPet(pet);
        pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
        List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
        for (Visit visit : visits) {
            pet.addVisit(visit);
        }
    }
}
 
Example #6
Source File: JdbcOwnerRepositoryImpl.java    From audit4j-demo with Apache License 2.0 6 votes vote down vote up
public void loadPetsAndVisits(final Owner owner) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("id", owner.getId().intValue());
    final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
            "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id",
            params,
            new JdbcPetRowMapper()
    );
    for (JdbcPet pet : pets) {
        owner.addPet(pet);
        pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
        List<Visit> visits = this.visitRepository.findByPetId(pet.getId());
        for (Visit visit : visits) {
            pet.addVisit(visit);
        }
    }
}
 
Example #7
Source File: JdbcOwnerRepositoryImpl.java    From audit4j-demo with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
 * for the corresponding owner, if not already loaded.
 */
@Override
public Owner findById(int id) throws DataAccessException {
    Owner owner;
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        owner = this.namedParameterJdbcTemplate.queryForObject(
                "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
                params,
                BeanPropertyRowMapper.newInstance(Owner.class)
        );
    } catch (EmptyResultDataAccessException ex) {
        throw new ObjectRetrievalFailureException(Owner.class, id);
    }
    loadPetsAndVisits(owner);
    return owner;
}
 
Example #8
Source File: OwnerController.java    From DevOps-for-Web-Development with MIT License 6 votes vote down vote up
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {

    // allow parameterless GET request for /owners to return all records
    if (owner.getLastName() == null) {
        owner.setLastName(""); // empty string signifies broadest possible search
    }

    // find owners by last name
    Collection<Owner> results = this.clinicService.findOwnerByLastName(owner.getLastName());
    if (results.isEmpty()) {
        // no owners found
        result.rejectValue("lastName", "notFound", "not found");
        return "owners/findOwners";
    } else if (results.size() == 1) {
        // 1 owner found
        owner = results.iterator().next();
        return "redirect:/owners/" + owner.getId();
    } else {
        // multiple owners found
        model.put("selections", results);
        return "owners/ownersList";
    }
}
 
Example #9
Source File: JdbcOwnerRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
public void loadPetsAndVisits(final Owner owner) {
    Map<String, Object> params = new HashMap<>();
    params.put("id", owner.getId());
    final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
        "SELECT pets.id, name, birth_date, type_id, owner_id, visits.id as visit_id, visit_date, description, pet_id FROM pets LEFT OUTER JOIN visits ON  pets.id = pet_id WHERE owner_id=:id",
        params,
        new JdbcPetVisitExtractor()
    );
    Collection<PetType> petTypes = getPetTypes();
    for (JdbcPet pet : pets) {
        pet.setType(EntityUtils.getById(petTypes, PetType.class, pet.getTypeId()));
        owner.addPet(pet);
    }
}
 
Example #10
Source File: OwnerControllerTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Before
public void setup() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(ownerController).build();

    george = new Owner();
    george.setId(TEST_OWNER_ID);
    george.setFirstName("George");
    george.setLastName("Franklin");
    george.setAddress("110 W. Liberty St.");
    george.setCity("Madison");
    george.setTelephone("6085551023");
    given(this.clinicService.findOwnerById(TEST_OWNER_ID)).willReturn(george);

}
 
Example #11
Source File: OwnerController.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/new", method = RequestMethod.POST)
public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
    if (result.hasErrors()) {
        return "owners/createOrUpdateOwnerForm";
    } else {
        this.clinicService.saveOwner(owner);
        status.setComplete();
        return "redirect:/owners/" + owner.getId();
    }
}
 
Example #12
Source File: JdbcOwnerRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Override
public void save(Owner owner) throws DataAccessException {
    BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner);
    if (owner.isNew()) {
        Number newKey = this.insertOwner.executeAndReturnKey(parameterSource);
        owner.setId(newKey.intValue());
    } else {
        this.namedParameterJdbcTemplate.update(
            "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " +
                "city=:city, telephone=:telephone WHERE id=:id",
            parameterSource);
    }
}
 
Example #13
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 "pets/createOrUpdatePetForm";
}
 
Example #14
Source File: OwnerController.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST)
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, @PathVariable("ownerId") int ownerId) {
    if (result.hasErrors()) {
        return "owners/createOrUpdateOwnerForm";
    } else {
        owner.setId(ownerId);
        this.clinicService.saveOwner(owner);
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #15
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
public void shouldFindSingleOwnerWithPet() {
    Owner owner = this.clinicService.findOwnerById(1);
    assertThat(owner.getLastName()).startsWith("Franklin");
    assertThat(owner.getPets().size()).isEqualTo(1);
    assertThat(owner.getPets().get(0).getType()).isNotNull();
    assertThat(owner.getPets().get(0).getType().getName()).isEqualTo("cat");
}
 
Example #16
Source File: OwnerController.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.PUT)
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
    if (result.hasErrors()) {
        return "owners/createOrUpdateOwnerForm";
    } else {
        this.clinicService.saveOwner(owner);
        status.setComplete();
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #17
Source File: JpaOwnerRepositoryImpl.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@Override
public Owner findById(int id) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
    query.setParameter("id", id);
    return (Owner) query.getSingleResult();
}
 
Example #18
Source File: OwnerController.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/owners/new", method = RequestMethod.POST)
public String processCreationForm(@Valid Owner owner, BindingResult result, SessionStatus status) {
    if (result.hasErrors()) {
        return "owners/createOrUpdateOwnerForm";
    } else {
        this.clinicService.saveOwner(owner);
        status.setComplete();
        return "redirect:/owners/" + owner.getId();
    }
}
 
Example #19
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 #20
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 #21
Source File: OwnerController.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/{ownerId}/edit", method = RequestMethod.POST)
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.clinicService.saveOwner(owner);
        return "redirect:/owners/{ownerId}";
    }
}
 
Example #22
Source File: JdbcOwnerRepositoryImpl.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@Override
public void save(Owner owner) throws DataAccessException {
    BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner);
    if (owner.isNew()) {
        Number newKey = this.insertOwner.executeAndReturnKey(parameterSource);
        owner.setId(newKey.intValue());
    } else {
        this.namedParameterJdbcTemplate.update(
                "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " +
                        "city=:city, telephone=:telephone WHERE id=:id",
                parameterSource);
    }
}
 
Example #23
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldFindOwnersByLastName() {
    Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis");
    assertThat(owners.size()).isEqualTo(2);

    owners = this.clinicService.findOwnerByLastName("Daviss");
    assertThat(owners.isEmpty());
}
 
Example #24
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
@Transactional
public void shouldUpdateOwner()  {
    Owner owner = this.clinicService.findOwnerById(1);
    String oldLastName = owner.getLastName();
    String newLastName = oldLastName + "X";
    
    owner.setLastName(newLastName);
    this.clinicService.saveOwner(owner);

    // retrieving new name from database
    owner = this.clinicService.findOwnerById(1);
    assertThat(owner.getLastName()).isEqualTo(newLastName);
}
 
Example #25
Source File: JpaOwnerRepositoryImpl.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Override
public Owner findById(int id) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
    query.setParameter("id", id);
    return (Owner) query.getSingleResult();
}
 
Example #26
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
public void shouldFindOwnersByLastName() {
    Collection<Owner> owners = this.clinicService.findOwnerByLastName("Davis");
    assertThat(owners.size()).isEqualTo(2);

    owners = this.clinicService.findOwnerByLastName("Daviss");
    assertThat(owners.isEmpty());
}
 
Example #27
Source File: JdbcOwnerRepositoryImpl.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Override
public void save(Owner owner) throws DataAccessException {
    BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner);
    if (owner.isNew()) {
        Number newKey = this.insertOwner.executeAndReturnKey(parameterSource);
        owner.setId(newKey.intValue());
    } else {
        this.namedParameterJdbcTemplate.update(
                "UPDATE owners SET first_name=:firstName, last_name=:lastName, address=:address, " +
                        "city=:city, telephone=:telephone WHERE id=:id",
                parameterSource);
    }
}
 
Example #28
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
@Transactional
public void shouldUpdateOwner() {
    Owner owner = this.clinicService.findOwnerById(1);
    String oldLastName = owner.getLastName();
    String newLastName = oldLastName + "X";

    owner.setLastName(newLastName);
    this.clinicService.saveOwner(owner);

    // retrieving new name from database
    owner = this.clinicService.findOwnerById(1);
    assertThat(owner.getLastName()).isEqualTo(newLastName);
}
 
Example #29
Source File: OwnerController.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@RequestMapping(value = "/owners/new", method = RequestMethod.POST)
public String processCreationForm(@Valid Owner owner, BindingResult result) {
    if (result.hasErrors()) {
        return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
    } else {
        this.clinicService.saveOwner(owner);
        return "redirect:/owners/" + owner.getId();
    }
}
 
Example #30
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}";
    }
}