All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.loom.simpleds.PagedQueryFactory Maven / Gradle / Ivy

The newest version!
package org.loom.simpleds;

import org.loom.exception.HttpException;
import org.loom.servlet.LoomServletRequest;
import org.loom.servlet.LoomServletRequestImpl;
import org.loom.servlet.names.RequestParameterNames;
import org.simpleds.EntityManagerFactory;
import org.simpleds.PagedQuery;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.SortDirection;

/**
 * Creates a PagedQuery instance according to the current Request
 * parameters 
 * @author icoloma
 */
public class PagedQueryFactory {

	private static PagedQuery initialize(PagedQuery query) {
		LoomServletRequest request = LoomServletRequestImpl.getThreadLocal();
		
		// set current page index
		String s = request.getParameter(RequestParameterNames.PAGED_INDEX);
		if (s != null) {
			try {
				query.setPageIndex(Integer.parseInt(s));
			} catch (NumberFormatException e) {
				throw new HttpException(400, "Malformed value specified for " + RequestParameterNames.PAGED_INDEX, e);
			}
		}
		
		// set current sort order, if any
		String sortProperty = request.getParameter(RequestParameterNames.PAGED_SORT_PROPERTY);
		if (sortProperty != null) {
			String so = request.getParameter(RequestParameterNames.PAGED_SORT_ORDER);
			SortDirection direction = so == null || "asc".equals(so)? SortDirection.ASCENDING : SortDirection.DESCENDING;
			query.sort(sortProperty, direction);
		}
		return query;
	}
	
	public static PagedQuery create(Class clazz) {
		return initialize(EntityManagerFactory.getEntityManager().createPagedQuery(clazz));
	}

	public static PagedQuery create(Key ancestor, Class clazz) {
		return initialize(EntityManagerFactory.getEntityManager().createPagedQuery(ancestor, clazz));
	}

	public static PagedQuery create(Key ancestor, String kind) {
		return initialize(EntityManagerFactory.getEntityManager().createPagedQuery(ancestor, kind));
	}

	public static PagedQuery create(String kind) {
		return initialize(EntityManagerFactory.getEntityManager().createPagedQuery(kind));
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy