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