1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.component.agent.impl.riksdagen.workgenerator.data;
20
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34 import org.springframework.transaction.annotation.Propagation;
35 import org.springframework.transaction.annotation.Transactional;
36
37 import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
38 import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentType;
39 import com.hack23.cia.model.external.riksdagen.votering.impl.VoteDataEmbeddedId;
40 import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
41 import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
42 import com.hack23.cia.service.data.api.ApplicationConfigurationService;
43 import com.hack23.cia.service.data.api.CommitteeProposalComponentDataDAO;
44 import com.hack23.cia.service.data.api.DocumentContentDataDAO;
45 import com.hack23.cia.service.data.api.DocumentElementDAO;
46 import com.hack23.cia.service.data.api.DocumentStatusContainerDAO;
47 import com.hack23.cia.service.data.api.PersonDataDAO;
48 import com.hack23.cia.service.data.api.VoteDataDAO;
49
50
51
52
53 @Component("RiksdagenImportService")
54 @Transactional(propagation = Propagation.MANDATORY)
55 final class RiksdagenImportServiceImpl implements RiksdagenImportService {
56
57
58 private static final Logger LOGGER = LoggerFactory.getLogger(RiksdagenImportServiceImpl.class);
59
60
61 private static final String RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT = "yyyy-MM-dd";
62
63
64 private static final String RIKSDAGEN_JAVA_SIMPLE_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
65
66
67 @Autowired
68 private ApplicationConfigurationService applicationConfigurationService;
69
70
71 @Autowired
72 private CommitteeProposalComponentDataDAO committeeProposalComponentDataDAO;
73
74
75 @Autowired
76 private DocumentContentDataDAO documentContentDataDAO;
77
78
79 @Autowired
80 private DocumentElementDAO documentElementDAO;
81
82
83 @Autowired
84 private DocumentStatusContainerDAO documentStatusContainerDAO;
85
86
87 @Autowired
88 private PersonDataDAO personDataDAO;
89
90
91 @Autowired
92 private VoteDataDAO voteDataDAO;
93
94
95
96
97 public RiksdagenImportServiceImpl() {
98 super();
99 }
100
101
102
103
104
105
106
107
108 private static Map<String, String> createMapFromList(final List<String> all) {
109 final Map<String, String> map = new ConcurrentHashMap<>();
110
111 for (final String documentElement : all) {
112 map.put(documentElement, documentElement);
113 }
114 return map;
115 }
116
117
118
119
120
121
122
123
124 private static Map<String, String> createMapFromListVote(final List<VoteDataEmbeddedId> list) {
125 final Map<String, String> map = new ConcurrentHashMap<>();
126
127 for (final VoteDataEmbeddedId documentElement : list) {
128 map.put(documentElement.getBallotId(), documentElement.getBallotId());
129 }
130 return map;
131 }
132
133 @Override
134 public List<String> getAvaibleCommitteeProposal() {
135 return documentStatusContainerDAO.getAvaibleCommitteeProposal();
136 }
137
138 @Override
139 public List<String> getAvaibleDocumentContent() {
140 return documentElementDAO.getAvaibleDocumentContent();
141 }
142
143 @Override
144 public Map<String, String> getCommitteeProposalComponentDataMap() {
145 return createMapFromList(committeeProposalComponentDataDAO.getIdList());
146 }
147
148 @Override
149 public Map<String, String> getDocumentContentMap() {
150 return createMapFromList(documentContentDataDAO.getIdList());
151 }
152
153 @Override
154 public Map<String, String> getDocumentElementMap() {
155 final List<String> all = documentElementDAO.getIdList();
156 return createMapFromList(all);
157 }
158
159 @Override
160 public Map<String, String> getDocumentElementMap(final Date after, final List<DocumentType> downloadType,
161 final boolean onlyWithDocStatus) {
162 final List<DocumentElement> all = documentElementDAO.getAll();
163
164 final List<String> documentTypeValues = new ArrayList<>();
165
166 for (final DocumentType docType : downloadType) {
167 documentTypeValues.add(docType.value());
168 }
169
170 final Map<String, String> map = new ConcurrentHashMap<>();
171
172 for (final DocumentElement documentElement : all) {
173 if (checkIncludeDate(after, documentTypeValues, documentElement) && checkIncludeStatus(onlyWithDocStatus, documentElement)) {
174 map.put(documentElement.getId(), documentElement.getDocumentType());
175 }
176 }
177 return map;
178 }
179
180
181
182
183
184
185
186
187
188
189 private static boolean checkIncludeStatus(final boolean onlyWithDocStatus, final DocumentElement documentElement) {
190 return !onlyWithDocStatus || documentElement.getDocumentStatusUrlXml() != null;
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204 private static boolean checkIncludeDate(final Date after, final List<String> documentTypeValues,
205 final DocumentElement documentElement) {
206 try {
207 if (documentElement.getMadePublicDate() != null && documentElement.getMadePublicDate().length() > 0) {
208 return getDate(documentElement.getMadePublicDate()).after(after)
209 && documentTypeValues.contains(documentElement.getDocumentType());
210 } else {
211 return getDate(documentElement.getCreatedDate()).after(after)
212 && documentTypeValues.contains(documentElement.getDocumentType());
213 }
214 } catch (final ParseException e) {
215 LOGGER.warn("Problem getting date from{} : exception:{}", documentElement, e);
216 return false;
217 }
218 }
219
220
221
222
223
224
225
226
227
228
229 private static Date getDate(final String date) throws ParseException {
230 if (RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT.length() > date.length()) {
231 return new SimpleDateFormat(RIKSDAGEN_JAVA_SIMPLE_DATE_TIME_FORMAT, Locale.ENGLISH).parse(date);
232 } else {
233 return new SimpleDateFormat(RIKSDAGEN_JAVA_SIMPLE_DATE_FORMAT, Locale.ENGLISH).parse(date);
234 }
235 }
236
237 @Override
238 public Map<String, String> getDocumentStatusContainerMap() {
239 return createMapFromList(documentStatusContainerDAO.getIdList());
240 }
241
242 @Override
243 public Map<String, String> getLoadedBallotIdMap() {
244 return createMapFromListVote(voteDataDAO.getBallotIdList());
245 }
246
247 @Override
248 public Map<String, String> getPersonMap() {
249 return createMapFromList(personDataDAO.getIdList());
250 }
251
252 @Override
253 public int getStartYearForDocumentElement() {
254 final ApplicationConfiguration registeredUsersGetAdminConfig = applicationConfigurationService
255 .checkValueOrLoadDefault("Load Riksdagen documents from year", "Load Riksdagen documents from year",
256 ConfigurationGroup.AGENT, RiksdagenImportService.class.getSimpleName(),
257 "Riksdagen Import Service", "Responsible import Riksdagen data",
258 "agent.riksdagen.documents.loadfromyear", "2000");
259
260 return documentElementDAO
261 .getMissingDocumentStartFromYear(Integer.parseInt(registeredUsersGetAdminConfig.getPropertyValue()));
262 }
263
264 }