I spent few hours to find nice example for
"com.google.appengine.api.datastore.Cursor".
Requirement is simple, for each gwt rpc call return one record along
with encoded cursor (similar to oracle pl/sql cursor).
On subsequent calls, decode cursor and reposition preparedquery to
fetch next record.
here is the sample code.
=============================================================
public static Question getNextQuestion(String encodedCursor,
                        String tableName) {

                DatastoreService ds = 
DatastoreServiceFactory.getDatastoreService();
                Query query = new Query(tableName);
                PreparedQuery pq = ds.prepare(query);
                // FetchOptions fetchOptions = Builder.withDefaults();
                FetchOptions fetchOptions = FetchOptions.Builder.withLimit(1);
                QueryResultList<Entity> nextBatch = null;
                String encodedCursortmp = null;
                Cursor cursor = null;
                if (encodedCursor == null) {
                        nextBatch = pq.asQueryResultList(fetchOptions);
                        cursor = nextBatch.getCursor();
                        encodedCursortmp = cursor.toWebSafeString();
                } else {
                        Cursor decodedCursor = 
Cursor.fromWebSafeString(encodedCursor);
                        nextBatch = pq.asQueryResultList(fetchOptions
                                        .startCursor(decodedCursor));
                        // pq.asQueryResultList(fetchOptions).getCursor();
                        cursor = nextBatch.getCursor();
                        encodedCursortmp = cursor.toWebSafeString();
                }
                //if (nextBatch.size() > 0 )
                Entity e = nextBatch.get(0);

                Question qs = new Question();
                if (e != null) {
                        if (e.getProperty("question") != null)
                                
qs.setQuestion(e.getProperty("question").toString());
                        if (e.getProperty("answera") != null)
                                
qs.setAnswera(e.getProperty("answera").toString());
                        if (e.getProperty("answerb") != null)
                                
qs.setAnswerb(e.getProperty("answerb").toString());
                        if (e.getProperty("answerc") != null)
                                
qs.setAnswerc(e.getProperty("answerc").toString());
                        if (e.getProperty("answerd") != null)
                                
qs.setAnswerc(e.getProperty("answerd").toString());
                        if (e.getProperty("correctanswer") != null)
                                
qs.setQuestion(e.getProperty("correctanswer").toString());
                        qs.setQuestionNumber(e.getKey().getId());
                }
                if (encodedCursortmp != null)
                        qs.setEncodedCursor(encodedCursortmp);
                else
                        qs.setEncodedCursor(encodedCursor);
                return qs;
        }

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to