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 * The Class SwedishParliamentOrganisationStructureImporterImpl.
29 */
30 public class SwedishParliamentOrganisationStructureImporterImpl implements
31 GovernmentImporter
32 {
33
34 /*** The log. */
35 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 WebClient webClient,PortalService portalService) throws MalformedURLException {
53 this.webClient = webClient;
54 this.portalService = portalService;
55 }
56
57
58
59
60
61
62 @SuppressWarnings("unchecked")
63 public void doImport() {
64 try {
65 Long orgId = portalService.findOrgByName(SwedishGovernmentImporterImpl.SVERIGES_RIKSDAG).getId();
66
67 createOrganisationWithMembers("Arbetsmarknadsutskottet", "AU",
68 "http://www.riksdagen.se/webbnav/index.aspx?nid=5304", orgId);
69 createOrganisationWithMembers("Civilutskottet", "CU",
70 "http://www.riksdagen.se/webbnav/index.aspx?nid=6104", orgId);
71 createOrganisationWithMembers("Finansutskottet", "FiU",
72 "http://www.riksdagen.se/webbnav/index.aspx?nid=5404", orgId);
73 createOrganisationWithMembers("Försvarsutskottet", "FöU",
74 "http://www.riksdagen.se/webbnav/index.aspx?nid=5454", orgId);
75 createOrganisationWithMembers("Justitieutskottet", "JuU",
76 "http://www.riksdagen.se/webbnav/index.aspx?nid=5504", orgId);
77 createOrganisationWithMembers("Konstitutionsutskottet", "KU",
78 "http://www.riksdagen.se/webbnav/index.aspx?nid=5554", orgId);
79 createOrganisationWithMembers("Kulturutskottet", "KrU",
80 "http://www.riksdagen.se/webbnav/index.aspx?nid=5604", orgId);
81 createOrganisationWithMembers("Miljö- och jordbruksutskottet", "MJU",
82 "http://www.riksdagen.se/webbnav/index.aspx?nid=5704", orgId);
83 createOrganisationWithMembers("Näringsutskottet", "NU",
84 "http://www.riksdagen.se/webbnav/index.aspx?nid=5754", orgId);
85 createOrganisationWithMembers("Skatteutskottet", "SkU",
86 "http://www.riksdagen.se/webbnav/index.aspx?nid=5804", orgId);
87 createOrganisationWithMembers("Socialförsäkringsutskottet", "SfU",
88 "http://www.riksdagen.se/webbnav/index.aspx?nid=5854", orgId);
89 createOrganisationWithMembers("Socialutskottet", "SoU",
90 "http://www.riksdagen.se/webbnav/index.aspx?nid=5904", orgId);
91 createOrganisationWithMembers("Trafikutskottet", "TU",
92 "http://www.riksdagen.se/webbnav/index.aspx?nid=5954", orgId);
93 createOrganisationWithMembers("Utbildningsutskottet", "UbU",
94 "http://www.riksdagen.se/webbnav/index.aspx?nid=6004", orgId);
95 createOrganisationWithMembers("Utrikesutskottet", "UU",
96 "http://www.riksdagen.se/webbnav/index.aspx?nid=6054", orgId);
97 createOrganisationWithMembers("Sammansatta konstitutions- och utrikesutskottet",
98 "KUU",
99 "http://www.riksdagen.se/webbnav/index.aspx?nid=16640", orgId);
100 createOrganisationWithMembers("Sammansatta utrikes- och försvarsutskottet", "UFöU",
101 "http://www.riksdagen.se/webbnav/index.aspx?nid=16110", orgId);
102 createOrganisationWithMembers("EU-nämnden", "",
103 "http://www.riksdagen.se/Webbnav/index.aspx?nid=6320", orgId);
104 } catch (Exception e) {
105 e.printStackTrace();
106 }
107 }
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 HtmlPage page = (HtmlPage) webClient.getPage(url);
121 OrgStructureAnswerPage answerPage = new OrgStructureAnswerPage(
122 (HtmlTable) page.getDocumentElement()
123 .getHtmlElementsByTagName("table").iterator().next(),
124 orgName, shortName);
125
126 try {
127 Organisation orgUnit = portalService.createOrgUnit(orgName, shortName, OrganisationType.Internal, orgId);
128
129 Person president = portalService.findMemberByNameInOrg(orgId,answerPage.presidentName);
130 if (president != null && president.getId() != null) {
131 portalService.addMemberById(orgUnit.getId(), president.getId());
132 Role presidentRole = portalService.createRoleInOrg(OrganisationRoles.President.toString(), orgUnit);
133 portalService.addRolePlayedById(presidentRole.getId(), president.getId());
134 } else {
135 System.out.println("Missing " + answerPage.presidentName);
136 }
137
138 Person vicePresident = portalService.findMemberByNameInOrg(orgId,answerPage.vicePresidentName);
139 if (vicePresident != null && vicePresident.getId() != null) {
140 portalService.addMemberById(orgUnit.getId(), vicePresident.getId());
141 Role vicePresidentRole = portalService.createRoleInOrg(OrganisationRoles.Vice_President.toString(), orgUnit);
142 portalService.addRolePlayedById(vicePresidentRole.getId(), vicePresident.getId());
143 } else {
144 System.out.println("Missing " + answerPage.vicePresidentName);
145 }
146
147 for (String name : answerPage.memberNames) {
148 Person member = portalService.findMemberByNameInOrg(orgId,name);
149
150 if (member != null && member.getId() != null) {
151 portalService.addMemberById(orgUnit.getId(), member.getId());
152 } else {
153 System.out.println("Missing " + name);
154 }
155 }
156
157 Role substituteRole = portalService.createRoleInOrg(OrganisationRoles.Substitute.toString(), orgUnit);
158
159 for (String name : answerPage.extrasNames) {
160 Person substitute = portalService.findMemberByNameInOrg(orgId,name);
161 if (substitute != null && substitute.getId() != null) {
162 portalService.addRolePlayedById(substituteRole.getId(), substitute.getId());
163 } else {
164 System.out.println("Missing " + name);
165 }
166 }
167
168 } catch (OrganisationAlreadyExistException e) {
169
170 e.printStackTrace();
171 }
172
173 }
174
175 /***
176 * The Class OrgStructureAnswerPage.
177 */
178 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 private List<String> memberNames = new ArrayList<String>();
195
196 /*** The extras names. */
197 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 String shortName) {
208 orgName = OrgName;
209 this.shortName = shortName;
210 List<HtmlTableRow> rows = new ArrayList<HtmlTableRow>(table
211 .getRows());
212
213 if (rows.remove(0).asText().equals("Ordförande")) {
214 presidentName = parseName(rows.remove(0).asText());
215 }
216 if (rows.remove(0).asText().equals("Vice ordförande")) {
217 vicePresidentName = parseName(rows.remove(0).asText());
218 }
219
220 if (rows.remove(0).asText().equals("Ledamot")) {
221
222 HtmlTableRow row = rows.remove(0);
223
224 while (!row.asText().equals("Suppleant")) {
225
226 HtmlTableCell cell1 = row.getCell(0);
227 memberNames.add(parseName(cell1.asText()));
228
229 if (row.getCells().size() > 1) {
230 HtmlTableCell cell2 = row.getCell(1);
231 memberNames.add(parseName(cell2.asText()));
232 }
233
234 row = rows.remove(0);
235 }
236
237 for (HtmlTableRow extras : rows) {
238
239 HtmlTableCell cell1 = extras.getCell(0);
240 extrasNames.add(parseName(cell1.asText()));
241
242 if (extras.getCells().size() > 1) {
243 HtmlTableCell cell2 = extras.getCell(1);
244 extrasNames.add(parseName(cell2.asText()));
245 }
246 }
247 }
248 }
249
250 /***
251 * Parses the name.
252 *
253 * @param str the str
254 * @return the string
255 */
256 private String parseName(String str) {
257 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 return orgName;
267 }
268
269 /***
270 * Gets the short name.
271 *
272 * @return the short name
273 */
274 public String getShortName() {
275 return shortName;
276 }
277
278 /***
279 * Gets the president name.
280 *
281 * @return the president name
282 */
283 public String getPresidentName() {
284 return presidentName;
285 }
286
287 /***
288 * Gets the vice president name.
289 *
290 * @return the vice president name
291 */
292 public String getVicePresidentName() {
293 return vicePresidentName;
294 }
295
296 /***
297 * Gets the member names.
298 *
299 * @return the member names
300 */
301 public List<String> getMemberNames() {
302 return memberNames;
303 }
304
305 /***
306 * Gets the extras names.
307 *
308 * @return the extras names
309 */
310 public List<String> getExtrasNames() {
311 return extrasNames;
312 }
313 }
314 }