Skip to content

Commit

Permalink
Merge pull request #1 from knaw-huc/poolparty
Browse files Browse the repository at this point in the history
Poolparty
  • Loading branch information
menzowindhouwer authored Nov 30, 2018
2 parents 0cee400 + 555c440 commit 2b99f02
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 6 deletions.
6 changes: 6 additions & 0 deletions conf/termennetwerk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
<nde:api>http://openskos.beeldengeluid.nl/api</nde:api>
<nde:conceptScheme>http://data.beeldengeluid.nl/gtaa/Onderwerpen</nde:conceptScheme>
</nde:dataset>
<nde:dataset id="poolparty" recipe="nl.knaw.huc.di.nde.recipe.PoolParty">
<nde:api>https://data.cultureelerfgoed.nl/PoolParty/sparql/term/id/cht</nde:api>
<nde:query>
PREFIX%20skos%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23%3E%0ACONSTRUCT%20%7B%0A%20%20%3Furi%20a%20skos%3AConcept%20%3B%0A%20%20%20%20%20%20%20%20%20skos%3AprefLabel%20%3FprefLabel%20%3B%0A%20%20%20%20%20%20%20%20%20skos%3AaltLabel%20%3FaltLabel%20%3B%0A%20%20%20%20%20%20%20%20%20skos%3AhiddenLabel%20%3FhiddenLabel%20%3B%0A%20%20%20%20%20%20%20%20%20skos%3AscopeNote%20%3FscopeNote%20.%0A%7D%0AWHERE%20%7B%0A%20%20%3Furi%20skos%3AprefLabel%20%3FprefLabel%20.%0A%20%20FILTER%20%28LANG%28%3FprefLabel%29%20%3D%20%22nl%22%29%20.%0A%20%20FILTER%20%28CONTAINS%28LCASE%28%3FprefLabel%29%2C%20LCASE%28%22${match}%22%29%29%29%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%3Furi%20skos%3AaltLabel%20%3FaltLabel%20.%0A%20%20%20%20%20%3Furi%20skos%3AhiddenLabel%20%3FhiddenLabel%20.%0A%20%20%20%20%20%3Furi%20skos%3AscopeNote%20%3FscopeNote%20.%0A%20%20%7D%0A%7D%0ALIMIT%201000%0A
</nde:query>
</nde:dataset>
</nde:NDE>
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-model</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-rdfxml</artifactId>
<version>2.4.2</version>
</dependency>

</dependencies>

<build>
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/nl/knaw/huc/di/nde/TermDTO.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package nl.knaw.huc.di.nde;

import com.google.common.collect.Lists;

import java.net.URI;
import java.util.List;

public class TermDTO {
public URI uri;
public List<String> prefLabel;
public List<String> altLabel;
public List<String> scopeNote;
public List<String> definition;
}
public URI uri;
public List<String> prefLabel = Lists.newArrayList();
public List<String> altLabel = Lists.newArrayList();
public List<String> scopeNote = Lists.newArrayList();
public List<String> definition = Lists.newArrayList();
}
87 changes: 87 additions & 0 deletions src/main/java/nl/knaw/huc/di/nde/recipe/PoolParty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package nl.knaw.huc.di.nde.recipe;

import com.google.common.collect.Lists;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmItem;
import nl.knaw.huc.di.nde.TermDTO;
import nl.mpi.tla.util.Saxon;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class PoolParty implements RecipeInterface {

private static final Logger LOG = LoggerFactory.getLogger(PoolParty.class);

@Override
public List<TermDTO> fetchMatchingTerms(XdmItem config, String match) {

ArrayList<TermDTO> terms = Lists.newArrayList();
try {
String api = Saxon.xpath2string(config, "nde:api", null, OpenSKOS.NAMESPACES);
String query = Saxon.xpath2string(config, "nde:query", null, OpenSKOS.NAMESPACES);

URLEncoder.encode(query, "UTF-8");
query = URLDecoder.decode(query.replace("${match}", match).trim(), "UTF-8");

CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(api);
ArrayList<BasicNameValuePair> parameters = Lists.newArrayList(
new BasicNameValuePair("query", query));
post.setEntity(new UrlEncodedFormEntity(parameters));
CloseableHttpResponse httpResponse = client.execute(post);

Model model = Rio.parse(httpResponse.getEntity().getContent(), "https://data.cultureelerfgoed.nl/term/id/cht/",
RDFFormat.RDFXML);
String subject = null;
TermDTO term = null;
for (Statement statement : model) {
if (subject == null || !statement.getSubject().stringValue().equals(subject)) {
subject = statement.getSubject().stringValue();
if (term != null) {
terms.add(term);
}
term = new TermDTO();
term.uri = new URI(subject);
}
if (statement.getPredicate().getLocalName().equals("prefLabel")) {
term.prefLabel.add(statement.getObject().stringValue());
}
if (statement.getPredicate().getLocalName().equals("altLabel")) {
term.altLabel.add(statement.getObject().stringValue());
}
if (statement.getPredicate().getLocalName().equals("hiddenLabel")) {
term.definition.add(statement.getObject().stringValue());
}
if (statement.getPredicate().getLocalName().equals("scopeNote")) {
term.scopeNote.add(statement.getObject().stringValue());
}

}
terms.add(term);

} catch (IOException | SaxonApiException | URISyntaxException e) {
LOG.error("Request failed: ", e);
}


return terms;
}
}

0 comments on commit 2b99f02

Please sign in to comment.