diff --git a/src/main/java/org/springframework/samples/petclinic/newDataStore/NewOwnerStore.java b/src/main/java/org/springframework/samples/petclinic/newDataStore/NewOwnerStore.java new file mode 100644 index 000000000..bd55e94e6 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/newDataStore/NewOwnerStore.java @@ -0,0 +1,36 @@ +/* + * Software property of Acquisio. Copyright 2003-2018. + */ +package org.springframework.samples.petclinic.newDataStore; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import org.springframework.samples.petclinic.owner.Owner; +import org.springframework.samples.petclinic.owner.OwnerRepository; +import org.springframework.samples.petclinic.owner.StaticOwner; + +/** + * @author Gibran + */ +public class NewOwnerStore { + + public Map ownerStore; + private final OwnerRepository owners; + + public NewOwnerStore(OwnerRepository owners) { + this.owners = owners; + this.ownerStore = new HashMap<>(); + } + + public void populateStore() { + Collection ownerRepositoryData = this.owners.findAll(); + for(Owner owner : ownerRepositoryData) { + ownerStore.put(owner.getId(), convertToStaticOwner(owner)); + } + } + + public StaticOwner convertToStaticOwner(Owner ownerEntity) { + return new StaticOwner(ownerEntity.getAddress(), ownerEntity.getCity(), ownerEntity.getTelephone()); + } +} diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index 068f5245d..cbac6b18e 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -21,6 +21,8 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.dao.DataAccessException; /** * Repository class for Owner domain objects All method names are compliant with Spring Data naming @@ -53,6 +55,15 @@ public interface OwnerRepository extends Repository { @Transactional(readOnly = true) Owner findById(@Param("id") Integer id); + /** + * Retrieve all Owners from the data store. + * + * @return a Collection of Owners + */ + @Transactional(readOnly = true) + @Cacheable("owner") + Collection findAll() throws DataAccessException; + /** * Save an {@link Owner} to the data store, either inserting or updating it. * @param owner the {@link Owner} to save diff --git a/src/main/java/org/springframework/samples/petclinic/owner/StaticOwner.java b/src/main/java/org/springframework/samples/petclinic/owner/StaticOwner.java new file mode 100644 index 000000000..2513bebde --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/owner/StaticOwner.java @@ -0,0 +1,20 @@ +/* + * Software property of Acquisio. Copyright 2003-2018. + */ +package org.springframework.samples.petclinic.owner; + +/** + * @author Gibran + */ +public class StaticOwner { + + private String address; + private String city; + private String telephone; + + public StaticOwner(String address, String city, String telephone){ + this.address = address; + this.city = city; + this.telephone = telephone; + } +}