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.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
38
39 @Component
40 final class RiksdagenPersonApiImpl implements RiksdagenPersonApi {
41
42
43
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
49
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
54 private static final String ID_KEY = "${ID_KEY}";
55
56
57 private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenPersonApiImpl.class);
58
59
60 private static final String PERSON = "http://data.riksdagen.se/person/${ID_KEY}";
61
62
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
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
69 private static final String PROBLEM_GETTING_PERSON_LIST_FROM_DATA_RIKSDAGEN_SE = "Problem getting person list from data.riksdagen.se";
70
71
72 @Autowired
73 @Qualifier("riksdagenPersonListMarshaller")
74 private Unmarshaller personListUnmarshaller;
75
76
77 @Autowired
78 @Qualifier("riksdagenPersonMarshaller")
79 private Unmarshaller personUnmarshaller;
80
81
82 @Autowired
83 private XmlAgent xmlAgent;
84
85
86
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 }