1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package org.directdemocracyportal.democracy.service.governmentloader; |
6 | |
|
7 | |
import java.net.MalformedURLException; |
8 | |
import java.net.URL; |
9 | |
import java.util.ArrayList; |
10 | |
import java.util.HashSet; |
11 | |
import java.util.List; |
12 | |
import java.util.Set; |
13 | |
|
14 | |
import org.apache.commons.logging.Log; |
15 | |
import org.apache.commons.logging.LogFactory; |
16 | |
import org.directdemocracyportal.democracy.model.core.Role; |
17 | |
import org.directdemocracyportal.democracy.model.world.Country; |
18 | |
import org.directdemocracyportal.democracy.model.world.Election; |
19 | |
import org.directdemocracyportal.democracy.model.world.ElectionType; |
20 | |
import org.directdemocracyportal.democracy.model.world.Government; |
21 | |
import org.directdemocracyportal.democracy.model.world.GovernmentType; |
22 | |
import org.directdemocracyportal.democracy.model.world.Organisation; |
23 | |
import org.directdemocracyportal.democracy.model.world.OrganisationType; |
24 | |
import org.directdemocracyportal.democracy.model.world.Person; |
25 | |
import org.directdemocracyportal.democracy.model.world.PoliticalParty; |
26 | |
import org.directdemocracyportal.democracy.model.world.Region; |
27 | |
import org.directdemocracyportal.democracy.model.world.RegionType; |
28 | |
import org.directdemocracyportal.democracy.service.OrganisationAlreadyExistException; |
29 | |
import org.directdemocracyportal.democracy.service.PortalService; |
30 | |
|
31 | |
import com.gargoylesoftware.htmlunit.WebClient; |
32 | |
import com.gargoylesoftware.htmlunit.html.HtmlAnchor; |
33 | |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
34 | |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
35 | |
import com.gargoylesoftware.htmlunit.html.HtmlTable; |
36 | |
import com.gargoylesoftware.htmlunit.html.HtmlTableBody; |
37 | |
import com.gargoylesoftware.htmlunit.html.HtmlTableCell; |
38 | |
import com.gargoylesoftware.htmlunit.html.HtmlTableRow; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class SwedishGovernmentImporterImpl implements GovernmentImporter |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
private static final String SVERIGE = "Sverige"; |
48 | |
|
49 | |
|
50 | |
public static final String SVERIGES_RIKSDAG = "Sveriges Riksdag"; |
51 | |
|
52 | |
|
53 | 1 | private static Log log = LogFactory |
54 | |
.getLog(SwedishGovernmentImporterImpl.class); |
55 | |
|
56 | |
|
57 | |
private final WebClient webClient; |
58 | |
|
59 | |
|
60 | |
private final URL url; |
61 | |
|
62 | |
|
63 | |
private final PortalService portalService; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 1 | public SwedishGovernmentImporterImpl(WebClient webClient, String url,PortalService portalService) throws MalformedURLException { |
74 | 1 | this.webClient = webClient; |
75 | 1 | this.portalService = portalService; |
76 | 1 | this.url = new URL(url); |
77 | 1 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
@SuppressWarnings("unchecked") |
85 | |
public void doImport() { |
86 | |
|
87 | 0 | log.info("Starting import"); |
88 | |
|
89 | 0 | Country sweden = portalService.findCountryByName(SVERIGE); |
90 | 0 | if (sweden == null) { |
91 | |
|
92 | 0 | sweden = portalService.createCountry(SVERIGE); |
93 | |
|
94 | 0 | Government government = portalService.createGovernment( |
95 | |
"Konungariket Sverige", sweden.getId(), |
96 | |
GovernmentType.Constitutional_Monarchy,"Carl Gustaf Folke Hubertus"); |
97 | |
|
98 | 0 | portalService.createElection("Sveriges Riksdags val",ElectionType.National,government.getId()); |
99 | |
|
100 | 0 | for (ActivePoliticalParty party : getCurrentActivePartyList()) { |
101 | 0 | PoliticalParty politicalParty = portalService |
102 | |
.createPoliticalPartyById(party.name, party.shortCode, |
103 | |
sweden.getId()); |
104 | 0 | } |
105 | |
|
106 | |
try { |
107 | 0 | portalService.createOrganisationById("Regeringskansliet", null, |
108 | |
sweden.getId(),sweden.getId(), OrganisationType.National, government.getId()); |
109 | 0 | } catch (OrganisationAlreadyExistException e2) { |
110 | |
|
111 | 0 | e2.printStackTrace(); |
112 | 0 | } |
113 | |
|
114 | 0 | Organisation parliament=null; |
115 | |
try { |
116 | 0 | parliament = portalService.createOrganisation( |
117 | |
SVERIGES_RIKSDAG, null, sweden, |
118 | |
sweden, OrganisationType.National, government); |
119 | 0 | } catch (OrganisationAlreadyExistException e2) { |
120 | |
|
121 | 0 | e2.printStackTrace(); |
122 | 0 | } |
123 | 0 | Role ledamot = portalService.createRoleInOrg("Ledamot", parliament); |
124 | |
|
125 | |
List<Politician> currentList; |
126 | |
try { |
127 | 0 | currentList = getCurrentList(); |
128 | 0 | } catch (Exception e) { |
129 | 0 | log.error("Problem importing swedish parliament members", e); |
130 | 0 | return; |
131 | 0 | } |
132 | |
|
133 | 0 | log.info("Importing members in " + parliament.getName()); |
134 | |
|
135 | 0 | for (Politician member : currentList) { |
136 | 0 | PoliticalParty politicalParty = portalService.findPartyByShortCode(member.getParty().toLowerCase().trim()); |
137 | |
|
138 | 0 | Person person= portalService.createPerson(member.getName()); |
139 | 0 | portalService.addMemberById(politicalParty.getId(), person.getId()); |
140 | 0 | portalService.addMemberById(parliament.getId(), person.getId()); |
141 | 0 | portalService.addRolePlayedById(ledamot.getId(), person.getId()); |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
try { |
146 | 0 | importRegiononalGovernments(government, sweden); |
147 | 0 | } catch (Exception e1) { |
148 | |
|
149 | 0 | e1.printStackTrace(); |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
try { |
154 | 0 | importRegionsAndMunicipals(government, sweden); |
155 | 0 | } catch (Exception e) { |
156 | |
|
157 | 0 | e.printStackTrace(); |
158 | 0 | } |
159 | |
|
160 | |
try { |
161 | 0 | importInternationalGovernments(government, sweden); |
162 | 0 | } catch (Exception e) { |
163 | |
|
164 | 0 | e.printStackTrace(); |
165 | 0 | } |
166 | |
} |
167 | 0 | log.info("Import completed"); |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private List<Politician> getCurrentList() throws Exception { |
177 | 0 | List<Politician> members = new ArrayList<Politician>(); |
178 | |
|
179 | 0 | HtmlPage page = (HtmlPage) webClient.getPage(url); |
180 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
181 | 0 | HtmlElement parliamentTable = pageContent |
182 | |
.getHtmlElementsByTagName("table").get(0); |
183 | 0 | List<HtmlElement> htmlElementsByTagName2 = parliamentTable |
184 | |
.getHtmlElementsByTagName("span"); |
185 | 0 | for (HtmlElement span : htmlElementsByTagName2) { |
186 | 0 | HtmlAnchor homePage = (HtmlAnchor) span.getFirstChild(); |
187 | 0 | String name = homePage.asText(); |
188 | 0 | String href = homePage.getHrefAttribute(); |
189 | 0 | String text = span.asText(); |
190 | 0 | String party = text.substring(text.indexOf('(') + 1, text |
191 | |
.indexOf(')')); |
192 | 0 | String region = text.substring(text.indexOf(')') + 1, |
193 | |
text.lastIndexOf(',')).trim(); |
194 | 0 | String role = text.substring(text.lastIndexOf(',') + 1, |
195 | |
text.length()).trim(); |
196 | 0 | members.add(new Politician(name, href, party, region, role)); |
197 | 0 | } |
198 | 0 | return members; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
private class Politician |
205 | |
{ |
206 | |
|
207 | |
|
208 | |
private final String name; |
209 | |
|
210 | |
|
211 | |
private final String href; |
212 | |
|
213 | |
|
214 | |
private final String party; |
215 | |
|
216 | |
|
217 | |
private final String region; |
218 | |
|
219 | |
|
220 | |
public final String role; |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
public Politician(String name, String href, String party, |
232 | 0 | String region, String role) { |
233 | 0 | super(); |
234 | 0 | this.name = name.trim(); |
235 | 0 | this.href = href.trim(); |
236 | 0 | this.party = party.trim().toLowerCase(); |
237 | 0 | this.region = region.trim(); |
238 | 0 | this.role = role.trim(); |
239 | 0 | } |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public String getName() { |
247 | 0 | return name; |
248 | |
} |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public String getHref() { |
256 | 0 | return href; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
public String getParty() { |
265 | 0 | return party; |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public String getRegion() { |
274 | 0 | return region; |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
public String getRole() { |
283 | 0 | return role; |
284 | |
} |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | 0 | private class ActivePoliticalParty |
291 | |
{ |
292 | |
|
293 | |
|
294 | |
private final String name; |
295 | |
|
296 | |
|
297 | |
private final String shortCode; |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | 0 | public ActivePoliticalParty(String name, String shortCode) { |
306 | 0 | this.name = name.trim(); |
307 | 0 | this.shortCode = shortCode.trim().toLowerCase(); |
308 | 0 | } |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public String getName() { |
316 | 0 | return name; |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
@Override |
325 | |
public int hashCode() { |
326 | |
final int prime = 31; |
327 | 0 | int result = 1; |
328 | 0 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
329 | 0 | result = prime * result |
330 | |
+ ((shortCode == null) ? 0 : shortCode.hashCode()); |
331 | 0 | return result; |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
@Override |
340 | |
public boolean equals(Object obj) { |
341 | 0 | if (this == obj) |
342 | 0 | return true; |
343 | 0 | if (obj == null) |
344 | 0 | return false; |
345 | 0 | if (getClass() != obj.getClass()) |
346 | 0 | return false; |
347 | 0 | final ActivePoliticalParty other = (ActivePoliticalParty) obj; |
348 | 0 | if (name == null) { |
349 | 0 | if (other.name != null) |
350 | 0 | return false; |
351 | 0 | } else if (!name.equals(other.name)) |
352 | 0 | return false; |
353 | 0 | if (shortCode == null) { |
354 | 0 | if (other.shortCode != null) |
355 | 0 | return false; |
356 | 0 | } else if (!shortCode.equals(other.shortCode)) |
357 | 0 | return false; |
358 | 0 | return true; |
359 | |
} |
360 | |
} |
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
private Set<ActivePoliticalParty> getCurrentActivePartyList() { |
368 | 0 | Set<ActivePoliticalParty> parties = new HashSet<ActivePoliticalParty>(); |
369 | |
|
370 | |
try { |
371 | 0 | HtmlPage nationalParties = (HtmlPage) webClient |
372 | |
.getPage("http://www.val.se/val/val2006/slutlig/R/rike/roster.html"); |
373 | 0 | parties.addAll(getPoliticalPartiesFromResultPage(nationalParties)); |
374 | |
|
375 | 0 | HtmlPage regionalParties = (HtmlPage) webClient |
376 | |
.getPage("http://www.val.se/val/val2006/slutlig/L/rike/delar.html"); |
377 | 0 | parties.addAll(getPoliticalPartiesForRegionalList(regionalParties)); |
378 | |
|
379 | 0 | HtmlPage localParties = (HtmlPage) webClient |
380 | |
.getPage("http://www.val.se/val/val2006/slutlig/K/rike/delar.html"); |
381 | 0 | parties.addAll(getPoliticalPartiesForRegionalList(localParties)); |
382 | |
|
383 | |
|
384 | 0 | parties.add(new ActivePoliticalParty("Junilistan", "jl")); |
385 | |
|
386 | 0 | } catch (Exception e) { |
387 | 0 | e.printStackTrace(); |
388 | 0 | } |
389 | 0 | return parties; |
390 | |
} |
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
@SuppressWarnings("unchecked") |
400 | |
private Set<ActivePoliticalParty> getPoliticalPartiesFromResultPage( |
401 | |
HtmlPage page) throws Exception { |
402 | 0 | Set<ActivePoliticalParty> parties = new HashSet<ActivePoliticalParty>(); |
403 | |
|
404 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
405 | 0 | HtmlTable table = (HtmlTable) pageContent.getElementsByAttribute( |
406 | |
"table", "class", "rostfordelning").get(0); |
407 | 0 | List<HtmlTableRow> rows = new ArrayList(table.getRows()); |
408 | |
|
409 | 0 | rows.remove(0); |
410 | 0 | rows.remove(0); |
411 | 0 | rows.remove(rows.size() - 1); |
412 | |
|
413 | 0 | for (HtmlTableRow row : rows) { |
414 | 0 | int mandat = Integer.valueOf(((HtmlElement) row.getCells().get(4)) |
415 | |
.asText().trim()); |
416 | |
|
417 | 0 | if (mandat != 0) { |
418 | 0 | ActivePoliticalParty activePoliticalParty = new ActivePoliticalParty( |
419 | |
((HtmlElement) row.getCells().get(1)).asText().trim(), |
420 | |
((HtmlElement) row.getCells().get(0)).asText() |
421 | |
.toLowerCase().trim()); |
422 | 0 | parties.add(activePoliticalParty); |
423 | |
} |
424 | 0 | } |
425 | 0 | return parties; |
426 | |
} |
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
@SuppressWarnings("unchecked") |
436 | |
private Set<ActivePoliticalParty> getPoliticalPartiesForRegionalList( |
437 | |
HtmlPage page) throws Exception { |
438 | 0 | Set<ActivePoliticalParty> parties = new HashSet<ActivePoliticalParty>(); |
439 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
440 | 0 | HtmlElement element = pageContent.getElementById("delar"); |
441 | 0 | List<HtmlAnchor> htmlElementsByTagName = element |
442 | |
.getHtmlElementsByTagName("a"); |
443 | |
|
444 | 0 | for (HtmlAnchor a : htmlElementsByTagName) { |
445 | 0 | parties.addAll(getPoliticalPartiesFromResultPage((HtmlPage) a |
446 | |
.openLinkInNewWindow())); |
447 | 0 | } |
448 | 0 | return parties; |
449 | |
} |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
@SuppressWarnings("unchecked") |
459 | |
public void importRegionsAndMunicipals(Government government, |
460 | |
Country country) throws Exception { |
461 | 0 | HtmlPage page = (HtmlPage) webClient |
462 | |
.getPage("http://www.val.se/val/val2006/valdaled/index_kommun.html"); |
463 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
464 | 0 | HtmlElement div = pageContent.getElementById("sik"); |
465 | |
|
466 | 0 | List<HtmlElement> htmlElementsByTagName = div |
467 | |
.getHtmlElementsByTagName("a"); |
468 | |
|
469 | 0 | for (HtmlElement element : htmlElementsByTagName) { |
470 | 0 | HtmlAnchor anchor = (HtmlAnchor) element; |
471 | 0 | ImportAllMunicipalsInRegion(anchor, government, country); |
472 | 0 | } |
473 | 0 | } |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
@SuppressWarnings("unchecked") |
483 | |
public void importRegiononalGovernments(Government government, |
484 | |
Country country) throws Exception { |
485 | 0 | HtmlPage page = (HtmlPage) webClient |
486 | |
.getPage("http://www.val.se/val/val2006/valdaled/index_landsting.html"); |
487 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
488 | 0 | HtmlElement div = pageContent.getElementById("sik"); |
489 | |
|
490 | 0 | List<HtmlElement> htmlElementsByTagName = div |
491 | |
.getHtmlElementsByTagName("a"); |
492 | |
|
493 | 0 | for (HtmlElement element : htmlElementsByTagName) { |
494 | 0 | HtmlAnchor anchor = (HtmlAnchor) element; |
495 | |
|
496 | 0 | importRegionalGovernment(anchor, government, country); |
497 | 0 | } |
498 | 0 | } |
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
private void importRegionalGovernment(HtmlAnchor anchor, |
509 | |
Government government, Country country) throws Exception { |
510 | 0 | HtmlPage page = (HtmlPage) anchor.openLinkInNewWindow(); |
511 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
512 | |
|
513 | 0 | String regionName = anchor.asText().trim().replace("s landsting", "") |
514 | |
.trim(); |
515 | |
|
516 | 0 | Region region = new Region(); |
517 | 0 | region.setParent(country); |
518 | 0 | country.getParts().add(region); |
519 | 0 | region.setRegionType(RegionType.Regional); |
520 | 0 | region.setName(regionName); |
521 | |
|
522 | |
|
523 | 0 | Government regionalGovernment = portalService.createRegionalGovernment( |
524 | |
anchor.asText(), country, region, OrganisationType.Regional, |
525 | |
government); |
526 | |
|
527 | 0 | Election election = new Election(); |
528 | 0 | election.setName(regionalGovernment.getName() + "sval"); |
529 | 0 | election.setType(ElectionType.Regional); |
530 | 0 | election.setGovernment(regionalGovernment); |
531 | 0 | regionalGovernment.getElections().add(election); |
532 | |
|
533 | |
|
534 | 0 | portalService.createOrganisation("Landstingsstyrelse " |
535 | |
+ region.getName() + " landsting", null, country, |
536 | |
region, OrganisationType.Regional, regionalGovernment); |
537 | |
|
538 | 0 | Organisation regionalParliament = portalService.createOrganisation( |
539 | |
"Landstingsfullmäktige " + region.getName() + " landsting", |
540 | |
null, country, region, OrganisationType.Regional, regionalGovernment); |
541 | |
|
542 | 0 | Role landstingFullmäktige = portalService.createRoleInOrg( |
543 | |
"Landstingsfullmäktige", regionalParliament); |
544 | |
|
545 | 0 | log.info("Importing members in " + regionalParliament.getName()); |
546 | |
|
547 | 0 | List<Politician> members = getAllRegionalGovernmentMembers(pageContent); |
548 | 0 | for (Politician member : members) { |
549 | 0 | PoliticalParty politicalParty = portalService.findPartyByShortCode(member.getParty().toLowerCase().trim()); |
550 | 0 | Person person = portalService.createPerson(member.getName()); |
551 | |
|
552 | 0 | portalService.addMemberById(politicalParty.getId(), person.getId()); |
553 | 0 | portalService.addMemberById(regionalParliament.getId(), person.getId()); |
554 | 0 | portalService.addRolePlayedById(landstingFullmäktige.getId(), person.getId()); |
555 | 0 | } |
556 | 0 | } |
557 | |
|
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
|
563 | |
|
564 | |
|
565 | |
|
566 | |
@SuppressWarnings("unchecked") |
567 | |
private void ImportAllMunicipalsInRegion(HtmlAnchor anchor, |
568 | |
Government government, Country country) throws Exception { |
569 | 0 | if (anchor.asText().equals("Gotland")) { |
570 | 0 | ImportMunicipal(anchor, government, country, country); |
571 | |
} else { |
572 | |
|
573 | 0 | HtmlPage page = (HtmlPage) anchor.openLinkInNewWindow(); |
574 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
575 | 0 | HtmlElement div = pageContent.getElementById("sik"); |
576 | 0 | List<HtmlElement> htmlElementsByTagName = div |
577 | |
.getHtmlElementsByTagName("a"); |
578 | |
|
579 | 0 | Region region = portalService.findRegionByName(anchor.asText()); |
580 | |
|
581 | 0 | for (HtmlElement element : htmlElementsByTagName) { |
582 | 0 | ImportMunicipal((HtmlAnchor) element, government, country, |
583 | |
region); |
584 | 0 | } |
585 | |
} |
586 | 0 | } |
587 | |
|
588 | |
|
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
private void ImportMunicipal(HtmlAnchor anchor, Government government, |
598 | |
Country country, Region region) throws Exception { |
599 | 0 | HtmlPage page = (HtmlPage) anchor.openLinkInNewWindow(); |
600 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
601 | |
|
602 | 0 | Region local = new Region(); |
603 | 0 | local.setParent(region); |
604 | 0 | region.getParts().add(local); |
605 | 0 | local.setRegionType(RegionType.Local); |
606 | 0 | local.setName(anchor.asText()); |
607 | |
|
608 | |
|
609 | 0 | Government localGovernment = portalService.createRegionalGovernment( |
610 | |
anchor.asText().trim() + " Kommun", country, local, |
611 | |
OrganisationType.Local, government); |
612 | |
|
613 | 0 | Election election = new Election(); |
614 | 0 | election.setName(localGovernment.getName() + " val"); |
615 | 0 | election.setType(ElectionType.Local); |
616 | 0 | election.setGovernment(localGovernment); |
617 | 0 | localGovernment.getElections().add(election); |
618 | |
|
619 | |
|
620 | 0 | portalService.createOrganisation("Kommunstyrelse " + local.getName(), |
621 | |
null, country, local, OrganisationType.Local, localGovernment); |
622 | |
|
623 | 0 | Organisation localParliament = portalService.createOrganisation( |
624 | |
"Kommunfullmäktige " + local.getName(), null, country, |
625 | |
local, OrganisationType.Local, localGovernment); |
626 | 0 | Role fullmäktigeLedamot = portalService.createRoleInOrg( |
627 | |
"Fullmäktigeledamot", localParliament); |
628 | |
|
629 | 0 | log.info("Importing members in " + localParliament.getName()); |
630 | |
|
631 | 0 | List<Politician> members = getAllMunicipialsMembers(pageContent); |
632 | 0 | for (Politician member : members) { |
633 | 0 | PoliticalParty politicalParty = portalService.findPartyByShortCode(member.getParty().toLowerCase().trim()); |
634 | |
|
635 | 0 | Person person = new Person(); |
636 | 0 | person.setName(member.getName()); |
637 | |
|
638 | |
|
639 | 0 | portalService.addMember(politicalParty, person); |
640 | 0 | portalService.addMember(localParliament, person); |
641 | 0 | portalService.addRolePlayed(fullmäktigeLedamot, person); |
642 | 0 | } |
643 | 0 | } |
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
@SuppressWarnings("unchecked") |
652 | |
private List<Politician> getAllMunicipialsMembers(HtmlElement pageContent) { |
653 | |
|
654 | 0 | HtmlTableBody tableBody = (HtmlTableBody) pageContent |
655 | |
.getElementById("tableBody"); |
656 | 0 | List<HtmlTableRow> rows = tableBody.getRows(); |
657 | |
|
658 | 0 | List<Politician> members = new ArrayList<Politician>(); |
659 | 0 | for (HtmlTableRow row : rows) { |
660 | 0 | List<HtmlTableCell> cells = row.getCells(); |
661 | |
|
662 | 0 | if (cells.size() == 3) { |
663 | 0 | members.add(new Politician(cells.get(0).asText().trim(), "", |
664 | |
cells.get(1).asText().trim(), cells.get(2).asText() |
665 | |
.trim(), "Fullmäktigeledamot")); |
666 | 0 | } else if (cells.size() == 2) { |
667 | 0 | members |
668 | |
.add(new Politician(cells.get(0).asText().trim(), "", |
669 | |
cells.get(1).asText().trim(), "", |
670 | |
"Fullmäktigeledamot")); |
671 | |
} |
672 | 0 | } |
673 | 0 | return members; |
674 | |
} |
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
|
681 | |
|
682 | |
@SuppressWarnings( { "unchecked", "unchecked" }) |
683 | |
private List<Politician> getAllRegionalGovernmentMembers( |
684 | |
HtmlElement pageContent) { |
685 | |
|
686 | 0 | HtmlTableBody tableBody = (HtmlTableBody) pageContent |
687 | |
.getElementById("tableBody"); |
688 | 0 | List<HtmlTableRow> rows = tableBody.getRows(); |
689 | |
|
690 | 0 | List<Politician> members = new ArrayList<Politician>(); |
691 | 0 | for (HtmlTableRow row : rows) { |
692 | 0 | List<HtmlTableCell> cells = row.getCells(); |
693 | |
|
694 | 0 | if (cells.size() == 3) { |
695 | 0 | members.add(new Politician(cells.get(0).asText().trim(), "", |
696 | |
cells.get(1).asText().trim(), cells.get(2).asText() |
697 | |
.trim(), "Landstingsfullmäktige")); |
698 | 0 | } else if (cells.size() == 2) { |
699 | 0 | members.add(new Politician(cells.get(0).asText().trim(), "", |
700 | |
cells.get(1).asText().trim(), "", |
701 | |
"Landstingsfullmäktige")); |
702 | |
} |
703 | 0 | } |
704 | 0 | return members; |
705 | |
} |
706 | |
|
707 | |
|
708 | |
|
709 | |
|
710 | |
|
711 | |
|
712 | |
|
713 | |
|
714 | |
public void importInternationalGovernments(Government government, |
715 | |
Country country) throws Exception { |
716 | 0 | HtmlPage page = (HtmlPage) webClient |
717 | |
.getPage("http://www.val.se/val/ep2004/valdaled/index.html"); |
718 | 0 | HtmlElement pageContent = page.getDocumentElement(); |
719 | |
|
720 | 0 | Region eu = new Region(); |
721 | 0 | eu.setRegionType(RegionType.International); |
722 | 0 | eu.setName("Europeiska unionen"); |
723 | 0 | country.setParent(eu); |
724 | 0 | eu.getParts().add(country); |
725 | |
|
726 | |
|
727 | |
|
728 | 0 | Organisation internationalGov = portalService.createOrganisation( |
729 | |
"Europaparlamentet", null, country, |
730 | |
eu, OrganisationType.International, government); |
731 | 0 | Role parlamentsLedamot = portalService.createRoleInOrg( |
732 | |
"Parlamentsledamot", internationalGov); |
733 | |
|
734 | 0 | List<Politician> members = getAllInternationalGovernmentMembers(pageContent); |
735 | 0 | for (Politician member : members) { |
736 | 0 | PoliticalParty politicalParty = portalService.findPartyByShortCode(member.getParty().toLowerCase().trim()); |
737 | |
|
738 | 0 | Person person = portalService.createPerson(member.getName()); |
739 | |
|
740 | 0 | portalService.addMemberById(politicalParty.getId(), person.getId()); |
741 | 0 | portalService.addMemberById(internationalGov.getId(), person.getId()); |
742 | 0 | portalService.addRolePlayedById(parlamentsLedamot.getId(), person.getId()); |
743 | 0 | } |
744 | 0 | } |
745 | |
|
746 | |
|
747 | |
|
748 | |
|
749 | |
|
750 | |
|
751 | |
|
752 | |
@SuppressWarnings("unchecked") |
753 | |
private List<Politician> getAllInternationalGovernmentMembers( |
754 | |
HtmlElement pageContent) { |
755 | |
|
756 | 0 | HtmlTableBody tableBody = (HtmlTableBody) pageContent |
757 | |
.getElementById("tableBody"); |
758 | 0 | List<HtmlTableRow> rows = tableBody.getRows(); |
759 | |
|
760 | 0 | List<Politician> members = new ArrayList<Politician>(); |
761 | 0 | for (HtmlTableRow row : rows) { |
762 | 0 | List<HtmlTableCell> cells = row.getCells(); |
763 | |
|
764 | 0 | if (cells.size() == 3) { |
765 | 0 | members.add(new Politician(cells.get(0).asText().trim(), "", |
766 | |
cells.get(1).asText().trim(), cells.get(2).asText() |
767 | |
.trim(), "Parlamentsledamot")); |
768 | 0 | } else if (cells.size() == 2) { |
769 | 0 | members.add(new Politician(cells.get(0).asText().trim(), "", |
770 | |
cells.get(1).asText().trim(), "", "Parlamentsledamot")); |
771 | |
} |
772 | 0 | } |
773 | 0 | return members; |
774 | |
} |
775 | |
} |