use of org.json.simple.parser.JSONParser in project spatial-portal by AtlasOfLivingAustralia.
the class BiocacheQuery method retrieveCustomFields.
private List<String> retrieveCustomFields() {
List<String> customFields = new ArrayList<String>();
//look up facets
final String jsonUri = biocacheServer + "/upload/dynamicFacets?q=" + getFullQ(true) + "&qc=" + getQc();
try {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(jsonUri);
get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
client.executeMethod(get);
String slist = get.getResponseBodyAsString();
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(slist);
for (Object arrayElement : ja) {
JSONObject jsonObject = (JSONObject) arrayElement;
String facetName = jsonObject.get(StringConstants.NAME).toString();
if (!facetName.endsWith("_RNG")) {
customFields.add(facetName);
}
}
} catch (Exception e) {
LOGGER.error("error loading custom facets for: " + jsonUri, e);
}
return customFields;
}
use of org.json.simple.parser.JSONParser in project spatial-portal by AtlasOfLivingAustralia.
the class BiocacheQuery method endemicSpeciesList.
public String endemicSpeciesList() {
if (endemicSpeciesList != null) {
return endemicSpeciesList;
}
if (CommonData.getSettings().containsKey("endemic.sp.method") && CommonData.getSettings().getProperty("endemic.sp.method").equals("true")) {
String speciesList = speciesList();
//can get species list counts as "kosher:true" or "kosher:*" only
Map speciesCounts;
if (getGeospatialKosher()[1]) {
//[1] is 'include kosher:false'
speciesCounts = CommonData.getSpeciesListCounts(false);
} else {
speciesCounts = CommonData.getSpeciesListCountsKosher(false);
}
StringBuilder sb = new StringBuilder();
int speciesCol = 0;
int countCol = 11;
try {
CSVReader csv = new CSVReader(new StringReader(speciesList));
String[] row;
int currentPos = 0;
int nextPos = speciesList.indexOf('\n', currentPos + 1);
//header
sb.append(speciesList.substring(currentPos, nextPos));
//header
csv.readNext();
while ((row = csv.readNext()) != null) {
//add if species is not present elsewhere
Long c = (Long) speciesCounts.get(row[speciesCol]);
if (c != null && c <= Long.parseLong(row[countCol])) {
if (nextPos > speciesList.length()) {
nextPos = speciesList.length();
}
sb.append(speciesList.substring(currentPos, nextPos));
} else if (c == null) {
LOGGER.error("failed to find species_guid: " + row[speciesCol] + " in CommonData.getSpeciesListCounts()");
}
currentPos = nextPos;
nextPos = speciesList.indexOf('\n', currentPos + 1);
}
} catch (Exception e) {
LOGGER.error("failed generating endemic species list", e);
}
endemicSpeciesList = sb.toString();
} else {
forMapping = true;
if (paramId == null)
makeParamId();
HttpClient client = new HttpClient();
String url = biocacheServer + ENDEMIC_LIST + paramId + "?facets=names_and_lsid";
LOGGER.debug(url);
GetMethod get = new GetMethod(url);
try {
client.executeMethod(get);
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(get.getResponseBodyAsString());
//extract endemic matches from the species list
String speciesList = speciesList();
StringBuilder sb = new StringBuilder();
int idx = speciesList.indexOf('\n');
if (idx > 0) {
sb.append(speciesList.substring(0, idx));
}
for (int j = 0; j < ja.size(); j++) {
JSONObject jo = (JSONObject) ja.get(j);
if (jo.containsKey("label")) {
idx = speciesList.indexOf("\n" + jo.get("label") + ",");
if (idx > 0) {
int lineEnd = speciesList.indexOf('\n', idx + 1);
if (lineEnd < 0)
lineEnd = speciesList.length();
sb.append(speciesList.substring(idx, lineEnd));
}
}
}
endemicSpeciesList = sb.toString();
} catch (Exception e) {
LOGGER.error("error getting endemic species result", e);
}
}
return endemicSpeciesList;
}
use of org.json.simple.parser.JSONParser in project spatial-portal by AtlasOfLivingAustralia.
the class RemoteLogger method getLogCSV.
public JSONObject getLogCSV() {
init();
try {
if (Util.isLoggedIn()) {
String url = loggerService + "/app/types/tool.json?" + "email=" + URLEncoder.encode(Util.getUserEmail(), StringConstants.UTF_8) + "&appid=" + URLEncoder.encode(appid, StringConstants.UTF_8) + "&api_key=" + URLEncoder.encode(CommonData.getSettings().getProperty("api_key"), StringConstants.UTF_8);
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(url);
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
client.executeMethod(get);
LOGGER.debug("get: " + url + ", response: " + get.getResponseBodyAsString());
JSONParser jp = new JSONParser();
return (JSONObject) jp.parse(get.getResponseBodyAsString());
}
} catch (Exception e) {
LOGGER.error("Error getting logging information from server:", e);
}
return null;
}
use of org.json.simple.parser.JSONParser in project spatial-portal by AtlasOfLivingAustralia.
the class RemoteLogger method getLogItem.
public JSONObject getLogItem(String logId) {
init();
try {
if (Util.isLoggedIn()) {
String url = loggerService + "/app/view/" + logId + ".json" + "?appid=" + URLEncoder.encode(appid, StringConstants.UTF_8) + "&api_key=" + URLEncoder.encode(CommonData.getSettings().getProperty("api_key"), StringConstants.UTF_8);
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(url);
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
client.executeMethod(get);
LOGGER.debug("get: " + url + ", response: " + get.getResponseBodyAsString());
JSONParser jp = new JSONParser();
return (JSONObject) jp.parse(get.getResponseBodyAsString());
}
} catch (Exception e) {
LOGGER.error("Error getting logging information from server:", e);
}
return null;
}
use of org.json.simple.parser.JSONParser in project spatial-portal by AtlasOfLivingAustralia.
the class Sampling method getDownloadUrl.
static String getDownloadUrl(String statusUrl) {
String downloadUrl = null;
try {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(statusUrl);
get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
client.executeMethod(get);
JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject) jp.parse(get.getResponseBodyAsString());
if ("finished".equals(jo.get(StringConstants.STATUS))) {
downloadUrl = jo.get("downloadUrl").toString();
} else if ("cancelled".equals(jo.get(StringConstants.STATUS)) || "error".equals(jo.get(StringConstants.STATUS))) {
downloadUrl = null;
} else {
downloadUrl = "";
}
} catch (Exception e) {
LOGGER.error("error getting response from : " + statusUrl, e);
}
return downloadUrl;
}
Aggregations