// Path: src/main/java/com/googlecode/batchfb/Later.java
// public interface Later<T> {
// 	/**
// 	 * <p>Get the value, triggering execution of the batch if necessary.  Once the batch
// 	 * has been executed, this method can be called repeatedly without incurring further
// 	 * calls to Facebook or triggering the execution of any subsequently created batches.
// 	 * It is as efficient as a simple value getter.</p>
// 	 * 
// 	 * <p>If the Facebook call produced an error, repeated calls to this method will produce
// 	 * the same exception.  BatchFB will *not* retry a Facebook call; you must create a
// 	 * new Later<?> object from the FacebookBatcher class.</p>
// 	 * 
// 	 * @throws FacebookException if anything went wrong with the Facebook interaction
// 	 */
// 	T get() throws FacebookException;
// }
// 
// Path: src/main/java/com/googlecode/batchfb/Request.java
// public class Request<T> extends LaterWrapper<T, T> {
// 	
// 	private String name;
// 	
// 	public Request(Later<T> source) {
// 		super(source);
// 	}
// 
// 	/**
// 	 * A name that might be referenced later in other requests in the same batch.
// 	 */
// 	public String getName() {
// 		return this.name;
// 	}
// 	
// 	/**
// 	 * Set an explicit name that can be referenced later in other requests in the same batch.
// 	 * This works within multiquery and within graph calls, but you can't use names across
// 	 * the two types of batches.
// 	 * 
// 	 * This method can be chained.
// 	 */
// 	public Request<T> setName(String value) {
// 		this.name = value;
// 		return this;
// 	}
// }
// 
// Path: src/main/java/com/googlecode/batchfb/util/LaterWrapper.java
// public class LaterWrapper<K, V> implements Later<V>
// {
// 	private Later<K> orig;
// 	private V cached;
// 	
// 	public LaterWrapper(Later<K> orig)
// 	{
// 		this.orig = orig;
// 	}
// 
// 	@Override
// 	public V get() throws FacebookException
// 	{
// 		if (this.cached == null)
// 			this.cached = this.convert(this.orig.get());
// 		
// 		return this.cached;
// 	}
// 	
// 	/** Override this; default is just to pass through which will be unsafe if types are incompatible */
// 	@SuppressWarnings("unchecked")
// 	protected V convert(K data)
// 	{
// 		return (V)data;
// 	}
// }


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.googlecode.batchfb.Later;
import com.googlecode.batchfb.Request;
import com.googlecode.batchfb.util.LaterWrapper;

/*
 * Copyright (c) 2010 Jeff Schnitzer.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.batchfb.impl;


/**
 * <p>Knows how to get the JsonNode for a particular query request out of a multiquery.</p>
 * 
 * <p>Sadly, this is what a multiquery result looks like:</p>
<pre>
[
  {
    "name": "query1",
    "fql_result_set": [ { "uid": 503702723 } ]
  },
  {
    "name": "query2",
    "fql_result_set": [ { "uid": 503702723 } ]
  }
]
</pre>
 * <p>We'd like to use positional indexing but facebook reorders the results.  So we basically need to
 * scan through and look for our node.  LAME.</p>
 * 
 * <p>Because we need to know the name, the Request<?> (which holds the final name) must be set
 * after this object is constructed but before it is executed.  It can't be passed in on construction
 * because the QueryNodeExtractor gets passed in (through a chain of wrappers) to the request.
 * 
 * <p>More info available here:
 * https://developers.facebook.com/docs/reference/rest/fql.multiquery/
 * </p>
 */
public class QueryNodeExtractor extends LaterWrapper<JsonNode, JsonNode>
{
	Request<?> request;

	/**
	 * @param multiqueryResult should be the graph selection of a MultiqueryRequest
	 */
	public QueryNodeExtractor(Later<JsonNode> multiqueryResult) {
		super(multiqueryResult);
	}
	
	/**
	 * Sets the request object related to this query.  The request provides the name
	 * that we use to extract the result.  This creates a convoluted construction
	 * process because the Request will actually contain a reference (buried in a chain
	 * of wrappers) to this QueryNodeExtractor.  This method must be called before
	 * execution. 
	 */

// Path: src/main/java/com/googlecode/batchfb/Later.java
// public interface Later<T> {
// 	/**
// 	 * <p>Get the value, triggering execution of the batch if necessary.  Once the batch
// 	 * has been executed, this method can be called repeatedly without incurring further
// 	 * calls to Facebook or triggering the execution of any subsequently created batches.
// 	 * It is as efficient as a simple value getter.</p>
// 	 * 
// 	 * <p>If the Facebook call produced an error, repeated calls to this method will produce
// 	 * the same exception.  BatchFB will *not* retry a Facebook call; you must create a
// 	 * new Later<?> object from the FacebookBatcher class.</p>
// 	 * 
// 	 * @throws FacebookException if anything went wrong with the Facebook interaction
// 	 */
// 	T get() throws FacebookException;
// }
// 
// Path: src/main/java/com/googlecode/batchfb/Request.java
// public class Request<T> extends LaterWrapper<T, T> {
// 	
// 	private String name;
// 	
// 	public Request(Later<T> source) {
// 		super(source);
// 	}
// 
// 	/**
// 	 * A name that might be referenced later in other requests in the same batch.
// 	 */
// 	public String getName() {
// 		return this.name;
// 	}
// 	
// 	/**
// 	 * Set an explicit name that can be referenced later in other requests in the same batch.
// 	 * This works within multiquery and within graph calls, but you can't use names across
// 	 * the two types of batches.
// 	 * 
// 	 * This method can be chained.
// 	 */
// 	public Request<T> setName(String value) {
// 		this.name = value;
// 		return this;
// 	}
// }
// 
// Path: src/main/java/com/googlecode/batchfb/util/LaterWrapper.java
// public class LaterWrapper<K, V> implements Later<V>
// {
// 	private Later<K> orig;
// 	private V cached;
// 	
// 	public LaterWrapper(Later<K> orig)
// 	{
// 		this.orig = orig;
// 	}
// 
// 	@Override
// 	public V get() throws FacebookException
// 	{
// 		if (this.cached == null)
// 			this.cached = this.convert(this.orig.get());
// 		
// 		return this.cached;
// 	}
// 	
// 	/** Override this; default is just to pass through which will be unsafe if types are incompatible */
// 	@SuppressWarnings("unchecked")
// 	protected V convert(K data)
// 	{
// 		return (V)data;
// 	}
// }


// Path: src/main/java/com/googlecode/batchfb/impl/QueryNodeExtractor.java
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.googlecode.batchfb.Later;
import com.googlecode.batchfb.Request;
import com.googlecode.batchfb.util.LaterWrapper;

/*
 * Copyright (c) 2010 Jeff Schnitzer.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.batchfb.impl;


/**
 * <p>Knows how to get the JsonNode for a particular query request out of a multiquery.</p>
 * 
 * <p>Sadly, this is what a multiquery result looks like:</p>
<pre>
[
  {
    "name": "query1",
    "fql_result_set": [ { "uid": 503702723 } ]
  },
  {
    "name": "query2",
    "fql_result_set": [ { "uid": 503702723 } ]
  }
]
</pre>
 * <p>We'd like to use positional indexing but facebook reorders the results.  So we basically need to
 * scan through and look for our node.  LAME.</p>
 * 
 * <p>Because we need to know the name, the Request<?> (which holds the final name) must be set
 * after this object is constructed but before it is executed.  It can't be passed in on construction
 * because the QueryNodeExtractor gets passed in (through a chain of wrappers) to the request.
 * 
 * <p>More info available here:
 * https://developers.facebook.com/docs/reference/rest/fql.multiquery/
 * </p>
 */
public class QueryNodeExtractor extends LaterWrapper<JsonNode, JsonNode>
{
	Request<?> request;

	/**
	 * @param multiqueryResult should be the graph selection of a MultiqueryRequest
	 */
	public QueryNodeExtractor(Later<JsonNode> multiqueryResult) {
		super(multiqueryResult);
	}
	
	/**
	 * Sets the request object related to this query.  The request provides the name
	 * that we use to extract the result.  This creates a convoluted construction
	 * process because the Request will actually contain a reference (buried in a chain
	 * of wrappers) to this QueryNodeExtractor.  This method must be called before
	 * execution. 
	 */

	public void setRequest(Request<?> req) {
