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());
}
}
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;
}
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);
}
}
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;
}
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());
}
}
Aggregations