Coverage Report - org.directdemocracyportal.democracy.service.governmentloader.SwedishGovernmentImporterImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SwedishGovernmentImporterImpl
2%
6/236
0%
0/46
2.96
SwedishGovernmentImporterImpl$ActivePoliticalParty
0%
0/28
0%
0/22
2.96
SwedishGovernmentImporterImpl$Politician
0%
0/13
N/A
2.96
 
 1  
 /*
 2  
 Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
 3  
         $Id
 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  
  * The Class SwedishGovernmentImporterImpl.
 42  
  */
 43  
 public class SwedishGovernmentImporterImpl implements GovernmentImporter
 44  
 {
 45  
 
 46  
     /** The Constant SVERIGE. */
 47  
     private static final String SVERIGE = "Sverige";
 48  
 
 49  
     /** The Constant SVERIGES_RIKSDAG. */
 50  
     public static final String SVERIGES_RIKSDAG = "Sveriges Riksdag";
 51  
 
 52  
     /** The log. */
 53  1
     private static Log log = LogFactory
 54  
             .getLog(SwedishGovernmentImporterImpl.class);
 55  
 
 56  
     /** The web client. */
 57  
     private final WebClient webClient;
 58  
 
 59  
     /** The url. */
 60  
     private final URL url;
 61  
 
 62  
     /** The portal service. */
 63  
     private final PortalService portalService;
 64  
 
 65  
     /**
 66  
      * Instantiates a new swedish government importer impl.
 67  
      *
 68  
      * @param webClient the web client
 69  
      * @param url the url
 70  
      * @param portalService the portal service
 71  
      * @throws MalformedURLException the malformed url exception
 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  
      * (non-Javadoc)
 81  
      *
 82  
      * @see org.directdemocracyportal.democracy.service.governmentloader.GovernmentImporter#doImport()
 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  
                 // TODO Auto-generated catch block
 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  
                 // TODO Auto-generated catch block
 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  
             // Import regional government and members
 145  
             try {
 146  0
                 importRegiononalGovernments(government, sweden);
 147  0
             } catch (Exception e1) {
 148  
                 // TODO Auto-generated catch block
 149  0
                 e1.printStackTrace();
 150  0
             }
 151  
 
 152  
             // import local government and members
 153  
             try {
 154  0
                 importRegionsAndMunicipals(government, sweden);
 155  0
             } catch (Exception e) {
 156  
                 // TODO Auto-generated catch block
 157  0
                 e.printStackTrace();
 158  0
             }
 159  
 
 160  
             try {
 161  0
                 importInternationalGovernments(government, sweden);
 162  0
             } catch (Exception e) {
 163  
                 // TODO Auto-generated catch block
 164  0
                 e.printStackTrace();
 165  0
             }
 166  
         }
 167  0
         log.info("Import completed");
 168  0
     }
 169  
 
 170  
     /**
 171  
      * Gets the current list.
 172  
      *
 173  
      * @return the current list
 174  
      * @throws Exception the exception
 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  
      * The Class Politician.
 203  
      */
 204  
     private class Politician
 205  
     {
 206  
 
 207  
         /** The name. */
 208  
         private final String name;
 209  
 
 210  
         /** The href. */
 211  
         private final String href;
 212  
 
 213  
         /** The party. */
 214  
         private final String party;
 215  
 
 216  
         /** The region. */
 217  
         private final String region;
 218  
 
 219  
         /** The role. */
 220  
         public final String role;
 221  
 
 222  
         /**
 223  
          * Instantiates a new politician.
 224  
          *
 225  
          * @param name the name
 226  
          * @param href the href
 227  
          * @param party the party
 228  
          * @param region the region
 229  
          * @param role the role
 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  
          * Gets the name.
 243  
          *
 244  
          * @return the name
 245  
          */
 246  
         public String getName() {
 247  0
             return name;
 248  
         }
 249  
 
 250  
         /**
 251  
          * Gets the href.
 252  
          *
 253  
          * @return the href
 254  
          */
 255  
         public String getHref() {
 256  0
             return href;
 257  
         }
 258  
 
 259  
         /**
 260  
          * Gets the party.
 261  
          *
 262  
          * @return the party
 263  
          */
 264  
         public String getParty() {
 265  0
             return party;
 266  
         }
 267  
 
 268  
         /**
 269  
          * Gets the region.
 270  
          *
 271  
          * @return the region
 272  
          */
 273  
         public String getRegion() {
 274  0
             return region;
 275  
         }
 276  
 
 277  
         /**
 278  
          * Gets the role.
 279  
          *
 280  
          * @return the role
 281  
          */
 282  
         public String getRole() {
 283  0
             return role;
 284  
         }
 285  
     }
 286  
 
 287  
     /**
 288  
      * The Class ActivePoliticalParty.
 289  
      */
 290  0
     private class ActivePoliticalParty
 291  
     {
 292  
 
 293  
         /** The name. */
 294  
         private final String name;
 295  
 
 296  
         /** The short code. */
 297  
         private final String shortCode;
 298  
 
 299  
         /**
 300  
          * Instantiates a new active political party.
 301  
          *
 302  
          * @param name the name
 303  
          * @param shortCode the short code
 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  
          * Gets the name.
 312  
          *
 313  
          * @return the name
 314  
          */
 315  
         public String getName() {
 316  0
             return name;
 317  
         }
 318  
 
 319  
         /*
 320  
          * (non-Javadoc)
 321  
          *
 322  
          * @see java.lang.Object#hashCode()
 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  
          * (non-Javadoc)
 336  
          *
 337  
          * @see java.lang.Object#equals(java.lang.Object)
 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  
      * Gets the current active party list.
 364  
      *
 365  
      * @return the current active party list
 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  
             // TODO fix Hack import all eu parlaments parties
 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  
      * Gets the political parties from result page.
 394  
      *
 395  
      * @param page the page
 396  
      * @return the political parties from result page
 397  
      * @throws Exception the exception
 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  
      * Gets the political parties for regional list.
 430  
      *
 431  
      * @param page the page
 432  
      * @return the political parties for regional list
 433  
      * @throws Exception the exception
 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  
      * Import regions and municipals.
 453  
      *
 454  
      * @param government the government
 455  
      * @param country the country
 456  
      * @throws Exception the exception
 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  
      * Import regiononal governments.
 477  
      *
 478  
      * @param government the government
 479  
      * @param country the country
 480  
      * @throws Exception the exception
 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  
      * Import regional government.
 502  
      *
 503  
      * @param anchor the anchor
 504  
      * @param government the government
 505  
      * @param country the country
 506  
      * @throws Exception the exception
 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  
         //countryDAO.save(country);
 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  
         //agentDAO.save(regionalGovernment);
 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  
      * Import all municipals in region.
 560  
      *
 561  
      * @param anchor the anchor
 562  
      * @param government the government
 563  
      * @param country the country
 564  
      * @throws Exception the exception
 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  
      * Import municipal.
 590  
      *
 591  
      * @param anchor the anchor
 592  
      * @param government the government
 593  
      * @param country the country
 594  
      * @param region the region
 595  
      * @throws Exception the exception
 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  
         //countryDAO.saveRegion(region);
 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  
         //agentDAO.save(localGovernment);
 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  
             //agentDAO.save(person);
 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  
      * Gets the all municipials members.
 647  
      *
 648  
      * @param pageContent the page content
 649  
      * @return the all municipials members
 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  
      * Gets the all regional government members.
 678  
      *
 679  
      * @param pageContent the page content
 680  
      * @return the all regional government members
 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  
      * Import international governments.
 709  
      *
 710  
      * @param government the government
 711  
      * @param country the country
 712  
      * @throws Exception the exception
 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  
         //countryDAO.saveRegion(eu);
 726  
         //countryDAO.save(country);
 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  
      * Gets the all international government members.
 748  
      *
 749  
      * @param pageContent the page content
 750  
      * @return the all international government members
 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  
 }