Example usage for com.google.gson JsonArray size

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of elements in the array.

Usage

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateAttendanceDetails(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    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 Long attendanceType = this.fromApiJsonHelper.extractLongNamed(attendanceTypeParamName,
                        attendanceElement);
                baseDataValidator.reset()
                        .parameter(clientsAttendanceParamName + "[" + i + "]." + clientIdParamName)
                        .value(clientId).notNull().integerGreaterThanZero();
                baseDataValidator.reset()
                        .parameter(clientsAttendanceParamName + "[" + i + "]." + attendanceTypeParamName)
                        .value(attendanceType).notNull().integerGreaterThanZero();
            }/*from  w  w w .  j ava2s .  com*/
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateDisbursementTransactions(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkDisbursementTransactionsParamName)
                && topLevelJsonElement.get(bulkDisbursementTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkDisbursementTransactionsParamName)
                    .getAsJsonArray();//w  w  w  .  j  a v  a2 s .  c om

            for (int i = 0; i < array.size(); i++) {
                final JsonObject loanTransactionElement = array.get(i).getAsJsonObject();
                final Long loanId = this.fromApiJsonHelper.extractLongNamed(loanIdParamName,
                        loanTransactionElement);
                final BigDecimal disbursementAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed(transactionAmountParamName, loanTransactionElement, locale);

                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].loan.id").value(loanId)
                        .notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].disbursement.amount")
                        .value(disbursementAmount).notNull().zeroOrPositiveAmount();
            }
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateRepaymentTransactions(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkRepaymentTransactionsParamName)
                && topLevelJsonElement.get(bulkRepaymentTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkRepaymentTransactionsParamName)
                    .getAsJsonArray();/*from   w ww.  j  a va  2 s  .c  o  m*/

            for (int i = 0; i < array.size(); i++) {
                final JsonObject loanTransactionElement = array.get(i).getAsJsonObject();
                final Long loanId = this.fromApiJsonHelper.extractLongNamed(loanIdParamName,
                        loanTransactionElement);
                final BigDecimal disbursementAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed(transactionAmountParamName, loanTransactionElement, locale);

                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].loan.id").value(loanId)
                        .notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].disbursement.amount")
                        .value(disbursementAmount).notNull().zeroOrPositiveAmount();

                validatePaymentDetails(baseDataValidator, loanTransactionElement, locale);
            }
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.data.CollectionSheetTransactionDataValidator.java

License:Apache License

private void validateSavingsDueTransactions(final JsonElement element,
        final DataValidatorBuilder baseDataValidator) {
    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    if (element.isJsonObject()) {
        if (topLevelJsonElement.has(bulkSavingsDueTransactionsParamName)
                && topLevelJsonElement.get(bulkSavingsDueTransactionsParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(bulkSavingsDueTransactionsParamName)
                    .getAsJsonArray();//from  w ww. ja v a 2  s  .  com

            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);

                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].savings.id")
                        .value(savingsId).notNull().integerGreaterThanZero();
                baseDataValidator.reset().parameter("bulktransaction" + "[" + i + "].due.amount")
                        .value(dueAmount).notNull().zeroOrPositiveAmount();
                validatePaymentDetails(baseDataValidator, savingsTransactionElement, locale);
            }
        }
    }
}

From source file:com.gst.portfolio.collectionsheet.serialization.CollectionSheetBulkDisbursalCommandFromApiJsonDeserializer.java

License:Apache License

@Override
public CollectionSheetBulkDisbursalCommand commandFromApiJson(final String json) {
    if (StringUtils.isBlank(json)) {
        throw new InvalidJsonException();
    }//  w ww  . j a  va 2  s  .co  m

    final JsonElement element = this.fromApiJsonHelper.parse(json);
    final JsonObject topLevelJsonElement = element.getAsJsonObject();

    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
    final LocalDate transactionDate = this.fromApiJsonHelper.extractLocalDateNamed("transactionDate", element);
    final String note = this.fromApiJsonHelper.extractStringNamed("note", element);

    SingleDisbursalCommand[] loanDisbursementTransactions = null;

    if (element.isJsonObject()) {
        if (topLevelJsonElement.has("bulkDisbursementTransactions")
                && topLevelJsonElement.get("bulkDisbursementTransactions").isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get("bulkDisbursementTransactions").getAsJsonArray();
            loanDisbursementTransactions = new SingleDisbursalCommand[array.size()];
            for (int i = 0; i < array.size(); i++) {
                final JsonObject loanTransactionElement = array.get(i).getAsJsonObject();

                final Long loanId = this.fromApiJsonHelper.extractLongNamed("loanId", loanTransactionElement);
                final BigDecimal disbursementAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed("transactionAmount", loanTransactionElement, locale);
                loanDisbursementTransactions[i] = new SingleDisbursalCommand(loanId, disbursementAmount,
                        transactionDate);
            }
        }
    }
    return new CollectionSheetBulkDisbursalCommand(note, transactionDate, loanDisbursementTransactions);
}

From source file:com.gst.portfolio.collectionsheet.serialization.CollectionSheetBulkRepaymentCommandFromApiJsonDeserializer.java

License:Apache License

public CollectionSheetBulkRepaymentCommand commandFromApiJson(final String json,
        final PaymentDetail paymentDetail) {
    if (StringUtils.isBlank(json)) {
        throw new InvalidJsonException();
    }/*from   w  w  w  .j a  va2  s .  c  om*/

    final JsonElement element = this.fromApiJsonHelper.parse(json);

    final LocalDate transactionDate = this.fromApiJsonHelper.extractLocalDateNamed("transactionDate", element);

    final String note = this.fromApiJsonHelper.extractStringNamed("note", element);

    final JsonObject topLevelJsonElement = element.getAsJsonObject();
    final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);

    SingleRepaymentCommand[] loanRepaymentTransactions = null;

    if (element.isJsonObject()) {
        if (topLevelJsonElement.has("bulkRepaymentTransactions")
                && topLevelJsonElement.get("bulkRepaymentTransactions").isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get("bulkRepaymentTransactions").getAsJsonArray();
            loanRepaymentTransactions = new SingleRepaymentCommand[array.size()];
            for (int i = 0; i < array.size(); i++) {
                final JsonObject loanTransactionElement = array.get(i).getAsJsonObject();

                final Long loanId = this.fromApiJsonHelper.extractLongNamed("loanId", loanTransactionElement);
                final BigDecimal transactionAmount = this.fromApiJsonHelper
                        .extractBigDecimalNamed("transactionAmount", loanTransactionElement, locale);
                PaymentDetail detail = paymentDetail;
                if (paymentDetail == null) {
                    detail = this.paymentDetailAssembler.fetchPaymentDetail(loanTransactionElement);
                }
                if (transactionAmount != null && transactionAmount.intValue() > 0) {
                    loanRepaymentTransactions[i] = new SingleRepaymentCommand(loanId, transactionAmount,
                            transactionDate, detail);
                }
            }
        }
    }
    return new CollectionSheetBulkRepaymentCommand(note, transactionDate, loanRepaymentTransactions);
}

From source file:com.gst.portfolio.floatingrates.serialization.FloatingRateDataValidator.java

License:Apache License

private void validateRatePeriods(DataValidatorBuilder baseDataValidator, JsonElement element,
        boolean isBaseLendingRate, boolean isBLRModifiedAsNonBLR) {
    if (this.fromApiJsonHelper.parameterExists("ratePeriods", element)) {
        final JsonArray ratePeriods = this.fromApiJsonHelper.extractJsonArrayNamed("ratePeriods", element);
        baseDataValidator.reset().parameter("ratePeriods").value(ratePeriods).notBlank().jsonArrayNotEmpty();

        if (ratePeriods != null) {
            List<LocalDate> fromDates = new ArrayList<>();
            for (int i = 0; i < ratePeriods.size(); i++) {
                final JsonElement ratePeriod = ratePeriods.get(i);

                this.fromApiJsonHelper.checkForUnsupportedParameters(ratePeriod.getAsJsonObject(),
                        this.supportedParametersForFloatingRatePeriods);

                final LocalDate fromDate = this.fromApiJsonHelper.extractLocalDateNamed("fromDate", ratePeriod);
                baseDataValidator.reset().parameter("fromDate").parameterAtIndexArray("fromDate", i + 1)
                        .value(fromDate).notBlank()
                        .validateDateAfter(DateUtils.getLocalDateOfTenant().plusDays(1));
                if (fromDate != null) {
                    fromDates.add(fromDate);
                }//www . j  a v  a2 s .  c o m

                final BigDecimal interestRatePerPeriod = this.fromApiJsonHelper
                        .extractBigDecimalWithLocaleNamed("interestRate", ratePeriod);
                baseDataValidator.reset().parameter("interestRate").parameterAtIndexArray("interestRate", i + 1)
                        .value(interestRatePerPeriod).notNull().zeroOrPositiveAmount();

                if (this.fromApiJsonHelper.parameterExists("isDifferentialToBaseLendingRate", ratePeriod)) {
                    final Boolean isDifferentialToBaseLendingRate = this.fromApiJsonHelper
                            .extractBooleanNamed("isDifferentialToBaseLendingRate", ratePeriod);
                    if (isDifferentialToBaseLendingRate == null) {
                        baseDataValidator.reset().parameter("isDifferentialToBaseLendingRate")
                                .parameterAtIndexArray("isDifferentialToBaseLendingRate", i + 1)
                                .trueOrFalseRequired(false);
                    } else if (isDifferentialToBaseLendingRate) {
                        FloatingRate baseLendingRate = this.floatingRateRepository.retrieveBaseLendingRate();
                        if (baseLendingRate == null || isBLRModifiedAsNonBLR) {
                            baseDataValidator.reset().parameter("isDifferentialToBaseLendingRate")
                                    .parameterAtIndexArray("isDifferentialToBaseLendingRate", i + 1)
                                    .value(isDifferentialToBaseLendingRate).failWithCode(
                                            "no.baselending.rate.defined", "Base Lending Rate doesn't exists");
                        }

                        if (isBaseLendingRate) {
                            baseDataValidator.reset().parameter("isDifferentialToBaseLendingRate")
                                    .parameterAtIndexArray("isDifferentialToBaseLendingRate", i + 1)
                                    .value(isDifferentialToBaseLendingRate)
                                    .failWithCode("cannot.be.true.for.baselendingrate",
                                            "isDifferentialToBaseLendingRate cannot be true for floating rate marked as Base Lending Rate.");
                        }

                    }
                }
            }
            Set<LocalDate> uniqueFromDates = new HashSet<>(fromDates);
            if (fromDates.size() != uniqueFromDates.size()) {
                baseDataValidator.reset().parameter("fromDate").failWithCode("multiple.same.date",
                        "More than one entry in ratePeriods have same fromDate.");
            }

        }
    }
}

From source file:com.gst.portfolio.interestratechart.data.InterestRateChartDataValidator.java

License:Apache License

private void validateChartSlabs(JsonElement element, DataValidatorBuilder baseDataValidator,
        final boolean isPrimaryGroupingByAmount) {

    if (element.isJsonObject()) {
        final JsonObject topLevelJsonElement = element.getAsJsonObject();
        final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement);
        if (topLevelJsonElement.has(chartSlabs) && topLevelJsonElement.get(chartSlabs).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(chartSlabs).getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {
                final JsonObject interstRateChartElement = array.get(i).getAsJsonObject();
                if (this.fromApiJsonHelper.parameterExists(idParamName, interstRateChartElement)) {
                    final Long id = this.fromApiJsonHelper.extractLongNamed(idParamName,
                            interstRateChartElement);
                    baseDataValidator.reset().parameter(idParamName).value(id).notNull()
                            .integerGreaterThanZero();
                    this.chartSlabDataValidator.validateChartSlabsUpdate(interstRateChartElement,
                            baseDataValidator, locale, isPrimaryGroupingByAmount);
                } else {
                    this.chartSlabDataValidator.validateChartSlabsCreate(interstRateChartElement,
                            baseDataValidator, locale, isPrimaryGroupingByAmount);
                }/*from ww w . j  a v a2  s  .  com*/
            }
        }
    }
}

From source file:com.gst.portfolio.interestratechart.data.InterestRateChartSlabDataValidator.java

License:Apache License

private void validateIncentives(JsonElement element, DataValidatorBuilder baseDataValidator,
        final Locale locale) {

    if (element.isJsonObject()) {
        final JsonObject topLevelJsonElement = element.getAsJsonObject();
        if (topLevelJsonElement.has(incentivesParamName)
                && topLevelJsonElement.get(incentivesParamName).isJsonArray()) {
            final JsonArray array = topLevelJsonElement.get(incentivesParamName).getAsJsonArray();
            for (int i = 0; i < array.size(); i++) {
                final JsonObject incentiveElement = array.get(i).getAsJsonObject();
                if (this.fromApiJsonHelper.parameterExists(InterestIncentiveApiConstants.idParamName,
                        incentiveElement)) {
                    final Long id = this.fromApiJsonHelper
                            .extractLongNamed(InterestIncentiveApiConstants.idParamName, incentiveElement);
                    baseDataValidator.reset().parameter(InterestIncentiveApiConstants.idParamName).value(id)
                            .notNull().integerGreaterThanZero();
                    this.interestIncentiveDataValidator.validateIncentiveUpdate(incentiveElement,
                            baseDataValidator, locale);
                } else {
                    this.interestIncentiveDataValidator.validateIncentiveCreate(incentiveElement,
                            baseDataValidator, locale);
                }//from  www.  j a v a 2 s.c o  m
            }
        }
    }
}

From source file:com.gst.portfolio.interestratechart.domain.InterestRateChart.java

License:Apache License

public void updateChartSlabs(JsonCommand command, final Map<String, Object> actualChanges,
        final DataValidatorBuilder baseDataValidator, String currencyCode) {

    final Map<String, Object> deleteChartSlabs = new HashMap<>();
    final Map<String, Object> chartSlabsChanges = new HashMap<>();
    final Locale locale = command.extractLocale();
    if (command.hasParameter(InterestRateChartApiConstants.chartSlabs)) {
        final JsonArray array = command.arrayOfParameterNamed(InterestRateChartApiConstants.chartSlabs);
        if (array != null) {
            for (int i = 0; i < array.size(); i++) {
                final JsonObject chartSlabsElement = array.get(i).getAsJsonObject();
                JsonCommand chartSlabsCommand = JsonCommand.fromExistingCommand(command, chartSlabsElement);
                if (chartSlabsCommand.parameterExists(idParamName)) {
                    final Long chartSlabId = chartSlabsCommand.longValueOfParameterNamed(idParamName);
                    final InterestRateChartSlab chartSlab = this.findChartSlab(chartSlabId);
                    if (chartSlab == null) {
                        baseDataValidator.parameter(idParamName).value(chartSlabId)
                                .failWithCode("no.chart.slab.associated.with.id");
                    } else if (chartSlabsCommand.parameterExists(deleteParamName)) {
                        if (this.removeChartSlab(chartSlab)) {
                            deleteChartSlabs.put(idParamName, chartSlabId);
                        }//from   w w w.ja va 2 s. c  om
                    } else {
                        chartSlab.update(chartSlabsCommand, chartSlabsChanges, baseDataValidator, locale);
                    }
                } else {

                    /**
                     * TODO: AA: Move this code to
                     * InterestRateChartSlabAssembler
                     */
                    final String description = chartSlabsCommand
                            .stringValueOfParameterNamed(descriptionParamName);
                    final Integer periodTypeId = chartSlabsCommand
                            .integerValueOfParameterNamed(periodTypeParamName, locale);
                    final SavingsPeriodFrequencyType periodFrequencyType = SavingsPeriodFrequencyType
                            .fromInt(periodTypeId);
                    final Integer fromPeriod = chartSlabsCommand
                            .integerValueOfParameterNamed(fromPeriodParamName, locale);
                    final Integer toPeriod = chartSlabsCommand.integerValueOfParameterNamed(toPeriodParamName,
                            locale);
                    final BigDecimal amountRangeFrom = chartSlabsCommand
                            .bigDecimalValueOfParameterNamed(amountRangeFromParamName, locale);
                    final BigDecimal amountRangeTo = chartSlabsCommand
                            .bigDecimalValueOfParameterNamed(amountRangeToParamName, locale);
                    final BigDecimal annualInterestRate = chartSlabsCommand
                            .bigDecimalValueOfParameterNamed(annualInterestRateParamName, locale);

                    final InterestRateChartSlabFields slabFields = InterestRateChartSlabFields.createNew(
                            description, periodFrequencyType, fromPeriod, toPeriod, amountRangeFrom,
                            amountRangeTo, annualInterestRate, currencyCode);
                    final InterestRateChartSlab chartSlab = InterestRateChartSlab.createNew(slabFields, this);
                    chartSlab.slabFields().validateChartSlabPlatformRules(chartSlabsCommand, baseDataValidator,
                            locale);
                    chartSlab.updateIncentives(chartSlabsCommand, actualChanges, baseDataValidator, chartSlab,
                            locale);
                    this.addChartSlab(chartSlab);
                }
            }
        }
    }

    // add chart slab changes to actual changes list.
    if (!chartSlabsChanges.isEmpty()) {
        actualChanges.put(InterestRateChartApiConstants.chartSlabs, chartSlabsChanges);
    }

    // add deleted chart Slabs to actual changes
    if (!deleteChartSlabs.isEmpty()) {
        actualChanges.put("deletedChartSlabs", deleteChartSlabs);
    }

    this.validateChartSlabs(baseDataValidator);
}