| 1 | |
|
| 2 | |
|
| 3 | |
|
| 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 | |
|
| 29 | |
|
| 30 | |
public class SwedishParliamentOrganisationStructureImporterImpl implements |
| 31 | |
GovernmentImporter |
| 32 | |
{ |
| 33 | |
|
| 34 | |
|
| 35 | 1 | private static Log log = LogFactory |
| 36 | |
.getLog(SwedishParliamentOrganisationStructureImporterImpl.class); |
| 37 | |
|
| 38 | |
|
| 39 | |
private final WebClient webClient; |
| 40 | |
|
| 41 | |
|
| 42 | |
private final PortalService portalService; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 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 | |
|
| 59 | |
|
| 60 | |
|
| 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 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 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 | |
|
| 170 | 0 | e.printStackTrace(); |
| 171 | 0 | } |
| 172 | |
|
| 173 | 0 | } |
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | 0 | class OrgStructureAnswerPage |
| 179 | |
{ |
| 180 | |
|
| 181 | |
|
| 182 | |
private String orgName; |
| 183 | |
|
| 184 | |
|
| 185 | |
private String shortName; |
| 186 | |
|
| 187 | |
|
| 188 | |
private String presidentName; |
| 189 | |
|
| 190 | |
|
| 191 | |
private String vicePresidentName; |
| 192 | |
|
| 193 | |
|
| 194 | 0 | private List<String> memberNames = new ArrayList<String>(); |
| 195 | |
|
| 196 | |
|
| 197 | 0 | private List<String> extrasNames = new ArrayList<String>(); |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 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 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
private String parseName(String str) { |
| 257 | 0 | return str.substring(0, str.indexOf("(") - 1).trim(); |
| 258 | |
} |
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
public String getOrgName() { |
| 266 | 0 | return orgName; |
| 267 | |
} |
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
public String getShortName() { |
| 275 | 0 | return shortName; |
| 276 | |
} |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
public String getPresidentName() { |
| 284 | 0 | return presidentName; |
| 285 | |
} |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
public String getVicePresidentName() { |
| 293 | 0 | return vicePresidentName; |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
public List<String> getMemberNames() { |
| 302 | 0 | return memberNames; |
| 303 | |
} |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
public List<String> getExtrasNames() { |
| 311 | 0 | return extrasNames; |
| 312 | |
} |
| 313 | |
} |
| 314 | |
} |