Example #1
0
 /*
  * Writes the SOLR query for temporal operator : After, Before, Begins, Ends, TEquals, BegunBy,
  * EndedBy, During, TContains
  *
  * @param filter temporal operator to encode
  */
 private Object visitBinaryTemporalOperator(BinaryTemporalOperator filter, Object extraData) {
   StringWriter output = asStringWriter(extraData);
   Expression e1 = filter.getExpression1();
   Expression e2 = filter.getExpression2();
   ExpressionToSolr visitor = new ExpressionToSolr(filter);
   if (filter instanceof After) {
     checkExpressionIsProperty(e1);
     checkExpressionIsLiteral(e2);
     PropertyName propertyName = (PropertyName) e1;
     propertyName.accept(visitor, output);
     output.append(":{");
     e2.accept(visitor, output);
     output.append(" TO *}");
   }
   if (filter instanceof Before) {
     checkExpressionIsProperty(e1);
     checkExpressionIsLiteral(e2);
     PropertyName propertyName = (PropertyName) e1;
     propertyName.accept(visitor, output);
     output.append(":{* TO ");
     e2.accept(visitor, output);
     output.append("}");
   }
   if (filter instanceof Begins || filter instanceof Ends || filter instanceof TEquals) {
     checkExpressionIsProperty(e1);
     checkExpressionIsLiteral(e2);
     PropertyName propertyName = (PropertyName) e1;
     propertyName.accept(visitor, output);
     output.append(":");
     e2.accept(visitor, output);
   }
   if (filter instanceof BegunBy || filter instanceof EndedBy) {
     checkExpressionIsProperty(e2);
     checkExpressionIsLiteral(e1);
     PropertyName propertyName = (PropertyName) e2;
     propertyName.accept(visitor, output);
     output.append(":");
     e1.accept(visitor, output);
   }
   if (filter instanceof During) {
     checkExpressionIsProperty(e1);
     checkExpressionIsLiteral(e2);
     PropertyName propertyName = (PropertyName) e1;
     propertyName.accept(visitor, output);
     output.append(":{");
     e2.accept(visitor, output);
     output.append("}");
   }
   if (filter instanceof TContains) {
     checkExpressionIsProperty(e2);
     checkExpressionIsLiteral(e1);
     PropertyName propertyName = (PropertyName) e2;
     propertyName.accept(visitor, output);
     output.append(":{");
     e1.accept(visitor, output);
     output.append("}");
   }
   return output;
 }
  private void handleTemporal(BinaryTemporalOperator filter) {

    Literal literalWrapper = (Literal) filter.getExpression2();
    logger.debug("literalWrapper.getValue() = " + literalWrapper.getValue());

    Object literal = literalWrapper.evaluate(null);
    if (literal instanceof Period) {
      Period period = (Period) literal;

      // Extract the start and end dates from the filter
      Date start = period.getBeginning().getPosition().getDate();
      Date end = period.getEnding().getPosition().getDate();

      temporalSearch = new TemporalFilter(start, end);

      filters.add(filter);
    } else if (literal instanceof PeriodDuration) {

      DefaultPeriodDuration duration = (DefaultPeriodDuration) literal;

      // Extract the start and end dates from the filter
      Date end = Calendar.getInstance().getTime();
      Date start = new Date(end.getTime() - duration.getTimeInMillis());

      temporalSearch = new TemporalFilter(start, end);

      filters.add(filter);
    }
  }
  protected Object visit(BinaryTemporalOperator filter, Object data) {
    if (original == null) original = filter;

    // supports it as a group -- no need to check the type
    if (!fcs.supports(filter)) {
      postStack.push(filter);
      return null;
    }

    Expression leftValue = filter.getExpression1();
    Expression rightValue = filter.getExpression2();

    int i = postStack.size();
    if (leftValue == null || rightValue == null) {
      postStack.push(filter);
      return null;
    }

    leftValue.accept(this, null);

    if (i < postStack.size()) {
      postStack.pop();
      postStack.push(filter);

      return null;
    }

    rightValue.accept(this, null);

    if (i < postStack.size()) {
      preStack.pop(); // left
      postStack.pop();
      postStack.push(filter);

      return null;
    }

    preStack.pop(); // left side
    preStack.pop(); // right side
    preStack.push(filter);
    return null;
  }