|
16 | 16 |
|
17 | 17 | package org.energyos.espi.thirdparty.web; |
18 | 18 |
|
| 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; |
19 | 23 | import org.energyos.espi.common.domain.RetailCustomer; |
20 | 24 | import org.energyos.espi.common.domain.Routes; |
21 | 25 | 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; |
22 | 29 | import org.energyos.espi.thirdparty.repository.UsagePointRESTRepository; |
23 | 30 | import org.springframework.beans.factory.annotation.Autowired; |
24 | 31 | import org.springframework.security.access.prepost.PreAuthorize; |
25 | 32 | import org.springframework.stereotype.Controller; |
| 33 | +import org.springframework.transaction.annotation.Transactional; |
26 | 34 | import org.springframework.ui.ModelMap; |
| 35 | +import org.springframework.web.bind.annotation.ModelAttribute; |
27 | 36 | import org.springframework.web.bind.annotation.PathVariable; |
28 | 37 | import org.springframework.web.bind.annotation.RequestMapping; |
29 | 38 | import org.springframework.web.bind.annotation.RequestMethod; |
|
32 | 41 | import javax.xml.bind.JAXBException; |
33 | 42 |
|
34 | 43 | import java.security.Principal; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.HashMap; |
| 46 | +import java.util.Iterator; |
35 | 47 | import java.util.List; |
36 | 48 |
|
37 | 49 | @Controller |
38 | 50 | @PreAuthorize("hasRole('ROLE_USER')") |
39 | 51 | public class UsagePointController extends BaseController { |
40 | 52 |
|
41 | 53 | @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; |
50 | 64 |
|
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"; |
55 | 73 | } |
56 | 74 |
|
| 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) |
57 | 106 | @RequestMapping(value = Routes.USAGE_POINT_SHOW_TP, method = RequestMethod.GET) |
58 | 107 | public String show(@PathVariable("UsagePointHashedId") String usagePointHashedId, ModelMap model, Principal principal) throws JAXBException { |
59 | 108 | RetailCustomer currentCustomer = currentCustomer(principal); |
60 | 109 | 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"; |
63 | 121 | |
64 | 122 | } catch (Exception e) { |
| 123 | + |
65 | 124 | System.out.printf("UX Error: %s\n", e.toString()); |
66 | 125 | List<UsagePoint> usagePointList = usagePointRESTRepository.findAllByRetailCustomerId(currentCustomer.getId()); |
67 | 126 | model.put("usagePointList", usagePointList); |
68 | 127 | return "/usagepoints/index"; |
69 | 128 | } |
70 | 129 | } |
| 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 | + |
71 | 163 |
|
72 | 164 | public void setUsagePointRESTRepository(UsagePointRESTRepository usagePointRESTRepository) { |
73 | 165 | this.usagePointRESTRepository = usagePointRESTRepository; |
74 | 166 | } |
| 167 | + |
| 168 | + public void setApplicationInformationService(ApplicationInformationService applicationInformationService) { |
| 169 | + this.applicationInformationService = applicationInformationService; |
| 170 | + } |
| 171 | + |
| 172 | + public void setResourceService(ResourceService resourceService) { |
| 173 | + this.resourceService = resourceService; |
| 174 | + } |
75 | 175 | } |
0 commit comments