/*
* 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";
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 {};
var queryCount:Integer = 0;
function search(): Void {
if (queryString == null or queryString.trim().length() == 0) {
return;
}
tagMap.clear();
PhotoRequest {
tag: queryString
queryNum: ++queryCount
}.start();
}
}
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:;
pm.photoUrl = "http:;
pm.tags = null;
TagRequest {
photoID: id
photo: Main.flockr.model.photos[photoNum++]
queryNum: queryNum
}.start();
}
}
}
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;
}
}
}
class PhotoRequest extends HttpRequest {
public-init var tag: String on replace {
location = "http:;
}
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();
}
}
}
class TagRequest extends HttpRequest {
public-init var photoID: String on replace {
location = "http:;
}
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();
}
}
}