@Test public void getPetTypes() { Collection<PetType> petTypes = this.clinicService.findPetTypes(); PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1); assertEquals("cat", petType1.getName()); PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4); assertEquals("snake", petType4.getName()); }
@Test public void findPet() { Collection<PetType> types = this.clinicService.findPetTypes(); Pet pet7 = this.clinicService.findPetById(7); assertTrue(pet7.getName().startsWith("Samantha")); assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId()); assertEquals("Jean", pet7.getOwner().getFirstName()); Pet pet6 = this.clinicService.findPetById(6); assertEquals("George", pet6.getName()); assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId()); assertEquals("Peter", pet6.getOwner().getFirstName()); }
@Test public void findVets() { Collection<Vet> vets = this.clinicService.findVets(); 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()); }
/** 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); } }
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); // } } }
@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; }
@Test @Transactional public void insertPet() { 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); assertEquals(found + 1, owner6.getPets().size()); // both storePet and storeOwner are necessary to cover all ORM tools this.clinicService.savePet(pet); this.clinicService.saveOwner(owner6); owner6 = this.clinicService.findOwnerById(6); assertEquals(found + 1, owner6.getPets().size()); assertNotNull("Pet Id should have been generated", pet.getId()); }
/** * 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); } } } }
@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; }