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.utskottsforslag.impl.CommitteeProposalComponentData;
31  import com.hack23.cia.service.external.common.api.XmlAgent;
32  import com.hack23.cia.service.external.riksdagen.api.DataFailureException;
33  import com.hack23.cia.service.external.riksdagen.api.RiksdagenCommitteeProposalApi;
34  
35  /**
36   * The Class RiksdagenCommitteeProposalApiImpl.
37   */
38  @Component
39  final class RiksdagenCommitteeProposalApiImpl implements RiksdagenCommitteeProposalApi {
40  
41  	/** The Constant COMMITTE_PROPOSAL. */
42  	private static final String COMMITTE_PROPOSAL = "http://data.riksdagen.se/utskottsforslag/${ID_KEY}/xml";
43  
44  	/**
45  	 * The Constant
46  	 * HTTP_UTSKOTTSFORSLAG_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL.
47  	 */
48  	private static final String HTTP_UTSKOTTSFORSLAG_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL = "http://utskottsforslag.riksdagen.external.model.cia.hack23.com/impl";
49  
50  	/** The Constant ID_KEY. */
51  	private static final String ID_KEY = "${ID_KEY}";
52  
53  	/** The Constant LOGGER. */
54  	private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenCommitteeProposalApiImpl.class);
55  
56  	/**
57  	 * The Constant
58  	 * PROBLEM_GETTING_COMMITTEE_PROPOSAL_FOR_ID_S_FROM_DATA_RIKSDAGEN_SE.
59  	 */
60  	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";
61  
62  	/** The riksdagen committee proposal marshaller. */
63  	@Autowired
64  	@Qualifier("riksdagenCommitteeProposalMarshaller")
65  	private Unmarshaller riksdagenCommitteeProposalMarshaller;
66  
67  	/** The xml agent. */
68  	@Autowired
69  	private XmlAgent xmlAgent;
70  
71  	/**
72  	 * Instantiates a new riksdagen committee proposal api impl.
73  	 */
74  	public RiksdagenCommitteeProposalApiImpl() {
75  		super();
76  	}
77  
78  	@Override
79  	public CommitteeProposalComponentData getCommitteeProposal(final String id) throws DataFailureException {
80  		try {
81  			final String url = COMMITTE_PROPOSAL.replace(ID_KEY, id);
82  			return ((JAXBElement<CommitteeProposalComponentData>) xmlAgent.unmarshallXml(
83  					riksdagenCommitteeProposalMarshaller, url,
84  					HTTP_UTSKOTTSFORSLAG_RIKSDAGEN_EXTERNAL_MODEL_CIA_HACK23_COM_IMPL, null, null)).getValue();
85  		} catch (final Exception e) {
86  			LOGGER.warn(PROBLEM_GETTING_COMMITTEE_PROPOSAL_FOR_ID_S_FROM_DATA_RIKSDAGEN_SE, id);
87  			throw new DataFailureException(e);
88  		}
89  	}
90  
91  }