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.List;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Qualifier;
27 import org.springframework.oxm.Unmarshaller;
28 import org.springframework.stereotype.Component;
29
30 import com.hack23.cia.model.external.worldbank.data.impl.DataElement;
31 import com.hack23.cia.model.external.worldbank.data.impl.WorldBankData;
32 import com.hack23.cia.service.external.worldbank.api.DataFailureException;
33 import com.hack23.cia.service.external.worldbank.api.WorldBankDataApi;
34
35
36
37
38 @Component
39 final class WorldbankDataApiImpl extends AbstractWorldBankApiImpl implements WorldBankDataApi {
40
41
42 private static final Logger LOGGER = LoggerFactory.getLogger(WorldbankDataApiImpl.class);
43
44
45 private static final String COUNTRY_KEY = "${COUNTRY_KEY}";
46
47
48 private static final String INDICATOR_COUNTRY_DATA ="http://api.worldbank.org/countries/${COUNTRY_KEY}/indicators/${INDICATOR_KEY}?per_page=1000";
49
50
51 private static final String INDICATOR_KEY = "${INDICATOR_KEY}";
52
53
54
55
56
57 private static final String PROBLEM_GETTING_WORLDBANK_DATA_FOR_COUNTRY_CODE_S_INDICATOR_ID_S = "Problem getting worldbank data for countryCode:{} ,indicatorId:{} ";
58
59
60
61
62 private static final String XMLNS_WB_HTTP_DATA_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "xmlns:wb=\"http://data.worldbank.external.model.cia.hack23.com/impl\"";
63
64
65 @Autowired
66 @Qualifier("worldbankOrgDataMarshaller")
67 private Unmarshaller dataUnmarshaller;
68
69
70
71
72
73 public WorldbankDataApiImpl() {
74 super();
75 }
76
77 @Override
78 public List<WorldBankData> getData(final String countryCode,final String indicatorId) throws DataFailureException {
79 try {
80 final String url = INDICATOR_COUNTRY_DATA.replace(COUNTRY_KEY,countryCode);
81 return ((DataElement) getXmlAgent().unmarshallXml(dataUnmarshaller, url.replace(INDICATOR_KEY, indicatorId),null,XMLNS_WB_HTTP_WWW_WORLDBANK_ORG, XMLNS_WB_HTTP_DATA_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL)).getData();
82 } catch (final Exception e) {
83 LOGGER.warn(PROBLEM_GETTING_WORLDBANK_DATA_FOR_COUNTRY_CODE_S_INDICATOR_ID_S,countryCode,indicatorId);
84 throw new DataFailureException(e);
85 }
86 }
87
88 }