View Javadoc

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   
6   package com.hack23.cia.service.impl.agent.sweden;
7   
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  import com.gargoylesoftware.htmlunit.WebClient;
15  import com.gargoylesoftware.htmlunit.html.HtmlPage;
16  import com.gargoylesoftware.htmlunit.html.HtmlTable;
17  import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
18  import com.hack23.cia.model.sweden.impl.PoliticalParty;
19  
20  /***
21   * The Class PoliticalPartyAgentImpl.
22   */
23  public class PoliticalPartyAgentImpl implements PoliticalPartyAgent {
24  
25  	/*** The Constant LAST_ELECTION_RESULT. */
26  	private static final String LAST_ELECTION_RESULT = "http://www.val.se/val/val2010/slutresultat/R/rike/index.html";
27  
28  	/*** The Constant LOGGER. */
29  	private static final Log LOGGER = LogFactory
30  			.getLog(PoliticalPartyAgentImpl.class);
31  
32  	/*** The web client. */
33  	private final WebClient webClient;
34  
35  	/***
36  	 * Instantiates a new political party agent impl.
37  	 *
38  	 * @param webClient the web client
39  	 */
40  	public PoliticalPartyAgentImpl(final WebClient webClient) {
41  		super();
42  		this.webClient = webClient;
43  		this.webClient.setJavaScriptEnabled(false);
44  	}
45  
46  	/* (non-Javadoc)
47  	 * @see com.hack23.cia.service.impl.agent.sweden.PoliticalPartyAgent#getCurrentList()
48  	 */
49  	@Override
50  	public final List<PoliticalParty> getCurrentList() {
51  		final List<PoliticalParty> result = new ArrayList<PoliticalParty>();
52  
53  		try {
54  			final HtmlPage page = (HtmlPage) webClient.getPage(LAST_ELECTION_RESULT);
55  
56  			final List<HtmlTable> tables = page.getDocumentElement()
57  					.getElementsByAttribute("table", "class",
58  							"rostfordelning");
59  
60  			final HtmlTable htmlTable = tables.get(0);
61  			final List<HtmlTableRow> rows = htmlTable.getRows();
62  			final int startRow = 2;
63  
64  			for (int i = startRow; i < (rows.size() - 1); i++) {
65  				final HtmlTableRow htmlTableRow = rows.get(i);
66  
67  				final PoliticalParty politicalParty = new PoliticalParty();
68  				politicalParty.setShortCode(htmlTableRow.getCell(0).asText().toLowerCase().trim());
69  				politicalParty.setName(htmlTableRow.getCell(1).asText());
70  				result.add(politicalParty);
71  			}
72  		} catch (final Exception e) {
73  			LOGGER.warn("Problem accessing Politicalparties", e); //$NON-NLS-1$
74  		}
75  
76  		return result;
77  	}
78  
79  }