Search in sources :

Example 1 with JSONException

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

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

the class KilometricService method getApiResponse.

protected JSONObject getApiResponse(String urlString, String exceptionMessage) throws IOException, JSONException, AxelorException {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    int responseCode = connection.getResponseCode();
    this.checkResponseStatus(responseCode, exceptionMessage);
    StringBuilder sb = new StringBuilder();
    try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine + "\n");
        }
    }
    String response = sb.toString();
    JSONObject json;
    // throw exception if response is not json
    try {
        json = new JSONObject(response);
    } catch (Exception e) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, exceptionMessage, response);
    }
    return json;
}
Also used : AxelorException(com.axelor.exception.AxelorException) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) JSONObject(wslite.json.JSONObject) BufferedReader(java.io.BufferedReader) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) IOException(java.io.IOException)

Example 3 with JSONException

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

the class KilometricService method computeDistance.

/**
 * Compute the distance between two cities.
 *
 * @param fromCity
 * @param toCity
 * @return
 * @throws AxelorException
 */
protected BigDecimal computeDistance(String fromCity, String toCity) throws AxelorException {
    BigDecimal distance = BigDecimal.ZERO;
    if (StringUtils.isEmpty(fromCity) || StringUtils.isEmpty(toCity) || fromCity.equalsIgnoreCase(toCity))
        return distance;
    AppBase appBase = appBaseService.getAppBase();
    try {
        switch(appBase.getMapApiSelect()) {
            case AppBaseRepository.MAP_API_GOOGLE:
                distance = this.getDistanceUsingGoogle(fromCity, toCity);
                break;
            case AppBaseRepository.MAP_API_OPEN_STREET_MAP:
                distance = this.getDistanceUsingOSM(fromCity, toCity);
                break;
        }
        return distance;
    } catch (URISyntaxException | IOException | JSONException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) AppBase(com.axelor.apps.base.db.AppBase)

Example 4 with JSONException

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

the class SaleOrderServiceImpl method addPack.

@Override
@Transactional
public SaleOrder addPack(SaleOrder saleOrder, Pack pack, BigDecimal packQty) {
    List<PackLine> packLineList = pack.getComponents();
    if (ObjectUtils.isEmpty(packLineList)) {
        return saleOrder;
    }
    packLineList.sort(Comparator.comparing(PackLine::getSequence));
    Integer sequence = -1;
    List<SaleOrderLine> soLines = saleOrder.getSaleOrderLineList();
    if (soLines != null && !soLines.isEmpty()) {
        sequence = soLines.stream().mapToInt(SaleOrderLine::getSequence).max().getAsInt();
    }
    BigDecimal conversionRate = new BigDecimal(1.00);
    if (pack.getCurrency() != null && !pack.getCurrency().getCode().equals(saleOrder.getCurrency().getCode())) {
        try {
            conversionRate = Beans.get(CurrencyConversionFactory.class).getCurrencyConversionService().convert(pack.getCurrency(), saleOrder.getCurrency());
        } catch (MalformedURLException | JSONException | AxelorException e) {
            TraceBackService.trace(e);
        }
    }
    if (Boolean.FALSE.equals(pack.getDoNotDisplayHeaderAndEndPack())) {
        if (saleOrderLineService.getPackLineTypes(packLineList) == null || !saleOrderLineService.getPackLineTypes(packLineList).contains(PackLineRepository.TYPE_START_OF_PACK)) {
            sequence++;
        }
        soLines = saleOrderLineService.createNonStandardSOLineFromPack(pack, saleOrder, packQty, soLines, sequence);
    }
    SaleOrderLine soLine;
    for (PackLine packLine : packLineList) {
        if (packLine.getTypeSelect() != PackLineRepository.TYPE_NORMAL && Boolean.TRUE.equals(pack.getDoNotDisplayHeaderAndEndPack())) {
            continue;
        }
        soLine = saleOrderLineService.createSaleOrderLine(packLine, saleOrder, packQty, conversionRate, ++sequence);
        if (soLine != null) {
            soLine.setSaleOrder(saleOrder);
            soLines.add(soLine);
        }
    }
    if (soLines != null && !soLines.isEmpty()) {
        try {
            saleOrder = Beans.get(SaleOrderComputeService.class).computeSaleOrder(saleOrder);
            Beans.get(SaleOrderMarginService.class).computeMarginSaleOrder(saleOrder);
        } catch (AxelorException e) {
            TraceBackService.trace(e);
        }
        Beans.get(SaleOrderRepository.class).save(saleOrder);
    }
    return saleOrder;
}
Also used : AxelorException(com.axelor.exception.AxelorException) MalformedURLException(java.net.MalformedURLException) JSONException(wslite.json.JSONException) CurrencyConversionFactory(com.axelor.apps.base.service.currency.CurrencyConversionFactory) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) BigDecimal(java.math.BigDecimal) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) PackLine(com.axelor.apps.sale.db.PackLine) Transactional(com.google.inject.persist.Transactional)

Example 5 with JSONException

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

the class FixerCurrencyConversionService method getRateFromJson.

@Override
public Float getRateFromJson(Currency currencyFrom, Currency currencyTo, HTTPResponse response) throws AxelorException {
    try {
        JSONObject jsonResult = new JSONObject(response.getContentAsString());
        if (jsonResult.containsKey("rates")) {
            if (isRelatedConversion) {
                String fromRate = jsonResult.getJSONObject("rates").optString(currencyFrom.getCode());
                String toRate = jsonResult.getJSONObject("rates").optString(currencyTo.getCode());
                isRelatedConversion = false;
                return Float.parseFloat(toRate) / Float.parseFloat(fromRate);
            } else {
                String rate = jsonResult.getJSONObject("rates").optString(currencyTo.getCode());
                return Float.parseFloat((rate));
            }
        } else if (jsonResult.containsKey("error") && currencyTo.getCode().equals(EURO_CURRENCY_CODE)) {
            int code = jsonResult.getJSONObject("error").getInt("code");
            if (code == 105 || code == 201) {
                return null;
            }
        }
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, response.getContentAsString());
    } catch (JSONException | NumberFormatException e) {
        throw new AxelorException(e.getCause(), TraceBackRepository.CATEGORY_INCONSISTENCY, e.getLocalizedMessage());
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) JSONObject(wslite.json.JSONObject) JSONException(wslite.json.JSONException)

Aggregations

AxelorException (com.axelor.exception.AxelorException)5 JSONException (wslite.json.JSONException)5 JSONObject (wslite.json.JSONObject)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 URISyntaxException (java.net.URISyntaxException)2 AppBase (com.axelor.apps.base.db.AppBase)1 CurrencyConversionFactory (com.axelor.apps.base.service.currency.CurrencyConversionFactory)1 PackLine (com.axelor.apps.sale.db.PackLine)1 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)1 SaleOrderRepository (com.axelor.apps.sale.db.repo.SaleOrderRepository)1 Transactional (com.google.inject.persist.Transactional)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 JSONArray (wslite.json.JSONArray)1