WorldbankDataApiImpl.java

  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. import java.util.List;

  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Qualifier;
  25. import org.springframework.oxm.Unmarshaller;
  26. import org.springframework.stereotype.Component;

  27. import com.hack23.cia.model.external.worldbank.data.impl.DataElement;
  28. import com.hack23.cia.model.external.worldbank.data.impl.WorldBankData;
  29. import com.hack23.cia.service.external.common.api.XmlAgent;
  30. import com.hack23.cia.service.external.common.api.XmlAgentException;
  31. import com.hack23.cia.service.external.worldbank.api.DataFailureException;
  32. import com.hack23.cia.service.external.worldbank.api.WorldBankDataApi;

  33. /**
  34.  * The Class WorldbankDataApiImpl.
  35.  */
  36. @Component
  37. final class WorldbankDataApiImpl extends BaseWorldBankApiImpl implements WorldBankDataApi {

  38.     /** The Constant LOGGER. */
  39.     private static final Logger LOGGER = LoggerFactory.getLogger(WorldbankDataApiImpl.class);

  40.     /** The Constant COUNTRY_KEY. */
  41.     private static final String COUNTRY_KEY = "${COUNTRY_KEY}";

  42.     /** The Constant INDICATOR_COUNTRY_DATA. */
  43.     private static final String INDICATOR_COUNTRY_DATA ="https://api.worldbank.org/countries/${COUNTRY_KEY}/indicators/${INDICATOR_KEY}?per_page=1000";

  44.     /** The Constant INDICATOR_KEY. */
  45.     private static final String INDICATOR_KEY = "${INDICATOR_KEY}";

  46.     /**
  47.      * The Constant
  48.      * PROBLEM_GETTING_WORLDBANK_DATA_FOR_COUNTRY_CODE_S_INDICATOR_ID_S.
  49.      */
  50.     private static final String PROBLEM_GETTING_WORLDBANK_DATA_FOR_COUNTRY_CODE_S_INDICATOR_ID_S = "Problem getting worldbank data for countryCode:{} ,indicatorId:{} ";
  51.     /**
  52.      * The Constant
  53.      * XMLNS_WB_HTTP_DATA_WORLDBANK_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
  54.      */
  55.     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\"";

  56.     /** The data unmarshaller. */
  57.     @Autowired
  58.     @Qualifier("worldbankOrgDataMarshaller")
  59.     private Unmarshaller dataUnmarshaller;


  60.     /**
  61.      * Instantiates a new worldbank data api impl.
  62.      */
  63.     @Autowired
  64.     public WorldbankDataApiImpl(final XmlAgent xmlAgent) {
  65.         super(xmlAgent);
  66.     }

  67.     @Override
  68.     public List<WorldBankData> getData(final String countryCode,final String indicatorId) throws DataFailureException {
  69.         try {
  70.             final String url = INDICATOR_COUNTRY_DATA.replace(COUNTRY_KEY,countryCode);
  71.             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();
  72.         } catch (final XmlAgentException e) {
  73.             LOGGER.warn(PROBLEM_GETTING_WORLDBANK_DATA_FOR_COUNTRY_CODE_S_INDICATOR_ID_S,countryCode,indicatorId);
  74.             throw new DataFailureException(e);
  75.         }
  76.     }

  77. }