RiksdagenPersonApiImpl.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.person.impl.PersonData;
  28. import com.hack23.cia.model.external.riksdagen.personlista.impl.PersonContainerElement;
  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.riksdagen.api.DataFailureException;
  32. import com.hack23.cia.service.external.riksdagen.api.RiksdagenPersonApi;

  33. /**
  34.  * The Class RiksdagenPersonApiImpl.
  35.  */
  36. @Component
  37. final class RiksdagenPersonApiImpl implements RiksdagenPersonApi {

  38.     /** The Constant HTTP_PERSON_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL. */
  39.     private static final String HTTP_PERSON_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "http://person.riksdagen.external.model.cia.hack23.com/impl";

  40.     /**
  41.      * The Constant HTTP_PERSONLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
  42.      */
  43.     private static final String HTTP_PERSONLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "http://personlista.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(RiksdagenPersonApiImpl.class);

  48.     /** The Constant PERSON. */
  49.     private static final String PERSON = "https://data.riksdagen.se/person/${ID_KEY}";

  50.     /** The Constant PERSON_LIST. */
  51.     private static final String PERSON_LIST = "https://data.riksdagen.se/personlista/?iid=&fnamn=&enamn=&f_ar=&kn=&parti=&valkrets=&rdlstatus=samtliga&org=";

  52.     /** The Constant PROBLEM_GETTING_PERSON_DATA_ID_S_FROM_DATA_RIKSDAGEN_SE. */
  53.     private static final String PROBLEM_GETTING_PERSON_DATA_ID_S_FROM_DATA_RIKSDAGEN_SE = "Problem getting person data id:{}  from data.riksdagen.se";

  54.     /** The Constant PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE. */
  55.     private static final String PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE = "Problem getting person list from data.riksdagen.se";

  56.     /** The person list unmarshaller. */
  57.     @Autowired
  58.     @Qualifier("riksdagenPersonListMarshaller")
  59.     private Unmarshaller personListUnmarshaller;

  60.     /** The person unmarshaller. */
  61.     @Autowired
  62.     @Qualifier("riksdagenPersonMarshaller")
  63.     private Unmarshaller personUnmarshaller;

  64.     /** The xml agent. */
  65.     private final XmlAgent xmlAgent;

  66.     /**
  67.      * Instantiates a new riksdagen person api impl.
  68.      *
  69.      * @param xmlAgent
  70.      *            the xml agent
  71.      */
  72.     @Autowired
  73.     public RiksdagenPersonApiImpl(final XmlAgent xmlAgent) {
  74.         super();
  75.         this.xmlAgent = xmlAgent;
  76.     }

  77.     /* (non-Javadoc)
  78.      * @see com.hack23.cia.service.external.riksdagen.api.RiksdagenPersonApi#getPerson(java.lang.String)
  79.      */
  80.     @Override
  81.     public PersonData getPerson(final String id) throws DataFailureException {
  82.         try {
  83.             final String url = PERSON.replace(ID_KEY, id);
  84.             return ((JAXBElement<com.hack23.cia.model.external.riksdagen.person.impl.PersonContainerData>) xmlAgent
  85.                     .unmarshallXml(personUnmarshaller, url, HTTP_PERSON_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL,
  86.                             null, null)).getValue().getPerson();
  87.         } catch (final XmlAgentException e) {
  88.             LOGGER.warn(PROBLEM_GETTING_PERSON_DATA_ID_S_FROM_DATA_RIKSDAGEN_SE, id);
  89.             throw new DataFailureException(e);
  90.         }
  91.     }

  92.     /* (non-Javadoc)
  93.      * @see com.hack23.cia.service.external.riksdagen.api.RiksdagenPersonApi#getPersonList()
  94.      */
  95.     @Override
  96.     public PersonContainerElement getPersonList() throws DataFailureException {
  97.         try {
  98.             return ((JAXBElement<PersonContainerElement>) xmlAgent.unmarshallXml(personListUnmarshaller, PERSON_LIST,
  99.                     HTTP_PERSONLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL, null, null)).getValue();
  100.         } catch (final XmlAgentException e) {
  101.             LOGGER.warn(PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE);
  102.             throw new DataFailureException(e);
  103.         }
  104.     }

  105. }