org.springframework.samples.petclinic.util.EntityUtils Java Examples

The following examples show how to use org.springframework.samples.petclinic.util.EntityUtils. 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: 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 #2
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 #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 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 #4
Source File: AbstractClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void getVets() {
	Collection<Vet> vets = this.clinic.getVets();
	// Use the inherited countRowsInTable() convenience method (from
	// AbstractTransactionalJUnit4SpringContextTests) to verify the results.
	assertEquals("JDBC query must show the same number of vets", super.countRowsInTable("VETS"), vets.size());
	Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
	assertEquals("Leary", v1.getLastName());
	assertEquals(1, v1.getNrOfSpecialties());
	assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
	Vet v2 = EntityUtils.getById(vets, Vet.class, 3);
	assertEquals("Douglas", v2.getLastName());
	assertEquals(2, v2.getNrOfSpecialties());
	assertEquals("dentistry", (v2.getSpecialties().get(0)).getName());
	assertEquals("surgery", (v2.getSpecialties().get(1)).getName());
}
 
Example #5
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 #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: AbstractClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void insertPet() {
	Owner o6 = this.clinic.loadOwner(6);
	int found = o6.getPets().size();
	Pet pet = new Pet();
	pet.setName("bowser");
	Collection<PetType> types = this.clinic.getPetTypes();
	pet.setType(EntityUtils.getById(types, PetType.class, 2));
	pet.setBirthDate(new Date());
	o6.addPet(pet);
	assertEquals(found + 1, o6.getPets().size());
	// both storePet and storeOwner are necessary to cover all ORM tools
	this.clinic.storePet(pet);
	this.clinic.storeOwner(o6);
	// assertTrue(!pet.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
	o6 = this.clinic.loadOwner(6);
	assertEquals(found + 1, o6.getPets().size());
}
 
Example #8
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 #9
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 #10
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 #11
Source File: SimpleJdbcClinic.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Transactional(readOnly = true)
public Pet loadPet(int id) throws DataAccessException {
	JdbcPet pet;
	try {
		pet = this.simpleJdbcTemplate.queryForObject(
				"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=?",
				new JdbcPetRowMapper(),
				id);
	}
	catch (EmptyResultDataAccessException ex) {
		throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
	}
	Owner owner = loadOwner(pet.getOwnerId());
	owner.addPet(pet);
	pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
	loadVisits(pet);
	return pet;
}
 
Example #12
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 #13
Source File: AbstractJpaClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testGetVets() {
	Collection<Vet> vets = this.clinic.getVets();
	// Use the inherited countRowsInTable() convenience method (from
	// AbstractTransactionalDataSourceSpringContextTests) to verify the
	// results.
	assertEquals("JDBC query must show the same number of vets", super.countRowsInTable("VETS"), vets.size());
	Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
	assertEquals("Leary", v1.getLastName());
	assertEquals(1, v1.getNrOfSpecialties());
	assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
	Vet v2 = EntityUtils.getById(vets, Vet.class, 3);
	assertEquals("Douglas", v2.getLastName());
	assertEquals(2, v2.getNrOfSpecialties());
	assertEquals("dentistry", (v2.getSpecialties().get(0)).getName());
	assertEquals("surgery", (v2.getSpecialties().get(1)).getName());
}
 
Example #14
Source File: AbstractClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void loadPet() {
	Collection<PetType> types = this.clinic.getPetTypes();
	Pet p7 = this.clinic.loadPet(7);
	assertTrue(p7.getName().startsWith("Samantha"));
	assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), p7.getType().getId());
	assertEquals("Jean", p7.getOwner().getFirstName());
	Pet p6 = this.clinic.loadPet(6);
	assertEquals("George", p6.getName());
	assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
	assertEquals("Peter", p6.getOwner().getFirstName());
}
 
Example #15
Source File: AbstractJpaClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testLoadPet() {
	Collection<PetType> types = this.clinic.getPetTypes();
	Pet p7 = this.clinic.loadPet(7);
	assertTrue(p7.getName().startsWith("Samantha"));
	assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), p7.getType().getId());
	assertEquals("Jean", p7.getOwner().getFirstName());
	Pet p6 = this.clinic.loadPet(6);
	assertEquals("George", p6.getName());
	assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
	assertEquals("Peter", p6.getOwner().getFirstName());
}
 
Example #16
Source File: JdbcVetRepositoryImpl.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
/**
 * Refresh the cache of Vets that the ClinicService is holding.
 *
 * @see org.springframework.samples.petclinic.model.service.ClinicService#shouldFindVets()
 */
@Override
public Collection<Vet> findAll() throws DataAccessException {
    List<Vet> vets = new ArrayList<Vet>();
    // Retrieve the list of all vets.
    vets.addAll(this.jdbcTemplate.query(
            "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
            BeanPropertyRowMapper.newInstance(Vet.class)));

    // Retrieve the list of all possible specialties.
    final List<Specialty> specialties = this.jdbcTemplate.query(
            "SELECT id, name FROM specialties",
            BeanPropertyRowMapper.newInstance(Specialty.class));

    // Build each vet's list of specialties.
    for (Vet vet : vets) {
        final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
                "SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
                new BeanPropertyRowMapper<Integer>() {
                    @Override
                    public Integer mapRow(ResultSet rs, int row) throws SQLException {
                        return Integer.valueOf(rs.getInt(1));
                    }
                },
                vet.getId().intValue());
        for (int specialtyId : vetSpecialtiesIds) {
            Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
            vet.addSpecialty(specialty);
        }
    }
    return vets;
}
 
Example #17
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldFindAllPetTypes() {
    Collection<PetType> petTypes = this.clinicService.findPetTypes();

    PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
    assertThat(petType1.getName()).isEqualTo("cat");
    PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
    assertThat(petType4.getName()).isEqualTo("snake");
}
 
Example #18
Source File: AbstractClinicServiceTests.java    From docker-workflow-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldFindVets() {
    Collection<Vet> vets = this.clinicService.findVets();

    Vet vet = EntityUtils.getById(vets, Vet.class, 3);
    assertThat(vet.getLastName()).isEqualTo("Douglas");
    assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
    assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
    assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
}
 
Example #19
Source File: JdbcVetRepositoryImpl.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
/**
 * Refresh the cache of Vets that the ClinicService is holding.
 *
 * @see org.springframework.samples.petclinic.model.service.ClinicService#shouldFindVets()
 */
@Override
public Collection<Vet> findAll() throws DataAccessException {
    List<Vet> vets = new ArrayList<Vet>();
    // Retrieve the list of all vets.
    vets.addAll(this.jdbcTemplate.query(
            "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
            BeanPropertyRowMapper.newInstance(Vet.class)));

    // Retrieve the list of all possible specialties.
    final List<Specialty> specialties = this.jdbcTemplate.query(
            "SELECT id, name FROM specialties",
            BeanPropertyRowMapper.newInstance(Specialty.class));

    // Build each vet's list of specialties.
    for (Vet vet : vets) {
        final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
                "SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
                new BeanPropertyRowMapper<Integer>() {
                    @Override
                    public Integer mapRow(ResultSet rs, int row) throws SQLException {
                        return Integer.valueOf(rs.getInt(1));
                    }
                },
                vet.getId().intValue());
        for (int specialtyId : vetSpecialtiesIds) {
            Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
            vet.addSpecialty(specialty);
        }
    }
    return vets;
}
 
Example #20
Source File: AbstractClinicServiceTests.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFindAllPetTypes() {
    Collection<PetType> petTypes = this.clinicService.findPetTypes();

    PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
    assertThat(petType1.getName()).isEqualTo("cat");
    PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
    assertThat(petType4.getName()).isEqualTo("snake");
}
 
Example #21
Source File: AbstractClinicServiceTests.java    From audit4j-demo with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFindVets() {
    Collection<Vet> vets = this.clinicService.findVets();

    Vet vet = EntityUtils.getById(vets, Vet.class, 3);
    assertThat(vet.getLastName()).isEqualTo("Douglas");
    assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
    assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
    assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
}
 
Example #22
Source File: AbstractClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void getPetTypes() {
	Collection<PetType> petTypes = this.clinic.getPetTypes();
	assertEquals("JDBC query must show the same number of pet types", super.countRowsInTable("TYPES"),
			petTypes.size());
	PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
	assertEquals("cat", t1.getName());
	PetType t4 = EntityUtils.getById(petTypes, PetType.class, 4);
	assertEquals("snake", t4.getName());
}
 
Example #23
Source File: AbstractJpaClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testInsertPet() {
	Owner o6 = this.clinic.loadOwner(6);
	int found = o6.getPets().size();
	Pet pet = new Pet();
	pet.setName("bowser");
	Collection<PetType> types = this.clinic.getPetTypes();
	pet.setType(EntityUtils.getById(types, PetType.class, 2));
	pet.setBirthDate(new Date());
	o6.addPet(pet);
	assertEquals(found + 1, o6.getPets().size());
	this.clinic.storeOwner(o6);
	// assertTrue(!pet.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
	o6 = this.clinic.loadOwner(6);
	assertEquals(found + 1, o6.getPets().size());
}
 
Example #24
Source File: JdbcVetRepositoryImpl.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
/**
 * Refresh the cache of Vets that the ClinicService is holding.
 */
@Override
public Collection<Vet> findAll() throws DataAccessException {
    List<Vet> vets = new ArrayList<>();
    // Retrieve the list of all vets.
    vets.addAll(this.jdbcTemplate.query(
        "SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
        BeanPropertyRowMapper.newInstance(Vet.class)));

    // Retrieve the list of all possible specialties.
    final List<Specialty> specialties = this.jdbcTemplate.query(
        "SELECT id, name FROM specialties",
        BeanPropertyRowMapper.newInstance(Specialty.class));

    // Build each vet's list of specialties.
    for (Vet vet : vets) {
        final List<Integer> vetSpecialtiesIds = this.jdbcTemplate.query(
            "SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
            new BeanPropertyRowMapper<Integer>() {
                @Override
                public Integer mapRow(ResultSet rs, int row) throws SQLException {
                    return rs.getInt(1);
                }
            },
            vet.getId());
        for (int specialtyId : vetSpecialtiesIds) {
            Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
            vet.addSpecialty(specialty);
        }
    }
    return vets;
}
 
Example #25
Source File: AbstractJpaClinicTests.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testGetPetTypes() {
	Collection<PetType> petTypes = this.clinic.getPetTypes();
	assertEquals("JDBC query must show the same number of pet types", super.countRowsInTable("TYPES"),
			petTypes.size());
	PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
	assertEquals("cat", t1.getName());
	PetType t4 = EntityUtils.getById(petTypes, PetType.class, 4);
	assertEquals("snake", t4.getName());
}
 
Example #26
Source File: SimpleJdbcClinic.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Loads the {@link Pet} and {@link Visit} data for the supplied
 * {@link Owner}.
 */
private void loadPetsAndVisits(final Owner owner) {
	final List<JdbcPet> pets = this.simpleJdbcTemplate.query(
			"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=?",
			new JdbcPetRowMapper(),
			owner.getId().intValue());
	for (JdbcPet pet : pets) {
		owner.addPet(pet);
		pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
		loadVisits(pet);
	}
}
 
Example #27
Source File: SimpleJdbcClinic.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Refresh the cache of Vets that the Clinic is holding.
 * @see org.springframework.samples.petclinic.Clinic#getVets()
 */
@ManagedOperation
@Transactional(readOnly = true)
public void refreshVetsCache() throws DataAccessException {
	synchronized (this.vets) {
		this.logger.info("Refreshing vets cache");

		// Retrieve the list of all vets.
		this.vets.clear();
		this.vets.addAll(this.simpleJdbcTemplate.query(
				"SELECT id, first_name, last_name FROM vets ORDER BY last_name,first_name",
				ParameterizedBeanPropertyRowMapper.newInstance(Vet.class)));

		// Retrieve the list of all possible specialties.
		final List<Specialty> specialties = this.simpleJdbcTemplate.query(
				"SELECT id, name FROM specialties",
				ParameterizedBeanPropertyRowMapper.newInstance(Specialty.class));

		// Build each vet's list of specialties.
		for (Vet vet : this.vets) {
			final List<Integer> vetSpecialtiesIds = this.simpleJdbcTemplate.query(
					"SELECT specialty_id FROM vet_specialties WHERE vet_id=?",
					new ParameterizedRowMapper<Integer>() {
						public Integer mapRow(ResultSet rs, int row) throws SQLException {
							return Integer.valueOf(rs.getInt(1));
						}},
					vet.getId().intValue());
			for (int specialtyId : vetSpecialtiesIds) {
				Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
				vet.addSpecialty(specialty);
			}
		}
	}
}
 
Example #28
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
public void shouldFindVets() {
    Collection<Vet> vets = this.clinicService.findVets();

    Vet vet = EntityUtils.getById(vets, Vet.class, 3);
    assertThat(vet.getLastName()).isEqualTo("Douglas");
    assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
    assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
    assertThat(vet.getSpecialties().get(1).getName()).isEqualTo("surgery");
}
 
Example #29
Source File: AbstractClinicServiceTests.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Test
public void shouldFindAllPetTypes() {
    Collection<PetType> petTypes = this.clinicService.findPetTypes();

    PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
    assertThat(petType1.getName()).isEqualTo("cat");
    PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
    assertThat(petType4.getName()).isEqualTo("snake");
}
 
Example #30
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);
}