de.qucosa.repository.FedoraRepository.java Source code

Java tutorial

Introduction

Here is the source code for de.qucosa.repository.FedoraRepository.java

Source

/*
 * Copyright 2016 Saxon State and University Library Dresden (SLUB)
 *
 * 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.
 */

package de.qucosa.repository;

import com.yourmediashelf.fedora.client.FedoraClient;
import com.yourmediashelf.fedora.client.FedoraClientException;
import com.yourmediashelf.fedora.client.request.GetDatastreamDissemination;
import com.yourmediashelf.fedora.client.request.GetDatastreams;
import com.yourmediashelf.fedora.client.request.GetObjectProfile;
import com.yourmediashelf.fedora.client.request.RiSearch;
import com.yourmediashelf.fedora.client.response.FedoraResponse;
import com.yourmediashelf.fedora.client.response.GetDatastreamsResponse;
import com.yourmediashelf.fedora.client.response.GetObjectProfileResponse;
import com.yourmediashelf.fedora.client.response.RiSearchResponse;
import com.yourmediashelf.fedora.generated.access.ObjectProfile;
import com.yourmediashelf.fedora.generated.management.DatastreamProfile;
import de.slubDresden.InfoDocument;
import gov.loc.mods.v3.ModsDocument;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.io.IOUtils;
import org.apache.xmlbeans.XmlException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class FedoraRepository {

    private static final Logger log = LoggerFactory.getLogger(FedoraRepository.class);
    private static final String MODS_DSID = "MODS";
    private static final String SLUB_INFO_DSID = "SLUB-INFO";
    private final Cache cache;

    private final FedoraClient fedoraClient;

    public FedoraRepository(FedoraClient fedoraClient, Cache cache) {
        this.fedoraClient = fedoraClient;
        this.cache = cache;
    }

    public String resolveIdentifier(String id) throws FedoraClientException, IOException {
        RiSearch riSearch = new RiSearch(String.format("?s <dc:identifier> \"%s\"", id)).type("triples").lang("spo")
                .format("N-Triples").limit(1);
        RiSearchResponse riSearchResult = riSearch.execute(fedoraClient);
        InputStream is = riSearchResult.getEntityInputStream();
        String result = IOUtils.toString(is);
        return (result.isEmpty()) ? null : result.substring(result.indexOf('/') + 1, result.indexOf('>'));
    }

    public ModsDocument getModsDocument(String pid) throws XmlException, IOException {
        return ModsDocument.Factory.parse(getDatastream(pid, MODS_DSID));
    }

    public InfoDocument getSlubInfoDocumentIfPresent(String pid) throws XmlException, IOException {
        return InfoDocument.Factory.parse(getDatastream(pid, SLUB_INFO_DSID));
    }

    public ObjectProfile getObjectProfile(String pid) throws FedoraClientException {
        GetObjectProfile profileRequest = new GetObjectProfile(pid);
        GetObjectProfileResponse profileResponse = profileRequest.execute(fedoraClient);
        return profileResponse.getObjectProfile();
    }

    public Map<String, DatastreamProfile> getDatastreamProfiles(String pid) throws FedoraClientException {
        GetDatastreams getDatastreamsRequest = new GetDatastreams(pid);
        GetDatastreamsResponse getDatastreamsResponse = getDatastreamsRequest.execute(fedoraClient);
        Map<String, DatastreamProfile> result = new HashMap<>();
        for (DatastreamProfile dp : getDatastreamsResponse.getDatastreamProfiles()) {
            result.put(dp.getDsID(), dp);
        }
        return result;
    }

    public Map<String, RelsExtPredicateEnum> identifyReferencingObjects(RelsExtPredicateEnum predicate,
            String referencedId) {

        final LinkedHashMap<String, RelsExtPredicateEnum> resultMap = new LinkedHashMap<>();
        final String query = String.format("select distinct ?source where {" + " ?target <dc:identifier> '%1$s' ."
                + " ?target <dc:identifier> ?refliteral ." + " ?source <fedora-rels-ext:%2$s> ?refiri"
                + " filter (regex(str(?refiri), str(?refliteral)))}", referencedId, predicate);

        RiSearchResponse riSearchResponse = null;
        try {
            RiSearch riSearch = new RiSearch(query).format("csv");
            riSearchResponse = riSearch.execute(fedoraClient);

            BufferedReader b = new BufferedReader(new InputStreamReader(riSearchResponse.getEntityInputStream()));
            b.readLine();
            while (b.ready()) {
                final String line = b.readLine();
                final String pid = line.substring(line.indexOf('/') + 1);
                resultMap.put(pid, predicate);
            }

        } catch (IOException | ArrayIndexOutOfBoundsException e) {
            log.warn("Failed to parse Fedora Resource Index result", e);
        } catch (FedoraClientException e) {
            log.warn("Failed querying Fedora Resource Index", e);
        } finally {
            if (riSearchResponse != null)
                riSearchResponse.close();
        }

        return resultMap;
    }

    private String getDatastream(String pid, String dsid) throws IOException {
        try {
            final String cacheKey = String.format("%s-%s", pid, dsid);
            final Element cacheElement = cache.get(cacheKey);
            if (cacheElement == null) {
                InputStream entityInputStream = getDatastreamDissemination(pid, dsid);
                final String xml = IOUtils.toString(entityInputStream);
                cache.put(new Element(cacheKey, xml));
                return xml;
            } else {
                return (String) cacheElement.getObjectValue();
            }
        } catch (FedoraClientException e) {
            throw new IOException(String.format("Cannot retrieve datastream '%s/%s'", pid, dsid));
        }
    }

    private InputStream getDatastreamDissemination(String pid, String dsid) throws FedoraClientException {
        GetDatastreamDissemination ds = new GetDatastreamDissemination(pid, dsid);
        FedoraResponse fr = ds.execute(fedoraClient);
        return fr.getEntityInputStream();
    }

}