Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
Add code to expose and query by creation and modification timestamps …
Browse files Browse the repository at this point in the history
…(JSDK-181)
  • Loading branch information
Anai Arroyo committed Mar 27, 2015
1 parent 1687ea0 commit 837c979
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public interface DateExpressionFactory {
* Returns a new expression indicating the property value must belong to the range specified by the {@code begin} and {@end} dates
* Using "in" is equivalent to gte(begin).lt(end) where begin time is inclusive and end time is exclusive.
* For example:
* .modifiedAt().in(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse("2014-01-01T12:00:00"), new Date()) matches those entities modified after or exactly the noon of 2014/01/01 and before the exact instant represented by {@code new Date()}
* .modifiedAt().in(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse("2014-01-01T12:00:00"), new Date()) matches those entities modified after or exactly at the noon of 2014/01/01 and before the exact instant represented by {@code new Date()}
*
* @param begin the {@link Date} to use as the range start
* @param end a {@link Date} to use as the range end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package com.stormpath.sdk.impl.query;

import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.stormpath.sdk.lang.Assert;
import com.stormpath.sdk.lang.Duration;
import com.stormpath.sdk.query.Criterion;
import com.stormpath.sdk.query.DateExpressionFactory;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @since 1.0.RC4
Expand All @@ -28,12 +32,18 @@ public class DefaultDateExpressionFactory implements DateExpressionFactory {

private final String propertyName;

private static final String INCLUSIVE_OPENING = "[";
private static final String INCLUSIVE_CLOSING = "]";
private static final String EXCLUSIVE_OPENING = "(";
private static final String EXCLUSIVE_CLOSING = ")";
private static final String COMMA = ", ";

public DefaultDateExpressionFactory(String propertyName) {
this.propertyName = propertyName;
}

/**
* Returns a new equals expression reflecting the specified value.
* Convenience method that returns a new equals expression reflecting the specified String value.
*
* @param value the value that should equal the property value.
* @return a new equals expression reflecting the current property name and the specified value.
Expand All @@ -45,36 +55,85 @@ public SimpleExpression matches(String value) {

@Override
public Criterion gt(Date date) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(date instanceof Date, "date needs to be a valid Date object");
DateFormat df = new ISO8601DateFormat();
String value = EXCLUSIVE_OPENING + df.format(date).toString() + COMMA + INCLUSIVE_CLOSING;
return new SimpleExpression(propertyName, value, Operator.EQUALS);
}

@Override
public Criterion gte(Date date) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(date instanceof Date, "date needs to be a valid Date object");
DateFormat df = new ISO8601DateFormat();
String value = INCLUSIVE_OPENING + df.format(date).toString() + COMMA + INCLUSIVE_CLOSING;
return new SimpleExpression(propertyName, value, Operator.EQUALS);
}

@Override
public Criterion lt(Date date) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(date instanceof Date, "date needs to be a valid Date object");
DateFormat df = new ISO8601DateFormat();
String value = INCLUSIVE_OPENING + COMMA + df.format(date).toString() + EXCLUSIVE_CLOSING;
return new SimpleExpression(propertyName, value, Operator.EQUALS);
}

@Override
public Criterion lte(Date date) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(date instanceof Date, "date needs to be a valid Date object");
DateFormat df = new ISO8601DateFormat();
String value = INCLUSIVE_OPENING + COMMA + df.format(date).toString() + INCLUSIVE_CLOSING;
return new SimpleExpression(propertyName, value, Operator.EQUALS);
}

@Override
public Criterion equals(Date date) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(date instanceof Date, "date needs to be a valid Date object");
DateFormat df = new ISO8601DateFormat();
return new SimpleExpression(propertyName, df.format(date).toString(), Operator.EQUALS);
}

@Override
public Criterion in(Date begin, Date end) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(begin instanceof Date, "begin needs to be a valid Date object");
Assert.isTrue(end instanceof Date, "end needs to be a valid Date object");
Assert.isTrue(begin.before(end), "begin date needs to be earlier than end date");
DateFormat df = new ISO8601DateFormat();
String value = INCLUSIVE_OPENING + df.format(begin).toString() + COMMA + df.format(end).toString() + EXCLUSIVE_CLOSING;
return new SimpleExpression(propertyName, value, Operator.EQUALS);
}

@Override
public Criterion in(Date begin, Duration duration) {
return null; //To change body of implemented methods use File | Settings | File Templates.
Assert.isTrue(begin instanceof Date, "begin needs to be a valid Date object");
Assert.isTrue(duration instanceof Duration, "duration needs to be a valid Duration object");
DateFormat df = new ISO8601DateFormat();
Date endDate = this.calculateDateFromDuration(begin, duration);
String value = INCLUSIVE_OPENING + df.format(begin).toString() + COMMA + df.format(endDate).toString() + EXCLUSIVE_CLOSING;
return new SimpleExpression(propertyName, value, Operator.EQUALS);
}

private Date calculateDateFromDuration(Date begin, Duration duration){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(begin.getTime() + this.DurationToIntMillis(duration));
return cal.getTime();
}

private long DurationToIntMillis(Duration duration){
switch (duration.getTimeUnit()){
case DAYS:
return TimeUnit.DAYS.toMillis(duration.getValue());
case HOURS:
return TimeUnit.HOURS.toMillis(duration.getValue());
case MINUTES:
return TimeUnit.MINUTES.toMillis(duration.getValue());
case MILLISECONDS:
return duration.getValue();
case MICROSECONDS:
return TimeUnit.MICROSECONDS.toMillis(duration.getValue());
case NANOSECONDS:
return TimeUnit.NANOSECONDS.toMillis(duration.getValue());
default:
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2015 Stormpath, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stormpath.sdk.impl.query

import com.stormpath.sdk.lang.Duration
import com.stormpath.sdk.query.Criterion
import org.testng.annotations.Test
import static org.testng.Assert.assertEquals

class DefaultDateExpressionFactoryTest {

@Test(expectedExceptions = IllegalArgumentException)
void testMethodErrors() {
DefaultDateExpressionFactory defaultDateExpressionFactory = new DefaultDateExpressionFactory("test");

//test invalid arguments
Criterion c = null;
c = defaultDateExpressionFactory.gt(null);
c = defaultDateExpressionFactory.gte(null);
c = defaultDateExpressionFactory.lt(null);
c = defaultDateExpressionFactory.lte(null);
c = defaultDateExpressionFactory.in(null, null);
c = defaultDateExpressionFactory.in(new Date(), null);
c = defaultDateExpressionFactory.in(null, new Duration());
}


}

0 comments on commit 837c979

Please sign in to comment.