License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package photoflockr;

import javafx.io.http.HttpRequest;
import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import javafx.data.xml.QName;
import java.util.Map;

def apiKey: String = "c8e56e2c73b31a14061f0981baa23a2a";

/**
 * This class is used to initiate HTTP requests to fetch pictures from the Flickr.
 * Requests are made on typing in the tag search field.
 */
public class Model {
    public var maxCount:Integer;
    public var tagMap:Map;
    public var queryString:String = "Flower" on replace { search(); }
    public var photos:PhotoModel[] = for (i in [ 1..maxCount ]) PhotoModel {};
    
    /* Every request is counted. The counter is used to detect if a subsequent request is made.
     * In such a case the previos request processing is interrupted
     */
    var queryCount:Integer = 0;

    // The function is called on typing in the tag search field (see Main.fx).
    function search(): Void {
        if (queryString == null or queryString.trim().length() == 0) {
            return;
        }
        tagMap.clear();
        PhotoRequest {
            tag: queryString
            queryNum: ++queryCount
        }.start();
    }
}

/**
 * Parses XML data and retrieves URLs of a small and a large image
 * for every fetched picture.
 */
class PhotoParser extends PullParser {
    public-init var queryNum: Integer;

    var photoNum = 0;

    public override var onEvent = function(event: Event) {
        if (queryNum != Main.flockr.model.queryCount) {
            input.close();
            return;
        }
        if (event.type == PullParser.END_ELEMENT and
            event.level == 2 and
            event.qname.name == "photo" and
            photoNum < Main.flockr.BOID_COUNT)
        {
            def id = event.getAttributeValue(QName {name: "id"});
            def secret = event.getAttributeValue(QName {name: "secret"});
            def server = event.getAttributeValue(QName {name: "server"});
            def farm = event.getAttributeValue(QName {name: "farm"});

            var pm = Main.flockr.model.photos[photoNum];
            pm.thumbUrl = "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}_s.jpg";
            pm.photoUrl = "http://farm{farm}.static.flickr.com/{server}/{id}_{secret}.jpg";
            pm.tags = null;

            TagRequest {
                photoID: id
                photo: Main.flockr.model.photos[photoNum++]
                queryNum: queryNum
            }.start();
        }
    }
}

/**
 * Parses XML data and retieves the tag list for every fetched picture.
 */
class TagParser extends PullParser {
    public-read var tags: String[];
    public-init var queryNum: Integer;

    def TAG_MAX_COUNT = 30;
    var tagNum = 0;

    public override var onEvent = function(event: Event) {
        if (queryNum != Main.flockr.model.queryCount) {
            input.close();
            return;
        }
        if (event.type == PullParser.END_ELEMENT and
            event.level == 3 and
            event.qname.name == "tag" and
            tagNum++ < TAG_MAX_COUNT)
        {
            insert (event.text as String).trim() into tags;
        }
    }
}

/**
 * Performs an HTTP request for fetching photos XML data.
 */
class PhotoRequest extends HttpRequest {
    public-init var tag: String on replace {
        location = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={apiKey}&tags={tag}&per_page=15&page=1";
    }
    public-init var queryNum: Integer;

    override var method = HttpRequest.GET;

    public override var onInput = function(input: java.io.InputStream) {
        try {
            if (queryNum == Main.flockr.model.queryCount) {
                PhotoParser {
                    input: input
                    queryNum: queryNum
                }.parse();
            }
        } finally {
            input.close();
        }
    }
}

/**
 * Performs an HTTP request for fetching tags XML data.
 */
class TagRequest extends HttpRequest {
    public-init var photoID: String on replace {
        location = "http://api.flickr.com/services/rest/?method=flickr.tags.getListPhoto&api_key={apiKey}&photo_id={photoID}";
    }
    public-init var photo: PhotoModel;
    public-init var queryNum: Integer;

    override var method = HttpRequest.GET;

    public override var onInput = function(input: java.io.InputStream) {
        try {
            if (queryNum == Main.flockr.model.queryCount) {
                var parser = TagParser {
                    input: input
                    queryNum: queryNum
                }
                parser.parse();
                photo.tags = parser.tags;
            }
        } finally {
            input.close();
        }
    }
}