Search in sources :

Example 1 with JSONObject

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

the class MapService method getMapGoogle.

public Map<String, Object> getMapGoogle(String qString) throws AxelorException, JSONException {
    LOG.debug("Query string: {}", qString);
    JSONObject googleResponse = geocodeGoogle(qString);
    LOG.debug("Google response: {}", googleResponse);
    if (googleResponse != null) {
        Map<String, Object> result = new HashMap<>();
        BigDecimal latitude = new BigDecimal(googleResponse.get("lat").toString());
        BigDecimal longitude = new BigDecimal(googleResponse.get("lng").toString());
        LOG.debug("URL:" + "map/gmaps.html?x=" + latitude + "&y=" + longitude + "&z=18");
        result.put("url", "map/gmaps.html?key=" + getGoogleMapsApiKey() + "&x=" + latitude + "&y=" + longitude + "&z=18");
        result.put("latitude", latitude);
        result.put("longitude", longitude);
        return result;
    }
    return null;
}
Also used : JSONObject(wslite.json.JSONObject) HashMap(java.util.HashMap) JSONObject(wslite.json.JSONObject) BigDecimal(java.math.BigDecimal)

Example 2 with JSONObject

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

the class MapService method getJSON.

private JSONObject getJSON(Response response) throws AxelorException, JSONException {
    LOG.debug("Gmap connection status code: {}, message: {}", response.getStatusCode(), response.getStatusMessage());
    AppBase appBase = appBaseService.getAppBase();
    if (response.getStatusCode() != HttpStatus.SC_OK) {
        String msg = String.format("%d: %s", response.getStatusCode(), response.getStatusMessage());
        throw new AxelorException(appBase, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, msg);
    }
    JSONObject json = new JSONObject(response.getContentAsString());
    String status = json.getString("status");
    if (!"OK".equalsIgnoreCase(status)) {
        String msg = json.has("error_message") ? String.format("%s: %s", status, json.getString("error_message")) : status;
        throw new AxelorException(appBase, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, msg);
    }
    return json;
}
Also used : AxelorException(com.axelor.exception.AxelorException) JSONObject(wslite.json.JSONObject) AppBase(com.axelor.apps.base.db.AppBase)

Example 3 with JSONObject

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

the class EmployeeController method showAnnualReport.

public void showAnnualReport(ActionRequest request, ActionResponse response) throws JSONException, NumberFormatException, AxelorException {
    String employeeId = request.getContext().get("_id").toString();
    String year = request.getContext().get("year").toString();
    int yearId = new JSONObject(year).getInt("id");
    String yearName = new JSONObject(year).getString("name");
    User user = AuthUtils.getUser();
    String name = I18n.get("Annual expenses report") + " :  " + user.getFullName() + " (" + yearName + ")";
    String fileLink = ReportFactory.createReport(IReport.EMPLOYEE_ANNUAL_REPORT, name).addParam("EmployeeId", Long.valueOf(employeeId)).addParam("Timezone", getTimezone(Beans.get(EmployeeRepository.class).find(Long.valueOf(employeeId)).getUser())).addParam("YearId", Long.valueOf(yearId)).addParam("Locale", ReportSettings.getPrintingLocale(null)).toAttach(Beans.get(EmployeeRepository.class).find(Long.valueOf(employeeId))).generate().getFileLink();
    response.setView(ActionView.define(name).add("html", fileLink).map());
    response.setCanClose(true);
}
Also used : EmployeeRepository(com.axelor.apps.hr.db.repo.EmployeeRepository) User(com.axelor.auth.db.User) JSONObject(wslite.json.JSONObject)

Example 4 with JSONObject

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

the class ECBCurrencyConversionService method validateAndGetRate.

@Override
public Float validateAndGetRate(int dayCount, Currency currencyFrom, Currency currencyTo, LocalDate date) throws AxelorException {
    try {
        HTTPResponse response = null;
        if (dayCount < 8) {
            HTTPClient httpclient = new HTTPClient();
            HTTPRequest request = new HTTPRequest();
            Map<String, Object> headers = new HashMap<>();
            headers.put("Accept", "application/json");
            request.setHeaders(headers);
            URL url = new URL(this.getUrlString(date, currencyFrom.getCode(), currencyTo.getCode()));
            // URL url = new URL(String.format(WSURL,currencyFrom.getCode()));
            LOG.trace("Currency conversion webservice URL: {}", new Object[] { url.toString() });
            request.setUrl(url);
            request.setMethod(HTTPMethod.GET);
            response = httpclient.execute(request);
            // JSONObject json = new JSONObject(response.getContentAsString());
            LOG.trace("Webservice response code: {}, response message: {}", response.getStatusCode(), response.getStatusMessage());
            if (response.getStatusCode() != 200)
                return -1f;
            if (response.getContentAsString().isEmpty()) {
                return this.validateAndGetRate((dayCount + 1), currencyFrom, currencyTo, date.minus(Period.ofDays(1)));
            } else {
                return this.getRateFromJson(currencyFrom, currencyTo, response);
            }
        } else {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.CURRENCY_7), date.plus(Period.ofDays(1)), appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null))));
        }
    } catch (Exception e) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) HTTPRequest(wslite.http.HTTPRequest) User(com.axelor.auth.db.User) HashMap(java.util.HashMap) HTTPResponse(wslite.http.HTTPResponse) HTTPClient(wslite.http.HTTPClient) JSONObject(wslite.json.JSONObject) URL(java.net.URL) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Example 5 with JSONObject

use of wslite.json.JSONObject 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)

Aggregations

JSONObject (wslite.json.JSONObject)13 AxelorException (com.axelor.exception.AxelorException)10 JSONException (wslite.json.JSONException)4 User (com.axelor.auth.db.User)3 HashMap (java.util.HashMap)3 BigDecimal (java.math.BigDecimal)2 URL (java.net.URL)2 JSONArray (wslite.json.JSONArray)2 AppBase (com.axelor.apps.base.db.AppBase)1 EmployeeRepository (com.axelor.apps.hr.db.repo.EmployeeRepository)1 MailBuilder (com.axelor.mail.MailBuilder)1 Transactional (com.google.inject.persist.Transactional)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 MediaType (okhttp3.MediaType)1 OkHttpClient (okhttp3.OkHttpClient)1