Coverage Report - org.directdemocracyportal.democracy.service.governmentloader.SwedishParliamentOrganisationStructureImporterImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
SwedishParliamentOrganisationStructureImporterImpl
8%
5/62
0%
0/20
2.727
SwedishParliamentOrganisationStructureImporterImpl$OrgStructureAnswerPage
0%
0/36
0%
0/14
2.727
 
 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.util.ArrayList;
 9  
 import java.util.List;
 10  
 
 11  
 import org.apache.commons.logging.Log;
 12  
 import org.apache.commons.logging.LogFactory;
 13  
 import org.directdemocracyportal.democracy.model.core.Role;
 14  
 import org.directdemocracyportal.democracy.model.world.Organisation;
 15  
 import org.directdemocracyportal.democracy.model.world.OrganisationType;
 16  
 import org.directdemocracyportal.democracy.model.world.Person;
 17  
 import org.directdemocracyportal.democracy.model.world.Organisation.OrganisationRoles;
 18  
 import org.directdemocracyportal.democracy.service.OrganisationAlreadyExistException;
 19  
 import org.directdemocracyportal.democracy.service.PortalService;
 20  
 
 21  
 import com.gargoylesoftware.htmlunit.WebClient;
 22  
 import com.gargoylesoftware.htmlunit.html.HtmlPage;
 23  
 import com.gargoylesoftware.htmlunit.html.HtmlTable;
 24  
 import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
 25  
 import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
 26  
 
 27  
 /**
 28  
  * The Class SwedishParliamentOrganisationStructureImporterImpl.
 29  
  */
 30  
 public class SwedishParliamentOrganisationStructureImporterImpl implements
 31  
         GovernmentImporter
 32  
 {
 33  
 
 34  
     /** The log. */
 35  1
     private static Log log = LogFactory
 36  
             .getLog(SwedishParliamentOrganisationStructureImporterImpl.class);
 37  
 
 38  
     /** The web client. */
 39  
     private final WebClient webClient;
 40  
 
 41  
     /** The portal service. */
 42  
     private final PortalService portalService;
 43  
 
 44  
     /**
 45  
      * Instantiates a new swedish parliament organisation structure importer impl.
 46  
      *
 47  
      * @param webClient the web client
 48  
      * @param portalService the portal service
 49  
      * @throws MalformedURLException the malformed url exception
 50  
      */
 51  
     public SwedishParliamentOrganisationStructureImporterImpl(
 52  1
             WebClient webClient,PortalService portalService) throws MalformedURLException {
 53  1
         this.webClient = webClient;
 54  1
         this.portalService = portalService;
 55  1
     }
 56  
 
 57  
     /*
 58  
      * (non-Javadoc)
 59  
      *
 60  
      * @see org.directdemocracyportal.democracy.service.governmentloader.GovernmentImporter#doImport()
 61  
      */
 62  
     @SuppressWarnings("unchecked")
 63  
     public void doImport() {
 64  
         try {
 65  0
             Long orgId = portalService.findOrgByName(SwedishGovernmentImporterImpl.SVERIGES_RIKSDAG).getId();
 66  
 
 67  0
             createOrganisationWithMembers("Arbetsmarknadsutskottet", "AU",
 68  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5304", orgId);
 69  0
             createOrganisationWithMembers("Civilutskottet", "CU",
 70  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=6104", orgId);
 71  0
             createOrganisationWithMembers("Finansutskottet", "FiU",
 72  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5404", orgId);
 73  0
             createOrganisationWithMembers("Försvarsutskottet", "FöU",
 74  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5454", orgId);
 75  0
             createOrganisationWithMembers("Justitieutskottet", "JuU",
 76  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5504", orgId);
 77  0
             createOrganisationWithMembers("Konstitutionsutskottet", "KU",
 78  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5554", orgId);
 79  0
             createOrganisationWithMembers("Kulturutskottet", "KrU",
 80  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5604", orgId);
 81  0
             createOrganisationWithMembers("Miljö- och jordbruksutskottet", "MJU",
 82  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5704", orgId);
 83  0
             createOrganisationWithMembers("Näringsutskottet", "NU",
 84  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5754", orgId);
 85  0
             createOrganisationWithMembers("Skatteutskottet", "SkU",
 86  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5804", orgId);
 87  0
             createOrganisationWithMembers("Socialförsäkringsutskottet", "SfU",
 88  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5854", orgId);
 89  0
             createOrganisationWithMembers("Socialutskottet", "SoU",
 90  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5904", orgId);
 91  0
             createOrganisationWithMembers("Trafikutskottet", "TU",
 92  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=5954", orgId);
 93  0
             createOrganisationWithMembers("Utbildningsutskottet", "UbU",
 94  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=6004", orgId);
 95  0
             createOrganisationWithMembers("Utrikesutskottet", "UU",
 96  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=6054", orgId);
 97  0
             createOrganisationWithMembers("Sammansatta konstitutions- och utrikesutskottet",
 98  
                     "KUU",
 99  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=16640", orgId);
 100  0
             createOrganisationWithMembers("Sammansatta utrikes- och försvarsutskottet", "UFöU",
 101  
                     "http://www.riksdagen.se/webbnav/index.aspx?nid=16110", orgId);
 102  0
             createOrganisationWithMembers("EU-nämnden", "",
 103  
                     "http://www.riksdagen.se/Webbnav/index.aspx?nid=6320", orgId);
 104  0
         } catch (Exception e) {
 105  0
             e.printStackTrace();
 106  0
         }
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Creates the organisation with members.
 111  
      *
 112  
      * @param orgName the org name
 113  
      * @param shortName the short name
 114  
      * @param url the url
 115  
      * @param orgId the org id
 116  
      * @throws Exception the exception
 117  
      */
 118  
     private void createOrganisationWithMembers(String orgName,
 119  
             String shortName, String url, Long orgId) throws Exception  {
 120  0
         HtmlPage page = (HtmlPage) webClient.getPage(url);
 121  0
         OrgStructureAnswerPage answerPage = new OrgStructureAnswerPage(
 122  
                 (HtmlTable) page.getDocumentElement()
 123  
                         .getHtmlElementsByTagName("table").iterator().next(),
 124  
                 orgName, shortName);
 125  
 
 126  
         try {
 127  0
             Organisation orgUnit = portalService.createOrgUnit(orgName, shortName, OrganisationType.Internal, orgId);
 128  
 
 129  0
             Person president = portalService.findMemberByNameInOrg(orgId,answerPage.presidentName);
 130  0
             if (president != null && president.getId() != null) {
 131  0
                 portalService.addMemberById(orgUnit.getId(), president.getId());
 132  0
                 Role presidentRole = portalService.createRoleInOrg(OrganisationRoles.President.toString(), orgUnit);
 133  0
                 portalService.addRolePlayedById(presidentRole.getId(), president.getId());
 134  0
             } else {
 135  0
                 System.out.println("Missing " + answerPage.presidentName);
 136  
             }
 137  
 
 138  0
             Person vicePresident = portalService.findMemberByNameInOrg(orgId,answerPage.vicePresidentName);
 139  0
             if (vicePresident != null && vicePresident.getId() != null) {
 140  0
                 portalService.addMemberById(orgUnit.getId(), vicePresident.getId());
 141  0
                 Role vicePresidentRole = portalService.createRoleInOrg(OrganisationRoles.Vice_President.toString(), orgUnit);
 142  0
                 portalService.addRolePlayedById(vicePresidentRole.getId(), vicePresident.getId());
 143  0
             } else {
 144  0
                 System.out.println("Missing " + answerPage.vicePresidentName);
 145  
             }
 146  
 
 147  0
             for (String name : answerPage.memberNames) {
 148  0
                 Person member = portalService.findMemberByNameInOrg(orgId,name);
 149  
 
 150  0
                 if (member != null && member.getId() != null) {
 151  0
                     portalService.addMemberById(orgUnit.getId(), member.getId());
 152  
                 } else {
 153  0
                     System.out.println("Missing " + name);
 154  
                 }
 155  0
             }
 156  
 
 157  0
             Role substituteRole = portalService.createRoleInOrg(OrganisationRoles.Substitute.toString(), orgUnit);
 158  
 
 159  0
             for (String name : answerPage.extrasNames) {
 160  0
                 Person substitute = portalService.findMemberByNameInOrg(orgId,name);
 161  0
                 if (substitute != null && substitute.getId() != null) {
 162  0
                     portalService.addRolePlayedById(substituteRole.getId(), substitute.getId());
 163  
                 } else {
 164  0
                     System.out.println("Missing " + name);
 165  
                 }
 166  0
             }
 167  
 
 168  0
         } catch (OrganisationAlreadyExistException e) {
 169  
             // TODO Auto-generated catch block
 170  0
             e.printStackTrace();
 171  0
         }
 172  
 
 173  0
     }
 174  
 
 175  
     /**
 176  
      * The Class OrgStructureAnswerPage.
 177  
      */
 178  0
     class OrgStructureAnswerPage
 179  
     {
 180  
 
 181  
         /** The org name. */
 182  
         private String orgName;
 183  
 
 184  
         /** The short name. */
 185  
         private String shortName;
 186  
 
 187  
         /** The president name. */
 188  
         private String presidentName;
 189  
 
 190  
         /** The vice president name. */
 191  
         private String vicePresidentName;
 192  
 
 193  
         /** The member names. */
 194  0
         private List<String> memberNames = new ArrayList<String>();
 195  
 
 196  
         /** The extras names. */
 197  0
         private List<String> extrasNames = new ArrayList<String>();
 198  
 
 199  
         /**
 200  
          * Instantiates a new org structure answer page.
 201  
          *
 202  
          * @param table the table
 203  
          * @param OrgName the org name
 204  
          * @param shortName the short name
 205  
          */
 206  
         public OrgStructureAnswerPage(HtmlTable table, String OrgName,
 207  0
                 String shortName) {
 208  0
             orgName = OrgName;
 209  0
             this.shortName = shortName;
 210  0
             List<HtmlTableRow> rows = new ArrayList<HtmlTableRow>(table
 211  
                     .getRows());
 212  
 
 213  0
             if (rows.remove(0).asText().equals("Ordförande")) {
 214  0
                 presidentName = parseName(rows.remove(0).asText());
 215  
             }
 216  0
             if (rows.remove(0).asText().equals("Vice ordförande")) {
 217  0
                 vicePresidentName = parseName(rows.remove(0).asText());
 218  
             }
 219  
 
 220  0
             if (rows.remove(0).asText().equals("Ledamot")) {
 221  
 
 222  0
                 HtmlTableRow row = rows.remove(0);
 223  
 
 224  0
                 while (!row.asText().equals("Suppleant")) {
 225  
 
 226  0
                     HtmlTableCell cell1 = row.getCell(0);
 227  0
                     memberNames.add(parseName(cell1.asText()));
 228  
 
 229  0
                     if (row.getCells().size() > 1) {
 230  0
                         HtmlTableCell cell2 = row.getCell(1);
 231  0
                         memberNames.add(parseName(cell2.asText()));
 232  
                     }
 233  
 
 234  0
                     row = rows.remove(0);
 235  0
                 }
 236  
 
 237  0
                 for (HtmlTableRow extras : rows) {
 238  
 
 239  0
                     HtmlTableCell cell1 = extras.getCell(0);
 240  0
                     extrasNames.add(parseName(cell1.asText()));
 241  
 
 242  0
                     if (extras.getCells().size() > 1) {
 243  0
                         HtmlTableCell cell2 = extras.getCell(1);
 244  0
                         extrasNames.add(parseName(cell2.asText()));
 245  
                     }
 246  0
                 }
 247  
             }
 248  0
         }
 249  
 
 250  
         /**
 251  
          * Parses the name.
 252  
          *
 253  
          * @param str the str
 254  
          * @return the string
 255  
          */
 256  
         private String parseName(String str) {
 257  0
             return str.substring(0, str.indexOf("(") - 1).trim();
 258  
         }
 259  
 
 260  
         /**
 261  
          * Gets the org name.
 262  
          *
 263  
          * @return the org name
 264  
          */
 265  
         public String getOrgName() {
 266  0
             return orgName;
 267  
         }
 268  
 
 269  
         /**
 270  
          * Gets the short name.
 271  
          *
 272  
          * @return the short name
 273  
          */
 274  
         public String getShortName() {
 275  0
             return shortName;
 276  
         }
 277  
 
 278  
         /**
 279  
          * Gets the president name.
 280  
          *
 281  
          * @return the president name
 282  
          */
 283  
         public String getPresidentName() {
 284  0
             return presidentName;
 285  
         }
 286  
 
 287  
         /**
 288  
          * Gets the vice president name.
 289  
          *
 290  
          * @return the vice president name
 291  
          */
 292  
         public String getVicePresidentName() {
 293  0
             return vicePresidentName;
 294  
         }
 295  
 
 296  
         /**
 297  
          * Gets the member names.
 298  
          *
 299  
          * @return the member names
 300  
          */
 301  
         public List<String> getMemberNames() {
 302  0
             return memberNames;
 303  
         }
 304  
 
 305  
         /**
 306  
          * Gets the extras names.
 307  
          *
 308  
          * @return the extras names
 309  
          */
 310  
         public List<String> getExtrasNames() {
 311  0
             return extrasNames;
 312  
         }
 313  
     }
 314  
 }