All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metafacture.linkeddata.OreAggregationAdder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 * 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 org.metafacture.linkeddata;

import org.metafacture.commons.ResourceUtil;
import org.metafacture.commons.types.ListMap;
import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.MetafactureException;
import org.metafacture.framework.StreamReceiver;
import org.metafacture.framework.annotations.Description;
import org.metafacture.framework.annotations.In;
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.helpers.DefaultStreamPipe;

import java.io.IOException;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Pattern;

/**
 * Adds ore:Aggregation to an Europeana Data Model stream. The aggregation id is
 * set by emitting literal('aggregation_id', id).
 *
 * @author Markus Michael Geipel
 *
 */
@Description("adds ore:Aggregation to an Europeana Data Model stream. The aggregation id is set by emitting literal('aggregation_id', id)")
@In(StreamReceiver.class)
@Out(StreamReceiver.class)
@FluxCommand("add-oreaggregation")
public final class OreAggregationAdder extends DefaultStreamPipe {

    private static final String RDF_ABOUT = "~rdf:about";
    private static final ListMap AGGREGATED_ENTITIES = new ListMap();
    private static final String ORE_AGGREGATION_PROPERTIES = "ore-aggregation.properties";
    private static final String ORE_AGGREGATION = "ore:Aggregation";
    private static final String AGGREGATION_ID = "aggregation_id";
    private static final String RDF_REFERENCE = "~rdf:resource";
    private static final Pattern SPLIT_PATTERN = Pattern.compile("\\s*,\\s*");
    private final Deque entityStack = new LinkedList();
    private final ListMap aggregation = new ListMap();
    private String aggregationId;

    static {
        final Properties properties;
        try {
            properties = ResourceUtil.loadProperties(ORE_AGGREGATION_PROPERTIES);
        }
        catch (final IOException e) {
            throw new MetafactureException("Failed to load properties", e);
        }
        for (final Entry entry : properties.entrySet()) {
            final String[] parts = SPLIT_PATTERN.split(entry.getValue().toString());
            final String name = entry.getKey().toString();
            for (final String value : parts) {
                AGGREGATED_ENTITIES.add(name, value);
            }
        }
    }

    /**
     * Creates an instance of {@link OreAggregationAdder}.
     */
    public OreAggregationAdder() {
    }

    @Override
    public void startRecord(final String identifier) {
        entityStack.clear();
        aggregationId = identifier;
        getReceiver().startRecord(identifier);
    }

    @Override
    public void endRecord() {
        writeAggregation();
        aggregation.clear();
        getReceiver().endRecord();
    }

    private void writeAggregation() {
        if (!aggregation.isEmpty()) {
            final StreamReceiver receiver = getReceiver();
            receiver.startEntity(ORE_AGGREGATION);
            receiver.literal(RDF_ABOUT, aggregationId);
            for (final Entry> entry : aggregation.entrySet()) {
                final String key = entry.getKey();
                if (AGGREGATED_ENTITIES.containsKey(key)) {

                    for (final String entity : AGGREGATED_ENTITIES.get(key)) {
                        for (final String value : entry.getValue()) {
                            receiver.startEntity(entity);
                            receiver.literal(RDF_REFERENCE, value);
                            receiver.endEntity();
                        }
                    }
                }
                else {
                    for (final String value : entry.getValue()) {
                        receiver.literal(key, value);
                    }
                }
            }
            receiver.endEntity();
        }
    }

    @Override
    public void startEntity(final String name) {
        entityStack.push(name);
        getReceiver().startEntity(name);
    }

    @Override
    public void endEntity() {
        entityStack.pop();
        getReceiver().endEntity();
    }

    @Override
    public void literal(final String name, final String value) {
        if (entityStack.isEmpty()) {
            if (AGGREGATION_ID.equals(name)) {
                aggregationId = value;
            }
            else {
                aggregation.add(name, value);
            }
            return;
        }

        if (entityStack.size() == 1 && RDF_ABOUT.equals(name) && AGGREGATED_ENTITIES.containsKey(entityStack.peek())) {
            aggregation.add(entityStack.peek(), value);
        }
        getReceiver().literal(name, value);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy