From 961b43d5d25098b369ac20abd6eadd6853513b11 Mon Sep 17 00:00:00 2001 From: jateeter Date: Tue, 4 Feb 2014 00:46:24 -0500 Subject: [PATCH] Subscription/ThirdParty Integration --- .../espi/common/domain/Subscription.java | 10 ++++---- .../jpa/SubscriptionRepositoryImpl.java | 2 +- .../ApplicationInformationService.java | 2 ++ .../espi/common/service/ResourceService.java | 2 ++ .../common/service/SubscriptionService.java | 2 ++ .../ApplicationInformationServiceImpl.java | 8 +++++++ .../service/impl/ExportServiceImpl.java | 8 +++++-- .../service/impl/ResourceServiceImpl.java | 8 ++++++- .../service/impl/SubscriptionServiceImpl.java | 24 +++++++++++++++++++ 9 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/energyos/espi/common/domain/Subscription.java b/src/main/java/org/energyos/espi/common/domain/Subscription.java index 6ecc3975..bb7242f6 100644 --- a/src/main/java/org/energyos/espi/common/domain/Subscription.java +++ b/src/main/java/org/energyos/espi/common/domain/Subscription.java @@ -36,7 +36,9 @@ import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.ArrayList; import java.util.Calendar; +import java.util.List; import java.util.Set; /** @@ -94,8 +96,8 @@ public class Subscription @XmlTransient @ManyToMany - @LazyCollection(LazyCollectionOption.FALSE) - private Set usagePoints; + @LazyCollection(LazyCollectionOption.TRUE) + private List usagePoints; public Calendar lastUpdate; @@ -131,11 +133,11 @@ public Calendar getLastUpdate() { return lastUpdate; } - public Set getUsagePoints() { + public List getUsagePoints() { return usagePoints; } - public void setUsagePoints(Set usagePoints) { + public void setUsagePoints(List usagePoints) { this.usagePoints = usagePoints; } diff --git a/src/main/java/org/energyos/espi/common/repositories/jpa/SubscriptionRepositoryImpl.java b/src/main/java/org/energyos/espi/common/repositories/jpa/SubscriptionRepositoryImpl.java index 7b6c8297..1c05b6b2 100755 --- a/src/main/java/org/energyos/espi/common/repositories/jpa/SubscriptionRepositoryImpl.java +++ b/src/main/java/org/energyos/espi/common/repositories/jpa/SubscriptionRepositoryImpl.java @@ -70,7 +70,7 @@ public Subscription findById(Long id) { @Transactional public void deleteById(Long id) { Subscription subscription = findById(id); - Set ups = subscription.getUsagePoints(); + List ups = subscription.getUsagePoints(); Iterator it = ups.iterator(); while (it.hasNext()) { UsagePoint up = it.next(); diff --git a/src/main/java/org/energyos/espi/common/service/ApplicationInformationService.java b/src/main/java/org/energyos/espi/common/service/ApplicationInformationService.java index f3f963f9..de9d76b1 100755 --- a/src/main/java/org/energyos/espi/common/service/ApplicationInformationService.java +++ b/src/main/java/org/energyos/espi/common/service/ApplicationInformationService.java @@ -54,5 +54,7 @@ public ApplicationInformation findByDataCustodianClientId( public void setApplicationInformation(ApplicationInformation applicationInformation); public String getDataCustodianResourceEndpoint(); + + public String getThirdPartyNotifyURI(); } diff --git a/src/main/java/org/energyos/espi/common/service/ResourceService.java b/src/main/java/org/energyos/espi/common/service/ResourceService.java index e58cfe72..506a7994 100755 --- a/src/main/java/org/energyos/espi/common/service/ResourceService.java +++ b/src/main/java/org/energyos/espi/common/service/ResourceService.java @@ -44,6 +44,8 @@ List findByAllParentsHref(String relatedHref, Long findIdByXPath(Long id1, Long id2, Long id3, Long id4, Class clazz); EntryTypeIterator findEntryTypeIterator(Class clazz); + + EntryTypeIterator findEntryTypeIterator(List ids, Class clazz); EntryType findEntryType(long id1, Class clazz); diff --git a/src/main/java/org/energyos/espi/common/service/SubscriptionService.java b/src/main/java/org/energyos/espi/common/service/SubscriptionService.java index 521c025e..ab4bfe95 100755 --- a/src/main/java/org/energyos/espi/common/service/SubscriptionService.java +++ b/src/main/java/org/energyos/espi/common/service/SubscriptionService.java @@ -51,5 +51,7 @@ public interface SubscriptionService { public Subscription findById(Long retailCustomerId, Long subscriptionId); public Subscription findByUUID(UUID uuid); + + public List findUsagePointIds(Long subscriptionId); } diff --git a/src/main/java/org/energyos/espi/common/service/impl/ApplicationInformationServiceImpl.java b/src/main/java/org/energyos/espi/common/service/impl/ApplicationInformationServiceImpl.java index 2198e5a3..c7b811cc 100755 --- a/src/main/java/org/energyos/espi/common/service/impl/ApplicationInformationServiceImpl.java +++ b/src/main/java/org/energyos/espi/common/service/impl/ApplicationInformationServiceImpl.java @@ -202,4 +202,12 @@ public ApplicationInformation importResource(InputStream stream) { return applicationInformation; } + @Override + public String getThirdPartyNotifyURI() { + ApplicationInformation applicationInformation; + // TODO note the assumption on the first (seed) entry + applicationInformation = resourceService.findById(1L, ApplicationInformation.class); + return applicationInformation.getThirdPartyNotifyUri(); + } + } diff --git a/src/main/java/org/energyos/espi/common/service/impl/ExportServiceImpl.java b/src/main/java/org/energyos/espi/common/service/impl/ExportServiceImpl.java index 42052845..43045b9c 100644 --- a/src/main/java/org/energyos/espi/common/service/impl/ExportServiceImpl.java +++ b/src/main/java/org/energyos/espi/common/service/impl/ExportServiceImpl.java @@ -485,7 +485,9 @@ public void exportSubscription(String subscriptionHashedId, OutputStream stream, @Override public void exportBatchSubscription(long subscriptionId, OutputStream stream, ExportFilter exportFilter) throws IOException { String hrefFragment = "/Batch/Subscription/" + subscriptionId; - exportEntriesFull(subscriptionService.findEntryTypeIterator(subscriptionId), stream, exportFilter, hrefFragment); + // first find all the usagePointIds this subscription is related to + List usagePointIdList = subscriptionService.findUsagePointIds(subscriptionId); + exportEntriesFull(resourceService.findEntryTypeIterator(usagePointIdList, UsagePoint.class), stream, exportFilter, hrefFragment); } @Override @@ -574,9 +576,11 @@ private String adjustFragment(String fragment, EntryType entry ) { // TODO here need the proper URI fragment for a subscription result = "/RetailCustomer/" + rc.getId() + "/UsagePoint"; } if (fragment.contains("Subscription")) { + // the entry contains a valid usagepoint at this stage + // UsagePoint up = entry.getContent().getUsagePoint(); RetailCustomer rc = up.getRetailCustomer(); - // TODO here need the proper URI fragment for a subscription + result = "/RetailCustomer/" + rc.getId() + "/UsagePoint"; } if (fragment.contains("/Batch/RetailCustomer")) { diff --git a/src/main/java/org/energyos/espi/common/service/impl/ResourceServiceImpl.java b/src/main/java/org/energyos/espi/common/service/impl/ResourceServiceImpl.java index 50e3268a..1d8a64e3 100755 --- a/src/main/java/org/energyos/espi/common/service/impl/ResourceServiceImpl.java +++ b/src/main/java/org/energyos/espi/common/service/impl/ResourceServiceImpl.java @@ -139,6 +139,12 @@ public void setRepository(ResourceRepository repository) { @Override public EntryTypeIterator findEntryTypeIterator(Class clazz) { List idList = repository.findAllIds(clazz); + return findEntryTypeIterator(idList, clazz); + } + + @Override + public EntryTypeIterator findEntryTypeIterator(List ids, Class clazz) { + List idList = ids; EntryTypeIterator result = null; try { result = (new EntryTypeIterator(this, idList, clazz)); @@ -149,7 +155,7 @@ public EntryTypeIterator findEntryTypeIterator(Clas } return result; } - + @Override public EntryType findEntryType(long id1, Class clazz) { EntryType result = null; diff --git a/src/main/java/org/energyos/espi/common/service/impl/SubscriptionServiceImpl.java b/src/main/java/org/energyos/espi/common/service/impl/SubscriptionServiceImpl.java index 686729bc..72d2279f 100755 --- a/src/main/java/org/energyos/espi/common/service/impl/SubscriptionServiceImpl.java +++ b/src/main/java/org/energyos/espi/common/service/impl/SubscriptionServiceImpl.java @@ -32,6 +32,7 @@ import org.energyos.espi.common.utils.DateConverter; import org.energyos.espi.common.utils.EntryTypeIterator; import org.energyos.espi.common.utils.ExportFilter; +import org.hibernate.mapping.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.oauth2.provider.OAuth2Authentication; @@ -75,6 +76,16 @@ public Subscription createSubscription(OAuth2Authentication authentication) { subscription.setUUID(UUID.randomUUID()); subscription.setApplicationInformation(applicationInformationService.findByClientId(authentication.getOAuth2Request().getClientId())); subscription.setRetailCustomer((RetailCustomer)authentication.getPrincipal()); + subscription.setUsagePoints(new ArrayList ()); + // set up the subscription's usagePoints list. Keep in mind that right + // now this is ALL usage points belonging to the RetailCustomer. + // TODO - scope this to only a selected (proper) subset of the usagePoints. + List upIds = resourceService.findAllIdsByXPath(subscription.getRetailCustomer().getId(), UsagePoint.class); + Iterator it = upIds.iterator(); + while (it.hasNext()) { + UsagePoint usagePoint = resourceService.findById(it.next(), UsagePoint.class); + subscription.getUsagePoints().add(usagePoint); + } subscription.setLastUpdate(new GregorianCalendar()); subscriptionRepository.persist(subscription); @@ -214,4 +225,17 @@ public Subscription findById(Long retailCustomerId, Long subscriptionId) { return subscriptionRepository.findById(subscriptionId); } + @Override + public List findUsagePointIds(Long subscriptionId) { + + List result = new ArrayList(); + Subscription subscription = subscriptionRepository.findById(subscriptionId); + Iterator it = subscription.getUsagePoints().iterator(); + while (it.hasNext()) { + result.add(it.next().getId()); + } + + return result; + } + }