1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
90
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"," & ", " & ")).getCountrydetails();
95 for (final Countrydetails countrydetails : list) {
96 countrydetails.setCurrentPeriod(current);
97 countrydetails.setParliamentPeriod(getTerm(current));
98 }
99 return list;
100 }
101
102
103
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"," & ", " & ");
109 countryinfoEuElement.setCurrentPeriod(current);
110 countryinfoEuElement.setParliamentPeriod(getTerm(current));
111 return countryinfoEuElement;
112 }
113
114
115
116
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"," & ", " & ")).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
152
153
154
155
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"," & ", " & ");
160 }
161
162
163
164
165
166
167
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"," & ", " & ");
172 }
173
174 }