com.sonyericsson.hudson.plugins.gerrit.gerritevents
Class GerritJsonEventFactory

java.lang.Object
  extended by com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritJsonEventFactory

public final class GerritJsonEventFactory
extends Object

Contains utility methods for handling JSONObjects.

Author:
Robert Sandell <robert.sandell@sonyericsson.com>

Method Summary
static GerritJsonEvent getEvent(net.sf.json.JSONObject jsonObject)
          Creates a GerritJsonEvent DTO out of the provided JSONObject.
static GerritJsonEvent getEventIfInteresting(String jsonString)
          Tries to parse the provided string into a GerritJsonEvent DTO if it is interesting and usable.
static net.sf.json.JSONObject getJsonObjectIfInterestingAndUsable(String jsonString)
          Tries to parse the provided string into a JSONObject and returns it if it is interesting and usable.
static String getString(net.sf.json.JSONObject json, String key)
          Returns the value of a JSON property as a String if it exists otherwise returns null.
static String getString(net.sf.json.JSONObject json, String key, String defaultValue)
          Returns the value of a JSON property as a String if it exists otherwise returns the defaultValue.
static boolean isInterestingAndUsable(String jsonString)
          Tells if the provided string is a valid JSON string and represents an interesting and usable GerritJsonEvent as defined by getJsonObjectIfInterestingAndUsable(java.lang.String).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getEvent

public static GerritJsonEvent getEvent(net.sf.json.JSONObject jsonObject)
Creates a GerritJsonEvent DTO out of the provided JSONObject. The jsonObject is assumed to be interesting and usable as defined by getJsonObjectIfInterestingAndUsable(java.lang.String)

Parameters:
jsonObject - the parsed JSON Object
Returns:
the POJO DTO representation of the jsonObject.

isInterestingAndUsable

public static boolean isInterestingAndUsable(String jsonString)
Tells if the provided string is a valid JSON string and represents an interesting and usable GerritJsonEvent as defined by getJsonObjectIfInterestingAndUsable(java.lang.String). This is the same as doing (getJsonObjectIfInterestingAndUsable(jsonString) != null)

Parameters:
jsonString - the JSON formatted String.
Returns:
true if it is so.

getJsonObjectIfInterestingAndUsable

public static net.sf.json.JSONObject getJsonObjectIfInterestingAndUsable(String jsonString)
Tries to parse the provided string into a JSONObject and returns it if it is interesting and usable. If it is interesting is determined by:
  1. The object contains a String field named type
  2. The String returns a non null GerritEventType from GerritEventType.findByTypeValue(java.lang.String)
  3. The property GerritEventType.isInteresting() == true
It is usable if the type's GerritEventType.getEventRepresentative() is not null.

Parameters:
jsonString - the string to parse.
Returns:
an interesting and usable JSONObject, or null if it is not.

getEventIfInteresting

public static GerritJsonEvent getEventIfInteresting(String jsonString)
Tries to parse the provided string into a GerritJsonEvent DTO if it is interesting and usable.

Parameters:
jsonString - the JSON formatted string.
Returns:
the Event.
See Also:
getJsonObjectIfInterestingAndUsable(String)

getString

public static String getString(net.sf.json.JSONObject json,
                               String key,
                               String defaultValue)
Returns the value of a JSON property as a String if it exists otherwise returns the defaultValue.

Parameters:
json - the JSONObject to check.
key - the key
defaultValue - the value to return if the key is missing
Returns:
the value for the key as a string.

getString

public static String getString(net.sf.json.JSONObject json,
                               String key)
Returns the value of a JSON property as a String if it exists otherwise returns null. Same as calling getString(net.sf.json.JSONObject, java.lang.String, java.lang.String) with null as defaultValue.

Parameters:
json - the JSONObject to check.
key - the key
Returns:
the value for the key as a string.


Copyright © 2004-2012 Hudson. All Rights Reserved.