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: ItsYourParliamentEuApiImpl.java 5448 2011-04-26 20:15:51Z pether $
17   *  $HeadURL: https://cia.svn.sourceforge.net/svnroot/cia/trunk/service.external.itsyourparliament/src/main/java/com/hack23/cia/service/external/itsyourparliament/impl/ItsYourParliamentEuApiImpl.java $
18  */
19  package com.hack23.cia.service.external.itsyourparliament.impl;
20  
21  import java.math.BigInteger;
22  import java.util.List;
23  
24  import javax.xml.bind.JAXBElement;
25  
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.beans.factory.annotation.Qualifier;
28  import org.springframework.oxm.Unmarshaller;
29  import org.springframework.stereotype.Component;
30  
31  import com.hack23.cia.model.external.itsyourparliament.countries.impl.CountriesEuElement;
32  import com.hack23.cia.model.external.itsyourparliament.countries.impl.Countrydetails;
33  import com.hack23.cia.model.external.itsyourparliament.countryinfo.impl.CountryinfoEuElement;
34  import com.hack23.cia.model.external.itsyourparliament.mepinfo.impl.MepinfoEuElement;
35  import com.hack23.cia.model.external.itsyourparliament.voteinfo.impl.VoteinfoEuElement;
36  import com.hack23.cia.model.external.itsyourparliament.votes.impl.VotesEuData;
37  import com.hack23.cia.service.external.common.impl.AbstractXmlAgentImpl;
38  import com.hack23.cia.service.external.itsyourparliament.api.ItsYourParliamentEuApi;
39  
40  /***
41   * The Class ItsYourParliamentEuApiImpl.
42   */
43  @Component
44  public final class ItsYourParliamentEuApiImpl extends AbstractXmlAgentImpl implements ItsYourParliamentEuApi {
45  
46  	/*** The eu itsyourparliament countries marshaller. */
47  	@Autowired
48  	@Qualifier("euItsyourparliamentCountriesMarshaller")
49  	private Unmarshaller euItsyourparliamentCountriesMarshaller;
50  
51  	/*** The eu itsyourparliament country info marshaller. */
52  	@Autowired
53  	@Qualifier("euItsyourparliamentCountryInfoMarshaller")
54  	private Unmarshaller euItsyourparliamentCountryInfoMarshaller;
55  
56  	/*** The eu itsyourparliament mep info marshaller. */
57  	@Autowired
58  	@Qualifier("euItsyourparliamentMepInfoMarshaller")
59  	private Unmarshaller euItsyourparliamentMepInfoMarshaller;
60  
61  	/*** The eu itsyourparliament vote info marshaller. */
62  	@Autowired
63  	@Qualifier("euItsyourparliamentVoteInfoMarshaller")
64  	private Unmarshaller euItsyourparliamentVoteInfoMarshaller;
65  
66  	/*** The eu itsyourparliament votes marshaller. */
67  	@Autowired
68  	@Qualifier("euItsyourparliamentVotesMarshaller")
69  	private Unmarshaller euItsyourparliamentVotesMarshaller;
70  
71  
72  
73  	/***
74  	 * Check access key.
75  	 *
76  	 * @param accessKey
77  	 *            the access key
78  	 * @return the string
79  	 */
80  	private String checkAccessKey(final String accessKey) {
81  		String result=accessKey;
82  		if (accessKey == null || accessKey.contains("$")) {
83  			result = System.getenv()
84  					.get("ITSYOURPARLIAMENT_EU_API_KEY");
85  		}
86  		return result;
87  	}
88  
89  	/* (non-Javadoc)
90  	 * @see com.hack23.cia.service.external.itsyourparliament.api.ItsYourParliamentEuApi#getCountries(java.lang.String)
91  	 */
92  	@Override
93  	public List<Countrydetails> getCountries(final String accessKey,final boolean current) throws Exception {
94  		final List<Countrydetails> list = ((CountriesEuElement) this.unmarshallXml(euItsyourparliamentCountriesMarshaller,"http://www.itsyourparliament.eu/api/"+  getTermModifier(current) +"countries.php?key="+checkAccessKey(accessKey),"http://countries.itsyourparliament.external.model.cia.hack23.com/impl"," & ", " &amp; ")).getCountrydetails();
95  		for (final Countrydetails countrydetails : list) {
96  			countrydetails.setCurrentPeriod(current);
97  			countrydetails.setParliamentPeriod(getTerm(current));
98  		}
99  		return list;
100 	}
101 
102 	/* (non-Javadoc)
103 	 * @see com.hack23.cia.service.external.itsyourparliament.api.ItsYourParliamentEuApi#getCountryInfo(java.lang.String, java.lang.String)
104 	 */
105 	@Override
106 	public CountryinfoEuElement getCountryInfo(final BigInteger id, final String accessKey,final boolean current)
107 			throws Exception {
108 		final CountryinfoEuElement countryinfoEuElement=  (CountryinfoEuElement) this.unmarshallXml(euItsyourparliamentCountryInfoMarshaller,"http://www.itsyourparliament.eu/api/"+  getTermModifier(current) +"country.php?id=" + id + "&key=" +checkAccessKey(accessKey),"http://countryinfo.itsyourparliament.external.model.cia.hack23.com/impl"," & ", " &amp; ");
109 		countryinfoEuElement.setCurrentPeriod(current);
110 		countryinfoEuElement.setParliamentPeriod(getTerm(current));
111 		return countryinfoEuElement;
112 	}
113 
114 
115 	/* (non-Javadoc)
116 	 * @see com.hack23.cia.service.impl.control.agent.eu.impl.agents.MepInfoAgent#getMepInfo(java.lang.String)
117 	 */
118 	@Override
119 	public MepinfoEuElement getMepInfo(final String mepId,final String accessKey,final boolean current) throws Exception {
120 		return ((JAXBElement<MepinfoEuElement>) this.unmarshallXml(euItsyourparliamentMepInfoMarshaller,"http://www.itsyourparliament.eu/api/"+  getTermModifier(current) +"mep.php?id=" +mepId +  "&key="+checkAccessKey(accessKey),"http://mepinfo.itsyourparliament.external.model.cia.hack23.com/impl"," & ", " &amp; ")).getValue();
121 
122 	}
123 
124 	/***
125 	 * Gets the term.
126 	 *
127 	 * @param current
128 	 *            the current
129 	 * @return the term
130 	 */
131 	private String getTerm(final boolean current) {
132 		if (current) return "2010-2015";
133 		else return "2004-2009";
134 	}
135 
136 	/***
137 	 * Gets the term modifier.
138 	 *
139 	 * @param current
140 	 *            the current
141 	 * @return the term modifier
142 	 */
143 	private String getTermModifier(final boolean current) {
144 		if (current) return "";
145 		else return "0409";
146 	}
147 
148 
149 
150 	/*
151 	 * (non-Javadoc)
152 	 *
153 	 * @see
154 	 * com.hack23.cia.service.impl.control.agent.sweden.api.PoliticianAgent#
155 	 * getCurrentList()
156 	 */
157 	@Override
158 	public VoteinfoEuElement getVoteInfo(final String voteId, final String accessKey,final boolean current) throws Exception {
159 		return (VoteinfoEuElement) this.unmarshallXml(euItsyourparliamentVoteInfoMarshaller,"http://www.itsyourparliament.eu/api/"+  getTermModifier(current) +"vote.php?id=" +voteId +  "&key="+checkAccessKey(accessKey),"http://voteinfo.itsyourparliament.external.model.cia.hack23.com/impl"," & ", " &amp; ");
160 	}
161 
162 	/*
163 	 * (non-Javadoc)
164 	 *
165 	 * @see
166 	 * com.hack23.cia.service.impl.control.agent.sweden.api.PoliticianAgent#
167 	 * getCurrentList()
168 	 */
169 	@Override
170 	public VotesEuData getVotes(final String year,final String accessKey,final boolean current) throws Exception {
171 		return (VotesEuData) this.unmarshallXml(euItsyourparliamentVotesMarshaller,"http://www.itsyourparliament.eu/api/"+  getTermModifier(current) +"votes.php?y=" + year + "&key=" +checkAccessKey(accessKey),"http://votes.itsyourparliament.external.model.cia.hack23.com/impl"," & ", " &amp; ");
172 	}
173 
174 }