1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.external.worldbank.impl;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Qualifier;
28 import org.springframework.oxm.Unmarshaller;
29 import org.springframework.stereotype.Component;
30
31 import com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorElement;
32 import com.hack23.cia.model.external.worldbank.indicators.impl.IndicatorsElement;
33 import com.hack23.cia.service.external.worldbank.api.DataFailureException;
34 import com.hack23.cia.service.external.worldbank.api.WorldBankIndicatorApi;
35
36
37
38
39 @Component
40 final class WorldbankIndicatorApiImpl extends AbstractWorldBankApiImpl implements WorldBankIndicatorApi {
41
42
43 private static final Logger LOGGER = LoggerFactory.getLogger(WorldbankIndicatorApiImpl.class);
44
45
46 private static final String INDICATORS = "http://api.worldbank.org/indicators?per_page=5000";
47
48 private static final String PAGE_NUMBER = "&page=";
49
50
51 private static final String PROBLEM_GETTING_WORLDBANK_INDICATOR_LIST = "Problem getting worldbank indicator list";
52
53 private static final int SECOND_PAGE = 2;
54
55
56
57
58
59 private static final String XMLNS_WB_HTTP_INDICATORS_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "xmlns:wb=\"http://indicators.worldbank.external.model.cia.hack23.com/impl\"";
60
61
62
63 @Autowired
64 @Qualifier("worldbankOrgIndicatorsMarshaller")
65 private Unmarshaller indicatorsUnmarshaller;
66
67
68
69
70 public WorldbankIndicatorApiImpl() {
71 super();
72 }
73
74
75
76
77 @Override
78 public List<IndicatorElement> getIndicators() throws DataFailureException {
79 final List<IndicatorElement> result = new ArrayList<>();
80
81 try {
82 final IndicatorsElement firstPage = (IndicatorsElement) getXmlAgent().unmarshallXml(indicatorsUnmarshaller,
83 INDICATORS, null, XMLNS_WB_HTTP_WWW_WORLDBANK_ORG,
84 XMLNS_WB_HTTP_INDICATORS_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL);
85 result.addAll(firstPage.getIndicator());
86
87 for (int pageNumber = SECOND_PAGE; pageNumber < firstPage.getPages().intValue(); pageNumber++) {
88 final IndicatorsElement otherPageResult = (IndicatorsElement) getXmlAgent().unmarshallXml(indicatorsUnmarshaller,
89 INDICATORS + PAGE_NUMBER + pageNumber, null, XMLNS_WB_HTTP_WWW_WORLDBANK_ORG,
90 XMLNS_WB_HTTP_INDICATORS_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL);
91 result.addAll(otherPageResult.getIndicator());
92 }
93
94 } catch (final Exception e) {
95 LOGGER.warn(PROBLEM_GETTING_WORLDBANK_INDICATOR_LIST);
96 throw new DataFailureException(e);
97 }
98
99 return result;
100 }
101
102 }