Search in sources :

Example 1 with HTTPResponse

use of wslite.http.HTTPResponse 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 2 with HTTPResponse

use of wslite.http.HTTPResponse in project axelor-open-suite by axelor.

the class FixerCurrencyConversionService method validateAndGetRate.

@Override
public Float validateAndGetRate(int dayCount, Currency currencyFrom, Currency currencyTo, LocalDate date) throws AxelorException {
    try {
        HTTPResponse response = null;
        if (dayCount < 8) {
            response = callApiBaseEuru(currencyFrom, currencyTo, date);
            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) User(com.axelor.auth.db.User) HTTPResponse(wslite.http.HTTPResponse) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) MalformedURLException(java.net.MalformedURLException)

Example 3 with HTTPResponse

use of wslite.http.HTTPResponse in project axelor-open-suite by axelor.

the class FixerCurrencyConversionService method updateCurrencyConverion.

@Override
public void updateCurrencyConverion() throws AxelorException {
    AppBase appBase = appBaseService.getAppBase();
    try {
        HTTPResponse response = null;
        LocalDate today = appBaseService.getTodayDate(Optional.ofNullable(AuthUtils.getUser()).map(User::getActiveCompany).orElse(null));
        response = callApiBaseEuru(null, null, today);
        if (response.getStatusCode() != 200) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
        }
        List<CurrencyConversionLine> currencyConversionLines = appBase.getCurrencyConversionLineList();
        currencyConversionLines = currencyConversionLines.stream().filter(it -> !it.getFromDate().isAfter(today) && it.getToDate() == null).collect(Collectors.toList());
        for (CurrencyConversionLine ccline : currencyConversionLines) {
            BigDecimal currentRate = BigDecimal.ZERO;
            Float rate = 0.0f;
            if (!ccline.getStartCurrency().getCode().equals(EURO_CURRENCY_CODE) && !ccline.getEndCurrency().getCode().equals(EURO_CURRENCY_CODE)) {
                isRelatedConversion = true;
                rate = this.getRateFromJson(ccline.getStartCurrency(), ccline.getEndCurrency(), response);
            } else if (ccline.getStartCurrency().getCode().equals(EURO_CURRENCY_CODE)) {
                rate = this.getRateFromJson(ccline.getStartCurrency(), ccline.getEndCurrency(), response);
            } else {
                rate = this.getRateFromJson(ccline.getEndCurrency(), ccline.getStartCurrency(), response);
                rate = 1.0f / rate;
            }
            currentRate = BigDecimal.valueOf(rate).setScale(8, RoundingMode.HALF_UP);
            if (currentRate.compareTo(new BigDecimal(-1)) == 0) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CURRENCY_6));
            }
            ccline = cclRepo.find(ccline.getId());
            BigDecimal previousRate = ccline.getExchangeRate();
            if (currentRate.compareTo(previousRate) != 0) {
                ccline.setToDate(today.minusDays(1).isAfter(ccline.getFromDate()) ? today.minusDays(1) : today);
                this.saveCurrencyConversionLine(ccline);
                String variations = this.getVariations(currentRate, previousRate);
                this.createCurrencyConversionLine(ccline.getStartCurrency(), ccline.getEndCurrency(), today, currentRate, appBase, variations);
            }
        }
    } catch (Exception e) {
        throw new AxelorException(e.getCause(), TraceBackRepository.CATEGORY_INCONSISTENCY, e.getLocalizedMessage());
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) User(com.axelor.auth.db.User) HTTPResponse(wslite.http.HTTPResponse) CurrencyConversionLine(com.axelor.apps.base.db.CurrencyConversionLine) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) MalformedURLException(java.net.MalformedURLException) AppBase(com.axelor.apps.base.db.AppBase)

Aggregations

User (com.axelor.auth.db.User)3 AxelorException (com.axelor.exception.AxelorException)3 MalformedURLException (java.net.MalformedURLException)3 HTTPResponse (wslite.http.HTTPResponse)3 JSONException (wslite.json.JSONException)3 AppBase (com.axelor.apps.base.db.AppBase)1 CurrencyConversionLine (com.axelor.apps.base.db.CurrencyConversionLine)1 BigDecimal (java.math.BigDecimal)1 URL (java.net.URL)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1 HTTPClient (wslite.http.HTTPClient)1 HTTPRequest (wslite.http.HTTPRequest)1 JSONObject (wslite.json.JSONObject)1