org.eiichiro.acidhouse.appengine.AppEngineGetList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of acidhouse-appengine Show documentation
Show all versions of acidhouse-appengine Show documentation
Acid House Implementation for Google App Engine
The newest version!
/*
* Copyright (C) 2011-2012 Eiichiro Uchiumi. All Rights Reserved.
*
* 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 org.eiichiro.acidhouse.appengine;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import org.eiichiro.acidhouse.ComparableFilter;
import org.eiichiro.acidhouse.Entities;
import org.eiichiro.acidhouse.Filter;
import org.eiichiro.acidhouse.GetList;
import org.eiichiro.acidhouse.InFilter;
import org.eiichiro.acidhouse.Order;
import org.eiichiro.acidhouse.metamodel.EmbeddedProperty;
import org.eiichiro.acidhouse.metamodel.Metamodel;
import org.eiichiro.acidhouse.metamodel.Property;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
/**
* {@code AppEngineGetList} is a App Engine Low-level Datastore API based
* implementation of {@code GetList}.
*
* @author Eiichiro Uchiumi
*/
public class AppEngineGetList implements GetList {
private Logger logger = Logger.getLogger(getClass().getName());
private final Metamodel metamodel;
private final AppEngineDatastoreSession session;
private List> filters = new ArrayList>(0);
private int limit = Integer.MAX_VALUE;
private int offset = 0;
private List> orders = new ArrayList>(0);
/**
* Constructs a new {@code AppEngineGetList} with the specified metamodel of
* entity and {@code AppEngineDatastoreSession}.
*
* @param metamodel The metamodel of entity to get with this command.
* @param session {@code AppEngineDatastoreSession} instance for this command.
*/
public AppEngineGetList(Metamodel metamodel, AppEngineDatastoreSession session) {
this.metamodel = metamodel;
this.session = session;
}
/**
* Executes {@code GetList} with {@code AppEngineDatastoreSession}.
*
* @return The entities match to the specified {@code Filter}s in the
* specified range ordered by the specified sort orders as a {@code List}
* view.
*/
@Override
public List execute() {
logger.fine("Executing [AppEngineGetList] command");
Class type = metamodel.type();
Query query = new Query(Translation.toKind(type));
QueryRestriction restriction = new QueryRestriction();
Set> subfilters = new HashSet>();
for (Filter> filter : filters) {
if (restriction.restricted(filter)) {
subfilters.add(filter);
} else {
Property, ?> property = filter.property();
String name = (property.parent() instanceof EmbeddedProperty)
? property.parent().name() + "." + property.name() : property.name();
boolean key = false;
if (name.equals(Entities.keyField(type).getName())) {
name = Entity.KEY_RESERVED_PROPERTY;
key = true;
}
if (filter instanceof ComparableFilter>) {
ComparableFilter> comparableFilter = (ComparableFilter>) filter;
Object value = comparableFilter.value();
if (key) {
value = Keys.create(Translation.toKind(type), value);
}
query.setFilter(new Query.FilterPredicate(
name, Translation.toFilterOperator(comparableFilter.operator()), value));
} else if (filter instanceof InFilter>) {
InFilter> inFilter = (InFilter>) filter;
List> values = inFilter.values();
if (key) {
List