001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.event.serialization; 019 020import static com.hp.hpl.jena.datatypes.xsd.XSDDatatype.XSDdateTime; 021import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel; 022import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty; 023import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource; 024import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral; 025import static com.hp.hpl.jena.vocabulary.RDF.type; 026import static com.hp.hpl.jena.vocabulary.DCTerms.identifier; 027import static com.hp.hpl.jena.vocabulary.DCTerms.isPartOf; 028import static org.fcrepo.kernel.api.RdfLexicon.PROV_NAMESPACE; 029import static org.fcrepo.kernel.api.observer.OptionalValues.BASE_URL; 030import static org.fcrepo.kernel.api.observer.OptionalValues.USER_AGENT; 031 032import com.hp.hpl.jena.rdf.model.Model; 033import com.hp.hpl.jena.rdf.model.Resource; 034import org.fcrepo.kernel.api.observer.FedoraEvent; 035 036/** 037 * A basic serialization API for Fedora events 038 * @author acoburn 039 */ 040public interface EventSerializer { 041 042 /** 043 * Convert an event to an Rdf Model 044 * @param evt the Fedora event 045 * @return an RDF model representing the event 046 */ 047 public static Model toModel(final FedoraEvent evt) { 048 final String FOAF = "http://xmlns.com/foaf/0.1/"; 049 050 final Model model = createDefaultModel(); 051 final String baseUrl = evt.getInfo().get(BASE_URL); 052 final String userAgent = evt.getInfo().get(USER_AGENT); 053 final Resource root = model.createResource((baseUrl != null ? baseUrl : "info:fedora" ) + evt.getPath()); 054 055 evt.getResourceTypes().forEach(rdfType -> { 056 root.addProperty(type, createResource(rdfType)); 057 }); 058 059 if (baseUrl != null) { 060 root.addProperty(isPartOf, createResource(baseUrl)); 061 } 062 063 final Resource activity = model.createResource() 064 .addProperty(type, createResource(PROV_NAMESPACE + "Activity")) 065 .addProperty(identifier, createResource(evt.getEventID())) 066 .addLiteral(createProperty(PROV_NAMESPACE, "atTime"), 067 createTypedLiteral(evt.getDate().toString(), XSDdateTime)); 068 069 evt.getTypes().stream().map(rdfType -> rdfType.getType()).forEach(rdfType -> { 070 activity.addProperty(type, createResource(rdfType)); 071 }); 072 073 root.addProperty(createProperty(PROV_NAMESPACE, "wasGeneratedBy"), activity); 074 075 root.addProperty(createProperty(PROV_NAMESPACE, "wasAttributedTo"), 076 model.createResource() 077 .addProperty(type, createResource(PROV_NAMESPACE + "Person")) 078 .addLiteral(createProperty(FOAF, "name"), evt.getUserID())); 079 080 if (userAgent != null) { 081 root.addProperty(createProperty(PROV_NAMESPACE, "wasAttributedTo"), 082 model.createResource() 083 .addProperty(type, createResource(PROV_NAMESPACE + "SoftwareAgent")) 084 .addLiteral(createProperty(FOAF, "name"), userAgent)); 085 } 086 087 return model; 088 } 089 090 /** 091 * Serialize a FedoraEvent into a JSON String 092 * @param evt the Fedora event 093 * @return a JSON string 094 */ 095 String serialize(final FedoraEvent evt); 096}