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.val.impl;
20  
21  import java.net.URL;
22  import java.util.List;
23  
24  import javax.xml.bind.JAXBElement;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.beans.factory.annotation.Qualifier;
30  import org.springframework.oxm.Unmarshaller;
31  import org.springframework.stereotype.Component;
32  
33  import com.hack23.cia.model.external.val.kommunvalkrets.impl.SwedenCountyData;
34  import com.hack23.cia.model.external.val.kommunvalkrets.impl.SwedenCountyDataContainer;
35  import com.hack23.cia.model.external.val.landstingvalkrets.impl.SwedenCountyElectoralRegion;
36  import com.hack23.cia.model.external.val.landstingvalkrets.impl.SwedenCountyElectoralRegionContainer;
37  import com.hack23.cia.model.external.val.partier.impl.SwedenElectionRegion;
38  import com.hack23.cia.model.external.val.partier.impl.SwedenElectionType;
39  import com.hack23.cia.model.external.val.partier.impl.SwedenElectionTypeContainerElement;
40  import com.hack23.cia.model.external.val.partier.impl.SwedenPoliticalParty;
41  import com.hack23.cia.model.external.val.riksdagsvalkrets.impl.SwedenParliamentElectoralRegion;
42  import com.hack23.cia.model.external.val.riksdagsvalkrets.impl.SwedenParliamentElectoralRegionContainer;
43  import com.hack23.cia.service.external.common.api.XmlAgent;
44  import com.hack23.cia.service.external.val.api.ValApi;
45  import com.hack23.cia.service.external.val.api.ValApiException;
46  
47  /**
48   * The Class ValApiImpl.
49   */
50  @Component
51  final class ValApiImpl implements ValApi {
52  
53  	/** The Constant LOGGER. */
54  	private static final Logger LOGGER = LoggerFactory.getLogger(ValApiImpl.class);
55  
56  	/** The xml agent. */
57  	@Autowired
58  	private XmlAgent xmlAgent;
59  
60  	/** The val partier marshaller. */
61  	@Autowired
62  	@Qualifier("valPartierMarshaller")
63  	private Unmarshaller valPartierMarshaller;
64  
65  	/** The val riksdag marshaller. */
66  	@Autowired
67  	@Qualifier("valRiksdagMarshaller")
68  	private Unmarshaller valRiksdagMarshaller;
69  
70  	/** The val landsting marshaller. */
71  	@Autowired
72  	@Qualifier("valLandstingMarshaller")
73  	private Unmarshaller valLandstingMarshaller;
74  
75  	/** The val kommun marshaller. */
76  	@Autowired
77  	@Qualifier("valKommunMarshaller")
78  	private Unmarshaller valKommunMarshaller;
79  
80  	/**
81  	 * Instantiates a new val api impl.
82  	 */
83  	public ValApiImpl() {
84  		super();
85  	}
86  
87  	@Override
88  	public List<SwedenElectionType> getElectionTypes() throws ValApiException {
89  		final URL resource = ValApiImpl.class.getResource("/partier20151217.xml");
90  
91  		try {
92  			return ((JAXBElement<SwedenElectionTypeContainerElement>) xmlAgent.unmarshallXml(valPartierMarshaller,
93  					resource.toString(), "http://partier.val.external.model.cia.hack23.com/impl", null, null))
94  							.getValue().getElectionTypes();
95  		} catch (Exception e) {
96  			LOGGER.warn("Problem getElectionTypes", e);
97  			throw new ValApiException(e);
98  		}
99  	}
100 
101 	@Override
102 	public List<SwedenParliamentElectoralRegion> getParliamentElectoralRegions() throws ValApiException {
103 		try {
104 			final URL resource = ValApiImpl.class.getResource("/riksdagsvalkrets.xml");
105 
106 			return ((JAXBElement<SwedenParliamentElectoralRegionContainer>) xmlAgent.unmarshallXml(valRiksdagMarshaller,
107 					resource.toString(), "http://riksdagsvalkrets.val.external.model.cia.hack23.com/impl", null, null))
108 							.getValue().getParliamentElectoralRegions();
109 		} catch (Exception e) {
110 			LOGGER.warn("Problem getParliamentElectoralRegions", e);
111 			throw new ValApiException(e);
112 		}
113 
114 	}
115 
116 	@Override
117 	public List<SwedenCountyElectoralRegion> getCountyElectoralRegions() throws ValApiException {
118 		try {
119 			final URL resource = ValApiImpl.class.getResource("/landstingvalkrets.xml");
120 
121 			return ((JAXBElement<SwedenCountyElectoralRegionContainer>) xmlAgent.unmarshallXml(valLandstingMarshaller,
122 					resource.toString(), "http://landstingvalkrets.val.external.model.cia.hack23.com/impl", null, null))
123 							.getValue().getCountyElectoralRegions();
124 		} catch (Exception e) {
125 			LOGGER.warn("Problem getCountyElectoralRegions", e);
126 			throw new ValApiException(e);
127 		}
128 
129 	}
130 
131 	@Override
132 	public List<SwedenCountyData> getCountyRegions() throws ValApiException {
133 		try {
134 			final URL resource = ValApiImpl.class.getResource("/kommunvalkrets.xml");
135 
136 			return ((JAXBElement<SwedenCountyDataContainer>) xmlAgent.unmarshallXml(valKommunMarshaller,
137 					resource.toString(), "http://kommunvalkrets.val.external.model.cia.hack23.com/impl", null, null))
138 							.getValue().getCountyRegions();
139 		} catch (Exception e) {
140 			LOGGER.warn("Problem getCountyRegions", e);
141 			throw new ValApiException(e);
142 		}
143 	}
144 
145 	@Override
146 	public SwedenElectionRegion getSwedenElectionRegion() throws ValApiException {
147 		final URL resource = ValApiImpl.class.getResource("/partier20151217.xml");
148 
149 		try {
150 			return ((JAXBElement<SwedenElectionTypeContainerElement>) xmlAgent.unmarshallXml(valPartierMarshaller,
151 					resource.toString(), "http://partier.val.external.model.cia.hack23.com/impl", null, null))
152 							.getValue().getElectionTypes().get(0).getRegion();
153 		} catch (final Exception e) {
154 			LOGGER.warn("Problem getSwedenElectionRegion", e);
155 			throw new ValApiException(e);
156 		}
157 
158 	}
159 
160 	@Override
161 	public List<SwedenPoliticalParty> getSwedenPoliticalParties() throws ValApiException {
162 		return getSwedenElectionRegion().getParties();
163 	}
164 
165 }