Created
April 19, 2012 14:53
-
-
Save handstandsam/2421461 to your computer and use it in GitHub Desktop.
Geomodel Geocell Query Engine for Objectify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.StringTokenizer; | |
import com.beoui.geocell.GeocellQueryEngine; | |
import com.beoui.geocell.model.GeocellQuery; | |
import com.googlecode.objectify.Objectify; | |
import com.googlecode.objectify.Query; | |
/** | |
* Objectify implementation for GeocellQueryEngine. | |
* <p> | |
* Example: | |
* | |
* <pre> | |
* // paramList contains values for each filter() condition | |
* List<Object> paramList = new ArrayList<Object>(); | |
* paramList.add(q); | |
* paramList.add(q + "\uFFFD"); | |
* | |
* // comma separated filter() conditions | |
* GeocellQuery baseQuery = new GeocellQuery("title >=, title | |
* <", paramList); | |
* GeocellQueryEngine queryEngine = new | |
* ObjectifyGeocellQueryEngine(ofy()); | |
* | |
* // perform search | |
* GeocellManager.proximitySearch( | |
* new Point(lat, lng), maxResults, maxDistance, | |
* MyEntity.class, | |
* baseQuery, queryEngine, | |
* GeocellManager.MAX_GEOCELL_RESOLUTION); | |
* </pre> | |
*/ | |
public class ObjectifyGeocellQueryEngine implements GeocellQueryEngine { | |
private String geocellsProperty; | |
private Objectify ofy; | |
public static final String DEFAULT_GEOCELLS_PROPERTY = "geocells"; | |
public ObjectifyGeocellQueryEngine(Objectify ofy) { | |
this(ofy, DEFAULT_GEOCELLS_PROPERTY); | |
} | |
public ObjectifyGeocellQueryEngine(Objectify ofy, String geocellsProperty) { | |
this.ofy = ofy; | |
this.geocellsProperty = geocellsProperty; | |
} | |
@Override | |
public <T> List<T> query(GeocellQuery baseQuery, List<String> geocells, Class<T> entityClass) { | |
StringTokenizer st; | |
int tokenNo = 0; | |
Query<T> query = ofy.query(entityClass); | |
if (baseQuery != null) { | |
st = new StringTokenizer(baseQuery.getBaseQuery(), ","); | |
while (st.hasMoreTokens()) { | |
query.filter(st.nextToken(), baseQuery.getParameters().get(tokenNo++)); | |
} | |
} | |
return query.filter(geocellsProperty + " IN", geocells).list(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment