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.io.IOException;
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  import com.gargoylesoftware.htmlunit.WebClient;
16  import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
17  import com.gargoylesoftware.htmlunit.html.HtmlPage;
18  import com.gargoylesoftware.htmlunit.html.HtmlTable;
19  import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
20  import com.hack23.cia.model.sweden.impl.Ballot;
21  import com.hack23.cia.model.sweden.impl.CommitteeReport;
22  import com.hack23.cia.model.sweden.impl.ParliamentMember;
23  import com.hack23.cia.model.sweden.impl.Vote;
24  
25  /***
26   * The Class BallotAgentImpl.
27   */
28  public class BallotAgentImpl implements BallotAgent {
29  
30  
31      /*** The Constant ABSENT. */
32      private static final String ABSENT = "Frånvarande"; //$NON-NLS-1$
33  
34      /*** The Constant ABSENT_STYLE2. */
35      private static final String ABSENT_STYLE2 = "Avstående"; //$NON-NLS-1$
36  
37      /*** The Constant ANCHOR. */
38      private static final String ANCHOR = "a"; //$NON-NLS-1$
39  
40      /*** The Constant COMMITEE_REPORT_AND_BALLOTS. */
41      private static final String COMMITEE_REPORT_AND_BALLOTS = "Utskottets förslag och kammarens omröstning"; //$NON-NLS-1$
42  
43      /*** The Constant HTTP_WWW_RIKSDAGEN_SE. */
44      private static final String HTTP_WWW_RIKSDAGEN_SE = "http://www.riksdagen.se"; //$NON-NLS-1$
45  
46      /*** The Constant LOGGER. */
47      private static final Log LOGGER = LogFactory
48              .getLog(BallotAgentImpl.class);
49  
50      /*** The Constant NEUTRAL. */
51      private static final String NEUTRAL = "Avstår"; //$NON-NLS-1$
52  
53      /*** The Constant NO. */
54      private static final String NO = "Nej"; //$NON-NLS-1$
55  
56      /*** The Constant SHOW_PARLIAMENT_MEMBERS_VOTES. */
57      private static final String SHOW_PARLIAMENT_MEMBERS_VOTES = "Visa ledamöternas röster"; //$NON-NLS-1$
58  
59      /*** The Constant TABLE. */
60      private static final String TABLE = "table"; //$NON-NLS-1$
61  
62      /*** The Constant YES. */
63      private static final String YES = "Ja"; //$NON-NLS-1$
64  
65      /*** The web client. */
66      private final WebClient webClient;
67  
68      /***
69       * Instantiates a new ballot agent impl.
70       *
71       * @param webClient the web client
72       */
73      public BallotAgentImpl(final WebClient webClient) {
74          super();
75          this.webClient = webClient;
76          this.webClient.setJavaScriptEnabled(false);
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see
83       * com.hack23.cia.service.agent.sweden.CommiteeReportAgent#findRöstadeballot
84       * (com.hack23.cia.model.sweden.CommiteeReport)
85       */
86      @Override
87  	public final List<Ballot> findBallots(final CommitteeReport commiteeReport) {
88          try {
89              final HtmlPage page = (HtmlPage) webClient.getPage(commiteeReport
90                      .getHref());
91  
92              final List<HtmlAnchor> anchors = page.getDocumentElement()
93                      .getHtmlElementsByTagName(ANCHOR);
94  
95              final HtmlAnchor findVoteAnchor = findVoteAnchor(anchors);
96  
97              if (findVoteAnchor != null) {
98                  return findVotedballots(findVoteAnchor);
99              }
100 
101         } catch (final Exception e) {
102             LOGGER.warn(e);
103         }
104         return null;
105     }
106 
107     /***
108      * Find vote anchor.
109      *
110      * @param anchors the anchors
111      * @return the html anchor
112      */
113     private HtmlAnchor findVoteAnchor(final List<HtmlAnchor> anchors) {
114         for (final HtmlAnchor anchor : anchors) {
115             if (COMMITEE_REPORT_AND_BALLOTS.equals(anchor.asText())) {
116                 return anchor;
117             }
118         }
119         return null;
120     }
121 
122     /***
123      * Find votedballots.
124      *
125      * @param votePage the vote page
126      * @return the list
127      */
128     private List<Ballot> findVotedballots(final HtmlAnchor votePage) {
129         final List<Ballot> resultat = new ArrayList<Ballot>();
130         HtmlPage page;
131         try {
132             page = (HtmlPage) votePage.click();
133             final List<HtmlAnchor> anchors = page.getDocumentElement()
134                     .getHtmlElementsByTagName(ANCHOR);
135             final List<HtmlTable> tables = page.getDocumentElement()
136                     .getHtmlElementsByTagName(TABLE);
137             int index = 0;
138 
139             for (final HtmlAnchor anchor : anchors) {
140                 if (SHOW_PARLIAMENT_MEMBERS_VOTES.equals(anchor.asText())) {
141 
142                     final Ballot ballot = new Ballot();
143                     ballot.setDescription(tables.get(index).getRow(0).asText());
144                     ballot.setOverviewHref(votePage.getHrefAttribute());
145                     ballot.setVoteResultsHref(HTTP_WWW_RIKSDAGEN_SE
146                             + anchor.getHrefAttribute());
147 
148                     resultat.add(ballot);
149                     index++;
150                 }
151             }
152 
153         } catch (final IOException e) {
154             LOGGER.warn("Problem accessing Votes", e); //$NON-NLS-1$
155         }
156         return resultat;
157     }
158 
159 
160     /*
161      * (non-Javadoc)
162      * 
163      * @see
164      * com.hack23.cia.service.agent.sweden.CommiteeReportAgent#getVoteResult
165      * (com.hack23.cia.model.sweden.Ballot)
166      */
167     @Override
168 	public final List<Vote> getVoteResult(final Ballot ballot) {
169         final List<Vote> resultat = new ArrayList<Vote>();
170         try {
171             final HtmlPage page = (HtmlPage) webClient.getPage(ballot
172                     .getVoteResultsHref());
173             final HtmlTable table = (HtmlTable) page.getDocumentElement()
174                     .getHtmlElementsByTagName(TABLE).iterator().next();
175 
176             final List<HtmlTableRow> rows = table.getRows();
177 
178             try {
179                 for (int i = 1; i < rows.size(); i++) {
180                     final HtmlTableRow row = rows.get(i);
181                     final String name = row.getCell(0).asText().trim();
182                     final String party = row.getCell(1).asText();
183                     final String electoralArea = row.getCell(2).asText();
184                     final String voteStr = row.getCell(3).asText().trim();
185 
186                     final ParliamentMember parliamentMember = new ParliamentMember();
187                     parliamentMember.setName(name);
188                     parliamentMember.setParty(party);
189                     parliamentMember.setElectoralRegion(electoralArea);
190                     parliamentMember.createFuzzyKey();
191 
192                     final Vote vote = new Vote();
193                     vote.setParliamentMember(parliamentMember);
194 
195                     if (voteStr.equalsIgnoreCase(YES)) {
196                         vote.setPosition(Vote.Position.Yes);
197                     } else if (voteStr.equalsIgnoreCase(NO)) {
198                         vote.setPosition(Vote.Position.No);
199                     } else if (voteStr.equalsIgnoreCase(ABSENT)) {
200                         vote.setPosition(Vote.Position.Absent);
201                     } else if (voteStr.equalsIgnoreCase(NEUTRAL)) {
202                         vote.setPosition(Vote.Position.Neutral);
203                     } else if (voteStr.equalsIgnoreCase(ABSENT_STYLE2)) {
204                         vote.setPosition(Vote.Position.Neutral);
205                     }
206 
207                     resultat.add(vote);
208                 }
209             } catch (final IndexOutOfBoundsException ie) {
210                 LOGGER.info("Vote result missing: " //$NON-NLS-1$
211                         + ballot.getVoteResultsHref()
212                         + "  for " + ballot.getOverviewHref()); //$NON-NLS-1$
213             }
214         } catch (final Exception e) {
215             LOGGER.warn("warn", e); //$NON-NLS-1$
216         }
217         return resultat;
218     }
219 
220 }