Java Code Examples for com.google.appengine.api.datastore.Cursor#fromWebSafeString()

The following examples show how to use com.google.appengine.api.datastore.Cursor#fromWebSafeString() . 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: CursorTest.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
@Test
public void testStartEndCursor() {
    int limit = total / testDat.length;
    Query query = new Query(kindName, rootKey);
    query.addSort("name", Query.SortDirection.ASCENDING);
    FetchOptions fetchOption = FetchOptions.Builder.withLimit(limit);
    // fetch 1st page and get cursor1
    QueryResultList<Entity> nextBatch = service.prepare(query)
        .asQueryResultList(fetchOption);
    Cursor cursor1 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString());
    // fetch 2nd page and get cursor2
    nextBatch = service.prepare(query).asQueryResultList(fetchOption.startCursor(cursor1));
    Cursor cursor2 = Cursor.fromWebSafeString(nextBatch.getCursor().toWebSafeString());
    // cursor1 as start and cursor2 as end and 15 in limit -- -- should return 2nd page.
    checkPage(query, cursor1, cursor2, limit, limit, testDat[1], testDat[1]);
    // cursor1 as start and cursor2 as end and 30 in limit -- should return 2nd page.
    checkPage(query, cursor1, cursor2, 2 * limit, limit, testDat[1], testDat[1]);
    // cursor2 as start and cursor1 as end and 15 in limit -- should not return any.
    checkPage(query, cursor2, cursor1, limit, 0, null, null);
}
 
Example 2
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilter() {
    int onePage = 5;
    String filterData = "ff";
    Query query = new Query(kindName, rootKey);
    query.setFilter(new FilterPredicate("name", Query.FilterOperator.EQUAL, filterData));
    // fetch first page
    Cursor cursor = checkPage(query, null, null, onePage, onePage, filterData, filterData);
    Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString());
    // fetch next page
    checkPage(query, decodedCursor, null, onePage, onePage, filterData, filterData);
}
 
Example 3
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndFetch() {
    int onePage = total - 30;
    Query query = new Query(kindName, rootKey);
    // fetch first page
    Cursor cursor = checkPage(query, null, null, onePage, onePage, null, null);
    Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString());
    // fetch next page,   get remaining after 1st page.
    checkPage(query, decodedCursor, null, onePage, total - onePage, null, null);
}
 
Example 4
Source File: CursorTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndCursor() {
    int limit = total / testDat.length;
    Query query = new Query(kindName, rootKey);
    query.addSort("name", Query.SortDirection.ASCENDING);
    // fetch 1st page
    Cursor cursor = checkPage(query, null, null, limit, limit, testDat[0], testDat[0]);
    Cursor decodedCursor = Cursor.fromWebSafeString(cursor.toWebSafeString());
    // fetch 1st page again since using decodedCursor as end cursor
    checkPage(query, null, decodedCursor, limit, limit, testDat[0], testDat[0]);
}
 
Example 5
Source File: OfferEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({"unchecked", "unused"})
@ApiMethod(name = "listOffer")
public CollectionResponse<Offer> listOffer(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

  EntityManager mgr = null;
  Cursor cursor = null;
  List<Offer> execute = null;

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from Offer as Offer");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
      query.setFirstResult(0);
      query.setMaxResults(limit);
    }

    execute = (List<Offer>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null) cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (Offer obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<Offer>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example 6
Source File: RecommendationEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({"unchecked", "unused"})
@ApiMethod(name = "listRecommendation")
public CollectionResponse<Recommendation> listRecommendation(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

  EntityManager mgr = null;
  Cursor cursor = null;
  List<Recommendation> execute = null;

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from Recommendation as Recommendation");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
      query.setFirstResult(0);
      query.setMaxResults(limit);
    }

    execute = (List<Recommendation>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null) cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (Recommendation obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<Recommendation>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example 7
Source File: CheckInEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({"unchecked", "unused"})
@ApiMethod(name = "listCheckIn")
public CollectionResponse<CheckIn> listCheckIn(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

  EntityManager mgr = null;
  Cursor cursor = null;
  List<CheckIn> execute = null;

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from CheckIn as CheckIn");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
      query.setFirstResult(0);
      query.setMaxResults(limit);
    }

    execute = (List<CheckIn>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null) cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (CheckIn obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<CheckIn>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example 8
Source File: DeviceInfoEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({"unchecked", "unused"})
@ApiMethod(name = "listDeviceInfo")
public CollectionResponse<DeviceInfo> listDeviceInfo(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

  EntityManager mgr = null;
  Cursor cursor = null;
  List<DeviceInfo> execute = null;

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from DeviceInfo as DeviceInfo");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
      query.setFirstResult(0);
      query.setMaxResults(limit);
    }

    execute = (List<DeviceInfo>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null) cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (DeviceInfo obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<DeviceInfo>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example 9
Source File: PlaceEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 5 votes vote down vote up
/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({"unchecked", "unused"})
@ApiMethod(name = "listPlace")
public CollectionResponse<Place> listPlace(
    @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) {

  EntityManager mgr = null;
  Cursor cursor = null;
  List<Place> execute = null;

  try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from Place as Place");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
      query.setFirstResult(0);
      query.setMaxResults(limit);
    }

    execute = (List<Place>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null) cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (Place obj : execute);
  } finally {
    mgr.close();
  }

  return CollectionResponse.<Place>builder()
      .setItems(execute).setNextPageToken(cursorString).build();
}
 
Example 10
Source File: MessageEndpoint.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 4 votes vote down vote up
/**
 * This function returns a list of messages starting with the newest message
 * first and in descending order from there
 * 
 * @param cursorString 
 *          for paging, empty for the first request, subsequent requests can
 *          use the returned information from an earlier request to fill this
 *          parameter
 * @param limit
 *          number of results returned for this query
 * @return
 *          A collection of MessageData items
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listMessages")
public CollectionResponse<MessageData> listMessages(
    @Nullable @Named("cursor") String cursorString,
    @Nullable @Named("limit") Integer limit) {

  EntityManager mgr = null;  
  Cursor cursor = null;
  List<MessageData> execute = null;
  
  try {
    mgr = getEntityManager();
    // query for messages, newest message first
    Query query = mgr
        .createQuery("select from MessageData as MessageData order by timestamp desc");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
      query.setFirstResult(0);
      query.setMaxResults(limit);
    }

    execute = (List<MessageData>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null)
      cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (MessageData obj : execute) {
      ;
    }
  } finally {
    mgr.close();
  }

  return CollectionResponse.<MessageData> builder().setItems(execute)
      .setNextPageToken(cursorString).build();
}
 
Example 11
Source File: PushPreProcessingServlet.java    From solutions-ios-push-notification-sample-backend-java with Apache License 2.0 4 votes vote down vote up
private void preprocessBatchOfDevices(String alertMessage, String cursorString) {
  EntityManager mgr = null;
  Cursor cursor = null;

  try {
    mgr = getEntityManager();

    // Retrieve entities (and not just deviceToken property) in order to paginate using cursor
    Query query = mgr.createQuery("select from DeviceRegistration as DeviceRegistration");
    if (cursorString != null && cursorString != "") {
      cursor = Cursor.fromWebSafeString(cursorString);
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    query.setMaxResults(BATCH_SIZE);

    @SuppressWarnings("unchecked")
    List<DeviceRegistration> deviceRegistrations = query.getResultList();
    cursor = JPACursorHelper.getCursor(deviceRegistrations);
    if (cursor != null) {
      cursorString = cursor.toWebSafeString();
    }

    List<String> deviceTokens = new ArrayList<String>();
    for (DeviceRegistration deviceRegistartion : deviceRegistrations) {
      deviceTokens.add(deviceRegistartion.getDeviceToken());
    }

    if (deviceTokens.isEmpty()) {
      // no more matching device tokens matching this query.
      return;
    }

    mgr.getTransaction().begin();

    try {
      PushNotificationUtility.enqueuePushAlert(alertMessage, deviceTokens);

      if (deviceRegistrations.size() == BATCH_SIZE) {
        PushNotificationUtility.continueEnqueueingPushAlertToAllDevices(
            alertMessage, cursorString);
      }

      mgr.getTransaction().commit();
    } catch (RuntimeException e) {
      if (mgr.getTransaction().isActive()) {
        mgr.getTransaction().rollback();
      }
      throw e;
    }

  } finally {
    mgr.close();
  }
}