Skip to content

Commit

Permalink
Support using Java 8 Date/Time API as query parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Mar 22, 2020
1 parent d6e155c commit dd74227
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 40 deletions.
14 changes: 12 additions & 2 deletions jopa-api/src/main/java/cz/cvut/kbss/jopa/vocabulary/XSD.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand Down Expand Up @@ -69,6 +69,16 @@ public class XSD {
*/
public static final String FLOAT = NAMESPACE + "float";

/**
* {@code date} XML Schema data type.
*/
public static final String DATE = NAMESPACE + "date";

/**
* {@code time} XML Schema data type.
*/
public static final String TIME = NAMESPACE + "time";

/**
* {@code dateTime} XML Schema data type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class BooleanParameterValue extends AbstractParameterValue {
class BooleanParameterValue extends AbstractParameterValue {

private final Boolean value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.query.parameter;

import cz.cvut.kbss.jopa.vocabulary.XSD;

import java.util.Date;
import java.time.LocalDate;
import java.util.Objects;

public class DateParameterValue extends AbstractParameterValue {
/**
* Parameter value which represents an XSD date.
*/
class DateParameterValue extends AbstractParameterValue {

private final Date value;
private final LocalDate value;

DateParameterValue(Date value) {
DateParameterValue(LocalDate value) {
this.value = Objects.requireNonNull(value);
}

@Override
public Date getValue() {
public LocalDate getValue() {
return value;
}

@Override
public String getQueryString() {
return "\"" + value + "\"^^<" + XSD.DATETIME + ">";
return "\"" + value + "\"^^<" + XSD.DATE + ">";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.query.parameter;

import cz.cvut.kbss.jopa.vocabulary.XSD;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.Objects;

/**
* Parameter values which represent XSD dateTime.
* <p>
* Currently, these are {@link Date}, {@link LocalDateTime}, {@link java.time.OffsetDateTime} and {@link Instant}.
*/
class DateTimeParameterValue extends AbstractParameterValue {

private final Object value;

DateTimeParameterValue(Object value) {
assert value instanceof Date || value instanceof LocalDateTime || value instanceof OffsetDateTime || value instanceof Instant;
this.value = Objects.requireNonNull(value);
}

@Override
public Object getValue() {
return value;
}

@Override
public String getQueryString() {
return "\"" + value + "\"^^<" + XSD.DATETIME + ">";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class DoubleParameterValue extends AbstractParameterValue {
class DoubleParameterValue extends AbstractParameterValue {

private final double value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import java.util.Objects;

public class EntityParameterValue extends AbstractParameterValue {
class EntityParameterValue extends AbstractParameterValue {

private final MetamodelProvider metamodelProvider;

private final Object value;

public EntityParameterValue(Object value, MetamodelProvider metamodelProvider) {
EntityParameterValue(Object value, MetamodelProvider metamodelProvider) {
this.value = Objects.requireNonNull(value);
this.metamodelProvider = metamodelProvider;
assert metamodelProvider.isEntityType(value.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class FloatParameterValue extends AbstractParameterValue {
class FloatParameterValue extends AbstractParameterValue {

private final float value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class IntegerParameterValue extends AbstractParameterValue {
class IntegerParameterValue extends AbstractParameterValue {

private final int value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class LongParameterValue extends AbstractParameterValue {
class LongParameterValue extends AbstractParameterValue {

private final long value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand All @@ -19,6 +19,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.*;
import java.util.Date;
import java.util.Objects;

Expand Down Expand Up @@ -97,15 +98,29 @@ public ParameterValue create(Object value) {
return new DoubleParameterValue((Double) value);
} else if (value instanceof Float) {
return new FloatParameterValue((Float) value);
} else if (value instanceof Date) {
return new DateParameterValue((Date) value);
} else if (isDateTime(value)) {
return new DateTimeParameterValue(value);
} else if (value instanceof ZonedDateTime) {
return new DateTimeParameterValue(((ZonedDateTime) value).toOffsetDateTime());
} else if (value instanceof LocalDate) {
return new DateParameterValue((LocalDate) value);
} else if (isTime(value)) {
return new TimeParameterValue(value);
} else if (metamodelProvider.isEntityType(value.getClass())) {
return new EntityParameterValue(value, metamodelProvider);
} else {
return new StringParameterValue(value.toString());
}
}

private boolean isDateTime(Object value) {
return value instanceof Date || value instanceof LocalDateTime || value instanceof OffsetDateTime || value instanceof Instant;
}

private boolean isTime(Object value) {
return value instanceof LocalTime || value instanceof OffsetTime;
}

/**
* Returns new untyped parameter value specification.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* <p>
* All query parameters start out as variable values, until real values are set for them.
*/
public class PositionalVariableParameterValue extends AbstractParameterValue {
class PositionalVariableParameterValue extends AbstractParameterValue {

private final Integer position;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.util.Objects;

public class ShortParameterValue extends AbstractParameterValue {
class ShortParameterValue extends AbstractParameterValue {

private final short value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.util.Objects;

public class StringParameterValue extends AbstractParameterValue {
class StringParameterValue extends AbstractParameterValue {

private final String value;
private final String language;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cz.cvut.kbss.jopa.query.parameter;

import cz.cvut.kbss.jopa.vocabulary.XSD;

import java.time.LocalTime;
import java.time.OffsetTime;

/**
* Parameter values which represent XSD time.
* <p>
* Currently, these are {@link LocalTime} and {@link OffsetTime}.
*/
class TimeParameterValue extends AbstractParameterValue {

private final Object value;

TimeParameterValue(Object value) {
assert value instanceof LocalTime || value instanceof OffsetTime;
this.value = value;
}

@Override
public Object getValue() {
return value;
}

@Override
public String getQueryString() {
return "\"" + value + "\"^^<" + XSD.TIME + ">";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Represents parameter value which is directly inserted into the query string, without any XSD type specification.
*/
public class UntypedParameterValue extends AbstractParameterValue {
class UntypedParameterValue extends AbstractParameterValue {

private final Object value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Parameter value that will be put as an IRI into the query. I.e. it will be enclosed in &lt; and &gt;.
*/
public class UriParameterValue extends AbstractParameterValue {
class UriParameterValue extends AbstractParameterValue {

private final URI uri;

Expand Down
Loading

0 comments on commit dd74227

Please sign in to comment.