/** * Demonstrates how to use the cursor to be able to pass over an complete set of data * * @param pm Persistence manager instance to use - let open at the end to allow possible object * updates later * @param cursorString Representation of a cursor, to be used to get the next set of data to * process * @param range Number of data to process at this particular stage * @param defaultSource Value to apply to any Demand instance without a default value * @return New representation of the cursor, ready for a next process call */ @SuppressWarnings("unchecked") public static String updateSource( PersistenceManager pm, String cursorString, int range, Source defaultSource) { Query query = null; try { query = pm.newQuery(Demand.class); if (cursorString != null) { Map<String, Object> extensionMap = new HashMap<String, Object>(); extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, Cursor.fromWebSafeString(cursorString)); query.setExtensions(extensionMap); } query.setRange(0, range); List<Demand> results = (List<Demand>) query.execute(); if (results.iterator().hasNext()) { for (Demand demand : results) { // Initialize the new field if necessary. By checking first that the field is null, we // allow this migration to be safely run multiple times. if (demand.getSource() == null) { demand.setSource(defaultSource); pm.makePersistent(demand); } } cursorString = JDOCursorHelper.getCursor(results).toWebSafeString(); } else { // no results cursorString = null; } } finally { query.closeAll(); } return cursorString; }
/** * 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 = "listUser") public CollectionResponse<User> listUser( @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) { PersistenceManager mgr = null; Cursor cursor = null; List<User> execute = null; try { mgr = getPersistenceManager(); Query query = mgr.newQuery(User.class); if (cursorString != null && cursorString != "") { cursor = Cursor.fromWebSafeString(cursorString); HashMap<String, Object> extensionMap = new HashMap<String, Object>(); extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor); query.setExtensions(extensionMap); } if (limit != null) { query.setRange(0, limit); } execute = (List<User>) query.execute(); cursor = JDOCursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString(); // Tight loop for fetching all entities from datastore and accomodate // for lazy fetch. for (User obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<User>builder() .setItems(execute) .setNextPageToken(cursorString) .build(); }