RiksdagenCommitteeProposalApiImpl.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.riksdagen.impl;

  20. import javax.xml.bind.JAXBElement;

  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.riksdagen.utskottsforslag.impl.CommitteeProposalComponentData;
  28. import com.hack23.cia.service.external.common.api.XmlAgent;
  29. import com.hack23.cia.service.external.common.api.XmlAgentException;
  30. import com.hack23.cia.service.external.riksdagen.api.DataFailureException;
  31. import com.hack23.cia.service.external.riksdagen.api.RiksdagenCommitteeProposalApi;

  32. /**
  33.  * The Class RiksdagenCommitteeProposalApiImpl.
  34.  */
  35. @Component
  36. final class RiksdagenCommitteeProposalApiImpl implements RiksdagenCommitteeProposalApi {

  37.     /** The Constant COMMITTE_PROPOSAL. */
  38.     private static final String COMMITTE_PROPOSAL = "https://data.riksdagen.se/utskottsforslag/${ID_KEY}/xml";

  39.     /**
  40.      * The Constant
  41.      * HTTP_UTSKOTTSFORSLAG_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
  42.      */
  43.     private static final String HTTP_UTSKOTTSFORSLAG_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "http://utskottsforslag.riksdagen.external.model.cia.hack23.com/impl";

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

  46.     /** The Constant LOGGER. */
  47.     private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenCommitteeProposalApiImpl.class);

  48.     /**
  49.      * The Constant
  50.      * PROBLEM_GETTING_COMMITTEE_PROPOSAL_FOR_ID_S_FROM_DATA_RIKSDAGEN_SE.
  51.      */
  52.     private static final String PROBLEM_GETTING_COMMITTEE_PROPOSAL_FOR_ID_S_FROM_DATA_RIKSDAGEN_SE = "Problem getting committee proposal for id:{} from data.riksdagen.se";

  53.     /** The riksdagen committee proposal marshaller. */
  54.     @Autowired
  55.     @Qualifier("riksdagenCommitteeProposalMarshaller")
  56.     private Unmarshaller riksdagenCommitteeProposalMarshaller;

  57.     /** The xml agent. */
  58.     private final XmlAgent xmlAgent;

  59.     /**
  60.      * Instantiates a new riksdagen committee proposal api impl.
  61.      *
  62.      * @param xmlAgent
  63.      *            the xml agent
  64.      */
  65.     @Autowired
  66.     public RiksdagenCommitteeProposalApiImpl(final XmlAgent xmlAgent) {
  67.         super();
  68.         this.xmlAgent = xmlAgent;
  69.     }

  70.     /* (non-Javadoc)
  71.      * @see com.hack23.cia.service.external.riksdagen.api.RiksdagenCommitteeProposalApi#getCommitteeProposal(java.lang.String)
  72.      */
  73.     @Override
  74.     public CommitteeProposalComponentData getCommitteeProposal(final String id) throws DataFailureException {
  75.         try {
  76.             final String url = COMMITTE_PROPOSAL.replace(ID_KEY, id);
  77.             return ((JAXBElement<CommitteeProposalComponentData>) xmlAgent.unmarshallXml(
  78.                     riksdagenCommitteeProposalMarshaller, url,
  79.                     HTTP_UTSKOTTSFORSLAG_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL, null, null)).getValue();
  80.         } catch (final XmlAgentException e) {
  81.             LOGGER.warn(PROBLEM_GETTING_COMMITTEE_PROPOSAL_FOR_ID_S_FROM_DATA_RIKSDAGEN_SE, id);
  82.             throw new DataFailureException(e);
  83.         }
  84.     }

  85. }