Search in sources :

Example 1 with JSONArray

use of wslite.json.JSONArray in project axelor-open-suite by axelor.

the class ECBCurrencyConversionService method getRateFromJson.

@Override
public Float getRateFromJson(Currency currencyFrom, Currency currencyTo, HTTPResponse response) throws AxelorException {
    try {
        Float rt = null;
        int compareCode = currencyFrom.getCode().compareTo(currencyTo.getCode());
        String[] currencyRateArr = new String[2];
        JSONObject jsonResult = new JSONObject(response.getContentAsString());
        JSONObject dataSets = new JSONObject(jsonResult.getJSONArray("dataSets").get(0).toString());
        JSONObject series = new JSONObject(dataSets.getJSONObject("series").toString());
        JSONObject seriesOf = null;
        JSONObject observations = null;
        JSONArray rateValue = null;
        if (series.size() > 1) {
            for (int i = 0; i < series.size(); i++) {
                seriesOf = new JSONObject(series.getJSONObject("0:" + i + ":0:0:0").toString());
                observations = new JSONObject(seriesOf.getJSONObject("observations").toString());
                rateValue = new JSONArray(observations.get(observations.length() - 1).toString());
                currencyRateArr[i] = rateValue.get(0).toString();
            }
            if (compareCode > 0) {
                rt = Float.parseFloat(currencyRateArr[0]) / Float.parseFloat(currencyRateArr[1]);
            } else {
                rt = Float.parseFloat(currencyRateArr[1]) / Float.parseFloat(currencyRateArr[0]);
            }
        } else {
            seriesOf = new JSONObject(series.getJSONObject("0:0:0:0:0").toString());
            observations = new JSONObject(seriesOf.getJSONObject("observations").toString());
            rateValue = new JSONArray(observations.get(observations.length() - 1).toString());
            if (currencyTo.getCode().equals("EUR")) {
                rt = 1.0f / Float.parseFloat(rateValue.get(0).toString());
            } else {
                rt = Float.parseFloat(rateValue.get(0).toString());
            }
        }
        return rt;
    } catch (JSONException e) {
        throw new AxelorException(e.getCause(), TraceBackRepository.CATEGORY_INCONSISTENCY, e.getLocalizedMessage());
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) JSONObject(wslite.json.JSONObject) JSONArray(wslite.json.JSONArray) JSONException(wslite.json.JSONException)

Example 2 with JSONArray

use of wslite.json.JSONArray in project axelor-open-suite by axelor.

the class MapService method geocodeGoogle.

public JSONObject geocodeGoogle(String qString) throws AxelorException, JSONException {
    if (StringUtils.isBlank(qString)) {
        return null;
    }
    // http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false
    // TODO inject the rest client, or better, run it in the browser
    RESTClient restClient = new RESTClient("https://maps.googleapis.com");
    Map<String, Object> responseQuery = new HashMap<>();
    responseQuery.put("address", qString.trim());
    responseQuery.put("sensor", "false");
    responseQuery.put("key", getGoogleMapsApiKey());
    Map<String, Object> responseMap = new HashMap<>();
    responseMap.put("path", "/maps/api/geocode/json");
    responseMap.put("accept", ContentType.JSON);
    responseMap.put("query", responseQuery);
    responseMap.put("connectTimeout", 5000);
    responseMap.put("readTimeout", 10000);
    responseMap.put("followRedirects", false);
    responseMap.put("useCaches", false);
    responseMap.put("sslTrustAllCerts", true);
    JSONObject restResponse = getJSON(restClient.get(responseMap));
    LOG.debug("Gmap response: {}", restResponse);
    if (restResponse.containsKey("results")) {
        JSONArray results = (JSONArray) restResponse.get("results");
        if (CollectionUtils.isNotEmpty(results)) {
            JSONObject result = (JSONObject) results.iterator().next();
            if (result != null && result.containsKey("geometry")) {
                return (JSONObject) ((JSONObject) result.get("geometry")).get("location");
            }
        }
    }
    throw new AxelorException(appBaseService.getAppBase(), TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.MAP_RESPONSE_ERROR), restResponse);
/*
     * log.debug("restResponse = {}", restResponse)
     * log.debug("restResponse.parsedResponseContent.text = {}",
     * restResponse.parsedResponseContent.text)
     */
// def searchresults = new
// JsonSlurper().parseText(restResponse.parsedResponseContent.text);
/*
     * LOG.debug("searchresults.status = {}", searchresults.status); if
     * (searchresults.status == "OK") { /*
     * log.debug("searchresults.results.size() = {}", searchresults.results.size())
     * log.debug("searchresults.results[0] = {}", searchresults.results[0])
     * log.debug("searchresults.results[0].address_components = {}",
     * searchresults.results[0].address_components)
     * log.debug("searchresults.results[0].geometry.location = {}",
     * searchresults.results[0].geometry.location)
     */
/*
     * def results = searchresults.results;
     *
     * if (results.size() > 1) { response.put("multiple", true); } def
     * firstPlaceFound = results[0];
     *
     * if (firstPlaceFound) { BigDecimal lat = new
     * BigDecimal(firstPlaceFound.geometry.location.lat); BigDecimal lng = new
     * BigDecimal(firstPlaceFound.geometry.location.lng);
     *
     * response.put("lat", lat.setScale(10, RoundingMode.HALF_UP));
     * response.put("lng", lng.setScale(10, RoundingMode.HALF_UP)); }
     */
// }
}
Also used : AxelorException(com.axelor.exception.AxelorException) RESTClient(wslite.rest.RESTClient) JSONObject(wslite.json.JSONObject) HashMap(java.util.HashMap) JSONArray(wslite.json.JSONArray) JSONObject(wslite.json.JSONObject)

Aggregations

AxelorException (com.axelor.exception.AxelorException)2 JSONArray (wslite.json.JSONArray)2 JSONObject (wslite.json.JSONObject)2 HashMap (java.util.HashMap)1 JSONException (wslite.json.JSONException)1 RESTClient (wslite.rest.RESTClient)1