use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.
the class GazetteerAutoComplete method refresh.
/**
* Refresh comboitem based on the specified value.
*/
private void refresh(String val) {
String searchString = val.trim().replaceAll("\\s+", "+");
searchString = (searchString.isEmpty()) ? "a" : searchString;
try {
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(CommonData.getLayersServer() + "/search?limit=40&q=" + searchString + "&userObjects=false");
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
client.executeMethod(get);
String slist = get.getResponseBodyAsString();
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(slist);
if (ja == null) {
return;
}
Iterator it = getItems().iterator();
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
String itemString = jo.get(StringConstants.NAME).toString();
String description = (jo.containsKey(StringConstants.DESCRIPTION) ? jo.get(StringConstants.DESCRIPTION).toString() : "") + " (" + jo.get("fieldname") + ")";
if (it != null && it.hasNext()) {
Comboitem ci = (Comboitem) it.next();
ci.setLabel(itemString);
ci.setValue(jo);
ci.setDescription(description);
} else {
it = null;
Comboitem ci = new Comboitem();
ci.setLabel(itemString);
ci.setValue(jo);
ci.setDescription(description);
ci.setParent(this);
}
}
while (it != null && it.hasNext()) {
it.next();
it.remove();
}
} catch (Exception e) {
LOGGER.error("error selecting gaz autocomplete item", e);
}
}
use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.
the class GazetteerPointSearch method pointSearch.
/**
* *
* Given a lon,lat and layer - queries the gaz for a polygon
*
* @param lon longitude
* @param lat latitude
* @param layer geoserver layer to search
* @return returns a link to a geojson feature in the gaz
*/
public static Map<String, String> pointSearch(String lon, String lat, String layer, String geoserver) {
try {
String uri = CommonData.getLayersServer() + "/intersect/" + layer + "/" + lat + "/" + lon;
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(uri);
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
int result = client.executeMethod(get);
String slist = get.getResponseBodyAsString();
LOGGER.debug("URI: " + uri);
LOGGER.debug("result: " + result);
LOGGER.debug("slist: " + slist);
JSONParser jp = new JSONParser();
JSONArray ja = (JSONArray) jp.parse(slist);
if (ja != null && !ja.isEmpty()) {
JSONObject jo = (JSONObject) ja.get(0);
Map<String, String> map = new HashMap<String, String>();
for (Object k : jo.keySet()) {
map.put((String) k, jo.get((String) k) == null ? "" : jo.get((String) k).toString());
}
return map;
}
} catch (Exception e1) {
LOGGER.error("error with gaz point search", e1);
}
return null;
}
use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.
the class ImportAnalysisController method afterCompose.
@Override
public void afterCompose() {
super.afterCompose();
try {
JSONObject jo = remoteLogger.getLogCSV();
if (jo != null && jo.containsKey("abe") && !((JSONArray) jo.get("abe")).isEmpty()) {
List<String[]> logEntries = new ArrayList<String[]>();
for (Object o : (JSONArray) jo.get("abe")) {
JSONObject j = (JSONObject) o;
String[] r = new String[5];
r[0] = j.containsKey(StringConstants.ID) ? j.get(StringConstants.ID).toString() : "";
r[1] = j.containsKey("category2") ? j.get("category2").toString() : "";
r[2] = j.containsKey("time") ? new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(new Date(Long.parseLong(j.get("time").toString()))) : "";
r[3] = "";
r[4] = j.containsKey("service") && ((JSONObject) j.get("service")).containsKey("processid") ? ((JSONObject) j.get("service")).get("processid").toString() : "";
if (r[4].length() > 0 && !"-1".equals(r[4]) && (StringConstants.CLASSIFICATION.equalsIgnoreCase(r[1]) || StringConstants.GDM.equalsIgnoreCase(r[1]) || StringConstants.PREDICTION.equalsIgnoreCase(r[1]) || StringConstants.SPECIES_TO_GRID.equalsIgnoreCase(r[1]))) {
try {
r[3] = StringConstants.TRUE;
refNum.setValue(r[4]);
pid = r[4];
if (!getJobStatus().contains(StringConstants.JOB_DOES_NOT_EXIST)) {
//not sure why, but sometimes data is missing even when the job exists
if (hasValidMetadata(r)) {
logEntries.add(r);
}
} else {
// older jobs don't exist, stop checking
break;
}
} catch (Exception e) {
r[4] = "unavailable";
r[3] = StringConstants.FALSE;
}
}
}
if (!logEntries.isEmpty()) {
divPriorAnalysis.setVisible(true);
lbLog.setModel(new SimpleListModel(logEntries));
setRenderer();
}
}
} catch (Exception e) {
LOGGER.error("getting log did not work", e);
}
refNum.setValue("");
}
use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.
the class ScatterplotListComposer method onFinish.
@Override
public boolean onFinish() {
LOGGER.debug("Area: " + getSelectedArea());
LOGGER.debug("Species: " + getSelectedSpecies());
Query lsid = getSelectedSpecies();
if (lsid == null) {
getMapComposer().showMessage("There was a problem selecting the species. Try to select the species again", this);
return false;
}
lsid = lsid.newFacet(new Facet("occurrence_status_s", "absent", false), false);
SelectedArea filterSa = getSelectedArea();
SelectedArea highlightSa = getSelectedAreaHighlight();
Query lsidQuery = QueryUtil.queryFromSelectedArea(lsid, filterSa, false, getGeospatialKosher());
if (lsidQuery == null || lsidQuery.getOccurrenceCount() == 0) {
getMapComposer().showMessage("No occurrences found for the selected species in the selected area.");
return false;
}
String pid = "";
Query backgroundLsid = getSelectedSpeciesBk();
backgroundLsid = backgroundLsid.newFacet(new Facet("occurrence_status_s", "absent", false), false);
if (bgSearchSpeciesACComp.hasValidAnnotatedItemSelected()) {
backgroundLsid = bgSearchSpeciesACComp.getQuery((Map) getMapComposer().getSession().getAttribute(StringConstants.USERPOINTS), false, getGeospatialKosher());
}
boolean envGrid = chkShowEnvIntersection.isChecked();
Query backgroundLsidQuery = null;
if (backgroundLsid != null) {
backgroundLsidQuery = QueryUtil.queryFromSelectedArea(backgroundLsid, filterSa, false, getGeospatialKosherBk());
}
String sbenvsel = getSelectedLayersWithDisplayNames();
String[] layers = sbenvsel.split(":");
if (layers.length > 20) {
getMapComposer().showMessage(sbenvsel.split(":").length + " layers selected. Please select fewer than 20 environmental layers in step 1.");
return false;
}
StringBuilder layernames = new StringBuilder();
String layerunits = "";
for (int i = 0; i < layers.length; i++) {
String[] split = layers[i].split("\\|");
if (layernames.length() > 0) {
layernames.append(",");
layerunits += ",";
}
layers[i] = split[0];
String layerDisplayName = split[1];
layernames.append("\"").append(layerDisplayName.replace("\"", "\"\"").replace("\\", "\\\\")).append("\"");
String units = "";
try {
units = String.valueOf(((JSONObject) CommonData.getLayer(layers[i]).get("layer")).get("environmentalvalueunits"));
} catch (Exception e) {
}
layerunits += units;
}
try {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(CommonData.getSatServer() + "/ws/scatterplotlist");
//add data parameters
post.addParameter("layers", getSelectedLayers());
post.addParameter("layernames", layernames.toString());
post.addParameter("layerunits", layerunits);
post.addParameter("foregroundOccurrencesQs", lsidQuery.getQ());
post.addParameter("foregroundOccurrencesBs", lsidQuery.getBS());
post.addParameter("foregroundName", lsidQuery.getName());
if (backgroundLsidQuery != null) {
post.addParameter("backgroundOccurrencesQs", backgroundLsidQuery.getQ());
post.addParameter("backgroundOccurrencesBs", backgroundLsidQuery.getBS());
post.addParameter("backgroundName", backgroundLsidQuery.getName());
}
if (envGrid) {
post.addParameter("gridDivisions", "20");
}
if (filterSa != null) {
post.addParameter("filterWkt", filterSa.getWkt());
}
//add style parameters (highlight area)
if (highlightSa != null) {
post.addParameter(StringConstants.HIGHLIGHT_WKT, highlightSa.getWkt());
}
post.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
client.executeMethod(post);
String hasId = post.getResponseBodyAsString();
JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject) jp.parse(hasId);
String htmlUrl = null;
String downloadUrl = null;
if (jo.containsKey(StringConstants.ID)) {
pid = jo.get(StringConstants.ID).toString();
}
if (jo.containsKey("htmlUrl")) {
htmlUrl = jo.get("htmlUrl").toString();
}
if (jo.containsKey("downloadUrl")) {
downloadUrl = jo.get("downloadUrl").toString();
}
if (htmlUrl != null && downloadUrl != null) {
Events.echoEvent(StringConstants.OPEN_URL, getMapComposer(), htmlUrl);
try {
Filedownload.save(new URL(downloadUrl).openStream(), "application/zip", tToolName.getValue().replaceAll(" ", "_") + ".zip");
} catch (Exception e) {
LOGGER.error("error preparing download for scatterplot data", e);
}
try {
String extras = "";
if (highlightSa != null) {
extras += "highlight=" + highlightSa.getWkt();
}
if (backgroundLsid instanceof BiocacheQuery) {
extras += "background=" + ((BiocacheQuery) backgroundLsid).getLsids();
} else if (backgroundLsid instanceof UserDataQuery) {
extras += "background=" + backgroundLsid.getQ();
} else {
extras += "background=none";
}
if (lsidQuery instanceof BiocacheQuery) {
BiocacheQuery bq = (BiocacheQuery) lsidQuery;
extras = bq.getWS() + "|" + bq.getBS() + "|" + bq.getFullQ(false) + "|" + extras;
remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Scatterplot List", filterSa.getWkt(), bq.getLsids(), sbenvsel, pid, extras, StringConstants.SUCCESSFUL);
} else if (lsidQuery instanceof UserDataQuery) {
remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Scatterplot List", filterSa.getWkt(), lsidQuery.getQ(), sbenvsel, pid, extras, StringConstants.SUCCESSFUL);
} else {
remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Scatterplot List", filterSa.getWkt(), "", sbenvsel, pid, extras, StringConstants.SUCCESSFUL);
}
} catch (Exception e) {
LOGGER.error("failed to produce a scatterplot list.", e);
}
} else {
LOGGER.error("failed to produce a scatterplot list. response: " + jo);
}
} catch (Exception e) {
LOGGER.error("error getting a new scatterplot id", e);
}
this.detach();
return true;
}
use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.
the class ToolComposer method processAdhoc.
JSONObject processAdhoc(String scientificName) {
String url = CommonData.getBiocacheServer() + "/process/adhoc";
try {
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);
StringRequestEntity sre = new StringRequestEntity("{ \"scientificName\": \"" + scientificName.replace("\"", "'") + "\" } ", StringConstants.APPLICATION_JSON, StringConstants.UTF_8);
post.setRequestEntity(sre);
int result = client.executeMethod(post);
if (result == 200) {
JSONParser jp = new JSONParser();
return (JSONObject) jp.parse(post.getResponseBodyAsString());
}
} catch (Exception e) {
LOGGER.error("error processing species request: " + url + ", scientificName=" + scientificName, e);
}
return null;
}
Aggregations