Repository: marmotta Updated Branches: refs/heads/develop a10c6efc8 -> 442763861
KiWi: - fix loader for date literals (MARMOTTA-565) Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/44276386 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/44276386 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/44276386 Branch: refs/heads/develop Commit: 442763861be1f234379222bc4f39ed8702b85a55 Parents: a10c6ef Author: Sebastian Schaffert <[email protected]> Authored: Mon Nov 10 20:01:03 2014 +0100 Committer: Sebastian Schaffert <[email protected]> Committed: Mon Nov 10 20:01:03 2014 +0100 ---------------------------------------------------------------------- .../kiwi/loader/csv/SQLDateTimeProcessor.java | 51 ++++++++++++++++++++ .../kiwi/loader/csv/SQLTimestampProcessor.java | 12 +++-- .../kiwi/loader/generic/KiWiHandler.java | 8 +-- .../kiwi/loader/mysql/MySQLLoadUtil.java | 4 +- .../marmotta/kiwi/loader/pgsql/PGCopyUtil.java | 6 +-- .../apache/marmotta/kiwi/loader/demo-data.foaf | 2 + 6 files changed, 69 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/44276386/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLDateTimeProcessor.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLDateTimeProcessor.java b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLDateTimeProcessor.java new file mode 100644 index 0000000..93878a9 --- /dev/null +++ b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLDateTimeProcessor.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.marmotta.kiwi.loader.csv; + +import org.joda.time.DateTime; +import org.joda.time.format.ISODateTimeFormat; +import org.supercsv.cellprocessor.CellProcessorAdaptor; +import org.supercsv.cellprocessor.ift.DateCellProcessor; +import org.supercsv.exception.SuperCsvCellProcessorException; +import org.supercsv.util.CsvContext; + +/** + * Add file description here! + * + * @author Sebastian Schaffert ([email protected]) + */ +public class SQLDateTimeProcessor extends CellProcessorAdaptor implements DateCellProcessor { + + /** + * This method is invoked by the framework when the processor needs to process data or check constraints. + * + * @since 1.0 + */ + @Override + public Object execute(Object value, CsvContext context) { + if(value == null) { + return null; + } + + if( !(value instanceof DateTime) ) { + throw new SuperCsvCellProcessorException(DateTime.class, value, context, this); + } + + return ISODateTimeFormat.dateTime().withZoneUTC().print((DateTime)value); + } +} http://git-wip-us.apache.org/repos/asf/marmotta/blob/44276386/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLTimestampProcessor.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLTimestampProcessor.java b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLTimestampProcessor.java index d1ed046..af0b45e 100644 --- a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLTimestampProcessor.java +++ b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/csv/SQLTimestampProcessor.java @@ -16,13 +16,14 @@ */ package org.apache.marmotta.kiwi.loader.csv; -import org.joda.time.DateTime; -import org.joda.time.format.ISODateTimeFormat; import org.supercsv.cellprocessor.CellProcessorAdaptor; import org.supercsv.cellprocessor.ift.DateCellProcessor; import org.supercsv.exception.SuperCsvCellProcessorException; import org.supercsv.util.CsvContext; +import java.sql.Timestamp; +import java.util.Date; + /** * Add file description here! * @@ -41,10 +42,11 @@ public class SQLTimestampProcessor extends CellProcessorAdaptor implements DateC return null; } - if( !(value instanceof DateTime) ) { - throw new SuperCsvCellProcessorException(DateTime.class, value, context, this); + if( !(value instanceof Date) ) { + throw new SuperCsvCellProcessorException(Date.class, value, context, this); } - return ISODateTimeFormat.dateTime().withZoneUTC().print((DateTime)value); + Timestamp date = new Timestamp(((Date) value).getTime()); + return date.toString(); } } http://git-wip-us.apache.org/repos/asf/marmotta/blob/44276386/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/generic/KiWiHandler.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/generic/KiWiHandler.java b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/generic/KiWiHandler.java index 8701dfe..7bf0eff 100644 --- a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/generic/KiWiHandler.java +++ b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/generic/KiWiHandler.java @@ -353,7 +353,7 @@ public class KiWiHandler implements RDFHandler { } else { nodesLoaded++; } - } else if(type.equals(Namespaces.NS_XSD+"dateTime") || type.equals(Namespaces.NS_XSD+"date") || type.equals(Namespaces.NS_XSD+"time")) { + } else if(type.stringValue().equals(Namespaces.NS_XSD + "dateTime") || type.stringValue().equals(Namespaces.NS_XSD + "date") || type.stringValue().equals(Namespaces.NS_XSD + "time")) { // parse if necessary final DateTime dvalue = ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(value.toString()); @@ -364,7 +364,7 @@ public class KiWiHandler implements RDFHandler { } else { nodesLoaded++; } - } else if(type.equals(Namespaces.NS_XSD+"integer") || type.equals(Namespaces.NS_XSD+"long")) { + } else if(type.stringValue().equals(Namespaces.NS_XSD + "integer") || type.stringValue().equals(Namespaces.NS_XSD + "long")) { long ivalue = Long.parseLong(value.toString()); result = connection.loadLiteral(ivalue); @@ -374,7 +374,7 @@ public class KiWiHandler implements RDFHandler { } else { nodesLoaded++; } - } else if(type.equals(Namespaces.NS_XSD+"double") || type.equals(Namespaces.NS_XSD+"float")) { + } else if(type.stringValue().equals(Namespaces.NS_XSD + "double") || type.stringValue().equals(Namespaces.NS_XSD + "float")) { double dvalue = Double.parseDouble(value.toString()); result = connection.loadLiteral(dvalue); @@ -384,7 +384,7 @@ public class KiWiHandler implements RDFHandler { } else { nodesLoaded++; } - } else if(type.equals(Namespaces.NS_XSD+"boolean")) { + } else if(type.stringValue().equals(Namespaces.NS_XSD+"boolean")) { boolean bvalue = Boolean.parseBoolean(value.toString()); result = connection.loadLiteral(bvalue); http://git-wip-us.apache.org/repos/asf/marmotta/blob/44276386/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/mysql/MySQLLoadUtil.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/mysql/MySQLLoadUtil.java b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/mysql/MySQLLoadUtil.java index 01b388f..07ab38e 100644 --- a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/mysql/MySQLLoadUtil.java +++ b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/mysql/MySQLLoadUtil.java @@ -56,7 +56,7 @@ public class MySQLLoadUtil { new NotNull(), // svalue new Optional(), // dvalue new Optional(), // ivalue - new SQLTimestampProcessor(), // tvalue + new SQLDateTimeProcessor(), // tvalue new Optional(), // tzoffset new Optional(new SQLBooleanProcessor()), // bvalue new Optional(new NodeIDProcessor()), // ltype @@ -151,7 +151,7 @@ public class MySQLLoadUtil { createNodeList(rowArray, l.getId(), l.getClass(), l.getContent(), null, null, null, null, l.booleanValue(), l.getDatatype(), l.getLocale(), l.getCreated()); } else if(n instanceof KiWiDateLiteral) { KiWiDateLiteral l = (KiWiDateLiteral)n; - createNodeList(rowArray, l.getId(), l.getClass(), l.getContent(), null, null, l.getDateContent(), l.getDateContent().getZone().getOffset(l.getDateContent()), null, l.getDatatype(), l.getLocale(), l.getCreated()); + createNodeList(rowArray, l.getId(), l.getClass(), l.getContent(), null, null, l.getDateContent(), l.getDateContent().getZone().getOffset(l.getDateContent()) / 1000, null, l.getDatatype(), l.getLocale(), l.getCreated()); } else if(n instanceof KiWiStringLiteral) { KiWiStringLiteral l = (KiWiStringLiteral)n; http://git-wip-us.apache.org/repos/asf/marmotta/blob/44276386/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/pgsql/PGCopyUtil.java ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/pgsql/PGCopyUtil.java b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/pgsql/PGCopyUtil.java index cd2495c..d6a8387 100644 --- a/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/pgsql/PGCopyUtil.java +++ b/libraries/kiwi/kiwi-loader/src/main/java/org/apache/marmotta/kiwi/loader/pgsql/PGCopyUtil.java @@ -58,7 +58,7 @@ public class PGCopyUtil { new NotNull(), // svalue new Optional(), // dvalue new Optional(), // ivalue - new SQLTimestampProcessor(), // tvalue + new SQLDateTimeProcessor(), // tvalue new Optional(), // tzoffset new Optional(new SQLBooleanProcessor()), // bvalue new Optional(new NodeIDProcessor()), // ltype @@ -141,7 +141,7 @@ public class PGCopyUtil { CsvListWriter writer = new CsvListWriter(new OutputStreamWriter(out), nodesPreference); // reuse the same array to avoid unnecessary object allocation - Object[] rowArray = new Object[10]; + Object[] rowArray = new Object[11]; List<Object> row = Arrays.asList(rowArray); for(KiWiNode n : nodeBacklog) { @@ -162,7 +162,7 @@ public class PGCopyUtil { createNodeList(rowArray, l.getId(), l.getClass(), l.getContent(), null, null, null, null, l.booleanValue(), l.getDatatype(), l.getLocale(), l.getCreated()); } else if(n instanceof KiWiDateLiteral) { KiWiDateLiteral l = (KiWiDateLiteral)n; - createNodeList(rowArray, l.getId(), l.getClass(), l.getContent(), null, null, l.getDateContent(), l.getDateContent().getZone().getOffset(l.getDateContent()), null, l.getDatatype(), l.getLocale(), l.getCreated()); + createNodeList(rowArray, l.getId(), l.getClass(), l.getContent(), null, null, l.getDateContent(), l.getDateContent().getZone().getOffset(l.getDateContent()) / 1000, null, l.getDatatype(), l.getLocale(), l.getCreated()); } else if(n instanceof KiWiStringLiteral) { KiWiStringLiteral l = (KiWiStringLiteral)n; http://git-wip-us.apache.org/repos/asf/marmotta/blob/44276386/libraries/kiwi/kiwi-loader/src/test/resources/org/apache/marmotta/kiwi/loader/demo-data.foaf ---------------------------------------------------------------------- diff --git a/libraries/kiwi/kiwi-loader/src/test/resources/org/apache/marmotta/kiwi/loader/demo-data.foaf b/libraries/kiwi/kiwi-loader/src/test/resources/org/apache/marmotta/kiwi/loader/demo-data.foaf index 219c341..24ec5c1 100644 --- a/libraries/kiwi/kiwi-loader/src/test/resources/org/apache/marmotta/kiwi/loader/demo-data.foaf +++ b/libraries/kiwi/kiwi-loader/src/test/resources/org/apache/marmotta/kiwi/loader/demo-data.foaf @@ -36,6 +36,8 @@ <foaf:knows rdf:resource="http://localhost:8080/LMF/resource/sepp_huber" /> <foaf:knows rdf:resource="http://localhost:8080/LMF/resource/anna_schmidt"/> + <foaf:birthday rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2010-12-21T15:38:02-08:00</foaf:birthday> + <foaf:account> <foaf:OnlineAccount> <foaf:accountName>Example</foaf:accountName>
