Class JSONResultsIterator
- java.lang.Object
-
- org.intermine.webservice.server.output.JSONResultsIterator
-
- All Implemented Interfaces:
java.util.Iterator<org.json.JSONObject>
public class JSONResultsIterator extends java.lang.Object implements java.util.Iterator<org.json.JSONObject>
A class to to produce a sequence of JSONObjects from a set of database rows. This requires that the view be set up in a specific way.- Author:
- Alexis Kalderimis
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
currentArray
protected java.util.Map<java.lang.String,java.lang.Object>
currentMap
-
Constructor Summary
Constructors Constructor Description JSONResultsIterator(ExportResultsIterator it)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addCollectionToCurrentNode(Path collectionPath)
Adds a new list, representing a collection to the current node (map)protected void
addFieldToMap(ResultElement cell, Path column, java.util.Map<java.lang.String,java.lang.Object> objectMap)
Adds the attributes contained in the cell to the map given.protected void
addReferencedCellToJsonMap(ResultElement cell, Path column, java.util.Map<java.lang.String,java.lang.Object> objectMap)
Adds the information from a cell representing a reference to the map given.protected void
addReferenceToCurrentNode(Path referencePath)
Adds an intermediate reference to the current node.protected boolean
aDescendsFromB(java.lang.String a, java.lang.String b)
Test whether a class named "a" descends from a class named "b".protected boolean
aIsaB(java.lang.String a, java.lang.String b)
Test whether a class named "A" is, or descends from, a class named "B".boolean
hasNext()
protected boolean
isCellValidForPath(ResultElement cell, Path path)
Test whether a result element matches the type of its path.org.json.JSONObject
next()
void
remove()
Remove is not supported.protected void
setCurrentMapFromCurrentArray()
Sets the current map to work with by getting the last one from the current array.protected void
setCurrentMapFromCurrentArray(ResultElement cell)
Finds the object we should be dealing with by getting it from the current array.protected void
setOrCheckClass(ResultElement cell, Path path, java.util.Map<java.lang.String,java.lang.Object> jsonMap)
Set the class, or if one is already set on the map, check that this one is valid for it.protected void
setOrCheckClassAndId(ResultElement cell, Path path, java.util.Map<java.lang.String,java.lang.Object> jsonMap)
Sets the basic information (class and objectId) on the jsonMap provided.protected void
setOrCheckId(ResultElement cell, java.util.Map<java.lang.String,java.lang.Object> jsonMap)
Set the id, or if one is already set, check that it matches the one on the map.
-
-
-
Constructor Detail
-
JSONResultsIterator
public JSONResultsIterator(ExportResultsIterator it)
Constructor. The JSON Iterator sits on top of the basic export results iterator.- Parameters:
it
- An ExportResultsIterator
-
-
Method Detail
-
hasNext
public boolean hasNext()
- Specified by:
hasNext
in interfacejava.util.Iterator<org.json.JSONObject>
-
next
public org.json.JSONObject next()
- Specified by:
next
in interfacejava.util.Iterator<org.json.JSONObject>
-
isCellValidForPath
protected boolean isCellValidForPath(ResultElement cell, Path path)
Test whether a result element matches the type of its path.- Parameters:
cell
- The Result elementpath
- The path which represents the view column- Returns:
- true if the cell is null, or contains null information, extends/implements the type of path.
-
aIsaB
protected boolean aIsaB(java.lang.String a, java.lang.String b)
Test whether a class named "A" is, or descends from, a class named "B".- Parameters:
a
- The name of a classb
- The name of a class- Returns:
- True if a isa b
- Throws:
java.lang.IllegalArgumentException
- if the names are not valid class names
-
aDescendsFromB
protected boolean aDescendsFromB(java.lang.String a, java.lang.String b)
Test whether a class named "a" descends from a class named "b".- Parameters:
a
- the name of a classb
- the name of a class- Returns:
- True if a descends from b
- Throws:
JSONFormattingException
- if we can't get the super classes for a
-
setOrCheckClassAndId
protected void setOrCheckClassAndId(ResultElement cell, Path path, java.util.Map<java.lang.String,java.lang.Object> jsonMap)
Sets the basic information (class and objectId) on the jsonMap provided. If the map already has values, it makes sure that these are compatible with those of the result element given.- Parameters:
cell
- The result elementpath
- The path representing the columnjsonMap
- The map to put the field on- Throws:
JSONFormattingException
- if the details do not match
-
setOrCheckClass
protected void setOrCheckClass(ResultElement cell, Path path, java.util.Map<java.lang.String,java.lang.Object> jsonMap)
Set the class, or if one is already set on the map, check that this one is valid for it.- Parameters:
cell
- The result elementpath
- The path it representsjsonMap
- The map to set it onto
-
setOrCheckId
protected void setOrCheckId(ResultElement cell, java.util.Map<java.lang.String,java.lang.Object> jsonMap)
Set the id, or if one is already set, check that it matches the one on the map.- Parameters:
cell
- The result element.jsonMap
- The map to set it onto,
-
addFieldToMap
protected void addFieldToMap(ResultElement cell, Path column, java.util.Map<java.lang.String,java.lang.Object> objectMap)
Adds the attributes contained in the cell to the map given. It will not set the id attribute, as that is handled by setOrCheckClassAndId.- Parameters:
cell
- The result elementcolumn
- The path representing the view columnobjectMap
- The map to put the values on- Throws:
JSONFormattingException
- if the map already has values for this attribute and they are different to the ones in the cell
-
addReferencedCellToJsonMap
protected void addReferencedCellToJsonMap(ResultElement cell, Path column, java.util.Map<java.lang.String,java.lang.Object> objectMap)
Adds the information from a cell representing a reference to the map given.- Parameters:
cell
- A cell representing the end of a trail of references.column
- The view column.objectMap
- The map to put the nested trail of object onto- Throws:
JSONFormattingException
- if the paths that make up the trail contain one that is not an attribute, collection or reference (the only known types at present)
-
setCurrentMapFromCurrentArray
protected void setCurrentMapFromCurrentArray(ResultElement cell)
Finds the object we should be dealing with by getting it from the current array. A search is made by looking for a map which has the objectId attribute set to the same value as the result element we have.- Parameters:
cell
- A result element- Throws:
JSONFormattingException
- if the current array is empty.
-
setCurrentMapFromCurrentArray
protected void setCurrentMapFromCurrentArray()
Sets the current map to work with by getting the last one from the current array.- Throws:
JSONFormattingException
- if the array is null, or empty
-
addReferenceToCurrentNode
protected void addReferenceToCurrentNode(Path referencePath)
Adds an intermediate reference to the current node.- Parameters:
referencePath
- The path representing the reference.- Throws:
JSONFormattingException
- if the node has this key set to an incompatible value.
-
addCollectionToCurrentNode
protected void addCollectionToCurrentNode(Path collectionPath)
Adds a new list, representing a collection to the current node (map)- Parameters:
collectionPath
- The path representing the collection.- Throws:
JSONFormattingException
- if the current node is not initialised, or is already set with an incompatible value.
-
remove
public void remove()
Remove is not supported.- Specified by:
remove
in interfacejava.util.Iterator<org.json.JSONObject>
-
-