Search in sources :

Example 6 with DateRangeFacetValue

use of nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue in project timbuctoo by HuygensING.

the class DutchCaribbeanArchiveAndArchiverPeriodFacetDescriptionTest method shouldContainJustRight.

private void shouldContainJustRight(GraphTraversal<Vertex, Vertex> traversal, long startYear, long endYear) {
    List<FacetValue> facetValues = Lists.newArrayList(new DateRangeFacetValue(FACET_NAME, startYear * 10_000, endYear * 10_000));
    instance.filter(traversal, facetValues);
    assertThat(traversal.toList(), contains(likeVertex().withTimId("just_right")));
}
Also used : DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue)

Example 7 with DateRangeFacetValue

use of nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue in project timbuctoo by HuygensING.

the class ChangeRangeFacetDescription method filter.

@Override
@SuppressWarnings("unchecked")
public void filter(GraphTraversal<Vertex, Vertex> graphTraversal, List<FacetValue> facets) {
    Optional<FacetValue> first = facets.stream().filter(facetValue -> Objects.equals(facetValue.getName(), facetName)).findFirst();
    if (!first.isPresent()) {
        return;
    }
    FacetValue facetValue = first.get();
    if (!(facetValue instanceof DateRangeFacetValue)) {
        return;
    }
    long lowerLimit = ((DateRangeFacetValue) facetValue).getLowerLimit();
    LocalDate lowerLimitDate = LocalDate.parse("" + lowerLimit, FORMATTER);
    long upperLimit = ((DateRangeFacetValue) facetValue).getUpperLimit();
    LocalDate upperLimitDate = LocalDate.parse(("" + upperLimit), FORMATTER);
    // Use range because the java.time.Period has no way to determine if a date falls in that Period.
    Range<LocalDate> period = Range.closed(lowerLimitDate, upperLimitDate);
    graphTraversal.where(__.has(propertyName, P.test((o1, o2) -> {
        try {
            LocalDate localDate = getChangeLocalDate(o1);
            return ((Range<LocalDate>) o2).contains(localDate);
        } catch (IOException e) {
            LOG.error("Date {} cannot be parsed.", o1);
        }
        return false;
    }, period)));
}
Also used : ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) Logger(org.slf4j.Logger) LocalPropertyValueGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.LocalPropertyValueGetter) Range(com.google.common.collect.Range) LoggerFactory(org.slf4j.LoggerFactory) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) IOException(java.io.IOException) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Instant(java.time.Instant) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ZoneId(java.time.ZoneId) ChangeRangeFacetGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.ChangeRangeFacetGetter) Objects(java.util.Objects) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Change(nl.knaw.huygens.timbuctoo.model.Change) P(org.apache.tinkerpop.gremlin.process.traversal.P) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) IOException(java.io.IOException) Range(com.google.common.collect.Range) LocalDate(java.time.LocalDate)

Example 8 with DateRangeFacetValue

use of nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue in project timbuctoo by HuygensING.

the class DatableRangeFacetDescription method filter.

@Override
@SuppressWarnings("unchecked")
public void filter(GraphTraversal<Vertex, Vertex> graphTraversal, List<FacetValue> facets) {
    final Optional<DateRangeFacetValue> facet = FacetParsingHelp.getValue(facetName, facets);
    if (!facet.isPresent()) {
        return;
    }
    String lowerLimitString = facet.get().getLowerLimit() + "";
    String upperLimitString = facet.get().getUpperLimit() + "";
    try {
        Range<Date> range = Range.closed(FILTER_FORMAT.parse(lowerLimitString), FILTER_FORMAT.parse(upperLimitString));
        graphTraversal.where(__.has(propertyName, P.test((o1, o2) -> {
            Datable datable = getDatable("" + o1);
            if (!datable.isValid()) {
                return false;
            }
            Range<Date> range1 = (Range<Date>) o2;
            return range1.contains(datable.getFromDate()) || range1.contains(datable.getToDate());
        }, range)));
    } catch (ParseException e) {
        LOG.error("Cannot parse date", e);
    }
}
Also used : DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) ParseException(java.text.ParseException) Range(com.google.common.collect.Range) Date(java.util.Date) Datable(nl.knaw.huygens.timbuctoo.model.Datable)

Example 9 with DateRangeFacetValue

use of nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue in project timbuctoo by HuygensING.

the class FacetParsingHelp method getValue.

static Optional<DateRangeFacetValue> getValue(String facetName, List<FacetValue> facets) {
    Optional<FacetValue> first = facets.stream().filter(facetValue -> Objects.equals(facetValue.getName(), facetName)).findFirst();
    if (!first.isPresent()) {
        return Optional.empty();
    }
    FacetValue facetValue = first.get();
    if (!(facetValue instanceof DateRangeFacetValue)) {
        return Optional.empty();
    }
    DateRangeFacetValue casted = (DateRangeFacetValue) facetValue;
    // normalize input, make sure its always YYYY
    // the input from the client is YNNNN YYNNNN YYYNNNN etc. NNNN goes from 0000 to 9999 and means 1/10th milliYear
    // (1/10,000th of a year)
    casted.setLowerLimit(casted.getLowerLimit() / 10000);
    casted.setUpperLimit(casted.getUpperLimit() / 10000);
    return Optional.of(casted);
}
Also used : Objects(java.util.Objects) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Optional(java.util.Optional) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue)

Example 10 with DateRangeFacetValue

use of nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue in project timbuctoo by HuygensING.

the class RelatedDatableRangeFacetDescription method filter.

@Override
@SuppressWarnings("unchecked")
public void filter(GraphTraversal<Vertex, Vertex> graphTraversal, List<FacetValue> facets) {
    Optional<FacetValue> first = facets.stream().filter(facetValue -> Objects.equals(facetValue.getName(), facetName)).findFirst();
    if (!first.isPresent()) {
        return;
    }
    FacetValue facetValue = first.get();
    if (!(facetValue instanceof DateRangeFacetValue)) {
        return;
    }
    // pad the strings to make them parsable
    String lowerLimitString = Strings.padStart("" + ((DateRangeFacetValue) facetValue).getLowerLimit(), 8, '0').substring(0, 4);
    String upperLimitString = Strings.padStart("" + ((DateRangeFacetValue) facetValue).getUpperLimit(), 8, '0').substring(0, 4);
    try {
        Range<Date> range = Range.closed(FILTER_FORMAT.parse(lowerLimitString), FILTER_FORMAT.parse(upperLimitString));
        graphTraversal.where(__.bothE(relations).otherV().has(propertyName, P.test((o1, o2) -> {
            Datable datable = getDatable("" + o1);
            if (!datable.isValid()) {
                return false;
            }
            Range<Date> range1 = (Range<Date>) o2;
            return range1.contains(datable.getFromDate()) || range1.contains(datable.getToDate());
        }, range)));
    } catch (ParseException e) {
        LOG.error("Cannot parse date", e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) Date(java.util.Date) Range(com.google.common.collect.Range) DatableRangeFacetGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.DatableRangeFacetGetter) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Objects(java.util.Objects) Strings(com.google.common.base.Strings) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Datable(nl.knaw.huygens.timbuctoo.model.Datable) Optional(java.util.Optional) ParseException(java.text.ParseException) P(org.apache.tinkerpop.gremlin.process.traversal.P) RelatedPropertyValueGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.RelatedPropertyValueGetter) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) ParseException(java.text.ParseException) Range(com.google.common.collect.Range) Date(java.util.Date) Datable(nl.knaw.huygens.timbuctoo.model.Datable)

Aggregations

DateRangeFacetValue (nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue)17 FacetValue (nl.knaw.huygens.timbuctoo.search.FacetValue)16 List (java.util.List)14 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)13 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)13 Lists (com.google.common.collect.Lists)11 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)11 VertexMatcher.likeVertex (nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex)11 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)11 Matchers.contains (org.hamcrest.Matchers.contains)11 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)11 Before (org.junit.Before)11 Test (org.junit.Test)11 Range (com.google.common.collect.Range)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Instant (java.time.Instant)2 LocalDate (java.time.LocalDate)2