001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.event.serialization;
007
008import static java.nio.charset.StandardCharsets.UTF_8;
009import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
010
011import java.io.ByteArrayInputStream;
012
013import org.apache.jena.rdf.model.Model;
014import org.fcrepo.kernel.api.observer.Event;
015
016/**
017 * A basic serialization API for Fedora events
018 * @author acoburn
019 * @author dbernstein
020 */
021public interface EventSerializer {
022
023    /**
024     * Convert an event to an Rdf Model
025     * @param evt the Fedora event
026     * @return an RDF model representing the event
027     */
028    static Model toModel(final Event evt) {
029        final EventSerializer serializer = new JsonLDSerializer();
030        final String json = serializer.serialize(evt);
031        final Model model = createDefaultModel();
032        final String baseUrl = evt.getBaseUrl();
033        model.read(new ByteArrayInputStream(json.getBytes(UTF_8)), baseUrl + evt.getPath(), "JSON-LD");
034        return model;
035    }
036
037    /**
038     * Serialize a Event into a JSON String
039     * @param evt the Fedora event
040     * @return a JSON string
041     */
042    String serialize(final Event evt);
043}