Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License 1.0
* which is available at http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
package org.eclipse.lyo.oslc4j.trs.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.namespace.QName;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.RDFWriterI;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.eclipse.lyo.core.trs.Base;
import org.eclipse.lyo.core.trs.ChangeEvent;
import org.eclipse.lyo.core.trs.Creation;
import org.eclipse.lyo.core.trs.Deletion;
import org.eclipse.lyo.core.trs.Modification;
import org.eclipse.lyo.core.trs.Page;
import org.eclipse.lyo.core.trs.TRSConstants;
import org.eclipse.lyo.oslc4j.core.exception.OslcCoreApplicationException;
import org.eclipse.lyo.oslc4j.core.model.OslcConstants;
import org.eclipse.lyo.oslc4j.provider.jena.JenaModelHelper;
/**
* A utility class for TRS operations
*
* @version $version-stub$
* @since 2.3.0
*/
public class TRSUtil {
public static final SimpleDateFormat XSD_DATETIME_FORMAT;
public final static URI NIL_URI = URI.create(TRSConstants.RDF_NIL);
public static QName dateModifiedQname = new QName(OslcConstants.DCTERMS_NAMESPACE, "modified");
// static Comparator changeEventComparator = Comparator.comparing(
// ChangeEvent::getOrder, BigInteger::compareTo).reversed();
static {
XSD_DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
XSD_DATETIME_FORMAT.setTimeZone(TimeZone.getDefault());
}
/**
* Return the list of resources from the rdf model with the given rdf type
*
* @param model the rdf model in which the resources will be looked 4
* @param fullQualifiedRDFTypeName the type of resources to look 4
*
* @return a list of rdf resources with the given type
*/
public static List getResourcesWithTypeFromModel(Model model,
String fullQualifiedRDFTypeName) {
List resources = new ArrayList<>();
ResIterator listSubjects = null;
listSubjects = model.listSubjectsWithProperty(
RDF.type,
model.getResource(fullQualifiedRDFTypeName));
for (final ResIterator resIterator = listSubjects; resIterator.hasNext(); ) {
Resource resource = resIterator.next();
resources.add(resource);
}
return resources;
}
/**
* look for and return the change events inside the given rdf model
*
* @param changeLogJenaModel the rdf model inside which the change events will be looked 4
*
* @return the list of change events found in the rdf model
*/
public static List getChangeEventsFromChangeLogJenaModel(Model changeLogJenaModel)
throws IllegalAccessException, IllegalArgumentException, InstantiationException,
InvocationTargetException, SecurityException, NoSuchMethodException,
DatatypeConfigurationException, OslcCoreApplicationException, URISyntaxException {
List changeEvents = new ArrayList<>();
List modificationResources = getResourcesWithTypeFromModel(changeLogJenaModel,
TRSConstants
.TRS_TYPE_MODIFICATION);
List creationResources = getResourcesWithTypeFromModel(changeLogJenaModel,
TRSConstants
.TRS_TYPE_CREATION);
List deletionResources = getResourcesWithTypeFromModel(changeLogJenaModel,
TRSConstants
.TRS_TYPE_DELETION);
for (Resource jenaRes : modificationResources) {
ChangeEvent changeEvent = (Modification) JenaModelHelper.fromJenaResource(
jenaRes,
Modification.class);
changeEvents.add(changeEvent);
}
for (Resource jenaRes : creationResources) {
ChangeEvent changeEvent = (Creation) JenaModelHelper.fromJenaResource(
jenaRes,
Creation.class);
changeEvents.add(changeEvent);
}
for (Resource jenaRes : deletionResources) {
ChangeEvent changeEvent = (Deletion) JenaModelHelper.fromJenaResource(
jenaRes,
Deletion.class);
changeEvents.add(changeEvent);
}
return changeEvents;
}
/**
* return an rdf jena model object after reading it from the given file path
*
* @param filePath the path of the file from which the rdf model shall be read
*
* @return the rdf model read from the file
*/
public static Model readJenaModelFromFile(String filePath) throws IOException {
final Model rdFModel = ModelFactory.createDefaultModel();
final InputStream is = new FileInputStream(filePath);
rdFModel.read(is, null);
is.close();
return rdFModel;
}
/**
* write the rdf model to the file with the given path in rdf/xml syntax
*
* @param jenaModel rdf model to be written to file
* @param filePath path to which the file shall be written 2
*/
public static void writeModelToFile(Model jenaModel, String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
final OutputStream os = new FileOutputStream(file);
final RDFWriterI rdfWriter = jenaModel.getWriter("RDF/XML");
rdfWriter.setProperty("showXmlDeclaration", true);
rdfWriter.setProperty("allowBadURIs", "true");
rdfWriter.setProperty("relativeURIs", "");
rdfWriter.write(jenaModel, os, null);
os.flush();
os.close();
}
public static HistoryData changeEventToHistoryData(ChangeEvent objToConvert) {
ChangeEvent changeEvent = objToConvert;
URI changed = changeEvent.getChanged();
Date modificationDate = (Date) changeEvent.getExtendedProperties()
.get(dateModifiedQname);
HistoryData hd = null;
if (changeEvent instanceof Deletion) {
hd = HistoryData.getInstance(modificationDate, changed, HistoryData.DELETED);
} else if (changeEvent instanceof Modification) {
hd = HistoryData.getInstance(modificationDate, changed, HistoryData.MODIFIED);
} else {
hd = HistoryData.getInstance(modificationDate, changed, HistoryData.CREATED);
}
return hd;
}
/**
* The value for the link header returned for each response for a base page
*
* @return the link header value
*/
// FIXME Andrew@2019-04-27: please please please
public static String linkHeaderValue(Base base) {
Page nextPage = base.getNextPage();
URI pageOf = nextPage.getPageOf().getAbout();
StringBuilder sb = new StringBuilder();
final String newline = "\n";
String headerValue = urize(pageOf.toString()) + "; rel=\"first\"," + newline + urize(
nextPage.getNextPage().toString()) + "; rel=\"next\"," + newline + "; " + "rel=\"type\"" + newline;
return headerValue;
}
public static String urize(String uri) {
return "<" + uri + ">";
}
}