com.endeca.portal.data.functions.util
Class QueryFunctionUtils

java.lang.Object
  extended by com.endeca.portal.data.functions.util.QueryFunctionUtils

public final class QueryFunctionUtils
extends java.lang.Object

A set of static utility methods for serializing QueryFunctions as JSON Strings and JSONObjects. Note on Exceptions for the toString/toJSONObject methods: Jackson's checked exceptions have been mapped to run time exceptions, as an exception in this code indicates a programming problem on the part of the QueryFunction implementer or the caller of this code, and not something that should happen as a matter of course. See the following page for a rationale: http://stackoverflow.com/a/322889/21220 The upshot of this is that clients will be less likely to catch and suppress errors around JSON serializing and parsing, and hence we should catch more programming errors.

Author:
huff

Method Summary
static void addSearchAdjustmentConfig(QueryState q)
          Adds a SearchAdjustmentConfig to the QueryState when the QueryState contains a SearchFilter.
static QueryFunction fromJSONObject(org.json.JSONObject fn)
          Utility method for bridging between Jackson based serialization and code which still uses org.json.JSONObject.
static
<T extends QueryFunction>
T
fromJSONObject(org.json.JSONObject fn, java.lang.Class<T> valueType)
          Utility method for bridging between Jackson based serialization and code which still uses org.json.JSONObject.
static QueryFunction fromString(java.lang.String fn)
          Converts a String representation of a QueryFunction to an instance of a QueryFunction.
static
<T extends QueryFunction>
T
fromString(java.lang.String fn, java.lang.Class<T> valueType)
          Converts a String representation of a QueryFunction to a subclass of QueryFunction.
static boolean hasSearchFilter(QueryState q)
          Returns true if the QueryState contains a SearchFilter.
static java.util.List<QueryFunction> listFromString(java.lang.String fns)
          Converts a String representation of an array of QueryFunctions to an instance of a List.
static
<T extends QueryFunction>
java.util.List<T>
listFromString(java.lang.String fns, java.lang.Class<T> itemType)
          Converts a String representation of an array of QueryFunctions to an instance of a List.
static java.lang.String listToString(java.util.List<QueryFunction> fns)
          Converts a potentially heterogenous List into a String version of its JSON Array.
static
<T extends QueryFunction>
java.lang.String
listToString(java.util.List<T> fns, java.lang.Class<T> itemType)
          Converts a homogeneous List into a String version of its JSON Array.
static org.json.JSONObject toJSONObject(QueryFunction fn)
          Utility method for bridging between Jackson based serialization and code which still uses org.json.JSONObject.
static java.lang.String toString(QueryFunction fn)
          Converts a QueryFunction into a String version of its JSON Object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fromString

public static <T extends QueryFunction> T fromString(java.lang.String fn,
                                                     java.lang.Class<T> valueType)
                                          throws java.io.IOException
Converts a String representation of a QueryFunction to a subclass of QueryFunction. The "class" property in the JSON should be an instance of the valueType passed to this function.

Parameters:
fn - the JSON representation of the QueryFunction.
valueType - the expected type of the QueryFunction. This should match or be a parent of the class attribute in the JSON.
Returns:
the parsed QueryFunction.
Throws:
java.io.IOException

fromString

public static QueryFunction fromString(java.lang.String fn)
                                throws java.io.IOException
Converts a String representation of a QueryFunction to an instance of a QueryFunction. The actual type of the parameter is contained in the "class" property of the JSON and will be returned up-casted to a QueryFunction.

Parameters:
fn - the JSON representation of the QueryFunction.
Returns:
the parsed QueryFunction.
Throws:
java.io.IOException

listFromString

public static <T extends QueryFunction> java.util.List<T> listFromString(java.lang.String fns,
                                                                         java.lang.Class<T> itemType)
                                                              throws java.io.IOException
Converts a String representation of an array of QueryFunctions to an instance of a List. The actual type of each List<> item is contained in the "class" property of each array object.

Parameters:
fns - the JSON representation of the array of QueryFunctions.
itemType - the expected type of the items in the Array. This should match or be a parent of each class attribute in the JSON.
Returns:
the parsed List.
Throws:
java.io.IOException

listFromString

public static java.util.List<QueryFunction> listFromString(java.lang.String fns)
                                                    throws java.io.IOException
Converts a String representation of an array of QueryFunctions to an instance of a List. The actual type of each List<> item is contained in the "class" property of each array object and will be up-casted to a QueryFunction.

Parameters:
fns - the JSON representation of the array of QueryFunctions.
Returns:
the parsed List.
Throws:
java.io.IOException

toString

public static java.lang.String toString(QueryFunction fn)
Converts a QueryFunction into a String version of its JSON Object. A "class" property will be added to the JSON Object with the concrete type of the QueryFunction.

Parameters:
fn - the QueryFunction to convert to a JSON Object.
Returns:
a string serialization of the JSON representing the QueryFunction.
Throws:
java.lang.IllegalArgumentException - If the QueryFunction is not JSON serializable

listToString

public static <T extends QueryFunction> java.lang.String listToString(java.util.List<T> fns,
                                                                      java.lang.Class<T> itemType)
Converts a homogeneous List into a String version of its JSON Array. A "class" property will be added to each Array item the concrete type of each QueryFunction.

Parameters:
fns - The list of QueryFunctions to convert to JSON.
itemType - the expected type of the items in the Array.
Returns:
a string serialization of the JSON representing the QueryFunction.
Throws:
java.lang.IllegalArgumentException - If the QueryFunction is not JSON serializable

listToString

public static java.lang.String listToString(java.util.List<QueryFunction> fns)
Converts a potentially heterogenous List into a String version of its JSON Array. A "class" property will be added to each Array item the concrete type of each QueryFunction.

Parameters:
fns - The list of QueryFunctions to convert to JSON.
Returns:
a string serialization of the JSON representing the QueryFunction.

fromJSONObject

public static <T extends QueryFunction> T fromJSONObject(org.json.JSONObject fn,
                                                         java.lang.Class<T> valueType)
                                              throws java.io.IOException
Utility method for bridging between Jackson based serialization and code which still uses org.json.JSONObject. Works by converting the JSONObject to a string and then parsing using Jackson.

Throws:
java.io.IOException

fromJSONObject

public static QueryFunction fromJSONObject(org.json.JSONObject fn)
                                    throws java.io.IOException
Utility method for bridging between Jackson based serialization and code which still uses org.json.JSONObject. Works by converting the JSONObject to a string and then parsing using Jackson.

Throws:
java.io.IOException

toJSONObject

public static org.json.JSONObject toJSONObject(QueryFunction fn)
Utility method for bridging between Jackson based serialization and code which still uses org.json.JSONObject. Works by serializing the QueryFunction to a string using Jackson and then parses with JSONObject.

Throws:
java.lang.IllegalArgumentException - If the QueryFunction is not JSON serializable

addSearchAdjustmentConfig

public static void addSearchAdjustmentConfig(QueryState q)
Adds a SearchAdjustmentConfig to the QueryState when the QueryState contains a SearchFilter.


hasSearchFilter

public static boolean hasSearchFilter(QueryState q)
Returns true if the QueryState contains a SearchFilter.