View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
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   * The Class WorldbankIndicatorApiImpl.
38   */
39  @Component
40  final class WorldbankIndicatorApiImpl extends AbstractWorldBankApiImpl implements WorldBankIndicatorApi {
41  
42  	/** The Constant LOGGER. */
43  	private static final Logger LOGGER = LoggerFactory.getLogger(WorldbankIndicatorApiImpl.class);
44  
45  	/** The Constant INDICATORS. */
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  	/** The Constant PROBLEM_GETTING_WORLDBANK_INDICATOR_LIST. */
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  	 * The Constant
57  	 * XMLNS_WB_HTTP_INDICATORS_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
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  	/** The indicators unmarshaller. */
63  	@Autowired
64  	@Qualifier("worldbankOrgIndicatorsMarshaller")
65  	private Unmarshaller indicatorsUnmarshaller;
66  
67  	/**
68  	 * Instantiates a new worldbank indicator api impl.
69  	 */
70  	public WorldbankIndicatorApiImpl() {
71  		super();
72  	}
73  
74  	/* (non-Javadoc)
75  	 * @see com.hack23.cia.service.external.worldbank.api.WorldBankIndicatorApi#getIndicators()
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 }