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.riksdagen.impl;
20  
21  import javax.xml.bind.JAXBElement;
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.riksdagen.person.impl.PersonData;
31  import com.hack23.cia.model.external.riksdagen.personlista.impl.PersonContainerElement;
32  import com.hack23.cia.service.external.common.api.XmlAgent;
33  import com.hack23.cia.service.external.riksdagen.api.DataFailureException;
34  import com.hack23.cia.service.external.riksdagen.api.RiksdagenPersonApi;
35  
36  /**
37   * The Class RiksdagenPersonApiImpl.
38   */
39  @Component
40  final class RiksdagenPersonApiImpl implements RiksdagenPersonApi {
41  
42  	/**
43  	 * The Constant HTTP_PERSON_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
44  	 */
45  	private static final String HTTP_PERSON_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "http://person.riksdagen.external.model.cia.hack23.com/impl";
46  
47  	/**
48  	 * The Constant
49  	 * HTTP_PERSONLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
50  	 */
51  	private static final String HTTP_PERSONLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "http://personlista.riksdagen.external.model.cia.hack23.com/impl";
52  
53  	/** The Constant ID_KEY. */
54  	private static final String ID_KEY = "${ID_KEY}";
55  
56  	/** The Constant LOGGER. */
57  	private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenPersonApiImpl.class);
58  
59  	/** The Constant PERSON. */
60  	private static final String PERSON = "http://data.riksdagen.se/person/${ID_KEY}";
61  
62  	/** The Constant PERSON_LIST. */
63  	private static final String PERSON_LIST = "http://data.riksdagen.se/personlista/?iid=&fnamn=&enamn=&f_ar=&kn=&parti=&valkrets=&rdlstatus=samtliga&org=";
64  
65  	/** The Constant PROBLEM_GETTING_PERSON_DATA_ID_S_FROM_DATA_RIKSDAGEN_SE. */
66  	private static final String PROBLEM_GETTING_PERSON_DATA_ID_S_FROM_DATA_RIKSDAGEN_SE = "Problem getting person data id:{}  from data.riksdagen.se";
67  
68  	/** The Constant PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE. */
69  	private static final String PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE = "Problem getting person list from data.riksdagen.se";
70  
71  	/** The person list unmarshaller. */
72  	@Autowired
73  	@Qualifier("riksdagenPersonListMarshaller")
74  	private Unmarshaller personListUnmarshaller;
75  
76  	/** The person unmarshaller. */
77  	@Autowired
78  	@Qualifier("riksdagenPersonMarshaller")
79  	private Unmarshaller personUnmarshaller;
80  
81  	/** The xml agent. */
82  	@Autowired
83  	private XmlAgent xmlAgent;
84  
85  	/**
86  	 * Instantiates a new riksdagen person api impl.
87  	 */
88  	public RiksdagenPersonApiImpl() {
89  		super();
90  	}
91  
92  	@Override
93  	public PersonData getPerson(final String id) throws DataFailureException {
94  		try {
95  			final String url = PERSON.replace(ID_KEY, id);
96  			return ((JAXBElement<com.hack23.cia.model.external.riksdagen.person.impl.PersonContainerData>) xmlAgent
97  					.unmarshallXml(personUnmarshaller, url, HTTP_PERSON_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL,
98  							null, null)).getValue().getPerson();
99  		} catch (final Exception e) {
100 			LOGGER.warn(PROBLEM_GETTING_PERSON_DATA_ID_S_FROM_DATA_RIKSDAGEN_SE, id);
101 			throw new DataFailureException(e);
102 		}
103 	}
104 
105 	@Override
106 	public PersonContainerElement getPersonList() throws DataFailureException {
107 		try {
108 			return ((JAXBElement<PersonContainerElement>) xmlAgent.unmarshallXml(personListUnmarshaller, PERSON_LIST,
109 					HTTP_PERSONLISTA_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL, null, null)).getValue();
110 		} catch (final Exception e) {
111 			LOGGER.warn(PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE);
112 			throw new DataFailureException(e);
113 		}
114 	}
115 
116 }