Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package com.gst.portfolio.interestratechart.domain; import static com.gst.portfolio.interestratechart.InterestIncentiveApiConstants.amountParamName; import static com.gst.portfolio.interestratechart.InterestIncentiveApiConstants.attributeNameParamName; import static com.gst.portfolio.interestratechart.InterestIncentiveApiConstants.attributeValueParamName; import static com.gst.portfolio.interestratechart.InterestIncentiveApiConstants.conditionTypeParamName; import static com.gst.portfolio.interestratechart.InterestIncentiveApiConstants.entityTypeParamName; import static com.gst.portfolio.interestratechart.InterestIncentiveApiConstants.incentiveTypeparamName; import static com.gst.portfolio.interestratechart.InterestRateChartApiConstants.deleteParamName; import static com.gst.portfolio.interestratechart.InterestRateChartApiConstants.idParamName; import static com.gst.portfolio.interestratechart.InterestRateChartSlabApiConstants.INTERESTRATE_CHART_SLAB_RESOURCE_NAME; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import com.gst.infrastructure.core.api.JsonCommand; import com.gst.infrastructure.core.data.ApiParameterError; import com.gst.infrastructure.core.data.DataValidatorBuilder; import com.gst.infrastructure.core.exception.PlatformApiDataValidationException; import com.gst.portfolio.interestratechart.InterestIncentiveApiConstants; import com.gst.portfolio.interestratechart.InterestRateChartSlabApiConstants; import com.gst.infrastructure.core.domain.AbstractPersistableCustom; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @Entity @Table(name = "m_interest_rate_slab") public class InterestRateChartSlab extends AbstractPersistableCustom<Long> { @Embedded private InterestRateChartSlabFields slabFields; @ManyToOne(optional = false) @JoinColumn(name = "interest_rate_chart_id", referencedColumnName = "id", nullable = false) private InterestRateChart interestRateChart; @OneToMany(mappedBy = "interestRateChartSlab", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) private Set<InterestIncentives> interestIncentives = new HashSet<>(); protected InterestRateChartSlab() { // } public static InterestRateChartSlab createNew(InterestRateChartSlabFields slabFields, InterestRateChart interestRateChart) { return new InterestRateChartSlab(slabFields, interestRateChart); } public void update(JsonCommand command, final Map<String, Object> actualChanges, final Locale locale) { final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) .resource(INTERESTRATE_CHART_SLAB_RESOURCE_NAME); this.update(command, actualChanges, baseDataValidator, locale); this.interestRateChart.validateChartSlabs(baseDataValidator); this.throwExceptionIfValidationWarningsExist(dataValidationErrors); } public void update(JsonCommand command, final Map<String, Object> actualChanges, final DataValidatorBuilder baseDataValidator, final Locale locale) { this.slabFields.update(command, actualChanges, baseDataValidator, locale); updateIncentives(command, actualChanges, baseDataValidator, this, locale); } private InterestRateChartSlab(InterestRateChartSlabFields slabFields, InterestRateChart interestRateChart) { this.slabFields = slabFields; this.interestRateChart = interestRateChart; if (this.interestRateChart != null) { interestRateChart.addChartSlab(this); } } public void setInterestRateChart(InterestRateChart interestRateChart) { this.interestRateChart = interestRateChart; } private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) { if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); } } public InterestRateChartSlabFields slabFields() { return this.slabFields; } public Set<InterestIncentives> setOfInterestIncentives() { if (this.interestIncentives == null) { this.interestIncentives = new HashSet<>(); } return this.interestIncentives; } public void addInterestIncentive(InterestIncentives interestIncentives) { interestIncentives.updateInterestRateChartSlab(this); setOfInterestIncentives().add(interestIncentives); } public InterestIncentives findInterestIncentive(Long interestIncentiveId) { final Set<InterestIncentives> interestIncentives = setOfInterestIncentives(); for (InterestIncentives interestIncentive : interestIncentives) { if (interestIncentive.getId().equals(interestIncentiveId)) { return interestIncentive; } } return null; } public boolean removeInterestIncentive(InterestIncentives incentive) { final Set<InterestIncentives> incentives = setOfInterestIncentives(); return incentives.remove(incentive); } public void updateIncentives(JsonCommand command, final Map<String, Object> actualChanges, final DataValidatorBuilder baseDataValidator, final InterestRateChartSlab chartSlab, final Locale locale) { final Map<String, Object> deleteIncentives = new HashMap<>(); final Map<String, Object> IncentiveChanges = new HashMap<>(); if (command.hasParameter(InterestRateChartSlabApiConstants.incentivesParamName)) { final JsonArray array = command .arrayOfParameterNamed(InterestRateChartSlabApiConstants.incentivesParamName); if (array != null) { for (int i = 0; i < array.size(); i++) { final JsonObject incentiveElement = array.get(i).getAsJsonObject(); JsonCommand incentivesCommand = JsonCommand.fromExistingCommand(command, incentiveElement); if (incentivesCommand.parameterExists(InterestIncentiveApiConstants.idParamName)) { final Long interestIncentiveId = incentivesCommand .longValueOfParameterNamed(InterestIncentiveApiConstants.idParamName); final InterestIncentives interestIncentives = chartSlab .findInterestIncentive(interestIncentiveId); if (interestIncentives == null) { baseDataValidator.parameter(InterestIncentiveApiConstants.idParamName) .value(interestIncentiveId) .failWithCode("no.interest.incentive.associated.with.id"); } else if (incentivesCommand.parameterExists(deleteParamName)) { if (chartSlab.removeInterestIncentive(interestIncentives)) { deleteIncentives.put(idParamName, interestIncentiveId); } } else { interestIncentives.update(incentivesCommand, IncentiveChanges, baseDataValidator, locale); } } else { Integer entityType = incentivesCommand.integerValueOfParameterNamed(entityTypeParamName, locale); Integer conditionType = incentivesCommand .integerValueOfParameterNamed(conditionTypeParamName, locale); Integer attributeName = incentivesCommand .integerValueOfParameterNamed(attributeNameParamName, locale); String attributeValue = incentivesCommand .stringValueOfParameterNamed(attributeValueParamName); Integer incentiveType = incentivesCommand .integerValueOfParameterNamed(incentiveTypeparamName, locale); BigDecimal amount = incentivesCommand.bigDecimalValueOfParameterNamed(amountParamName, locale); InterestIncentivesFields incentivesFields = InterestIncentivesFields.createNew(entityType, attributeName, conditionType, attributeValue, incentiveType, amount, baseDataValidator); InterestIncentives incentives = new InterestIncentives(chartSlab, incentivesFields); chartSlab.addInterestIncentive(incentives); } } } // add chart slab changes to actual changes list. if (!IncentiveChanges.isEmpty()) { actualChanges.put(InterestRateChartSlabApiConstants.incentivesParamName, IncentiveChanges); } // add deleted chart Slabs to actual changes if (!deleteIncentives.isEmpty()) { actualChanges.put("deletedIncentives", deleteIncentives); } } } }