Java Code Examples for javax.jdo.Query#setRange()

The following examples show how to use javax.jdo.Query#setRange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuestServiceImpl.java    From appengine-gwtguestbook-namespaces-java with Apache License 2.0 6 votes vote down vote up
@Override
public List<GuestbookEntryTransferObject> getTenLatestEntries() {
  PersistenceManager pm = PersistenceManagerHelper.getPersistenceManager();
  try {
    // Set the query to get the ten latest guest entries
    Query query = pm.newQuery(GuestbookEntry.class);
    query.setOrdering("timestamp DESC");
    query.setRange("0, 10");
    List<GuestbookEntry> entries = (List<GuestbookEntry>) query.execute();

    // Create a new guestbook entry transfer object for each entry and add
    // them to the list
    List<GuestbookEntryTransferObject> entryTransferObjects =
        new ArrayList<GuestbookEntryTransferObject>(entries.size());
    for (GuestbookEntry entry : entries) {
      entryTransferObjects.add(new GuestbookEntryTransferObject(entry
          .getName(), entry.getMessage()));
    }
    return entryTransferObjects;
  } finally {
    if (pm.currentTransaction().isActive()) {
      pm.currentTransaction().rollback();
    }
  }
}
 
Example 2
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Counts the entities returned by the specified query.
 * @param query  query whose results to count
 * @param params optional parameters of the query
 * @return the number of entities returned by the query
 * 
 * @deprecated Use of this method does not count toward <b>"Datastore Small Ops"</b> but toward <b>"Datastore Read Ops"</b>. Use {@link #countEntities(DatastoreService, com.google.appengine.api.datastore.Query)} instead.
 */
public static int countEntitiesJdo( final Query query, final Object... params ) {
	int count = 0;
	
	query.setRange( 0, 1000 );
	
	while ( true ) {
		final List< ? > resultList = (List< ? >) query.executeWithArray( params );
		count += resultList.size();
		
		if ( resultList.size() < 1000 )
			return count;
		
		setQueryCursor( query, resultList );
	}
}
 
Example 3
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public long getMinItemId(Date after, Integer dimension,ConsumerBean cb) {
	String typeFilter = "";
	if(dimension != null && dimension != Constants.DEFAULT_DIMENSION) {
		DimensionBean d = ItemService.getDimension(cb, dimension);
		Integer type = d.getItemType();
		if(type!=null) { typeFilter = " and type ="+ type; }
	}
	Query query = pm.newQuery( Item.class, "lastOp >= d" + typeFilter );
	query.declareParameters("java.util.Date d");
	query.setRange(0, 1);
	query.setOrdering("lastOp ASC,itemId ASC");
	Collection<Item> c = (Collection<Item>) query.execute(after);
	if (c.size() >= 1)
		return c.iterator().next().getItemId();
	else
		return 0L;
}
 
Example 4
Source File: AbstractAlpineQueryManager.java    From Alpine with Apache License 2.0 6 votes vote down vote up
/**
 * Given a query, this method will decorate that query with pagination, ordering,
 * and sorting direction. Specific checks are performed to ensure the execution
 * of the query is capable of being paged and that ordering can be securely performed.
 * @param query the JDO Query object to execute
 * @return a Collection of objects
 * @since 1.0.0
 */
public Query decorate(final Query query) {
    // Clear the result to fetch if previously specified (i.e. by getting count)
    query.setResult(null);
    if (pagination != null && pagination.isPaginated()) {
        final long begin = pagination.getOffset();
        final long end = begin + pagination.getLimit();
        query.setRange(begin, end);
    }
    if (orderBy != null && RegexSequence.Pattern.STRING_IDENTIFIER.matcher(orderBy).matches() && orderDirection != OrderDirection.UNSPECIFIED) {
        // Check to see if the specified orderBy field is defined in the class being queried.
        boolean found = false;
        final org.datanucleus.store.query.Query iq = ((JDOQuery) query).getInternalQuery();
        final String candidateField = orderBy.contains(".") ? orderBy.substring(0, orderBy.indexOf('.')) : orderBy;
        for (final Field field: iq.getCandidateClass().getDeclaredFields()) {
            if (candidateField.equals(field.getName())) {
                found = true;
                break;
            }
        }
        if (found) {
            query.setOrdering(orderBy + " " + orderDirection.name().toLowerCase());
        }
    }
    return query;
}
 
Example 5
Source File: SqlUserPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<User> getRecentUsers(int limit)
{
	Query query = pm.newQuery( User.class, " active==true " );
	query.setOrdering("userId desc");
	query.setRange(0, limit);
	Collection<User> c = (Collection<User>) query.execute();
	return c;
}
 
Example 6
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getUserItemActions(long itemId,long userId,int limit) {
	Query query = pm.newQuery( Action.class, "itemId == i &&  userId == u" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i,java.lang.Long u");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(itemId,userId);
	return c;
}
 
Example 7
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Action> getRecentUserActions(long userId, int actionType,
		int limit) {
	Query query = pm.newQuery( Action.class, "userId == i && type == t" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i,java.lang.Integer t");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(userId,actionType);
	return c;
}
 
Example 8
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Action> getRecentUserActions(String clientUserId,
		int actionType, int limit) {
	Query query = pm.newQuery( Action.class, "clientUserId == i && type == t" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.String i,java.lang.Integer t");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(clientUserId,actionType);
	return c;
}
 
Example 9
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getUserActions(long userId,int limit) {
	Query query = pm.newQuery( Action.class, "userId == i" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(userId);
	return c;
}
 
Example 10
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getItemActions(long itemId, int limit) {
	Query query = pm.newQuery( Action.class, "itemId == i" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(itemId);
	return c;
}
 
Example 11
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getRecentActions(int limit)
{
	Query query = pm.newQuery( Action.class, "" );
	query.setOrdering("actionId desc");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute();
	return c;
}
 
Example 12
Source File: SqlUserPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<User> getActiveUsers(int limit)
{
	Query query = pm.newQuery( User.class, "active" );
	query.setOrdering("userId desc");
	query.setRange(0, limit);
	Collection<User> c = (Collection<User>) query.execute();
	return c;
}
 
Example 13
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Item> getItemsFromUserActions(long userId, String actionType, int limit) {
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select item_id,name,first_op,last_op,popular,type,client_item_id from (select i.* from actions a, action_type at,items i where at.name=? and at.type_id=a.type and a.user_id=? and i.item_id=a.item_id group by i.item_id order by a.date desc) i" );
	query.setClass(Item.class);
	query.setRange(0, limit);
	Collection<Item> items = (List<Item>) query.execute(actionType, userId);
	return items;
}
 
Example 14
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Item> getItemsByName(String name, int limit, int dimension,ConsumerBean cb) {
	String typeFilter = "";
	if(dimension != Constants.DEFAULT_DIMENSION) {
		DimensionBean d = ItemService.getDimension(cb, dimension);
		Integer type = d.getItemType();
		if(type!=null) { typeFilter = " && type =="+ type; }
	}
	Query query = pm.newQuery( Item.class, "name.matches('(?i).*"+name+".*')" + typeFilter );
	query.setRange(0, limit);
	Collection<Item> c = (Collection<Item>) query.execute();
	return c;
}
 
Example 15
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Item> getAlphabeticItems(int limit,int dimension,ConsumerBean cb) {
	Integer type = null;
	//check if there is a filter over item_type
	if(dimension != Constants.DEFAULT_DIMENSION) {
		DimensionBean d = ItemService.getDimension(cb, dimension);
		type = d.getItemType();
	}
	Query query = pm.newQuery( Item.class, "" );
	if(type != null) { query.setFilter("type == " + type); }
	query.setOrdering("name asc,itemId desc");
	query.setRange(0, limit);
	Collection<Item> c = (Collection<Item>) query.execute();
	return c;
}
 
Example 16
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Item> getRecentItems(int limit, int dimension,ConsumerBean cb) {
	Integer type = null;
	//check if there is a filter over item_type
	if(dimension != Constants.DEFAULT_DIMENSION) {
		DimensionBean d = ItemService.getDimension(cb, dimension);
		type = d.getItemType();
	}
	Query query = pm.newQuery( Item.class, "" );
	if(type != null) { query.setFilter("type == " + type); }
	query.setOrdering("itemId desc");
	query.setRange(0, limit);
	Collection<Item> c = (Collection<Item>) query.execute();
	return c;
}
 
Example 17
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Item> getItems(int skip,int limit) {
	Query query = pm.newQuery( Item.class, "" );
	query.setRange(skip, limit);
	Collection<Item> c = (Collection<Item>) query.execute();
	return c;
}
 
Example 18
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Item> getItems(int limit,int dimension,ConsumerBean cb) {
	Integer type = null;
	//check if there is a filter over item_type
	if(dimension != Constants.DEFAULT_DIMENSION) {
		DimensionBean d = ItemService.getDimension(cb, dimension);
		type = d.getItemType();
	}
	Query query = pm.newQuery( Item.class, "" );
	if(type != null) { query.setFilter("type == " + type); }
	query.setOrdering("itemId desc");
	query.setRange(0, limit);
	Collection<Item> c = (Collection<Item>) query.execute();
	return c;
}
 
Example 19
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the page info to a query.
 * @param query    query to set the page info of
 * @param pageInfo page info to be set
 */
public static void setQueryPageInfo( final Query query, final PageInfo pageInfo ) {
	if ( pageInfo.getCursorString() == null )
		query.setRange( pageInfo.getOffset(), pageInfo.getOffset() + pageInfo.getLimit() );
	else {
		query.setRange( 0, pageInfo.getLimit() );
		
		final Map< String, Object > extensionMap = new HashMap< String, Object >( 2 ); // initial size of 2 because 1 with default load factor=0.75 would result in 0-size internal cache...
		extensionMap.put( JDOCursorHelper.CURSOR_EXTENSION, Cursor.fromWebSafeString( pageInfo.getCursorString() ) );
		
		query.setExtensions( extensionMap );
	}
}
 
Example 20
Source File: SqlUserPeer.java    From seldon-server with Apache License 2.0 4 votes vote down vote up
public Collection<User> getUserByName(String name, int limit) {
	Query query = pm.newQuery( User.class, "username.matches('(?i).*"+name+".*')");
	query.setRange(0, limit);
	Collection<User> c = (Collection<User>) query.execute();
	return c;
}