aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGabriel Ruelas2013-12-06 11:24:05 (EST)
committerSamuel Padgett2014-01-24 12:16:03 (EST)
commita2502d880ec1bd9b1d8774b47e53355db6f5bad7 (patch)
tree98f6fc60fad3aaf9fb955b68b0005df4ff77d0e8
parent1dfd88521bc7ffcc92da0034849c4e8b402ae37e (diff)
downloadorg.eclipse.lyo.core-a2502d880ec1bd9b1d8774b47e53355db6f5bad7.zip
org.eclipse.lyo.core-a2502d880ec1bd9b1d8774b47e53355db6f5bad7.tar.gz
org.eclipse.lyo.core-a2502d880ec1bd9b1d8774b47e53355db6f5bad7.tar.bz2
Bug 422862 - Date attributes are sent as DateTime attributes on PUTrefs/changes/44/19444/5
Change-Id: Ia328c66b591b33d5537e7246d066a3ec730a3f0a Signed-off-by: Gabriel Ruelas <gra@mx1.ibm.com>
-rw-r--r--org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java70
-rw-r--r--org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/model/ValueType.java3
-rw-r--r--org.eclipse.lyo.oslc4j.provider.jena/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java47
3 files changed, 115 insertions, 5 deletions
diff --git a/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java b/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java
index f781271..9a32b6a 100644
--- a/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java
+++ b/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/OSLC4JUtils.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012, 2013 IBM Corporation.
+ * Copyright (c) 2012, 2014 IBM Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -44,6 +44,7 @@ import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
+import com.hp.hpl.jena.rdf.model.Property;
public class OSLC4JUtils {
@@ -453,4 +454,71 @@ public class OSLC4JUtils {
return null;
}
+
+ /**
+ * This method receives the property name and the property value and tries
+ * to infer the property Data Type from the pre-defined list of Resource Shapes.
+ * Returns a null object when it was not possible to infer the property Data Type
+ * from the list of Resource Shapes.
+ *
+ * @param rdfTypesList
+ * @param propertyQName
+ * Property information
+ * @param originalValue
+ * Property value
+ * @return Java object related to the Resource Shape type.
+ * @throws DatatypeConfigurationException
+ * , IllegalArgumentException, InstantiationException,
+ * InvocationTargetException
+ on
+ *
+ */
+ public static RDFDatatype getDataTypeBasedOnResourceShapeType(final HashSet<String> rdfTypesList,
+ final Property property )
+ {
+ if (null != rdfTypesList && !rdfTypesList.isEmpty() && null != property )
+ {
+ try {
+ // get the pre-defined list of ResourceShapes
+ List<ResourceShape> shapes = OSLC4JUtils.getShapes();
+
+ if (null != shapes && !shapes.isEmpty()) {
+
+ // try to find the attribute type in the list of
+ // resource shapes
+ String propertyName = property.getURI();
+
+ TypeMapper typeMapper = TypeMapper.getInstance();
+
+ for (ResourceShape shape : shapes) {
+
+ // ensure that the current resource shape matches the resource rdf:type
+ if (doesResourceShapeMatchRdfTypes(shape, rdfTypesList)) {
+
+ org.eclipse.lyo.oslc4j.core.model.Property[] props = shape.getProperties();
+
+ for (org.eclipse.lyo.oslc4j.core.model.Property prop : props) {
+ URI propDefinition = prop.getPropertyDefinition();
+
+ if (propertyName.equals(propDefinition.toString())) {
+ URI propValueType = prop.getValueType();
+
+ if (null == propValueType) {
+ continue;
+ }
+ RDFDatatype dataTypeFromShape = typeMapper.getTypeByName(propValueType.toString());
+ return dataTypeFromShape;
+ }
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ // if there is any error, return null,
+ logger.warning("Could not find Data Type <" + property + " +> based on shape: " + e.getLocalizedMessage());
+ return null;
+ }
+ }
+ return null;
+ }
}
diff --git a/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/model/ValueType.java b/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/model/ValueType.java
index 112ebc7..2f38485 100644
--- a/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/model/ValueType.java
+++ b/org.eclipse.lyo.oslc4j.core/src/main/java/org/eclipse/lyo/oslc4j/core/model/ValueType.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012 IBM Corporation.
+ * Copyright (c) 2012, 2014 IBM Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -23,6 +23,7 @@ import java.net.URI;
public enum ValueType {
Boolean(OslcConstants.XML_NAMESPACE + "boolean"),
DateTime(OslcConstants.XML_NAMESPACE + "dateTime"),
+ Date(OslcConstants.XML_NAMESPACE + "date"),
Decimal(OslcConstants.XML_NAMESPACE + "decimal"),
Double(OslcConstants.XML_NAMESPACE + "double"),
Float(OslcConstants.XML_NAMESPACE + "float"),
diff --git a/org.eclipse.lyo.oslc4j.provider.jena/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java b/org.eclipse.lyo.oslc4j.provider.jena/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
index 028c272..13e5271 100644
--- a/org.eclipse.lyo.oslc4j.provider.jena/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
+++ b/org.eclipse.lyo.oslc4j.provider.jena/src/main/java/org/eclipse/lyo/oslc4j/provider/jena/JenaModelHelper.java
@@ -100,6 +100,7 @@ import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
+import com.hp.hpl.jena.datatypes.xsd.impl.XSDDateType;
import com.hp.hpl.jena.rdf.model.Container;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
@@ -1484,7 +1485,28 @@ public final class JenaModelHelper
final Calendar cal = Calendar.getInstance();
cal.setTime((Date) value);
- resource.addProperty(property, model.createTypedLiteral(cal));
+ RDFDatatype dataType = null;
+
+ // Check if it is a Date field
+ if ( OSLC4JUtils.inferTypeFromShape() ) {
+ // get the list of resource rdf type
+ HashSet<String> rdfTypes = new HashSet<String>();
+ rdfTypes = getTypesFromResource(resource, rdfTypes);
+ dataType = OSLC4JUtils.getDataTypeBasedOnResourceShapeType(rdfTypes, property);
+ }
+
+ if ( dataType != null && dataType instanceof XSDDateType) {
+ XSDDateTime valuec = new XSDDateTime( cal);
+ valuec.narrowType(XSDDatatype.XSDdate);
+ String valueDate = valuec.toString();
+ if ( valueDate != null && valueDate.endsWith("Z")){
+ valueDate = valueDate.replaceAll("Z","");
+ }
+ resource.addProperty(property, model.createTypedLiteral(valueDate, XSDDatatype.XSDdate));
+ } else {
+ resource.addProperty(property, model.createTypedLiteral(cal));
+ }
+
}
else if (value instanceof XMLLiteral)
{
@@ -1855,8 +1877,27 @@ public final class JenaModelHelper
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime((Date) value);
-
- nestedNode = model.createTypedLiteral(calendar);
+ RDFDatatype dataType = null;
+
+ // Check if it is a Date field
+ if ( OSLC4JUtils.inferTypeFromShape() ) {
+ // get the list of resource rdf type
+ HashSet<String> rdfTypes = new HashSet<String>();
+ rdfTypes = getTypesFromResource(resource, rdfTypes);
+ dataType = OSLC4JUtils.getDataTypeBasedOnResourceShapeType(rdfTypes, attribute);
+ }
+
+ if ( dataType != null && dataType instanceof XSDDateType) {
+ XSDDateTime valuec = new XSDDateTime( calendar);
+ valuec.narrowType(XSDDatatype.XSDdate);
+ String valueDate = valuec.toString();
+ if ( valueDate != null && valueDate.endsWith("Z")){
+ valueDate = valueDate.replaceAll("Z","");
+ }
+ nestedNode = model.createTypedLiteral(valueDate, XSDDatatype.XSDdate);
+ } else {
+ nestedNode = model.createTypedLiteral(calendar);
+ }
}
else if (objectClass.getAnnotation(OslcResourceShape.class) != null)
{