Random selection

From: Jim Menard (jim..o.com)
Date: Sat Dec 20 2003 - 09:48:43 EST

  • Next message: Mike Kienenberger: "Re: Random selection"

    I have an album of photos, and I want to select a random photo to
    display. I figured I could use setFetchingDataRows, select the entire
    list of photos, then randomly select one and call objectFromDataRow to
    "flesh out" the photo.

    I started with this code, then got stuck at the place marked "????"

    public Photo getRandomPhoto() {
            Photo photo = null;
         SelectQuery q = new SelectQuery(Photo.class);
         q.setFetchingDataRows(true); // Don't create and register whole
    object
            // ???? How do I specify the query that selects the album's photos?
         List list = getContext().performQuery(q);

         if (list.size() > 0) {
             Map row = (Map)list.get(new Random().nextInt(list.size()));
             photo = (Photo)getContext().objectFromDataRow("Photo", row);
         }
            return photo;
    }

    Cayenne normally performs magic to retrieve the list of photos. How do
    I do it myself? Alternately, how can I tell the album to use
    setFetchingDataRows when it selects the list of photos?

    Finally, is this an unnecessary optimization? Should I not worry about
    reading all of the photos into memory and just call album.getPhotos()?

    Thank you for your help.

    Jim

    -- 
    Jim Menard, jim..o.com, http://www.io.com/~jimm/
    "Dvorak users of the world flgkd!" -- Kirsten Chevalier in 
    rec.humor.oracle.d
    



    This archive was generated by hypermail 2.0.0 : Sat Dec 20 2003 - 09:48:50 EST