edu.cornell.mannlib.ld4lindexing.documents.AgentDocument.java Source code

Java tutorial

Introduction

Here is the source code for edu.cornell.mannlib.ld4lindexing.documents.AgentDocument.java

Source

/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.ld4lindexing.documents;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.ld4lindexing.QueryRunner;
import edu.cornell.mannlib.ld4lindexing.solrservers.SolrDocument;
import edu.cornell.mannlib.ld4lindexing.triplestores.TripleStore;

/**
 * Builds a Solr document for an Agent.
 */
public class AgentDocument extends BaseDocument {
    private static final Log log = LogFactory.getLog(AgentDocument.class);

    private static final String PROP_NAME = "http://http://xmlns.com/foaf/0.1/name";
    private static final String PROP_BIRTHDATE = "http://schema.org/birthDate";
    private static final String QUERY_CONTRIBUTIONS = "" //
            + "PREFIX ld4l: <http://bib.ld4l.org/ontology/> \n" //
            + "PREFIX prov: <http://www.w3.org/ns/prov#> \n" //
            + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" //
            + "SELECT ?work ?title ?isAuthor \n" //
            + "WHERE { \n" //
            + "  ?work ld4l:hasContribution ?c . \n" //
            + "  ?c prov:agent ?agent . \n" //
            + "  OPTIONAL { \n" //
            + "    ?work ld4l:hasTitle ?t . \n" //
            + "    ?t rdfs:label ?title . \n" //
            + "  } \n" //
            + "  OPTIONAL { \n" //
            + "    ?c a ld4l:AuthorContribution . \n" //
            + "    BIND(?c as ?isAuthor) \n" //
            + "  } \n" //
            + "} LIMIT 1000 \n";

    public AgentDocument(String uri, DocumentStatsAccumulator stats, TripleStore ts) {
        super(uri, stats, ts);
    }

    @Override
    public void populate() {
        populateProperties();
        populateValues();
        assembleSolrDocument();
        addToStats();
    }

    private void populateValues() {
        values.put("classes", findClasses());
        values.put("names", findPropertyStrings(PROP_NAME, "NO NAME"));

        List<List<LinkToken>> cc = findCreatedAndContributed();
        values.put("created", cc.get(0));
        values.put("contributed", cc.get(1));

        values.put("birthdate", findPropertyStrings(PROP_BIRTHDATE));
    }

    private List<List<LinkToken>> findCreatedAndContributed() {
        List<LinkToken> created = new ArrayList<>();
        List<LinkToken> contributed = new ArrayList<>();
        List<Map<String, Object>> results = new QueryRunner(ts, QUERY_CONTRIBUTIONS).bindUri("agent", uri)
                .execute();
        for (Map<String, Object> row : results) {
            String title = (String) row.get("title");
            if (title == null) {
                title = "NO TITLE";
            }
            if (row.get("isAuthor") == null) {
                contributed.add(new LinkToken(title, (String) row.get("work")));
            } else {
                created.add(new LinkToken(title, (String) row.get("work")));
            }
        }
        return Arrays.asList(created, contributed);
    }

    private void assembleSolrDocument() {
        log.debug("Creating Solr document: " + this);
        SolrDocument doc = new SolrDocument();

        doc.addFieldValue("id", UriUtils.uriToId(uri));
        doc.addFieldValues("class_facet", values.get("classes"));
        doc.addFieldValues("class_display", values.get("classes"));
        doc.addFieldValue("source_site_facet", sourceSite);
        doc.addFieldValue("source_site_display", sourceSite);

        addNameFields(doc);

        doc.addFieldValues("birthdate_t", values.get("birthdate"));
        doc.addFieldValues("created_token", LinkToken.tokens(values.get("created")));
        doc.addFieldValues("contributed_token", LinkToken.tokens(values.get("contributed")));

        doc.addFieldValues("text", values.get("names"));
        doc.addFieldValues("text", LinkToken.labels(values.get("created")));
        doc.addFieldValues("text", LinkToken.labels(values.get("contributed")));
        this.solrDocument = doc;
    }

    private void addNameFields(SolrDocument doc) {
        List<? extends Object> names = values.get("names");
        doc.addFieldValue("title_display", names.get(0));
        if (names.size() > 1) {
            doc.addFieldValues("alt_names_t", names.subList(1, names.size()));
        }
    }

}