public void doRun()

in database/src/main/java/com/google/firebase/quickstart/email/WeeklyEmailJob.java [34:69]


    public void doRun() throws JobInterruptException {
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

        // Top 5 Posts in the database, ordered by stars
        // [START top_posts_query]
        Query topPostsQuery = ref.child("posts").orderByChild("starCount").limitToLast(5);
        // [END top_posts_query]

        // All Users
        final DatabaseReference allUsersRef = ref.child("users");

        topPostsQuery.addListenerForSingleValueEvent(new ValueEventListener() {
            public void onDataChange(final DataSnapshot topPostsSnapshot) {
                allUsersRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    public void onDataChange(DataSnapshot allUsersSnapshot) {
                        // Get users and posts as lists
                        Map<String,User> users = allUsersSnapshot.getValue(new GenericTypeIndicator<Map<String, User>>() {});
                        List<Post> posts = topPostsSnapshot.getValue(new GenericTypeIndicator<List<Post>>() {});

                        // Send email to all users about the top 5 posts
                        MyEmailer.sendWeeklyEmail(users, posts);
                    }

                    public void onCancelled(DatabaseError databaseError) {
                        System.out.println("WeeklyEmailJob: could not get all users");
                        System.out.println("WeeklyEmailJob: " + databaseError.getMessage());
                    }
                });
            }

            public void onCancelled(DatabaseError databaseError) {
                System.out.println("WeeklyEmailJob: could not get top posts");
                System.out.println("WeeklyEmailJob: " + databaseError.getMessage());
            }
        });
    }