Example usage for com.google.gson JsonArray get

List of usage examples for com.google.gson JsonArray get

Introduction

In this page you can find the example usage for com.google.gson JsonArray get.

Prototype

public JsonElement get(int i) 

Source Link

Document

Returns the ith element of the array.

Usage

From source file:com.gst.portfolio.loanproduct.service.LoanProductWritePlatformServiceJpaRepositoryImpl.java

License:Apache License

private List<Charge> assembleListOfProductCharges(final JsonCommand command, final String currencyCode) {

    final List<Charge> charges = new ArrayList<>();

    String loanProductCurrencyCode = command.stringValueOfParameterNamed("currencyCode");
    if (loanProductCurrencyCode == null) {
        loanProductCurrencyCode = currencyCode;
    }//from w  ww. java2  s. co  m

    if (command.parameterExists("charges")) {
        final JsonArray chargesArray = command.arrayOfParameterNamed("charges");
        if (chargesArray != null) {
            for (int i = 0; i < chargesArray.size(); i++) {

                final JsonObject jsonObject = chargesArray.get(i).getAsJsonObject();
                if (jsonObject.has("id")) {
                    final Long id = jsonObject.get("id").getAsLong();

                    final Charge charge = this.chargeRepository.findOneWithNotFoundDetection(id);

                    if (!loanProductCurrencyCode.equals(charge.getCurrencyCode())) {
                        final String errorMessage = "Charge and Loan Product must have the same currency.";
                        throw new InvalidCurrencyException("charge", "attach.to.loan.product", errorMessage);
                    }
                    charges.add(charge);
                }
            }
        }
    }

    return charges;
}

From source file:com.gst.portfolio.meeting.service.MeetingWritePlatformServiceJpaRepositoryImpl.java

License:Apache License

private Collection<ClientAttendance> getClientsAttendance(final Meeting meeting, final JsonCommand command) {
    final Collection<ClientAttendance> clientsAttendance = new ArrayList<>();

    Collection<Group> childGroups = null;
    if (meeting.isCenterEntity()) {
        childGroups = this.groupRepository.findByParentId(meeting.entityId());
    }//from   ww  w. j  a v  a2s.  c om

    final String json = command.json();
    final JsonElement element = this.fromApiJsonHelper.parse(json);
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(clientsAttendanceParamName)
                && topLevelJsonElement.get(clientsAttendanceParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(clientsAttendanceParamName).getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {
                final JsonObject attendanceElement = array.get(i).getAsJsonObject();
                final Long clientId = this.fromApiJsonHelper.extractLongNamed(clientIdParamName,
                        attendanceElement);
                final Integer attendanceTypeId = this.fromApiJsonHelper
                        .extractIntegerSansLocaleNamed(attendanceTypeParamName, attendanceElement);

                final Client client = this.clientRepositoryWrapper.findOneWithNotFoundDetection(clientId, true);

                if (meeting.isGroupEntity() && !client.isChildOfGroup(meeting.entityId())) {
                    throw new ClientNotInGroupException(clientId, meeting.entityId());
                } else if (meeting.isCenterEntity()) {
                    if (childGroups != null && !childGroups.isEmpty()) {
                        boolean isChildClient = false;
                        for (final Group group : childGroups) {
                            if (group.isChildClient(clientId)) {
                                isChildClient = true;
                                break;
                            }
                        }
                        if (!isChildClient) {
                            final String defaultUserMessage = "Client with identifier " + clientId
                                    + " is not in center " + meeting.entityId();
                            throw new ClientNotInGroupException("client.not.in.center", defaultUserMessage,
                                    clientId, meeting.entityId());
                        }
                    }
                }

                final ClientAttendance clientAttendance = ClientAttendance.createClientAttendance(client,
                        meeting, attendanceTypeId);
                clientsAttendance.add(clientAttendance);
            }
        }
    }
    return clientsAttendance;
}

From source file:com.gst.portfolio.savings.data.DepositAccountDataValidator.java

License:Apache License

private void validateSavingsCharges(final JsonElement element, final DataValidatorBuilder baseDataValidator) {

    if (element.isJsonObject()) {
        final JsonObject topLevelJsonElement = element.getAsJsonObject();
        final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
        final String monthDayFormat = this.fromApiJsonHelper
                .extractMonthDayFormatParameter(topLevelJsonElement);
        if (topLevelJsonElement.has(chargesParamName)
                && topLevelJsonElement.get(chargesParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(chargesParamName).getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {

                final JsonObject savingsChargeElement = array.get(i).getAsJsonObject();

                // final Long id =
                // this.fromApiJsonHelper.extractLongNamed(idParamName,
                // savingsChargeElement);

                final Long chargeId = this.fromApiJsonHelper.extractLongNamed(chargeIdParamName,
                        savingsChargeElement);
                baseDataValidator.reset().parameter(chargeIdParamName).value(chargeId).longGreaterThanZero();

                final BigDecimal amount = this.fromApiJsonHelper.extractBigDecimalNamed(amountParamName,
                        savingsChargeElement, locale);
                baseDataValidator.reset().parameter(amountParamName).value(amount).notNull().positiveAmount();

                if (this.fromApiJsonHelper.parameterExists(feeOnMonthDayParamName, savingsChargeElement)) {
                    final MonthDay monthDay = this.fromApiJsonHelper.extractMonthDayNamed(
                            feeOnMonthDayParamName, savingsChargeElement, monthDayFormat, locale);
                    baseDataValidator.reset().parameter(feeOnMonthDayParamName).value(monthDay).notNull();
                }//w w  w  . j  a  va  2 s.c om

                if (this.fromApiJsonHelper.parameterExists(feeIntervalParamName, savingsChargeElement)) {
                    final Integer feeInterval = this.fromApiJsonHelper.extractIntegerNamed(feeIntervalParamName,
                            savingsChargeElement, Locale.getDefault());
                    baseDataValidator.reset().parameter(feeIntervalParamName).value(feeInterval).notNull()
                            .inMinMaxRange(1, 12);
                }
            }
        }
    }
}

From source file:com.gst.portfolio.savings.data.DepositProductDataValidator.java

License:Apache License

private void validateChartsData(JsonElement element, DataValidatorBuilder baseDataValidator) {
    if (element.isJsonObject()) {
        final JsonObject topLevelJsonElement = element.getAsJsonObject();
        if (topLevelJsonElement.has(chartsParamName)
                && topLevelJsonElement.get(chartsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(chartsParamName).getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {
                final JsonObject interestRateChartElement = array.get(i).getAsJsonObject();
                final String json = this.fromApiJsonHelper.toJson(interestRateChartElement);
                // chart for create
                if (!this.fromApiJsonHelper.parameterExists(idParamName, interestRateChartElement)) {
                    this.chartDataValidator.validateForCreate(json, baseDataValidator);
                } else { // chart for update
                    this.chartDataValidator.validateForUpdate(json, baseDataValidator);
                }/* w w  w  . jav  a2  s .  c o m*/
            }
        }
    }
}

From source file:com.gst.portfolio.savings.data.DepositProductDataValidator.java

License:Apache License

/**
 * Validation for advanced accounting options
 *///from   w  w w. j a va 2s  .c  o m
private void validatePaymentChannelFundSourceMappings(final FromJsonHelper fromApiJsonHelper,
        final DataValidatorBuilder baseDataValidator, final JsonElement element) {
    if (fromApiJsonHelper.parameterExists(
            SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue(), element)) {
        final JsonArray paymentChannelMappingArray = fromApiJsonHelper.extractJsonArrayNamed(
                SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue(), element);
        if (paymentChannelMappingArray != null && paymentChannelMappingArray.size() > 0) {
            int i = 0;
            do {
                final JsonObject jsonObject = paymentChannelMappingArray.get(i).getAsJsonObject();
                final Long paymentTypeId = jsonObject
                        .get(SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_TYPE.getValue()).getAsLong();
                final Long paymentSpecificFundAccountId = jsonObject
                        .get(SAVINGS_PRODUCT_ACCOUNTING_PARAMS.FUND_SOURCE.getValue()).getAsLong();
                baseDataValidator.reset().parameter(
                        SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue() + "["
                                + i + "]." + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_TYPE.toString())
                        .value(paymentTypeId).notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter(
                        SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue() + "["
                                + i + "]." + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.FUND_SOURCE.getValue())
                        .value(paymentSpecificFundAccountId).notNull().integerGreaterThanZero();
                i++;
            } while (i < paymentChannelMappingArray.size());
        }
    }
}

From source file:com.gst.portfolio.savings.data.DepositProductDataValidator.java

License:Apache License

private void validateChargeToIncomeAccountMappings(final FromJsonHelper fromApiJsonHelper,
        final DataValidatorBuilder baseDataValidator, final JsonElement element, final boolean isPenalty) {
    String parameterName;//  w  w w  . j a  va2  s.  c o  m
    if (isPenalty) {
        parameterName = SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PENALTY_INCOME_ACCOUNT_MAPPING.getValue();
    } else {
        parameterName = SAVINGS_PRODUCT_ACCOUNTING_PARAMS.FEE_INCOME_ACCOUNT_MAPPING.getValue();
    }

    if (fromApiJsonHelper.parameterExists(parameterName, element)) {
        final JsonArray chargeToIncomeAccountMappingArray = fromApiJsonHelper
                .extractJsonArrayNamed(parameterName, element);
        if (chargeToIncomeAccountMappingArray != null && chargeToIncomeAccountMappingArray.size() > 0) {
            int i = 0;
            do {
                final JsonObject jsonObject = chargeToIncomeAccountMappingArray.get(i).getAsJsonObject();
                final Long chargeId = fromApiJsonHelper
                        .extractLongNamed(SAVINGS_PRODUCT_ACCOUNTING_PARAMS.CHARGE_ID.getValue(), jsonObject);
                final Long incomeAccountId = fromApiJsonHelper.extractLongNamed(
                        SAVINGS_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), jsonObject);
                baseDataValidator.reset()
                        .parameter(parameterName + "[" + i + "]."
                                + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.CHARGE_ID.getValue())
                        .value(chargeId).notNull().integerGreaterThanZero();
                baseDataValidator.reset()
                        .parameter(parameterName + "[" + i + "]."
                                + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue())
                        .value(incomeAccountId).notNull().integerGreaterThanZero();
                i++;
            } while (i < chargeToIncomeAccountMappingArray.size());
        }
    }
}

From source file:com.gst.portfolio.savings.data.SavingsProductDataValidator.java

License:Apache License

/**
 * Validation for advanced accounting options
 *///from ww w  . j a v a2 s . c  o  m
private void validatePaymentChannelFundSourceMappings(final DataValidatorBuilder baseDataValidator,
        final JsonElement element) {
    if (this.fromApiJsonHelper.parameterExists(
            SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue(), element)) {
        final JsonArray paymentChannelMappingArray = this.fromApiJsonHelper.extractJsonArrayNamed(
                SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue(), element);
        if (paymentChannelMappingArray != null && paymentChannelMappingArray.size() > 0) {
            int i = 0;
            do {
                final JsonObject jsonObject = paymentChannelMappingArray.get(i).getAsJsonObject();
                final Long paymentTypeId = jsonObject
                        .get(SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_TYPE.getValue()).getAsLong();
                final Long paymentSpecificFundAccountId = jsonObject
                        .get(SAVINGS_PRODUCT_ACCOUNTING_PARAMS.FUND_SOURCE.getValue()).getAsLong();
                baseDataValidator.reset().parameter(
                        SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue() + "["
                                + i + "]." + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_TYPE.toString())
                        .value(paymentTypeId).notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter(
                        SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PAYMENT_CHANNEL_FUND_SOURCE_MAPPING.getValue() + "["
                                + i + "]." + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.FUND_SOURCE.getValue())
                        .value(paymentSpecificFundAccountId).notNull().integerGreaterThanZero();
                i++;
            } while (i < paymentChannelMappingArray.size());
        }
    }
}

From source file:com.gst.portfolio.savings.data.SavingsProductDataValidator.java

License:Apache License

private void validateChargeToIncomeAccountMappings(final DataValidatorBuilder baseDataValidator,
        final JsonElement element, final boolean isPenalty) {
    String parameterName;//from   w  w w  .j a v a  2  s  .  c  o m
    if (isPenalty) {
        parameterName = SAVINGS_PRODUCT_ACCOUNTING_PARAMS.PENALTY_INCOME_ACCOUNT_MAPPING.getValue();
    } else {
        parameterName = SAVINGS_PRODUCT_ACCOUNTING_PARAMS.FEE_INCOME_ACCOUNT_MAPPING.getValue();
    }

    if (this.fromApiJsonHelper.parameterExists(parameterName, element)) {
        final JsonArray chargeToIncomeAccountMappingArray = this.fromApiJsonHelper
                .extractJsonArrayNamed(parameterName, element);
        if (chargeToIncomeAccountMappingArray != null && chargeToIncomeAccountMappingArray.size() > 0) {
            int i = 0;
            do {
                final JsonObject jsonObject = chargeToIncomeAccountMappingArray.get(i).getAsJsonObject();
                final Long chargeId = this.fromApiJsonHelper
                        .extractLongNamed(SAVINGS_PRODUCT_ACCOUNTING_PARAMS.CHARGE_ID.getValue(), jsonObject);
                final Long incomeAccountId = this.fromApiJsonHelper.extractLongNamed(
                        SAVINGS_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue(), jsonObject);
                baseDataValidator.reset()
                        .parameter(parameterName + "[" + i + "]."
                                + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.CHARGE_ID.getValue())
                        .value(chargeId).notNull().integerGreaterThanZero();
                baseDataValidator.reset()
                        .parameter(parameterName + "[" + i + "]."
                                + SAVINGS_PRODUCT_ACCOUNTING_PARAMS.INCOME_ACCOUNT_ID.getValue())
                        .value(incomeAccountId).notNull().integerGreaterThanZero();
                i++;
            } while (i < chargeToIncomeAccountMappingArray.size());
        }
    }
}

From source file:com.gst.portfolio.savings.domain.DepositAccountAssembler.java

License:Apache License

public Collection<SavingsAccountTransactionDTO> assembleBulkMandatorySavingsAccountTransactionDTOs(
        final JsonCommand command, final PaymentDetail paymentDetail) {
    AppUser user = getAppUserIfPresent();
    final String json = command.json();
    if (StringUtils.isBlank(json)) {
        throw new InvalidJsonException();
    }//  w  w  w.j a v  a2s  .  c  o  m
    final JsonElement element = this.fromApiJsonHelper.parse(json);
    final Collection<SavingsAccountTransactionDTO> savingsAccountTransactions = new ArrayList<>();
    final LocalDate transactionDate = this.fromApiJsonHelper.extractLocalDateNamed(transactionDateParamName,
            element);
    final String dateFormat = this.fromApiJsonHelper.extractDateFormatParameter(element.getAsJsonObject());
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    final DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat).withLocale(locale);

    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkSavingsDueTransactionsParamName)
                && topLevelJsonElement.get(bulkSavingsDueTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkSavingsDueTransactionsParamName)
                    .getAsJsonArray();

            for (int i = 0; i < array.size(); i++) {
                final JsonObject savingsTransactionElement = array.get(i).getAsJsonObject();
                final Long savingsId = this.fromApiJsonHelper.extractLongNamed(savingsIdParamName,
                        savingsTransactionElement);
                final BigDecimal dueAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed(transactionAmountParamName, savingsTransactionElement, locale);
                PaymentDetail detail = paymentDetail;
                if (paymentDetail == null) {
                    detail = this.paymentDetailAssembler.fetchPaymentDetail(savingsTransactionElement);
                }
                final SavingsAccountTransactionDTO savingsAccountTransactionDTO = new SavingsAccountTransactionDTO(
                        formatter, transactionDate, dueAmount, detail, new Date(), savingsId, user);
                savingsAccountTransactions.add(savingsAccountTransactionDTO);
            }
        }
    }

    return savingsAccountTransactions;
}

From source file:com.gst.portfolio.savings.domain.DepositProductAssembler.java

License:Apache License

public Set<Charge> assembleListOfSavingsProductCharges(final JsonCommand command,
        final String savingsProductCurrencyCode) {

    final Set<Charge> charges = new HashSet<>();

    if (command.parameterExists(chargesParamName)) {
        final JsonArray chargesArray = command.arrayOfParameterNamed(chargesParamName);
        if (chargesArray != null) {
            for (int i = 0; i < chargesArray.size(); i++) {

                final JsonObject jsonObject = chargesArray.get(i).getAsJsonObject();
                if (jsonObject.has(idParamName)) {
                    final Long id = jsonObject.get(idParamName).getAsLong();

                    final Charge charge = this.chargeRepository.findOneWithNotFoundDetection(id);

                    if (!charge.isSavingsCharge()) {
                        final String errorMessage = "Charge with identifier " + charge.getId()
                                + " cannot be applied to Savings product.";
                        throw new ChargeCannotBeAppliedToException("savings.product", errorMessage,
                                charge.getId());
                    }/*www  .j  av  a  2s.  co  m*/

                    if (!savingsProductCurrencyCode.equals(charge.getCurrencyCode())) {
                        final String errorMessage = "Charge and Savings Product must have the same currency.";
                        throw new InvalidCurrencyException("charge", "attach.to.savings.product", errorMessage);
                    }
                    charges.add(charge);
                }
            }
        }
    }

    return charges;
}