Support Temporal parameters in LoadContext query (ability to specify TemporalType). #PL-6229

This commit is contained in:
Konstantin Krivopustov 2015-11-25 15:08:53 +00:00
parent 60813cd6fb
commit 47f352f735
3 changed files with 45 additions and 5 deletions

View File

@ -111,7 +111,12 @@ public class DataServiceQueryBuilder {
value = list;
}
query.setParameter(name, value);
if (value instanceof LoadContext.Query.TemporalValue) {
LoadContext.Query.TemporalValue temporalValue = (LoadContext.Query.TemporalValue) value;
query.setParameter(name, temporalValue.date, temporalValue.type);
} else {
query.setParameter(name, value);
}
} else
throw new DevelopmentException("Parameter '" + name + "' is not used in the query");
}

View File

@ -9,8 +9,11 @@ import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.Server;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.security.entity.User;
import org.apache.commons.lang.time.DateUtils;
import javax.persistence.TemporalType;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@ -163,6 +166,16 @@ public class DataManagerTest extends CubaTestCase {
assertEquals(0, count);
}
public void testTemporalType() throws Exception {
Date nextYear = DateUtils.addYears(AppBeans.get(TimeSource.class).currentTimestamp(), 1);
LoadContext<User> loadContext = LoadContext.create(User.class).setQuery(
LoadContext.createQuery("select u from sec$User u where u.createTs = :ts")
.setParameter("ts", nextYear, TemporalType.DATE));
List<User> users = dataManager.loadList(loadContext);
assertTrue(users.isEmpty());
}
public void testExtendedLoadContext() throws Exception {
LoadContext<User> loadContext = new MyLoadContext<>(User.class, "test").setQuery(
LoadContext.createQuery("select u from sec$User u where u.login = :login").setParameter("login", "admin"));

View File

@ -8,11 +8,9 @@ import com.haulmont.bali.util.Preconditions;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.cuba.core.entity.Entity;
import javax.persistence.TemporalType;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -261,6 +259,18 @@ public class LoadContext<E extends Entity> implements Serializable {
private int firstResult;
private int maxResults;
public static class TemporalValue implements Serializable {
private static final long serialVersionUID = 4972088045550018312L;
public final Date date;
public final TemporalType type;
public TemporalValue(Date date, TemporalType type) {
this.date = date;
this.type = type;
}
}
/**
* @param queryString JPQL query string. Only named parameters are supported.
*/
@ -293,6 +303,18 @@ public class LoadContext<E extends Entity> implements Serializable {
return this;
}
/**
* Set value for a parameter of java.util.Date type.
* @param name parameter name
* @param value date value
* @param temporalType temporal type
* @return this query instance for chaining
*/
public Query setParameter(String name, Date value, TemporalType temporalType) {
parameters.put(name, new TemporalValue(value, temporalType));
return this;
}
/**
* @return editable map of the query parameters
*/