You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2013/10/29 08:53:41 UTC
[2/2] git commit: ISIS-579: support range queries in JDO
ISIS-579: support range queries in JDO
Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/ecc55d1e
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/ecc55d1e
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/ecc55d1e
Branch: refs/heads/master
Commit: ecc55d1e545cebdc4f484e48f57fde3839091235
Parents: b8ea493
Author: Dan Haywood <da...@apache.org>
Authored: Tue Oct 29 07:52:59 2013 +0000
Committer: Dan Haywood <da...@apache.org>
Committed: Tue Oct 29 07:52:59 2013 +0000
----------------------------------------------------------------------
...tenceQueryFindUsingApplibQueryProcessor.java | 23 ++--
.../apache/isis/applib/query/QueryAbstract.java | 4 +-
.../apache/isis/applib/query/QueryDefault.java | 16 +++
...QueryDefaultTest_withStart_or_withCount.java | 104 +++++++++++++++++++
...istenceQueryFindUsingApplibQueryDefault.java | 11 ++
5 files changed, 150 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/isis/blob/ecc55d1e/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/persistence/queries/PersistenceQueryFindUsingApplibQueryProcessor.java
----------------------------------------------------------------------
diff --git a/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/persistence/queries/PersistenceQueryFindUsingApplibQueryProcessor.java b/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/persistence/queries/PersistenceQueryFindUsingApplibQueryProcessor.java
index ca72b9f..03bdf6e 100644
--- a/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/persistence/queries/PersistenceQueryFindUsingApplibQueryProcessor.java
+++ b/component/objectstore/jdo/jdo-datanucleus/src/main/java/org/apache/isis/objectstore/jdo/datanucleus/persistence/queries/PersistenceQueryFindUsingApplibQueryProcessor.java
@@ -50,22 +50,25 @@ public class PersistenceQueryFindUsingApplibQueryProcessor extends PersistenceQu
public List<ObjectAdapter> process(final PersistenceQueryFindUsingApplibQueryDefault persistenceQuery) {
final String queryName = persistenceQuery.getQueryName();
- final Map<String, Object> map = unwrap(persistenceQuery.getArgumentsAdaptersByParameterName());
- final QueryCardinality cardinality = persistenceQuery.getCardinality();
final ObjectSpecification objectSpec = persistenceQuery.getSpecification();
final List<?> results;
if((objectSpec.getFullIdentifier() + "#pk").equals(queryName)) {
- results = getResultsPk(queryName, map, objectSpec);
+ results = getResultsPk(persistenceQuery);
} else {
- results = getResults(objectSpec, queryName, map, cardinality);
+ results = getResults(persistenceQuery);
}
return loadAdapters(objectSpec, results);
}
// special case handling
- private List<?> getResultsPk(final String queryName, final Map<String, Object> map, final ObjectSpecification objectSpec) {
+ private List<?> getResultsPk(final PersistenceQueryFindUsingApplibQueryDefault persistenceQuery) {
+
+ final String queryName = persistenceQuery.getQueryName();
+ final Map<String, Object> map = unwrap(persistenceQuery.getArgumentsAdaptersByParameterName());
+ final ObjectSpecification objectSpec = persistenceQuery.getSpecification();
+
final Class<?> cls = objectSpec.getCorrespondingClass();
if(!JdoPropertyUtils.hasPrimaryKeyProperty(objectSpec)) {
throw new UnsupportedOperationException("cannot search by primary key for DataStore-assigned entities");
@@ -82,11 +85,19 @@ public class PersistenceQueryFindUsingApplibQueryProcessor extends PersistenceQu
return (List<?>) jdoQuery.execute();
}
- private List<?> getResults(ObjectSpecification objectSpec, final String queryName, final Map<String, Object> argumentsByParameterName, final QueryCardinality cardinality) {
+ private List<?> getResults(final PersistenceQueryFindUsingApplibQueryDefault persistenceQuery) {
+ final String queryName = persistenceQuery.getQueryName();
+ final Map<String, Object> argumentsByParameterName = unwrap(persistenceQuery.getArgumentsAdaptersByParameterName());
+ final QueryCardinality cardinality = persistenceQuery.getCardinality();
+ final ObjectSpecification objectSpec = persistenceQuery.getSpecification();
+
final PersistenceManager persistenceManager = getJdoObjectStore().getPersistenceManager();
final Class<?> cls = objectSpec.getCorrespondingClass();
final Query jdoQuery = persistenceManager.newNamedQuery(cls, queryName);
+ if(persistenceQuery.hasRange()) {
+ jdoQuery.setRange(persistenceQuery.getStart(), persistenceQuery.getEnd());
+ }
if (LOG.isDebugEnabled()) {
LOG.debug("query: " + queryName + ", args: " + argumentsByParameterName);
http://git-wip-us.apache.org/repos/asf/isis/blob/ecc55d1e/core/applib/src/main/java/org/apache/isis/applib/query/QueryAbstract.java
----------------------------------------------------------------------
diff --git a/core/applib/src/main/java/org/apache/isis/applib/query/QueryAbstract.java b/core/applib/src/main/java/org/apache/isis/applib/query/QueryAbstract.java
index 093a7a5..7c2497e 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/query/QueryAbstract.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/query/QueryAbstract.java
@@ -27,8 +27,8 @@ package org.apache.isis.applib.query;
*/
public abstract class QueryAbstract<T> implements Query<T> {
- private final long start;
- private final long count;
+ protected long start;
+ protected long count;
private static final long serialVersionUID = 1L;
private final String resultTypeName;
http://git-wip-us.apache.org/repos/asf/isis/blob/ecc55d1e/core/applib/src/main/java/org/apache/isis/applib/query/QueryDefault.java
----------------------------------------------------------------------
diff --git a/core/applib/src/main/java/org/apache/isis/applib/query/QueryDefault.java b/core/applib/src/main/java/org/apache/isis/applib/query/QueryDefault.java
index cd0d524..530de0b 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/query/QueryDefault.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/query/QueryDefault.java
@@ -97,6 +97,22 @@ public class QueryDefault<T> extends QueryAbstract<T> {
return Collections.unmodifiableMap(argumentsByParameterName);
}
+ public QueryDefault<T> withStart(final long start) {
+ if(start<0) {
+ throw new IllegalArgumentException("require start>=0");
+ }
+ this.start = start;
+ return this;
+ }
+
+ public QueryDefault<T> withCount(final long count) {
+ if(count<=0) {
+ throw new IllegalArgumentException("require count>0");
+ }
+ this.count = count;
+ return this;
+ }
+
@Override
public String getDescription() {
return getQueryName() + " with " + getArgumentsByParameterName();
http://git-wip-us.apache.org/repos/asf/isis/blob/ecc55d1e/core/applib/src/test/java/org/apache/isis/applib/query/QueryDefaultTest_withStart_or_withCount.java
----------------------------------------------------------------------
diff --git a/core/applib/src/test/java/org/apache/isis/applib/query/QueryDefaultTest_withStart_or_withCount.java b/core/applib/src/test/java/org/apache/isis/applib/query/QueryDefaultTest_withStart_or_withCount.java
new file mode 100644
index 0000000..df1c3bb
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/applib/query/QueryDefaultTest_withStart_or_withCount.java
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.isis.applib.query;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+import com.google.common.collect.ImmutableMap;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class QueryDefaultTest_withStart_or_withCount {
+
+ private QueryDefault<Customer> queryDefault;
+
+ @Rule
+ public ExpectedException thrown= ExpectedException.none();
+
+ static class Customer {}
+
+ @Before
+ public void setUp() throws Exception {
+ queryDefault = new QueryDefault<Customer>(Customer.class, "findByLastName", "lastName", "Smith");
+ }
+
+ @Test
+ public void defaults() throws Exception {
+ assertThat(queryDefault.getStart(), is(0L));
+ assertThat(queryDefault.getCount(), is(0L));
+ }
+
+
+ @Test
+ public void typicalHappyCase() throws Exception {
+ final QueryDefault<Customer> q = queryDefault.withStart(10L).withCount(5L);
+
+ assertThat(q, is(queryDefault));
+ assertThat(q.getStart(), is(10L));
+ assertThat(q.getCount(), is(5L));
+ }
+
+ @Test
+ public void happyCase_startOnly() throws Exception {
+ final QueryDefault<Customer> q = queryDefault.withStart(10L);
+
+ assertThat(q, is(queryDefault));
+ assertThat(q.getStart(), is(10L));
+ assertThat(q.getCount(), is(0L));
+ }
+
+ @Test
+ public void happyCase_startZero() throws Exception {
+ final QueryDefault<Customer> q = queryDefault.withStart(0);
+
+ assertThat(q, is(queryDefault));
+ assertThat(q.getStart(), is(0L));
+ }
+
+ @Test
+ public void startNegative() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ queryDefault.withStart(-1);
+ }
+
+ @Test
+ public void happyCase_countOnly() throws Exception {
+ final QueryDefault<Customer> q = queryDefault.withCount(20L);
+
+ assertThat(q, is(queryDefault));
+ assertThat(q.getStart(), is(0L));
+ assertThat(q.getCount(), is(20L));
+ }
+
+ @Test
+ public void countNegative() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ queryDefault.withCount(-1);
+ }
+
+ @Test
+ public void countZero() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ queryDefault.withCount(0);
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/isis/blob/ecc55d1e/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/query/PersistenceQueryFindUsingApplibQueryDefault.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/query/PersistenceQueryFindUsingApplibQueryDefault.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/query/PersistenceQueryFindUsingApplibQueryDefault.java
index d86dbde..e66307b 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/query/PersistenceQueryFindUsingApplibQueryDefault.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/persistence/query/PersistenceQueryFindUsingApplibQueryDefault.java
@@ -94,4 +94,15 @@ public class PersistenceQueryFindUsingApplibQueryDefault extends PersistenceQuer
str.append("spec", getSpecification().getShortIdentifier());
return str.toString();
}
+
+ public long getEnd() {
+ // we default to Integer.MAX_VALUE because HSQLDB blows up
+ // (with a ClassCastException from Long to Integer)
+ // if we return Long.MAX_VALUE
+ return getCount() != 0? getStart() + getCount(): Integer.MAX_VALUE;
+ }
+
+ public boolean hasRange() {
+ return getStart() != 0 || getCount() != 0;
+ }
}