Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Commit c352e0e

Browse files
committed
Subscription/Repository integration (at last:)
Unification of the .jsp substructure (step 1)
1 parent 77b52fe commit c352e0e

File tree

18 files changed

+1182
-122
lines changed

18 files changed

+1182
-122
lines changed

src/main/java/org/energyos/espi/thirdparty/web/AuthorizationController.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import org.energyos.espi.common.domain.AccessToken;
2020
import org.energyos.espi.common.domain.ApplicationInformation;
2121
import org.energyos.espi.common.domain.Authorization;
22+
import org.energyos.espi.common.domain.RetailCustomer;
2223
import org.energyos.espi.common.domain.Routes;
2324
import org.energyos.espi.common.service.AuthorizationService;
25+
import org.energyos.espi.thirdparty.repository.UsagePointRESTRepository;
2426
import org.springframework.beans.factory.annotation.Autowired;
2527
import org.springframework.beans.factory.annotation.Qualifier;
2628
import org.springframework.dao.EmptyResultDataAccessException;
@@ -37,12 +39,20 @@
3739
import java.util.GregorianCalendar;
3840

3941
import javax.persistence.NoResultException;
42+
import javax.xml.bind.JAXBException;
4043

4144
@Controller
4245
public class AuthorizationController extends BaseController {
4346

4447
@Autowired
4548
private AuthorizationService authorizationService;
49+
50+
//TODO - the following is legacy code that will do the import
51+
// of a subscription from a DC. Needs to be separated out of the "repository"
52+
// level and be callable as a TP specific service level.
53+
//
54+
@Autowired
55+
private UsagePointRESTRepository usagePointRESTRepository;
4656

4757
@Autowired
4858
@Qualifier("clientRestTemplateFactory")
@@ -93,6 +103,19 @@ public String authorization(String code, String state, ModelMap model, Principal
93103
// Update authorization record with /oauth/token response data
94104
authorizationService.merge(authorization);
95105

106+
// now do the initial import of the Authorized Resouce, if it is
107+
// not ready, then we will wait till we receive a Notify or the UX call for it.
108+
// TODO: create a Subscription to work with if needed
109+
//
110+
RetailCustomer currentCustomer = currentCustomer(principal);
111+
try {
112+
usagePointRESTRepository.findAllByRetailCustomerId(currentCustomer.getId());
113+
} catch (JAXBException e) {
114+
// nothing there, so log the fact and move on. It will
115+
// get imported later.
116+
System.out.printf("ThirdParty Import Exception: %s\n", e.toString());
117+
e.printStackTrace();
118+
}
96119
} catch (HttpClientErrorException x) {
97120

98121
//TODO: Extract error, error_description and error_uri from JSON response. Currently recording null for all three fields.

src/main/java/org/energyos/espi/thirdparty/web/BaseController.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818

1919
import org.energyos.espi.common.domain.RetailCustomer;
2020
import org.springframework.security.core.Authentication;
21+
import org.springframework.web.bind.annotation.ModelAttribute;
2122

2223
import java.security.Principal;
2324

2425
public class BaseController {
26+
27+
@ModelAttribute("currentCustomer")
2528
public RetailCustomer currentCustomer(Principal principal) {
26-
if (principal == null) return null;
27-
return (RetailCustomer)((Authentication)principal).getPrincipal();
29+
try {
30+
return (RetailCustomer) ((Authentication)principal).getPrincipal();
31+
} catch (Exception e) {
32+
return null;
33+
}
2834
}
2935

3036
public boolean isUserCustodian(Principal principal) {

src/main/java/org/energyos/espi/thirdparty/web/MeterReadingController.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,64 @@
1616

1717
package org.energyos.espi.thirdparty.web;
1818

19+
import org.energyos.espi.common.domain.IntervalBlock;
20+
import org.energyos.espi.common.domain.IntervalReading;
21+
import org.energyos.espi.common.domain.MeterReading;
1922
import org.energyos.espi.common.domain.RetailCustomer;
2023
import org.energyos.espi.common.domain.Routes;
24+
import org.energyos.espi.common.service.MeterReadingService;
25+
import org.energyos.espi.thirdparty.web.BaseController;
2126
import org.energyos.espi.thirdparty.service.MeterReadingRESTService;
2227
import org.springframework.beans.factory.annotation.Autowired;
2328
import org.springframework.security.access.prepost.PreAuthorize;
2429
import org.springframework.stereotype.Controller;
30+
import org.springframework.transaction.annotation.Transactional;
2531
import org.springframework.ui.ModelMap;
2632
import org.springframework.web.bind.annotation.PathVariable;
2733
import org.springframework.web.bind.annotation.RequestMapping;
2834
import org.springframework.web.bind.annotation.RequestMethod;
2935

3036
import javax.xml.bind.JAXBException;
37+
3138
import java.security.Principal;
39+
import java.util.Iterator;
3240
import java.util.UUID;
3341

42+
43+
@Controller
44+
@RequestMapping()
45+
public class MeterReadingController extends BaseController {
46+
47+
@Autowired
48+
protected MeterReadingService meterReadingService;
49+
50+
@Transactional (readOnly = true)
51+
@RequestMapping(value = Routes.METER_READINGS_SHOW, method = RequestMethod.GET)
52+
public String show(@PathVariable Long retailCustomerId, @PathVariable Long usagePointId, @PathVariable Long meterReadingId, ModelMap model) {
53+
// TODO need to walk the subtree to force the load (for now)
54+
MeterReading mr = meterReadingService.findById(retailCustomerId, usagePointId, meterReadingId);
55+
MeterReading newMeterReading = new MeterReading();
56+
newMeterReading.merge(mr);
57+
Iterator <IntervalBlock> it = newMeterReading.getIntervalBlocks().iterator();
58+
while (it.hasNext()) {
59+
IntervalBlock temp = it.next();
60+
Iterator <IntervalReading> it1 = temp.getIntervalReadings().iterator();
61+
while (it1.hasNext()) {
62+
IntervalReading temp1 = it1.next();
63+
temp1.getCost();
64+
}
65+
66+
}
67+
model.put("meterReading", newMeterReading);
68+
return "/customer/meterreadings/show";
69+
}
70+
71+
public void setMeterReadingService(MeterReadingService meterReadingService) {
72+
this.meterReadingService = meterReadingService;
73+
}
74+
}
75+
76+
/*
3477
@Controller
3578
@PreAuthorize("hasRole('ROLE_USER')")
3679
public class MeterReadingController extends BaseController {
@@ -48,3 +91,4 @@ public void setMeterReadingService(MeterReadingRESTService meterReadingService)
4891
this.meterReadingService = meterReadingService;
4992
}
5093
}
94+
*/

src/main/java/org/energyos/espi/thirdparty/web/UsagePointController.java

Lines changed: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@
1616

1717
package org.energyos.espi.thirdparty.web;
1818

19+
import org.energyos.espi.common.domain.ApplicationInformation;
20+
import org.energyos.espi.common.domain.ElectricPowerQualitySummary;
21+
import org.energyos.espi.common.domain.ElectricPowerUsageSummary;
22+
import org.energyos.espi.common.domain.MeterReading;
1923
import org.energyos.espi.common.domain.RetailCustomer;
2024
import org.energyos.espi.common.domain.Routes;
2125
import org.energyos.espi.common.domain.UsagePoint;
26+
import org.energyos.espi.common.service.ApplicationInformationService;
27+
import org.energyos.espi.common.service.ResourceService;
28+
import org.energyos.espi.common.service.UsagePointService;
2229
import org.energyos.espi.thirdparty.repository.UsagePointRESTRepository;
2330
import org.springframework.beans.factory.annotation.Autowired;
2431
import org.springframework.security.access.prepost.PreAuthorize;
2532
import org.springframework.stereotype.Controller;
33+
import org.springframework.transaction.annotation.Transactional;
2634
import org.springframework.ui.ModelMap;
35+
import org.springframework.web.bind.annotation.ModelAttribute;
2736
import org.springframework.web.bind.annotation.PathVariable;
2837
import org.springframework.web.bind.annotation.RequestMapping;
2938
import org.springframework.web.bind.annotation.RequestMethod;
@@ -32,44 +41,135 @@
3241
import javax.xml.bind.JAXBException;
3342

3443
import java.security.Principal;
44+
import java.util.ArrayList;
45+
import java.util.HashMap;
46+
import java.util.Iterator;
3547
import java.util.List;
3648

3749
@Controller
3850
@PreAuthorize("hasRole('ROLE_USER')")
3951
public class UsagePointController extends BaseController {
4052

4153
@Autowired
42-
private UsagePointRESTRepository usagePointRESTRepository;
43-
44-
@RequestMapping(value = Routes.USAGE_POINT_INDEX_TP, method = RequestMethod.GET)
45-
public String index(ModelMap model, Principal principal) throws JAXBException {
46-
RetailCustomer currentCustomer = currentCustomer(principal);
47-
try {
48-
List<UsagePoint> usagePointList = usagePointRESTRepository.findAllByRetailCustomerId(currentCustomer.getId());
49-
model.put("usagePointList", usagePointList);
54+
private UsagePointRESTRepository usagePointRESTRepository;
55+
56+
@Autowired
57+
private UsagePointService usagePointService;
58+
59+
@Autowired
60+
private ApplicationInformationService applicationInformationService;
61+
62+
@Autowired
63+
private ResourceService resourceService;
5064

51-
return "/usagepoints/index";
52-
} catch(IndexOutOfBoundsException | HttpClientErrorException x) {
53-
return "redirect:/RetailCustomer/" + currentCustomer.getHashedId() + "/DataCustodianList";
54-
}
65+
@ModelAttribute
66+
public List<UsagePoint> usagePoints(Principal principal) {
67+
return usagePointService.findAllByRetailCustomer(currentCustomer(principal));
68+
}
69+
70+
@RequestMapping(value = Routes.USAGE_POINT_INDEX, method = RequestMethod.GET)
71+
public String index(ModelMap model, Principal principal) {
72+
return "/customer/usagepoints/index";
5573
}
5674

75+
@Transactional(readOnly = true)
76+
@RequestMapping(value = Routes.USAGE_POINT_SHOW, method = RequestMethod.GET)
77+
public String show(@PathVariable Long retailCustomerId, @PathVariable Long usagePointId, ModelMap model) {
78+
try {
79+
80+
UsagePoint usagePoint = resourceService.testById(usagePointId, UsagePoint.class);
81+
// because of the lazy loading from DB it's easier to build a bag and hand it off
82+
// in a separate transaction, fill up a display bag lazily - do it in a private method
83+
// so the transaction is scoped appropriately.
84+
85+
HashMap<String, Object> displayBag = buildDisplayBag(retailCustomerId, usagePointId);
86+
87+
model.put("displayBag", displayBag);
88+
89+
return "/customer/usagepoints/show";
90+
} catch (Exception e) {
91+
// got to do a dummy DB access to satify the transaction rollback needs ...
92+
ApplicationInformation ai = resourceService.findById(1L, ApplicationInformation.class);
93+
System.out.printf("UX Error: %s\n", e.toString());
94+
model.put("errorString", e.toString());
95+
try {
96+
// try again (and maybe we can catch the rollback error ...
97+
return "/customer/error";
98+
} catch (Exception ex) {
99+
return "/customer/error";
100+
}
101+
}
102+
}
103+
104+
/*
105+
@Transactional(readOnly = true)
57106
@RequestMapping(value = Routes.USAGE_POINT_SHOW_TP, method = RequestMethod.GET)
58107
public String show(@PathVariable("UsagePointHashedId") String usagePointHashedId, ModelMap model, Principal principal) throws JAXBException {
59108
RetailCustomer currentCustomer = currentCustomer(principal);
60109
try {
61-
model.put("usagePoint", usagePointRESTRepository.findByHashedId(currentCustomer.getId(), usagePointHashedId));
62-
return "/usagepoints/show";
110+
111+
UsagePoint usagePoint = usagePointRESTRepository.findByHashedId(currentCustomer.getId(), usagePointHashedId);
112+
// because of the lazy loading from DB it's easier to build a bag and hand it off
113+
// in a separate transaction, fill up a display bag lazily - do it in a private method
114+
// so the transaction is scoped appropriately.
115+
116+
HashMap<String, Object> displayBag = buildDisplayBag(usagePoint.getRetailCustomer().getId(), usagePoint.getId());
117+
118+
model.put("displayBag", displayBag);
119+
120+
return "/usagepoints/show";
63121
64122
} catch (Exception e) {
123+
65124
System.out.printf("UX Error: %s\n", e.toString());
66125
List<UsagePoint> usagePointList = usagePointRESTRepository.findAllByRetailCustomerId(currentCustomer.getId());
67126
model.put("usagePointList", usagePointList);
68127
return "/usagepoints/index";
69128
}
70129
}
130+
*/
131+
@Transactional(readOnly=true)
132+
private HashMap<String, Object> buildDisplayBag(Long retailCustomerId, Long usagePointId) {
133+
134+
HashMap<String, Object> displayBag = new HashMap<String, Object> ();
135+
UsagePoint usagePoint = resourceService.findById(usagePointId, UsagePoint.class);
136+
displayBag.put("Description", usagePoint.getDescription());
137+
displayBag.put("ServiceCategory", usagePoint.getServiceCategory());
138+
displayBag.put("Uri", usagePoint.getSelfHref());
139+
displayBag.put("usagePointId", usagePoint.getId());
140+
// put the meterReadings
141+
List<HashMap> meterReadings = new ArrayList<HashMap> ();
142+
Iterator <MeterReading> it = usagePoint.getMeterReadings().iterator();
143+
while (it.hasNext()) {
144+
HashMap<String, Object> mrBag = new HashMap<String, Object> ();
145+
MeterReading mr = it.next();
146+
mrBag.put("Description", mr.getDescription());
147+
// TODO build the real IntervalBlocks URI
148+
String uriTail = "/RetailCustomer/" + retailCustomerId + "/UsagePoint/" + usagePointId + "/MeterReading/" + mr.getId() + "/show";
149+
mrBag.put("Uri", applicationInformationService.getThirdPartyNotifyURI().replace("/espi/1_1/Notification","") + uriTail);
150+
mrBag.put("ReadingType", mr.getReadingType().getDescription());
151+
meterReadings.add(mrBag);
152+
}
153+
displayBag.put("MeterReadings", meterReadings);
154+
// find the summary rollups
155+
List<ElectricPowerQualitySummary> qualitySummaryList = usagePoint.getElectricPowerQualitySummaries();
156+
List <ElectricPowerUsageSummary> usageSummaryList = usagePoint.getElectricPowerUsageSummaries();
157+
displayBag.put("QualitySummaryList", qualitySummaryList);
158+
displayBag.put("UsageSummaryList", usageSummaryList);
159+
160+
return displayBag;
161+
}
162+
71163

72164
public void setUsagePointRESTRepository(UsagePointRESTRepository usagePointRESTRepository) {
73165
this.usagePointRESTRepository = usagePointRESTRepository;
74166
}
167+
168+
public void setApplicationInformationService(ApplicationInformationService applicationInformationService) {
169+
this.applicationInformationService = applicationInformationService;
170+
}
171+
172+
public void setResourceService(ResourceService resourceService) {
173+
this.resourceService = resourceService;
174+
}
75175
}

src/main/webapp/WEB-INF/jsp/meterreadings/show.jsp renamed to src/main/webapp/WEB-INF/jsp/customer/meterreadings/show.jsp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
<!DOCTYPE html>
1919
<html lang="en">
2020

21-
<jsp:include page="../tiles/head.jsp"/>
21+
<jsp:include page="../../tiles/head.jsp"/>
2222

2323
<body>
2424

25-
<jsp:include page="../tiles/customer/header.jsp"/>
25+
<jsp:include page="../../tiles/customer/header.jsp"/>
2626

2727
<div class="container">
2828
<div class="row">
@@ -42,15 +42,15 @@
4242
</tr>
4343
</thead>
4444
<tbody>
45-
<tr>
46-
<td><c:out value="${meterReading.readingType.description}"/></td>
47-
<td><c:out value="${meterReading.readingType.accumulationBehaviour}"/></td>
48-
<td><c:out value="${meterReading.readingType.commodity}"/></td>
49-
<td><c:out value="${meterReading.readingType.currency}"/></td>
50-
<td><c:out value="${meterReading.readingType.dataQualifier}"/></td>
51-
<td><c:out value="${meterReading.readingType.argument.numerator}/${meterReading.readingType.argument.denominator}"/></td>
52-
<td><c:out value="${meterReading.readingType.interharmonic.numerator}/${meterReading.readingType.interharmonic.denominator}"/></td>
53-
</tr>
45+
<tr>
46+
<td><c:out value="${meterReading.readingType.description}"/></td>
47+
<td><c:out value="${meterReading.readingType.accumulationBehaviour}"/></td>
48+
<td><c:out value="${meterReading.readingType.commodity}"/></td>
49+
<td><c:out value="${meterReading.readingType.currency}"/></td>
50+
<td><c:out value="${meterReading.readingType.dataQualifier}"/></td>
51+
<td><c:out value="${meterReading.readingType.argument.numerator}/${meterReading.readingType.argument.denominator}"/></td>
52+
<td><c:out value="${meterReading.readingType.interharmonic.numerator}/${meterReading.readingType.interharmonic.denominator}"/></td>
53+
</tr>
5454
</tbody>
5555
</table>
5656

@@ -65,14 +65,14 @@
6565
</tr>
6666
</thead>
6767
<tbody>
68-
<tr>
69-
<td>
70-
<c:out value="${intervalBlock.interval.duration}"/>
71-
</td>
72-
<td>
73-
<c:out value="${intervalBlock.interval.start}"/>
74-
</td>
75-
</tr>
68+
<tr>
69+
<td>
70+
<c:out value="${intervalBlock.interval.duration}"/>
71+
</td>
72+
<td>
73+
<c:out value="${intervalBlock.interval.start}"/>
74+
</td>
75+
</tr>
7676
</tbody>
7777
</table>
7878
<table class="table table-striped">
@@ -118,7 +118,7 @@
118118

119119
<hr>
120120

121-
<jsp:include page="../tiles/footer.jsp"/>
121+
<jsp:include page="../../tiles/footer.jsp"/>
122122

123123
</div>
124124

0 commit comments

Comments
 (0)