From 1ba3cad8ddaab0a2e59ca96fdcb2b197863611ae Mon Sep 17 00:00:00 2001 From: Carsten Hollmann Date: Tue, 26 May 2020 11:21:52 +0200 Subject: [PATCH 1/2] Add abstract and nullable timetprimitive field descritpors to define resulttime fileld as nullable --- .../util/SosTemporalRestrictions.java | 13 ++- .../AbstractTimePrimitiveFieldDescriptor.java | 103 ++++++++++++++++++ .../hibernate/util/TemporalRestriction.java | 89 +++++++++++++-- .../hibernate/util/TemporalRestrictions.java | 46 ++++---- .../util/TimePrimitiveFieldDescriptor.java | 70 +----------- .../TimePrimitiveNullableFieldDescriptor.java | 54 +++++++++ 6 files changed, 276 insertions(+), 99 deletions(-) create mode 100644 hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/AbstractTimePrimitiveFieldDescriptor.java create mode 100644 hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveNullableFieldDescriptor.java diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/SosTemporalRestrictions.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/SosTemporalRestrictions.java index 6dbf272536..2d0c9e35e2 100644 --- a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/SosTemporalRestrictions.java +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/SosTemporalRestrictions.java @@ -86,7 +86,7 @@ public final class SosTemporalRestrictions { * @see DataEntity#PROPERTY_SAMPLING_TIME_START * @see DataEntity#PROPERTY_SAMPLING_TIME_END */ - public static final TimePrimitiveFieldDescriptor PHENOMENON_TIME_FIELDS = new TimePrimitiveFieldDescriptor( + public static final AbstractTimePrimitiveFieldDescriptor PHENOMENON_TIME_FIELDS = new TimePrimitiveFieldDescriptor( DataEntity.PROPERTY_SAMPLING_TIME_START, DataEntity.PROPERTY_SAMPLING_TIME_END); /** @@ -94,8 +94,9 @@ public final class SosTemporalRestrictions { * * @see DataEntity#PROPERTY_RESULT_TIME */ - public static final TimePrimitiveFieldDescriptor RESULT_TIME_FIELDS = - new TimePrimitiveFieldDescriptor(DataEntity.PROPERTY_RESULT_TIME); + public static final AbstractTimePrimitiveFieldDescriptor RESULT_TIME_FIELDS = + new TimePrimitiveNullableFieldDescriptor(DataEntity.PROPERTY_RESULT_TIME, + new TimePrimitiveFieldDescriptor(DataEntity.PROPERTY_SAMPLING_TIME_END)); /** * Fields describing the valid time of a {@code Observation}. @@ -103,7 +104,7 @@ public final class SosTemporalRestrictions { * @see DataEntity#PROPERTY_VALID_TIME_START * @see DataEntity#PROPERTY_VALID_TIME_END */ - public static final TimePrimitiveFieldDescriptor VALID_TIME_FIELDS = + public static final AbstractTimePrimitiveFieldDescriptor VALID_TIME_FIELDS = new TimePrimitiveFieldDescriptor(DataEntity.PROPERTY_VALID_TIME_START, DataEntity.PROPERTY_VALID_TIME_END); /** @@ -112,7 +113,7 @@ public final class SosTemporalRestrictions { * @see ProcedureHistoryEntity#START_TIME * @see ProcedureHistoryEntity#END_TIME */ - public static final TimePrimitiveFieldDescriptor VALID_TIME_DESCRIBE_SENSOR_FIELDS = + public static final AbstractTimePrimitiveFieldDescriptor VALID_TIME_DESCRIBE_SENSOR_FIELDS = new TimePrimitiveFieldDescriptor(ProcedureHistoryEntity.START_TIME, ProcedureHistoryEntity.END_TIME); /** @@ -263,7 +264,7 @@ private static Collection getDisjunctionHql(IterableChristian Autermann + * @since 4.0.0 + */ +public abstract class AbstractTimePrimitiveFieldDescriptor { + private final String begin; + private final String end; + + /** + * Creates a new descriptor for a period. + * + * @param begin + * the begin field + * @param end + * the end field + */ + public AbstractTimePrimitiveFieldDescriptor(String begin, String end) { + if (begin == null) { + throw new NullPointerException("start may not be null"); + } + this.begin = begin; + this.end = end; + } + + /** + * Creates a new descriptor for a time instant. + * + * @param position + * the field name + */ + public AbstractTimePrimitiveFieldDescriptor(String position) { + this(position, null); + } + + /** + * @return the begin position of the period + */ + public String getBeginPosition() { + return begin; + } + + /** + * @return the end position of the period + */ + public String getEndPosition() { + return end; + } + + /** + * @return if this descriptor describes a period + */ + public boolean isPeriod() { + return getEndPosition() != null; + } + + /** + * @return the field name of the instant + */ + public String getPosition() { + return getBeginPosition(); + } + + /** + * @return if this descriptor describes a instant + */ + public boolean isInstant() { + return !isPeriod(); + } + +} diff --git a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java index 26d62077df..2cef344fe6 100644 --- a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java +++ b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java @@ -74,7 +74,7 @@ public interface TemporalRestriction { * @throws UnsupportedTimeException * if the supplied time can not be used with this restriction */ - default Criterion getCriterion(TimePrimitiveFieldDescriptor ref, Time time, Integer count) + default Criterion getCriterion(AbstractTimePrimitiveFieldDescriptor ref, Time time, Integer count) throws UnsupportedTimeException { if (time instanceof TimePeriod) { return filterWithPeriod((TimePeriod) time, ref, false, count); @@ -194,10 +194,43 @@ default Criterion filterInstantWithInstant(String selfPosition, String otherPosi return null; } + /** + * Create a filter for the specified period and fields that are nullable, + * e.g. resultTime. If the period is no real period but a instance, the + * method will call + * {@link #filterWithInstant(TimeInstant, AbstractTimePrimitiveFieldDescriptor)}. + * + * @param time + * the time + * @param r + * the property name(s) + * + * @return the {@code Criterion} that describes this restriction + */ + default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveNullableFieldDescriptor r, + boolean periodFromReducedPrecisionInstant) { + return Restrictions.or( + Restrictions.and(Restrictions.isNotNull(r.getPosition()), + filterWithPeriod(time, (AbstractTimePrimitiveFieldDescriptor) r, + periodFromReducedPrecisionInstant)), + Restrictions.and(Restrictions.isNull(r.getPosition()), + filterWithPeriod(time, r.getAlternative(), periodFromReducedPrecisionInstant))); + } + + default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveNullableFieldDescriptor r, + boolean periodFromReducedPrecisionInstant, Integer count) { + return Restrictions.or( + Restrictions.and(Restrictions.isNotNull(r.getPosition()), + filterWithPeriod(time, (AbstractTimePrimitiveFieldDescriptor) r, + periodFromReducedPrecisionInstant, count)), + Restrictions.and(Restrictions.isNull(r.getPosition()), + filterWithPeriod(time, r.getAlternative(), periodFromReducedPrecisionInstant, count))); + } + /** * Create a filter for the specified period and fields. If the period is no * real period but a instance, the method will call - * {@link #filterWithInstant(TimeInstant, TimePrimitiveFieldDescriptor)}. + * {@link #filterWithInstant(TimeInstant, AbstractTimePrimitiveFieldDescriptor)}. * * @param time * the time @@ -206,7 +239,7 @@ default Criterion filterInstantWithInstant(String selfPosition, String otherPosi * * @return the {@code Criterion} that describes this restriction */ - default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor r, + default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, boolean periodFromReducedPrecisionInstant) { Date begin = time.resolveStart().toDate(); // FIXME should also incorporate reduced precision like @@ -225,7 +258,7 @@ default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor } - default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor r, + default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, boolean periodFromReducedPrecisionInstant, Integer count) { Date begin = time.resolveStart().toDate(); // FIXME should also incorporate reduced precision like @@ -247,10 +280,50 @@ default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor } } + /** + * Creates a filter for the specfied instant and fields that are nullable. In case of a + * instance with reduced precision a the method will call + * {@link #filterWithPeriod(TimePeriod, AbstractTimePrimitiveFieldDescriptor, boolean)}. + * + * @param time + * the time + * @param r + * the property name(s) + * + * @return the {@code Criterion} that describes this restriction + */ + default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableFieldDescriptor r) { + /* + * Saved primitives can be periods, but can also be instants. As begin + * < end has to be true for all periods those are instants and have + * to be treated as such. Also instants with reduced precision are + * semantically periods and have to be handled like periods. + */ + return Restrictions.or( + Restrictions.and(Restrictions.isNotNull(r.getPosition()), + filterWithInstant(time, (AbstractTimePrimitiveFieldDescriptor) r)), + Restrictions.and(Restrictions.isNull(r.getPosition()), filterWithInstant(time, r.getAlternative()))); + } + + default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableFieldDescriptor r, Integer count) { + /* + * Saved primitives can be periods, but can also be instants. As begin + * < end has to be true for all periods those are instants and have + * to be treated as such. Also instants with reduced precision are + * semantically periods and have to be handled like periods. + */ + return Restrictions.or( + Restrictions.and(Restrictions.isNotNull(r.getPosition()), + filterWithInstant(time, (AbstractTimePrimitiveFieldDescriptor) r, count)), + Restrictions.and(Restrictions.isNull(r.getPosition()), + filterWithInstant(time, r.getAlternative(), count))); + } + + /** * Creates a filter for the specfied instant and fields. In case of a * instance with reduced precision a the method will call - * {@link #filterWithPeriod(TimePeriod, TimePrimitiveFieldDescriptor, boolean)}. + * {@link #filterWithPeriod(TimePeriod, AbstractTimePrimitiveFieldDescriptor, boolean)}. * * @param time * the time @@ -259,7 +332,7 @@ default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor * * @return the {@code Criterion} that describes this restriction */ - default Criterion filterWithInstant(TimeInstant time, TimePrimitiveFieldDescriptor r) { + default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r) { /* * Saved primitives can be periods, but can also be instants. As begin * < end has to be true for all periods those are instants and have @@ -280,7 +353,7 @@ default Criterion filterWithInstant(TimeInstant time, TimePrimitiveFieldDescript } } - default Criterion filterWithInstant(TimeInstant time, TimePrimitiveFieldDescriptor r, Integer count) { + default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r, Integer count) { /* * Saved primitives can be periods, but can also be instants. As begin * < end has to be true for all periods those are instants and have @@ -340,7 +413,7 @@ static Date checkInstantWithReducedPrecision(TimeInstant time) { * could be applied */ static Criterion getPropertyCheckingCriterion(Criterion periods, Criterion instants, - TimePrimitiveFieldDescriptor r) { + AbstractTimePrimitiveFieldDescriptor r) { if (periods != null && instants != null) { return Restrictions.or(periods, instants); } else if (periods != null && instants == null) { diff --git a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestrictions.java b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestrictions.java index 7bdd01a9e0..3ee8e0c4f7 100644 --- a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestrictions.java +++ b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestrictions.java @@ -185,7 +185,8 @@ public static Criterion before(String property, Time value) throws UnsupportedTi * if the value and property combination is not applicable for * this restriction */ - public static Criterion before(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion before(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.before(), property, value); } @@ -270,7 +271,8 @@ public static Criterion after(String property, Time value) throws UnsupportedTim * if the value and property combination is not applicable for * this restriction */ - public static Criterion after(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion after(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.after(), property, value); } @@ -355,7 +357,8 @@ public static Criterion begins(String property, Time value) throws UnsupportedTi * if the value and property combination is not applicable for * this restriction */ - public static Criterion begins(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion begins(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.begins(), property, value); } @@ -440,7 +443,8 @@ public static Criterion ends(String property, Time value) throws UnsupportedTime * if the value and property combination is not applicable for * this restriction */ - public static Criterion ends(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion ends(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.ends(), property, value); } @@ -525,7 +529,7 @@ public static Criterion endedBy(String property, Time value) throws UnsupportedT * if the value and property combination is not applicable for * this restriction */ - public static Criterion endedBy(TimePrimitiveFieldDescriptor property, Time value) + public static Criterion endedBy(AbstractTimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { return filter(TemporalRestrictions.endedBy(), property, value); } @@ -611,7 +615,7 @@ public static Criterion begunBy(String property, Time value) throws UnsupportedT * if the value and property combination is not applicable for * this restriction */ - public static Criterion begunBy(TimePrimitiveFieldDescriptor property, Time value) + public static Criterion begunBy(AbstractTimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { return filter(TemporalRestrictions.begunBy(), property, value); } @@ -697,7 +701,8 @@ public static Criterion during(String property, Time value) throws UnsupportedTi * if the value and property combination is not applicable for * this restriction */ - public static Criterion during(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion during(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.during(), property, value); } @@ -783,7 +788,8 @@ public static Criterion equals(String property, Time value) throws UnsupportedTi * if the value and property combination is not applicable for * this restriction */ - public static Criterion equals(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion equals(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.equals(), property, value); } @@ -869,7 +875,7 @@ public static Criterion contains(String property, Time value) throws Unsupported * if the value and property combination is not applicable for * this restriction */ - public static Criterion contains(TimePrimitiveFieldDescriptor property, Time value) + public static Criterion contains(AbstractTimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { return filter(TemporalRestrictions.contains(), property, value); } @@ -957,7 +963,7 @@ public static Criterion overlaps(String property, Time value) throws Unsupported * if the value and property combination is not applicable for * this restriction */ - public static Criterion overlaps(TimePrimitiveFieldDescriptor property, Time value) + public static Criterion overlaps(AbstractTimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { return filter(TemporalRestrictions.overlaps(), property, value); } @@ -1043,7 +1049,8 @@ public static Criterion meets(String property, Time value) throws UnsupportedTim * if the value and property combination is not applicable for * this restriction */ - public static Criterion meets(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion meets(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.meets(), property, value); } @@ -1128,7 +1135,8 @@ public static Criterion metBy(String property, Time value) throws UnsupportedTim * if the value and property combination is not applicable for * this restriction */ - public static Criterion metBy(TimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { + public static Criterion metBy(AbstractTimePrimitiveFieldDescriptor property, Time value) + throws UnsupportedTimeException { return filter(TemporalRestrictions.metBy(), property, value); } @@ -1215,7 +1223,7 @@ public static Criterion overlappedBy(String property, Time value) throws Unsuppo * if the value and property combination is not applicable for * this restriction */ - public static Criterion overlappedBy(TimePrimitiveFieldDescriptor property, Time value) + public static Criterion overlappedBy(AbstractTimePrimitiveFieldDescriptor property, Time value) throws UnsupportedTimeException { return filter(TemporalRestrictions.overlappedBy(), property, value); } @@ -1283,13 +1291,13 @@ private static Criterion filter(TemporalRestriction restriction, String property * if the value and property combination is not applicable for * this restriction */ - private static Criterion filter(TemporalRestriction restriction, TimePrimitiveFieldDescriptor property, Time value) - throws UnsupportedTimeException { + private static Criterion filter(TemporalRestriction restriction, AbstractTimePrimitiveFieldDescriptor property, + Time value) throws UnsupportedTimeException { return filter(restriction, property, value, null); } - private static Criterion filter(TemporalRestriction restriction, TimePrimitiveFieldDescriptor property, Time value, - Integer count) throws UnsupportedTimeException { + private static Criterion filter(TemporalRestriction restriction, AbstractTimePrimitiveFieldDescriptor property, + Time value, Integer count) throws UnsupportedTimeException { Criterion c = restriction.getCriterion(property, value, count); if (c != null) { return c; @@ -1347,12 +1355,12 @@ public static Criterion filter(TimeOperator operator, String property, Time valu * if the value and property combination is not applicable for * this restriction */ - public static Criterion filter(TimeOperator operator, TimePrimitiveFieldDescriptor property, Time value) + public static Criterion filter(TimeOperator operator, AbstractTimePrimitiveFieldDescriptor property, Time value) throws UnsupportedOperatorException, UnsupportedTimeException { return filter(forOperator(operator), property, value, null); } - public static Criterion filter(TimeOperator operator, TimePrimitiveFieldDescriptor property, Time value, + public static Criterion filter(TimeOperator operator, AbstractTimePrimitiveFieldDescriptor property, Time value, Integer count) throws UnsupportedOperatorException, UnsupportedTimeException { return filter(forOperator(operator), property, value, count); } diff --git a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveFieldDescriptor.java b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveFieldDescriptor.java index 6c5df1ce02..e22fc71ce8 100644 --- a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveFieldDescriptor.java +++ b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveFieldDescriptor.java @@ -28,76 +28,14 @@ */ package org.n52.sos.ds.hibernate.util; -/** - * Class that describes a time primitive of an entity. Instants are represented - * by one field and periods by two. - * - * @author Christian Autermann - * @since 4.0.0 - */ -public class TimePrimitiveFieldDescriptor { - private final String begin; - private final String end; +public class TimePrimitiveFieldDescriptor extends AbstractTimePrimitiveFieldDescriptor { - /** - * Creates a new descriptor for a period. - * - * @param begin - * the begin field - * @param end - * the end field - */ - public TimePrimitiveFieldDescriptor(String begin, String end) { - if (begin == null) { - throw new NullPointerException("start may not be null"); - } - this.begin = begin; - this.end = end; - } - - /** - * Creates a new descriptor for a time instant. - * - * @param position - * the field name - */ public TimePrimitiveFieldDescriptor(String position) { - this(position, null); - } - - /** - * @return the begin position of the period - */ - public String getBeginPosition() { - return begin; + super(position); } - /** - * @return the end position of the period - */ - public String getEndPosition() { - return end; - } - - /** - * @return if this descriptor describes a period - */ - public boolean isPeriod() { - return getEndPosition() != null; - } - - /** - * @return the field name of the instant - */ - public String getPosition() { - return getBeginPosition(); - } - - /** - * @return if this descriptor describes a instant - */ - public boolean isInstant() { - return !isPeriod(); + public TimePrimitiveFieldDescriptor(String begin, String end) { + super(begin, end); } } diff --git a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveNullableFieldDescriptor.java b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveNullableFieldDescriptor.java new file mode 100644 index 0000000000..18c09c7063 --- /dev/null +++ b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TimePrimitiveNullableFieldDescriptor.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +public class TimePrimitiveNullableFieldDescriptor extends AbstractTimePrimitiveFieldDescriptor { + + private TimePrimitiveFieldDescriptor alternative; + + public TimePrimitiveNullableFieldDescriptor(String position, TimePrimitiveFieldDescriptor alternative) { + super(position); + this.setAlternative(alternative); + } + + /** + * @return the alternative + */ + public TimePrimitiveFieldDescriptor getAlternative() { + return alternative; + } + + /** + * @param alternative the alternative to set + */ + public void setAlternative(TimePrimitiveFieldDescriptor alternative) { + this.alternative = alternative; + } + +} From 02b581e7dc16ca95c655bacf01473d951f990684 Mon Sep 17 00:00:00 2001 From: Carsten Hollmann Date: Wed, 27 May 2020 13:33:50 +0200 Subject: [PATCH 2/2] Add support for null as resultTime value - Add and fix tests --- .../concrete/UnsupportedTimeException.java | 8 + ...poralRestrictionHqlInstantInstantTest.java | 185 ++++++------ ...mporalRestrictionHqlInstantPeriodTest.java | 176 +++++------ ...mporalRestrictionHqlPeriodInstantTest.java | 143 ++++----- ...emporalRestrictionHqlPeriodPeriodTest.java | 173 ++++++----- .../util/TemporalRestrictionTest.java | 4 - hibernate/utils/pom.xml | 10 + .../hibernate/util/TemporalRestriction.java | 237 ++++++++++----- ...poralRestrictionHqlInstantInstantTest.java | 248 ++++++++++++++++ ...mporalRestrictionHqlInstantPeriodTest.java | 249 ++++++++++++++++ ...mporalRestrictionHqlPeriodInstantTest.java | 234 +++++++++++++++ ...emporalRestrictionHqlPeriodPeriodTest.java | 245 +++++++++++++++ .../TemporalRestrictionTestConstants.java | 50 ++++ .../util/TestTemporalRestrictions.java | 280 ++++++++++++++++++ 14 files changed, 1834 insertions(+), 408 deletions(-) create mode 100644 hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java create mode 100644 hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java create mode 100644 hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java create mode 100644 hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java create mode 100644 hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTestConstants.java create mode 100644 hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TestTemporalRestrictions.java diff --git a/core/api/src/main/java/org/n52/sos/exception/ows/concrete/UnsupportedTimeException.java b/core/api/src/main/java/org/n52/sos/exception/ows/concrete/UnsupportedTimeException.java index e69aa85113..3495c76cc4 100644 --- a/core/api/src/main/java/org/n52/sos/exception/ows/concrete/UnsupportedTimeException.java +++ b/core/api/src/main/java/org/n52/sos/exception/ows/concrete/UnsupportedTimeException.java @@ -43,4 +43,12 @@ public UnsupportedTimeException(Time time) { withMessage("Time %s is not supported", time); } + public UnsupportedTimeException(TimeType referenced, TimeType requested) { + withMessage("%s is not supported with %s", referenced.name(), requested.name()); + } + + public enum TimeType { + TimePeriod, TimeInstant + } + } diff --git a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java index 20a6577dda..da216bcd1d 100644 --- a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java +++ b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java @@ -28,13 +28,9 @@ */ package org.n52.sos.ds.hibernate.util; -/** - * Test with instant as temporal filter - * - * @author Carsten Hollmann - * - * @since 4.4.0 - */ +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + import org.hibernate.criterion.Criterion; import org.joda.time.DateTime; import org.junit.Test; @@ -42,22 +38,24 @@ import org.n52.shetland.ogc.filter.TemporalFilter; import org.n52.shetland.ogc.gml.time.TimeInstant; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; -import org.n52.sos.ds.hibernate.ExtendedHibernateTestCase; -import org.n52.sos.ds.hibernate.util.SosTemporalRestrictions; import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; -public class TemporalRestrictionHqlInstantInstantTest extends ExtendedHibernateTestCase - implements TemporalRestrictionTestConstants { +/** + * Test with instant as temporal filter + * + * @author Carsten Hollmann + * + * @since 4.4.0 + */ +public class TemporalRestrictionHqlInstantInstantTest implements TemporalRestrictionTestConstants { private static final String STE_LT_INSTANT = "samplingTimeEnd<:instant1"; private static final String STS_GT_INSTANT_AND_STE_LT_INSTANT = "samplingTimeStart>:instant1 and samplingTimeEnd<:instant1"; - private static final String RT_GT_INSTANT_AND_RT_LT_INSTANT = "resultTime>:instant1 and resultTime<:instant1"; - @Test public void testAfterPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -66,14 +64,6 @@ public void testAfterPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart>:instant1")); } - @Test - public void testAfterResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:instant1")); - } - @Test public void testBeforePhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -82,14 +72,6 @@ public void testBeforePhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:instant1")); } - @Test - public void testBeforeResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime<:instant1")); - } - @Test public void testEqualsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -98,14 +80,6 @@ public void testEqualsPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1 and samplingTimeEnd=:instant1")); } - @Test - public void testEqualsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime=:instant1")); - } - @Test public void testContainsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -113,14 +87,6 @@ public void testContainsPhenomenonTime() SosTemporalRestrictions.filterHql(tf, 1); } - @Test(expected = UnsupportedTimeException.class) - public void testContainsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo(RT_GT_INSTANT_AND_RT_LT_INSTANT)); - } - @Test(expected = UnsupportedTimeException.class) public void testDuringPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -129,26 +95,12 @@ public void testDuringPhenomenonTime() assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); } - @Test(expected = UnsupportedTimeException.class) - public void testDuringResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:resultTime1 and resultTime<:resultTime1")); - } - @Test(expected = UnsupportedTimeException.class) public void testBeginsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } - @Test(expected = UnsupportedTimeException.class) - public void testBeginsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testBegunByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); @@ -157,81 +109,130 @@ public void testBegunByPhenomenonTime() throws OwsExceptionReport { } @Test(expected = UnsupportedTimeException.class) - public void testBegunByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); + public void testEndsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } + @Test + public void testEndedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:instant1")); + } + @Test(expected = UnsupportedTimeException.class) - public void testEndsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); + public void testOverlapsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testEndsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); + public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STE_LT_INSTANT)); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); } @Test - public void testEndedByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:instant1")); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:instant1) or (resultTime is null and samplingTimeEnd>:instant1))")); + } + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime<:instant1) or (resultTime is null and samplingTimeEnd<:instant1))")); + } + @Test + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime=:instant1) or (resultTime is null and samplingTimeEnd=:instant1))")); } @Test(expected = UnsupportedTimeException.class) - public void testEndedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:instant1 and resultTime<:instant1) or (resultTime is null and samplingTimeEnd>:instant1 and samplingTimeEnd<:instant1))")); } @Test(expected = UnsupportedTimeException.class) - public void testOverlapsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:resultTime1 and resultTime<:resultTime1) or (resultTime is null and samplingTimeEnd>:resultTime1 and samplingTimeEnd<:resultTime1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlapsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo(STE_LT_INSTANT)); + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlappedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testMeetsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testMeetsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testMetByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) diff --git a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java index c6e8251362..c1b479b3dd 100644 --- a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java +++ b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java @@ -28,6 +28,9 @@ */ package org.n52.sos.ds.hibernate.util; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + import org.hibernate.criterion.Criterion; import org.joda.time.DateTime; import org.junit.Test; @@ -35,8 +38,6 @@ import org.n52.shetland.ogc.filter.TemporalFilter; import org.n52.shetland.ogc.gml.time.TimePeriod; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; -import org.n52.sos.ds.hibernate.ExtendedHibernateTestCase; -import org.n52.sos.ds.hibernate.util.SosTemporalRestrictions; import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; @@ -48,8 +49,7 @@ * * @since 4.4.0 */ -public class TemporalRestrictionHqlInstantPeriodTest extends ExtendedHibernateTestCase - implements TemporalRestrictionTestConstants { +public class TemporalRestrictionHqlInstantPeriodTest implements TemporalRestrictionTestConstants { @Test public void testAfterPhenomenonTime() @@ -59,14 +59,6 @@ public void testAfterPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart>:end1")); } - @Test - public void testAfterResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:end1")); - } - @Test public void testBeforePhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -75,14 +67,6 @@ public void testBeforePhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:start1")); } - @Test - public void testBeforeResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime<:start1")); - } - @Test public void testEqualsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -91,13 +75,6 @@ public void testEqualsPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd=:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testEqualsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testContainsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -105,13 +82,6 @@ public void testContainsPhenomenonTime() SosTemporalRestrictions.filterHql(tf, 1); } - @Test(expected = UnsupportedTimeException.class) - public void testContainsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testDuringPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -120,14 +90,6 @@ public void testDuringPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd<:end1")); } - @Test - public void testDuringResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:start1 and resultTime<:end1")); - } - @Test public void testBeginsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); @@ -135,13 +97,6 @@ public void testBeginsPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd<:end1")); } - @Test - public void testBeginsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime=:start1")); - } - @Test public void testBegunByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); @@ -149,12 +104,6 @@ public void testBegunByPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd>:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testBegunByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testEndsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); @@ -162,13 +111,6 @@ public void testEndsPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd=:end1")); } - @Test - public void testEndsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime=:end1")); - } - @Test public void testEndedByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); @@ -176,12 +118,6 @@ public void testEndedByPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart<:start1 and samplingTimeEnd=:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testEndedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testOverlapsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); @@ -190,12 +126,6 @@ public void testOverlapsPhenomenonTime() throws OwsExceptionReport { equalTo("(samplingTimeStart<:start1 and samplingTimeEnd>:start1 and samplingTimeEnd<:end1)")); } - @Test(expected = UnsupportedTimeException.class) - public void testOverlapsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); @@ -204,30 +134,105 @@ public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { equalTo("(samplingTimeStart>:start1 and samplingTimeStart<:end1 and samplingTimeEnd>:end1)")); } + @Test + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:start1")); + } + + @Test + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:end1")); + } + + @Test + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:end1) or (resultTime is null and samplingTimeEnd>:end1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime<:start1) or (resultTime is null and samplingTimeEnd<:start1))")); + } + @Test(expected = UnsupportedTimeException.class) - public void testOverlappedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test - public void testMeetsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:start1")); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:start1 and resultTime<:end1) or (resultTime is null and samplingTimeEnd>:start1 and samplingTimeEnd<:end1))")); + } + + @Test + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime=:start1) or (resultTime is null and samplingTimeEnd=:start1))")); } @Test(expected = UnsupportedTimeException.class) - public void testMeetsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test - public void testMetByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("samplingTimeStart=:end1")); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime=:end1) or (resultTime is null and samplingTimeEnd=:end1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) @@ -237,7 +242,8 @@ public void testMetByResultTime() throws OwsExceptionReport { } private TemporalFilter create(FilterConstants.TimeOperator op, String vr) { - return new TemporalFilter(op, new TimePeriod(DateTime.now().minusHours(1), DateTime.now()), vr); + return new TemporalFilter(op, new TimePeriod(DateTime.now() + .minusHours(1), DateTime.now()), vr); } } diff --git a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java index 35a2e396c8..f9638e9d2a 100644 --- a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java +++ b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java @@ -28,6 +28,9 @@ */ package org.n52.sos.ds.hibernate.util; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + import org.hibernate.criterion.Criterion; import org.joda.time.DateTime; import org.junit.Test; @@ -35,8 +38,6 @@ import org.n52.shetland.ogc.filter.TemporalFilter; import org.n52.shetland.ogc.gml.time.TimeInstant; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; -import org.n52.sos.ds.hibernate.ExtendedHibernateTestCase; -import org.n52.sos.ds.hibernate.util.SosTemporalRestrictions; import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; @@ -48,7 +49,7 @@ * * @since 4.4.0 */ -public class TemporalRestrictionHqlPeriodInstantTest extends ExtendedHibernateTestCase +public class TemporalRestrictionHqlPeriodInstantTest implements TemporalRestrictionTestConstants { @Test @@ -59,14 +60,6 @@ public void testAfterPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart>:instant1")); } - @Test - public void testAfterResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:instant1")); - } - @Test public void testBeforePhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -75,14 +68,6 @@ public void testBeforePhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:instant1")); } - @Test - public void testBeforeResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime<:instant1")); - } - @Test public void testEqualsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -91,14 +76,6 @@ public void testEqualsPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1 and samplingTimeEnd=:instant1")); } - @Test - public void testEqualsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime=:instant1")); - } - @Test public void testContainsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -107,13 +84,6 @@ public void testContainsPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart<:instant1 and samplingTimeEnd>:instant1")); } - @Test(expected = UnsupportedTimeException.class) - public void testContainsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test(expected = UnsupportedTimeException.class) public void testDuringPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -121,102 +91,133 @@ public void testDuringPhenomenonTime() SosTemporalRestrictions.filterHql(tf, 1); } - @Test(expected = UnsupportedTimeException.class) - public void testDuringResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test(expected = UnsupportedTimeException.class) public void testBeginsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } + @Test + public void testBegunByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1")); + } + @Test(expected = UnsupportedTimeException.class) - public void testBeginsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + public void testEndsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test - public void testBegunByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); + public void testEndedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1")); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:instant1")); } @Test(expected = UnsupportedTimeException.class) - public void testBegunByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); + public void testOverlapsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testEndsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); + public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testEndsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test - public void testEndedByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:instant1")); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime>:instant1) or (resultTime is null and samplingTimeEnd>:instant1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime<:instant1) or (resultTime is null and samplingTimeEnd<:instant1))")); + } + + @Test + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime=:instant1) or (resultTime is null and samplingTimeEnd=:instant1))")); } @Test(expected = UnsupportedTimeException.class) - public void testEndedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlapsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlapsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testOverlappedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testMeetsPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testMeetsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) - public void testMetByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } diff --git a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java index aea50608f7..40067aa312 100644 --- a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java +++ b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java @@ -28,6 +28,9 @@ */ package org.n52.sos.ds.hibernate.util; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + import org.hibernate.criterion.Criterion; import org.joda.time.DateTime; import org.junit.Test; @@ -35,8 +38,6 @@ import org.n52.shetland.ogc.filter.TemporalFilter; import org.n52.shetland.ogc.gml.time.TimePeriod; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; -import org.n52.sos.ds.hibernate.ExtendedHibernateTestCase; -import org.n52.sos.ds.hibernate.util.SosTemporalRestrictions; import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; @@ -48,11 +49,9 @@ * * @since 4.4.0 */ -public class TemporalRestrictionHqlPeriodPeriodTest extends ExtendedHibernateTestCase +public class TemporalRestrictionHqlPeriodPeriodTest implements TemporalRestrictionTestConstants { - private static final String RESULT_TIME_GT_RESULT_TIME_1 = "resultTime>:resultTime1"; - @Test public void testAfterPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -61,14 +60,6 @@ public void testAfterPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart>:end1")); } - @Test - public void testAfterResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:end1")); - } - @Test public void testBeforePhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -77,14 +68,6 @@ public void testBeforePhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:start1")); } - @Test - public void testBeforeResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime<:start1")); - } - @Test public void testEqualsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -93,14 +76,6 @@ public void testEqualsPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd=:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testEqualsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo(RESULT_TIME_GT_RESULT_TIME_1)); - } - @Test public void testContainsPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -109,14 +84,6 @@ public void testContainsPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart<:start1 and samplingTimeEnd>:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testContainsResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo(RESULT_TIME_GT_RESULT_TIME_1)); - } - @Test public void testDuringPhenomenonTime() throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { @@ -125,14 +92,6 @@ public void testDuringPhenomenonTime() assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd<:end1")); } - @Test - public void testDuringResultTime() - throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime>:start1 and resultTime<:end1")); - } - @Test public void testBeginsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); @@ -140,13 +99,6 @@ public void testBeginsPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd<:end1")); } - @Test - public void testBeginsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime=:start1")); - } - @Test public void testBegunByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); @@ -154,12 +106,6 @@ public void testBegunByPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd>:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testBegunByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testEndsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); @@ -167,13 +113,6 @@ public void testEndsPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd=:end1")); } - @Test - public void testEndsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); - Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("resultTime=:end1")); - } - @Test public void testEndedByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); @@ -181,12 +120,6 @@ public void testEndedByPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeStart<:start1 and samplingTimeEnd=:end1")); } - @Test(expected = UnsupportedTimeException.class) - public void testEndedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testOverlapsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); @@ -195,12 +128,6 @@ public void testOverlapsPhenomenonTime() throws OwsExceptionReport { equalTo("(samplingTimeStart<:start1 and samplingTimeEnd>:start1 and samplingTimeEnd<:end1)")); } - @Test(expected = UnsupportedTimeException.class) - public void testOverlapsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); @@ -209,12 +136,6 @@ public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { equalTo("(samplingTimeStart>:start1 and samplingTimeStart<:end1 and samplingTimeEnd>:end1)")); } - @Test(expected = UnsupportedTimeException.class) - public void testOverlappedByResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); - SosTemporalRestrictions.filterHql(tf, 1); - } - @Test public void testMeetsPhenomenonTime() throws OwsExceptionReport { TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); @@ -222,17 +143,93 @@ public void testMeetsPhenomenonTime() throws OwsExceptionReport { assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:start1")); } + @Test + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:end1")); + } + + @Test + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime>:end1) or (resultTime is null and samplingTimeEnd>:end1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime<:start1) or (resultTime is null and samplingTimeEnd<:start1))")); + } + @Test(expected = UnsupportedTimeException.class) - public void testMeetsResultTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime>:start1 and resultTime<:end1) or (resultTime is null and samplingTimeEnd>:start1 and samplingTimeEnd<:end1))")); + } + + @Test + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime=:start1) or (resultTime is null and samplingTimeEnd=:start1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); SosTemporalRestrictions.filterHql(tf, 1); } @Test - public void testMetByPhenomenonTime() throws OwsExceptionReport { - TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); Criterion filterHql = SosTemporalRestrictions.filterHql(tf, 1); - assertThat(filterHql.toString(), equalTo("samplingTimeStart=:end1")); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime=:end1) or (resultTime is null and samplingTimeEnd=:end1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + SosTemporalRestrictions.filterHql(tf, 1); } @Test(expected = UnsupportedTimeException.class) diff --git a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTest.java b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTest.java index 7f24d8c7d9..b0c76707de 100644 --- a/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTest.java +++ b/hibernate/common/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTest.java @@ -36,7 +36,6 @@ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.criterion.CriteriaQuery; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.Projections; import org.junit.After; @@ -52,9 +51,6 @@ import org.n52.sos.ds.hibernate.ExtendedHibernateTestCase; import org.n52.sos.ds.hibernate.H2Configuration; import org.n52.sos.ds.hibernate.dao.DaoFactory; -import org.n52.sos.ds.hibernate.util.HibernateMetadataCache; -import org.n52.sos.ds.hibernate.util.ScrollableIterable; -import org.n52.sos.ds.hibernate.util.SosTemporalRestrictions; import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; /** diff --git a/hibernate/utils/pom.xml b/hibernate/utils/pom.xml index 46930cac70..c45dc413fb 100644 --- a/hibernate/utils/pom.xml +++ b/hibernate/utils/pom.xml @@ -42,5 +42,15 @@ org.locationtech.jts jts-core + + org.hamcrest + hamcrest + test + + + junit + junit + test + \ No newline at end of file diff --git a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java index 2cef344fe6..4f00c64b67 100644 --- a/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java +++ b/hibernate/utils/src/main/java/org/n52/sos/ds/hibernate/util/TemporalRestriction.java @@ -38,6 +38,7 @@ import org.n52.shetland.ogc.gml.time.TimePeriod; import org.n52.shetland.util.DateTimeHelper; import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; +import org.n52.sos.exception.ows.concrete.UnsupportedTimeException.TimeType; /** * Implements the 13 temporal relationships identified by Allen as @@ -115,13 +116,16 @@ default String getInstantPlaceHolder(Integer count) { * * @return the criterion for the temporal relation (or {@code null} if not * applicable) + * @throws UnsupportedTimeException If the filter is not supported! */ - default Criterion filterPeriodWithPeriod(String selfBegin, String selfEnd, Date otherBegin, Date otherEnd) { - return null; + default Criterion filterPeriodWithPeriod(String selfBegin, String selfEnd, Date otherBegin, Date otherEnd) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(new TimePeriod(otherBegin, otherEnd)); } - default Criterion filterPeriodWithPeriod(String selfBegin, String selfEnd, Integer count) { - return null; + default Criterion filterPeriodWithPeriod(String selfBegin, String selfEnd, Integer count) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(TimeType.TimePeriod, TimeType.TimePeriod); } /** @@ -139,19 +143,21 @@ default Criterion filterPeriodWithPeriod(String selfBegin, String selfEnd, Integ * * @return the criterion for the temporal relation (or {@code null} if not * applicable) + * @throws UnsupportedTimeException If the filter is not supported! */ default Criterion filterInstantWithPeriod(String selfPosition, Date otherBegin, Date otherEnd, - boolean isOtherPeriodFromReducedPrecisionInstant) { - return null; + boolean isOtherPeriodFromReducedPrecisionInstant) throws UnsupportedTimeException { + throw new UnsupportedTimeException(new TimePeriod(otherBegin, otherEnd)); } - default Criterion filterInstantWithPeriod(String selfPosition, String otherPosition, Integer count) { - return null; + default Criterion filterInstantWithPeriod(String selfPosition, String otherPosition, Integer count) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(TimeType.TimeInstant, TimeType.TimePeriod); } default Criterion filterInstantWithPeriod(String position, String endPosition, Date begin, Date end, - boolean periodFromReducedPrecisionInstant) { - return null; + boolean periodFromReducedPrecisionInstant) throws UnsupportedTimeException { + throw new UnsupportedTimeException(new TimePeriod(begin, end)); } /** @@ -166,13 +172,16 @@ default Criterion filterInstantWithPeriod(String position, String endPosition, D * * @return the criterion for the temporal relation (or {@code null} if not * applicable) + * @throws UnsupportedTimeException If the filter is not supported! */ - default Criterion filterPeriodWithInstant(String selfBegin, String selfEnd, Date otherPosition) { - return null; + default Criterion filterPeriodWithInstant(String selfBegin, String selfEnd, Date otherPosition) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(new TimeInstant(otherPosition)); } - default Criterion filterPeriodWithInstant(String selfBegin, String selfEnd, Integer count) { - return null; + default Criterion filterPeriodWithInstant(String selfBegin, String selfEnd, Integer count) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(TimeType.TimePeriod, TimeType.TimeInstant); } /** @@ -185,20 +194,23 @@ default Criterion filterPeriodWithInstant(String selfBegin, String selfEnd, Inte * * @return the criterion for the temporal relation (or {@code null} if not * applicable) + * @throws UnsupportedTimeException If the filter is not supported! */ - default Criterion filterInstantWithInstant(String selfPosition, Date otherPosition) { - return null; + default Criterion filterInstantWithInstant(String selfPosition, Date otherPosition) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(new TimeInstant(otherPosition)); } - default Criterion filterInstantWithInstant(String selfPosition, String otherPosition, Integer count) { - return null; + default Criterion filterInstantWithInstant(String selfPosition, String otherPosition, Integer count) + throws UnsupportedTimeException { + throw new UnsupportedTimeException(TimeType.TimeInstant, TimeType.TimeInstant); } /** * Create a filter for the specified period and fields that are nullable, * e.g. resultTime. If the period is no real period but a instance, the * method will call - * {@link #filterWithInstant(TimeInstant, AbstractTimePrimitiveFieldDescriptor)}. + * {@link #filterWithInstant(TimeInstant, TimePrimitiveNullableFieldDescriptor)}. * * @param time * the time @@ -206,25 +218,34 @@ default Criterion filterInstantWithInstant(String selfPosition, String otherPosi * the property name(s) * * @return the {@code Criterion} that describes this restriction + * @throws UnsupportedTimeException If the filter is not supported! */ default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveNullableFieldDescriptor r, - boolean periodFromReducedPrecisionInstant) { - return Restrictions.or( - Restrictions.and(Restrictions.isNotNull(r.getPosition()), - filterWithPeriod(time, (AbstractTimePrimitiveFieldDescriptor) r, + boolean periodFromReducedPrecisionInstant) throws UnsupportedTimeException { + return Restrictions.disjunction( + Restrictions.conjunction(Restrictions.isNotNull(r.getPosition()), + createFilterWithPeriod(time, (AbstractTimePrimitiveFieldDescriptor) r, periodFromReducedPrecisionInstant)), - Restrictions.and(Restrictions.isNull(r.getPosition()), - filterWithPeriod(time, r.getAlternative(), periodFromReducedPrecisionInstant))); + Restrictions.conjunction(Restrictions.isNull(r.getPosition()), + createFilterWithPeriod(time, r.getAlternative(), periodFromReducedPrecisionInstant))); } - default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveNullableFieldDescriptor r, - boolean periodFromReducedPrecisionInstant, Integer count) { - return Restrictions.or( - Restrictions.and(Restrictions.isNotNull(r.getPosition()), - filterWithPeriod(time, (AbstractTimePrimitiveFieldDescriptor) r, - periodFromReducedPrecisionInstant, count)), - Restrictions.and(Restrictions.isNull(r.getPosition()), - filterWithPeriod(time, r.getAlternative(), periodFromReducedPrecisionInstant, count))); + /** + * Create a filter for the specified period and fields. If the period is no + * real period but a instance, the method will call + * {@link #filterWithInstant(TimeInstant, TimePrimitiveFieldDescriptor)}. + * + * @param time + * the time + * @param r + * the property name(s) + * + * @return the {@code Criterion} that describes this restriction + * @throws UnsupportedTimeException If the filter is not supported! + */ + default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor r, + boolean periodFromReducedPrecisionInstant) throws UnsupportedTimeException { + return createFilterWithPeriod(time, r, periodFromReducedPrecisionInstant); } /** @@ -238,14 +259,46 @@ default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveNullableFieldDe * the property name(s) * * @return the {@code Criterion} that describes this restriction + * @throws UnsupportedTimeException If the filter is not supported! */ default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, - boolean periodFromReducedPrecisionInstant) { - Date begin = time.resolveStart().toDate(); + boolean periodFromReducedPrecisionInstant) throws UnsupportedTimeException { + return r instanceof TimePrimitiveFieldDescriptor + ? createFilterWithPeriod(time, r, periodFromReducedPrecisionInstant) + : filterWithPeriod(time, (TimePrimitiveNullableFieldDescriptor) r, periodFromReducedPrecisionInstant); + } + + default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveNullableFieldDescriptor r, + boolean periodFromReducedPrecisionInstant, Integer count) throws UnsupportedTimeException { + return Restrictions.disjunction( + Restrictions.conjunction(Restrictions.isNotNull(r.getPosition()), + createFilterWithPeriod(time, r, periodFromReducedPrecisionInstant, count)), + Restrictions.conjunction(Restrictions.isNull(r.getPosition()), + createFilterWithPeriod(time, r.getAlternative(), periodFromReducedPrecisionInstant, count))); + } + + default Criterion filterWithPeriod(TimePeriod time, TimePrimitiveFieldDescriptor r, + boolean periodFromReducedPrecisionInstant, Integer count) throws UnsupportedTimeException { + return createFilterWithPeriod(time, r, periodFromReducedPrecisionInstant, count); + } + + default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, + boolean periodFromReducedPrecisionInstant, Integer count) throws UnsupportedTimeException { + return r instanceof TimePrimitiveFieldDescriptor + ? createFilterWithPeriod(time, r, periodFromReducedPrecisionInstant, count) + : filterWithPeriod(time, (TimePrimitiveNullableFieldDescriptor) r, periodFromReducedPrecisionInstant, + count); + } + + default Criterion createFilterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, + boolean periodFromReducedPrecisionInstant) throws UnsupportedTimeException { + Date begin = time.resolveStart() + .toDate(); // FIXME should also incorporate reduced precision like // getRequestedTimeLength() // (Partially?) fixed with use of periodFromReducedPrecisionInstant? - Date end = time.resolveEnd().toDate(); + Date end = time.resolveEnd() + .toDate(); if (begin.equals(end)) { return filterWithInstant(new TimeInstant(time.resolveStart()), r); } @@ -255,16 +308,17 @@ default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDe } else { return filterInstantWithPeriod(r.getPosition(), begin, end, periodFromReducedPrecisionInstant); } - } - default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, - boolean periodFromReducedPrecisionInstant, Integer count) { - Date begin = time.resolveStart().toDate(); + default Criterion createFilterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDescriptor r, + boolean periodFromReducedPrecisionInstant, Integer count) throws UnsupportedTimeException { + Date begin = time.resolveStart() + .toDate(); // FIXME should also incorporate reduced precision like // getRequestedTimeLength() // (Partially?) fixed with use of periodFromReducedPrecisionInstant? - Date end = time.resolveEnd().toDate(); + Date end = time.resolveEnd() + .toDate(); if (begin.equals(end)) { return filterWithInstant(new TimeInstant(time.resolveStart()), r); } @@ -278,11 +332,12 @@ default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDe return count != null ? filterInstantWithPeriod(r.getBeginPosition(), r.getEndPosition(), count) : filterInstantWithPeriod(r.getPosition(), begin, end, periodFromReducedPrecisionInstant); } + } /** - * Creates a filter for the specfied instant and fields that are nullable. In case of a - * instance with reduced precision a the method will call + * Creates a filter for the specfied instant and fields that are nullable. + * In case of a instance with reduced precision a the method will call * {@link #filterWithPeriod(TimePeriod, AbstractTimePrimitiveFieldDescriptor, boolean)}. * * @param time @@ -291,35 +346,41 @@ default Criterion filterWithPeriod(TimePeriod time, AbstractTimePrimitiveFieldDe * the property name(s) * * @return the {@code Criterion} that describes this restriction + * @throws UnsupportedTimeException If the filter is not supported! */ - default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableFieldDescriptor r) { + default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableFieldDescriptor r) + throws UnsupportedTimeException { /* * Saved primitives can be periods, but can also be instants. As begin * < end has to be true for all periods those are instants and have * to be treated as such. Also instants with reduced precision are * semantically periods and have to be handled like periods. */ - return Restrictions.or( - Restrictions.and(Restrictions.isNotNull(r.getPosition()), - filterWithInstant(time, (AbstractTimePrimitiveFieldDescriptor) r)), - Restrictions.and(Restrictions.isNull(r.getPosition()), filterWithInstant(time, r.getAlternative()))); + return Restrictions.disjunction( + Restrictions.conjunction(Restrictions.isNotNull(r.getPosition()), + createFilterWithInstant(time, (AbstractTimePrimitiveFieldDescriptor) r)), + Restrictions.conjunction(Restrictions.isNull(r.getPosition()), + createFilterWithInstant(time, r.getAlternative()))); } - default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableFieldDescriptor r, Integer count) { - /* - * Saved primitives can be periods, but can also be instants. As begin - * < end has to be true for all periods those are instants and have - * to be treated as such. Also instants with reduced precision are - * semantically periods and have to be handled like periods. - */ - return Restrictions.or( - Restrictions.and(Restrictions.isNotNull(r.getPosition()), - filterWithInstant(time, (AbstractTimePrimitiveFieldDescriptor) r, count)), - Restrictions.and(Restrictions.isNull(r.getPosition()), - filterWithInstant(time, r.getAlternative(), count))); + /** + * Creates a filter for the specfied instant and fields. In case of a + * instance with reduced precision a the method will call + * {@link #filterWithPeriod(TimePeriod, TimePrimitiveFieldDescriptor, boolean)}. + * + * @param time + * the time + * @param r + * the property name(s) + * + * @return the {@code Criterion} that describes this restriction + * @throws UnsupportedTimeException If the filter is not supported! + */ + default Criterion filterWithInstant(TimeInstant time, TimePrimitiveFieldDescriptor r) + throws UnsupportedTimeException { + return createFilterWithInstant(time, r); } - /** * Creates a filter for the specfied instant and fields. In case of a * instance with reduced precision a the method will call @@ -331,15 +392,50 @@ default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableField * the property name(s) * * @return the {@code Criterion} that describes this restriction + * @throws UnsupportedTimeException If the filter is not supported! */ - default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r) { + default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r) + throws UnsupportedTimeException { + return r instanceof TimePrimitiveFieldDescriptor ? createFilterWithInstant(time, r) + : filterWithInstant(time, (TimePrimitiveNullableFieldDescriptor) r); + } + + default Criterion filterWithInstant(TimeInstant time, TimePrimitiveNullableFieldDescriptor r, Integer count) + throws UnsupportedTimeException { /* * Saved primitives can be periods, but can also be instants. As begin * < end has to be true for all periods those are instants and have * to be treated as such. Also instants with reduced precision are * semantically periods and have to be handled like periods. */ - Date begin = time.resolveValue().toDate(); + return Restrictions.disjunction( + Restrictions.conjunction(Restrictions.isNotNull(r.getPosition()), + createFilterWithInstant(time, (AbstractTimePrimitiveFieldDescriptor) r, count)), + Restrictions.conjunction(Restrictions.isNull(r.getPosition()), + createFilterWithInstant(time, r.getAlternative(), count))); + } + + default Criterion filterWithInstant(TimeInstant time, TimePrimitiveFieldDescriptor r, Integer count) + throws UnsupportedTimeException { + return createFilterWithInstant(time, r, count); + } + + default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r, Integer count) + throws UnsupportedTimeException { + return r instanceof TimePrimitiveFieldDescriptor ? createFilterWithInstant(time, r, count) + : filterWithInstant(time, (TimePrimitiveNullableFieldDescriptor) r, count); + } + + default Criterion createFilterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r) + throws UnsupportedTimeException { + /* + * Saved primitives can be periods, but can also be instants. As begin + * < end has to be true for all periods those are instants and have + * to be treated as such. Also instants with reduced precision are + * semantically periods and have to be handled like periods. + */ + Date begin = time.resolveValue() + .toDate(); Date end = checkInstantWithReducedPrecision(time); if (end != null) { return filterWithPeriod(new TimePeriod(new DateTime(begin), new DateTime(end)), r, true); @@ -353,14 +449,16 @@ default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveField } } - default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r, Integer count) { + default Criterion createFilterWithInstant(TimeInstant time, AbstractTimePrimitiveFieldDescriptor r, Integer count) + throws UnsupportedTimeException { /* * Saved primitives can be periods, but can also be instants. As begin * < end has to be true for all periods those are instants and have * to be treated as such. Also instants with reduced precision are * semantically periods and have to be handled like periods. */ - Date begin = time.resolveValue().toDate(); + Date begin = time.resolveValue() + .toDate(); Date end = checkInstantWithReducedPrecision(time); if (end != null) { return filterWithPeriod(new TimePeriod(new DateTime(begin), new DateTime(end)), r, true, count); @@ -369,8 +467,10 @@ default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveField return count != null ? getPropertyCheckingCriterion( filterPeriodWithInstant(r.getBeginPosition(), r.getEndPosition(), count), null, r) - : getPropertyCheckingCriterion(filterPeriodWithInstant(r.getBeginPosition(), r.getEndPosition(), - time.getValue().toDate()), null, r); + : getPropertyCheckingCriterion( + filterPeriodWithInstant(r.getBeginPosition(), r.getEndPosition(), time.getValue() + .toDate()), + null, r); } else { return count != null ? filterInstantWithInstant(r.getPosition(), r.getBeginPosition(), count) @@ -391,7 +491,8 @@ default Criterion filterWithInstant(TimeInstant time, AbstractTimePrimitiveField static Date checkInstantWithReducedPrecision(TimeInstant time) { DateTime end = DateTimeHelper.setDateTime2EndOfMostPreciseUnit4RequestedEndPosition(time.getValue(), time.getRequestedTimeLength()); - return time.getValue().equals(end) ? null : end.toDate(); + return time.getValue() + .equals(end) ? null : end.toDate(); } /** diff --git a/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java new file mode 100644 index 0000000000..fb4edf459a --- /dev/null +++ b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantInstantTest.java @@ -0,0 +1,248 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.hibernate.criterion.Criterion; +import org.joda.time.DateTime; +import org.junit.Test; +import org.n52.shetland.ogc.filter.FilterConstants; +import org.n52.shetland.ogc.filter.TemporalFilter; +import org.n52.shetland.ogc.gml.time.TimeInstant; +import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; +import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; +import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; +import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; + +/** + * Test with instant as temporal filter + * + * @author Carsten Hollmann + * + * @since 4.4.0 + */ +public class TemporalRestrictionHqlInstantInstantTest implements TemporalRestrictionTestConstants { + + private static final String STE_LT_INSTANT = "samplingTimeEnd<:instant1"; + + private static final String STS_GT_INSTANT_AND_STE_LT_INSTANT = + "samplingTimeStart>:instant1 and samplingTimeEnd<:instant1"; + + @Test + public void testAfterPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:instant1")); + } + + @Test + public void testBeforePhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:instant1")); + } + + @Test + public void testEqualsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1 and samplingTimeEnd=:instant1")); + } + + @Test + public void testContainsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testDuringPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBeginsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testBegunByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testEndedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:instant1")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STE_LT_INSTANT)); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo(STS_GT_INSTANT_AND_STE_LT_INSTANT)); + } + + @Test + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:instant1) or (resultTime is null and samplingTimeEnd>:instant1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime<:instant1) or (resultTime is null and samplingTimeEnd<:instant1))")); + } + + @Test + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime=:instant1) or (resultTime is null and samplingTimeEnd=:instant1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:instant1 and resultTime<:instant1) or (resultTime is null and samplingTimeEnd>:instant1 and samplingTimeEnd<:instant1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:resultTime1 and resultTime<:resultTime1) or (resultTime is null and samplingTimeEnd>:resultTime1 and samplingTimeEnd<:resultTime1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + private TemporalFilter create(FilterConstants.TimeOperator op, String vr) { + return new TemporalFilter(op, new TimeInstant(DateTime.now()), vr); + } + +} diff --git a/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java new file mode 100644 index 0000000000..d31905e78f --- /dev/null +++ b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlInstantPeriodTest.java @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.hibernate.criterion.Criterion; +import org.joda.time.DateTime; +import org.junit.Test; +import org.n52.shetland.ogc.filter.FilterConstants; +import org.n52.shetland.ogc.filter.TemporalFilter; +import org.n52.shetland.ogc.gml.time.TimePeriod; +import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; +import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; +import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; +import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; + +/** + * Test with period as temporal filter + * + * @author Carsten Hollmann + * + * @since 4.4.0 + */ +public class TemporalRestrictionHqlInstantPeriodTest implements TemporalRestrictionTestConstants { + + @Test + public void testAfterPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:end1")); + } + + @Test + public void testBeforePhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:start1")); + } + + @Test + public void testEqualsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd=:end1")); + } + + @Test + public void testContainsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testDuringPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd<:end1")); + } + + @Test + public void testBeginsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd<:end1")); + } + + @Test + public void testBegunByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd>:end1")); + } + + @Test + public void testEndsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd=:end1")); + } + + @Test + public void testEndedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart<:start1 and samplingTimeEnd=:end1")); + } + + @Test + public void testOverlapsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), + equalTo("(samplingTimeStart<:start1 and samplingTimeEnd>:start1 and samplingTimeEnd<:end1)")); + } + + @Test + public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), + equalTo("(samplingTimeStart>:start1 and samplingTimeStart<:end1 and samplingTimeEnd>:end1)")); + } + + @Test + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:start1")); + } + + @Test + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:end1")); + } + + @Test + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:end1) or (resultTime is null and samplingTimeEnd>:end1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime<:start1) or (resultTime is null and samplingTimeEnd<:start1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime>:start1 and resultTime<:end1) or (resultTime is null and samplingTimeEnd>:start1 and samplingTimeEnd<:end1))")); + } + + @Test + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime=:start1) or (resultTime is null and samplingTimeEnd=:start1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo( + "((resultTime is not null and resultTime=:end1) or (resultTime is null and samplingTimeEnd=:end1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + private TemporalFilter create(FilterConstants.TimeOperator op, String vr) { + return new TemporalFilter(op, new TimePeriod(DateTime.now() + .minusHours(1), DateTime.now()), vr); + } + +} diff --git a/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java new file mode 100644 index 0000000000..84907246a0 --- /dev/null +++ b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodInstantTest.java @@ -0,0 +1,234 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.hibernate.criterion.Criterion; +import org.joda.time.DateTime; +import org.junit.Test; +import org.n52.shetland.ogc.filter.FilterConstants; +import org.n52.shetland.ogc.filter.TemporalFilter; +import org.n52.shetland.ogc.gml.time.TimeInstant; +import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; +import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; +import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; +import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; + +/** + * Test with instant as temporal filter + * + * @author Carsten Hollmann + * + * @since 4.4.0 + */ +public class TemporalRestrictionHqlPeriodInstantTest + implements TemporalRestrictionTestConstants { + + @Test + public void testAfterPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:instant1")); + } + + @Test + public void testBeforePhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:instant1")); + } + + @Test + public void testEqualsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1 and samplingTimeEnd=:instant1")); + } + + @Test + public void testContainsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart<:instant1 and samplingTimeEnd>:instant1")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testDuringPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBeginsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testBegunByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:instant1")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testEndedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:instant1")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime>:instant1) or (resultTime is null and samplingTimeEnd>:instant1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime<:instant1) or (resultTime is null and samplingTimeEnd<:instant1))")); + } + + @Test + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime=:instant1) or (resultTime is null and samplingTimeEnd=:instant1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + private TemporalFilter create(FilterConstants.TimeOperator op, String vr) { + return new TemporalFilter(op, new TimeInstant(DateTime.now()), vr); + } + +} diff --git a/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java new file mode 100644 index 0000000000..593a840314 --- /dev/null +++ b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionHqlPeriodPeriodTest.java @@ -0,0 +1,245 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.hibernate.criterion.Criterion; +import org.joda.time.DateTime; +import org.junit.Test; +import org.n52.shetland.ogc.filter.FilterConstants; +import org.n52.shetland.ogc.filter.TemporalFilter; +import org.n52.shetland.ogc.gml.time.TimePeriod; +import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; +import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; +import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; +import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; + +/** + * Test with period as temporal filter + * + * @author Carsten Hollmann + * + * @since 4.4.0 + */ +public class TemporalRestrictionHqlPeriodPeriodTest + implements TemporalRestrictionTestConstants { + + @Test + public void testAfterPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:end1")); + } + + @Test + public void testBeforePhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd<:start1")); + } + + @Test + public void testEqualsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd=:end1")); + } + + @Test + public void testContainsPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart<:start1 and samplingTimeEnd>:end1")); + } + + @Test + public void testDuringPhenomenonTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd<:end1")); + } + + @Test + public void testBeginsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd<:end1")); + } + + @Test + public void testBegunByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:start1 and samplingTimeEnd>:end1")); + } + + @Test + public void testEndsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart>:start1 and samplingTimeEnd=:end1")); + } + + @Test + public void testEndedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart<:start1 and samplingTimeEnd=:end1")); + } + + @Test + public void testOverlapsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), + equalTo("(samplingTimeStart<:start1 and samplingTimeEnd>:start1 and samplingTimeEnd<:end1)")); + } + + @Test + public void testOverlappedByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), + equalTo("(samplingTimeStart>:start1 and samplingTimeStart<:end1 and samplingTimeEnd>:end1)")); + } + + @Test + public void testMeetsPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeEnd=:start1")); + } + + @Test + public void testMetByPhenomenonTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, PHENOMENON_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("samplingTimeStart=:end1")); + } + + @Test + public void testAfterResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_After, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime>:end1) or (resultTime is null and samplingTimeEnd>:end1))")); + } + + @Test + public void testBeforeResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Before, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime<:start1) or (resultTime is null and samplingTimeEnd<:start1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEqualsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Equals, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testContainsResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Contains, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testDuringResultTime() + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_During, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime>:start1 and resultTime<:end1) or (resultTime is null and samplingTimeEnd>:start1 and samplingTimeEnd<:end1))")); + } + + @Test + public void testBeginsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Begins, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime=:start1) or (resultTime is null and samplingTimeEnd=:start1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testBegunByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_BegunBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test + public void testEndsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Ends, RESULT_TIME); + Criterion filterHql = TestTemporalRestrictions.filterHql(tf, 1); + assertThat(filterHql.toString(), equalTo("((resultTime is not null and resultTime=:end1) or (resultTime is null and samplingTimeEnd=:end1))")); + } + + @Test(expected = UnsupportedTimeException.class) + public void testEndedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_EndedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlapsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Overlaps, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testOverlappedByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_OverlappedBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMeetsResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_Meets, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + @Test(expected = UnsupportedTimeException.class) + public void testMetByResultTime() throws OwsExceptionReport { + TemporalFilter tf = create(FilterConstants.TimeOperator.TM_MetBy, RESULT_TIME); + TestTemporalRestrictions.filterHql(tf, 1); + } + + private TemporalFilter create(FilterConstants.TimeOperator op, String vr) { + return new TemporalFilter(op, new TimePeriod(DateTime.now().minusHours(1), DateTime.now()), vr); + } + +} diff --git a/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTestConstants.java b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTestConstants.java new file mode 100644 index 0000000000..606124419d --- /dev/null +++ b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TemporalRestrictionTestConstants.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +public interface TemporalRestrictionTestConstants { + + String PHENOMENON_TIME = "phenomenonTime"; + + String RESULT_TIME = "resultTime"; + + String PROPERTY_SAMPLING_TIME_START = "samplingTimeStart"; + + String PROPERTY_SAMPLING_TIME_END = "samplingTimeEnd"; + + String PROPERTY_RESULT_TIME = "resultTime"; + + String PROPERTY_VALID_TIME_START = "validTimeStart"; + + String PROPERTY_VALID_TIME_END = "validTimeEnd"; + + String START_TIME = "startTime"; + + String END_TIME = "endTime"; +} diff --git a/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TestTemporalRestrictions.java b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TestTemporalRestrictions.java new file mode 100644 index 0000000000..823e5af4ad --- /dev/null +++ b/hibernate/utils/src/test/java/org/n52/sos/ds/hibernate/util/TestTemporalRestrictions.java @@ -0,0 +1,280 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * 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. + */ +package org.n52.sos.ds.hibernate.util; + +import java.util.Collection; +import java.util.Map; + +import org.hibernate.criterion.Conjunction; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Disjunction; +import org.hibernate.criterion.Restrictions; +import org.n52.shetland.ogc.filter.FilterConstants.TimeOperator; +import org.n52.shetland.ogc.filter.TemporalFilter; +import org.n52.sos.ds.hibernate.util.restriction.AfterRestriction; +import org.n52.sos.ds.hibernate.util.restriction.BeforeRestriction; +import org.n52.sos.ds.hibernate.util.restriction.BeginsRestriction; +import org.n52.sos.ds.hibernate.util.restriction.BegunByRestriction; +import org.n52.sos.ds.hibernate.util.restriction.ContainsRestriction; +import org.n52.sos.ds.hibernate.util.restriction.DuringRestriction; +import org.n52.sos.ds.hibernate.util.restriction.EndedByRestriction; +import org.n52.sos.ds.hibernate.util.restriction.EndsRestriction; +import org.n52.sos.ds.hibernate.util.restriction.EqualsRestriction; +import org.n52.sos.ds.hibernate.util.restriction.MeetsRestriction; +import org.n52.sos.ds.hibernate.util.restriction.MetByRestriction; +import org.n52.sos.ds.hibernate.util.restriction.OverlappedByRestriction; +import org.n52.sos.ds.hibernate.util.restriction.OverlapsRestriction; +import org.n52.sos.exception.ows.concrete.UnsupportedOperatorException; +import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; +import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; + +import com.google.common.collect.Maps; + +/** + * Factory methods to create {@link Criterion Criterions} for + * {@link TemporalFilter TemporalFilters}. + * + * @see AfterRestriction + * @see BeforeRestriction + * @see BeginsRestriction + * @see BegunByRestriction + * @see ContainsRestriction + * @see DuringRestriction + * @see EndedByRestriction + * @see EndsRestriction + * @see EqualsRestriction + * @see MeetsRestriction + * @see MetByRestriction + * @see OverlappedByRestriction + * @see OverlapsRestriction + * @author Christian Autermann + * @since 4.0.0 + */ +public final class TestTemporalRestrictions implements TemporalRestrictionTestConstants { + + /** + * Fields describing the phenomenon time of a {@code Observation}. + * + * @see DataEntity#PROPERTY_SAMPLING_TIME_START + * @see DataEntity#PROPERTY_SAMPLING_TIME_END + */ + public static final AbstractTimePrimitiveFieldDescriptor PHENOMENON_TIME_FIELDS = new TimePrimitiveFieldDescriptor( + PROPERTY_SAMPLING_TIME_START, PROPERTY_SAMPLING_TIME_END); + + /** + * Fields describing the result time of a {@code Observation}. + * + * @see DataEntity#PROPERTY_RESULT_TIME + */ + public static final AbstractTimePrimitiveFieldDescriptor RESULT_TIME_FIELDS = + new TimePrimitiveNullableFieldDescriptor(PROPERTY_RESULT_TIME, + new TimePrimitiveFieldDescriptor(PROPERTY_SAMPLING_TIME_END)); + + /** + * Fields describing the valid time of a {@code Observation}. + * + * @see DataEntity#PROPERTY_VALID_TIME_START + * @see DataEntity#PROPERTY_VALID_TIME_END + */ + public static final AbstractTimePrimitiveFieldDescriptor VALID_TIME_FIELDS = + new TimePrimitiveFieldDescriptor(PROPERTY_VALID_TIME_START, PROPERTY_VALID_TIME_END); + + /** + * Fields describing the valid time of a {@code ValidProcedureTime}. + * + * @see ProcedureHistoryEntity#START_TIME + * @see ProcedureHistoryEntity#END_TIME + */ + public static final AbstractTimePrimitiveFieldDescriptor VALID_TIME_DESCRIBE_SENSOR_FIELDS = + new TimePrimitiveFieldDescriptor(START_TIME, END_TIME); + + /** + * Private constructor due to static access. + */ + private TestTemporalRestrictions() { + // noop + } + + /** + * Create a new {@code Criterion} using the specified filter. + * + * @param filter + * the filter + * + * @return the {@code Criterion} + * + * @throws UnsupportedTimeException + * if the value and property combination is not applicable for + * this restriction + * @throws UnsupportedValueReferenceException + * if the {@link TemporalFilter#getValueReference() value + * reference} can not be decoded + * @throws UnsupportedOperatorException + * if no restriction definition for the {@link TimeOperator} is + * found + */ + public static Criterion filter(TemporalFilter filter) + throws UnsupportedTimeException, UnsupportedValueReferenceException, UnsupportedOperatorException { + return TemporalRestrictions.filter(filter.getOperator(), getFields(filter.getValueReference()), + filter.getTime()); + } + + /** + * Creates a {@link Conjunction} for the specified temporal filters. + * + * @param temporalFilters + * the filters + * + * @return Hibernate temporal filter criterion + * + * @throws UnsupportedTimeException + * if the value and property combination is not applicable for + * this restriction + * @throws UnsupportedValueReferenceException + * if the {@link TemporalFilter#getValueReference() value + * reference} can not be decoded + * @throws UnsupportedOperatorException + * if no restriction definition for the {@link TimeOperator} is + * found + */ + public static Criterion filter(Iterable temporalFilters) + throws UnsupportedTimeException, UnsupportedValueReferenceException, UnsupportedOperatorException { + Conjunction conjunction = Restrictions.conjunction(); + Collection disjunctions = getDisjunction(temporalFilters); + if (disjunctions.size() == 1) { + return disjunctions.iterator().next(); + } + disjunctions.forEach(conjunction::add); + return conjunction; + } + + public static Criterion filterHql(TemporalFilter filter, Integer count) + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + return TemporalRestrictions.filter(filter.getOperator(), getFields(filter.getValueReference()), + filter.getTime(), count); + } + + public static Criterion filterHql(Iterable temporalFilters) + throws UnsupportedTimeException, UnsupportedValueReferenceException, UnsupportedOperatorException { + Conjunction conjunction = Restrictions.conjunction(); + Collection disjunctions = getDisjunctionHql(temporalFilters); + if (disjunctions.size() == 1) { + return disjunctions.iterator().next(); + } + for (Disjunction disjunction : disjunctions) { + conjunction.add(disjunction); + } + return conjunction; + } + + /** + * Creates {@link Disjunction}s for the specified temporal filters with the + * same valueReference. + * + * @param temporalFilters + * the filters + * + * @return {@link Collection} of {@link Disjunction} + * + * @throws UnsupportedTimeException + * if the value and property combination is not applicable for + * this restriction + * @throws UnsupportedValueReferenceException + * if the {@link TemporalFilter#getValueReference() value + * reference} can not be decoded + * @throws UnsupportedOperatorException + * if no restriction definition for the {@link TimeOperator} is + * found + */ + private static Collection getDisjunction(Iterable temporalFilters) + throws UnsupportedTimeException, UnsupportedValueReferenceException, UnsupportedOperatorException { + Map map = Maps.newHashMap(); + for (TemporalFilter temporalFilter : temporalFilters) { + if (map.containsKey(temporalFilter.getValueReference())) { + map.get(temporalFilter.getValueReference()).add(filter(temporalFilter)); + } else { + Disjunction disjunction = Restrictions.disjunction(); + disjunction.add(filter(temporalFilter)); + map.put(temporalFilter.getValueReference(), disjunction); + } + } + return map.values(); + } + + private static Collection getDisjunctionHql(Iterable temporalFilters) + throws UnsupportedValueReferenceException, UnsupportedTimeException, UnsupportedOperatorException { + Map map = Maps.newHashMap(); + Integer count = Integer.valueOf(1); + for (TemporalFilter temporalFilter : temporalFilters) { + if (map.containsKey(temporalFilter.getValueReference())) { + map.get(temporalFilter.getValueReference()).add(filterHql(temporalFilter, count)); + } else { + Disjunction disjunction = Restrictions.disjunction(); + disjunction.add(filterHql(temporalFilter, count)); + map.put(temporalFilter.getValueReference(), disjunction); + } + count++; + } + return map.values(); + } + + /** + * Gets the field descriptor for the specified value reference. + * + * @param valueReference + * the value reference + * + * @return the property descriptor + * + * @see TemporalRestrictions#PHENOMENON_TIME_VALUE_REFERENCE + * @see TemporalRestrictions #RESULT_TIME_VALUE_REFERENCE + * @see TemporalRestrictions#VALID_TIME_VALUE_REFERENCE + * @see #PHENOMENON_TIME_FIELDS + * @see #RESULT_TIME_FIELDS + * @see #VALID_TIME_FIELDS + * + * @throws UnsupportedValueReferenceException + * if the {@code valueReference} can not be decoded + */ + public static AbstractTimePrimitiveFieldDescriptor getFields(String valueReference) + throws UnsupportedValueReferenceException { + if (valueReference.contains(TemporalRestrictions.PHENOMENON_TIME_VALUE_REFERENCE)) { + return PHENOMENON_TIME_FIELDS; + } else if (valueReference.contains(TemporalRestrictions.RESULT_TIME_VALUE_REFERENCE)) { + return RESULT_TIME_FIELDS; + } else if (valueReference.contains(TemporalRestrictions.VALID_TIME_VALUE_REFERENCE)) { + return VALID_TIME_FIELDS; + } else if (valueReference.contains(TemporalRestrictions.VALID_DESCRIBE_SENSOR_TIME_VALUE_REFERENCE)) { + return VALID_TIME_DESCRIBE_SENSOR_FIELDS; + } else { + throw new UnsupportedValueReferenceException(valueReference); + } + } + +}