vidakovic commented on code in PR #6167: URL: https://github.com/apache/fineract/pull/6167#discussion_r3747781626
########## fineract-provider/src/main/java/org/apache/fineract/portfolio/charge/service/ChargeWriteServiceImpl.java: ########## @@ -0,0 +1,491 @@ +/** + * 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 org.apache.fineract.portfolio.charge.service; + +import static org.apache.fineract.portfolio.charge.domain.Charge.CHARGE_CALCULATION_TYPE_PARAM_NAME; +import static org.apache.fineract.portfolio.charge.domain.Charge.CHARGE_TIME_PARAM_NAME; +import static org.apache.fineract.portfolio.charge.domain.Charge.FEE_FREQUENCY_PARAM_NAME; +import static org.apache.fineract.portfolio.charge.domain.Charge.FEE_INTERVAL_PARAM_NAME; +import static org.apache.fineract.portfolio.charge.domain.Charge.FEE_ON_MONTH_DAY_PARAM_NAME; +import static org.apache.fineract.portfolio.charge.domain.Charge.LOCALE_PARAM_NAME; + +import jakarta.persistence.PersistenceException; +import jakarta.validation.ConstraintViolation; +import jakarta.validation.ConstraintViolationException; +import jakarta.validation.Validator; +import java.math.BigDecimal; +import java.time.MonthDay; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.LocaleUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.fineract.accounting.glaccount.domain.GLAccount; +import org.apache.fineract.accounting.glaccount.domain.GLAccountRepositoryWrapper; +import org.apache.fineract.infrastructure.core.exception.ErrorHandler; +import org.apache.fineract.infrastructure.core.exception.PlatformDataIntegrityException; +import org.apache.fineract.infrastructure.entityaccess.domain.FineractEntityAccessType; +import org.apache.fineract.infrastructure.entityaccess.service.FineractEntityAccessUtil; +import org.apache.fineract.portfolio.charge.api.ChargesApiConstants; +import org.apache.fineract.portfolio.charge.data.ChargeCreateRequest; +import org.apache.fineract.portfolio.charge.data.ChargeCreateResponse; +import org.apache.fineract.portfolio.charge.data.ChargeDeleteRequest; +import org.apache.fineract.portfolio.charge.data.ChargeDeleteResponse; +import org.apache.fineract.portfolio.charge.data.ChargeUpdateRequest; +import org.apache.fineract.portfolio.charge.data.ChargeUpdateResponse; +import org.apache.fineract.portfolio.charge.domain.Charge; +import org.apache.fineract.portfolio.charge.domain.ChargeAppliesTo; +import org.apache.fineract.portfolio.charge.domain.ChargeCalculationType; +import org.apache.fineract.portfolio.charge.domain.ChargePaymentMode; +import org.apache.fineract.portfolio.charge.domain.ChargeRepository; +import org.apache.fineract.portfolio.charge.domain.ChargeTimeType; +import org.apache.fineract.portfolio.charge.exception.ChargeCannotBeDeletedException; +import org.apache.fineract.portfolio.charge.exception.ChargeCannotBeUpdatedException; +import org.apache.fineract.portfolio.charge.exception.ChargeDueAtDisbursementCannotBePenaltyException; +import org.apache.fineract.portfolio.charge.exception.ChargeMustBePenaltyException; +import org.apache.fineract.portfolio.charge.exception.ChargeNotFoundException; +import org.apache.fineract.portfolio.charge.exception.ChargeParameterUpdateNotSupportedException; +import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct; +import org.apache.fineract.portfolio.loanproduct.domain.LoanProductRepository; +import org.apache.fineract.portfolio.paymenttype.domain.PaymentType; +import org.apache.fineract.portfolio.paymenttype.domain.PaymentTypeRepository; +import org.apache.fineract.portfolio.paymenttype.exception.PaymentTypeNotFoundException; +import org.apache.fineract.portfolio.tax.domain.TaxGroup; +import org.apache.fineract.portfolio.tax.domain.TaxGroupRepositoryWrapper; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.orm.jpa.JpaSystemException; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@RequiredArgsConstructor +public class ChargeWriteServiceImpl implements ChargeWriteService { + + private static final String NAME_PARAM_NAME = "name"; + private static final String CURRENCY_CODE_PARAM_NAME = "currencyCode"; + private static final String AMOUNT_PARAM_NAME = "amount"; + private static final String PENALTY_PARAM_NAME = "penalty"; + private static final String ACTIVE_PARAM_NAME = "active"; + private static final String MIN_CAP_PARAM_NAME = "minCap"; + private static final String MAX_CAP_PARAM_NAME = "maxCap"; + private static final String CHARGE_PAYMENT_MODE_PARAM_NAME = "chargePaymentMode"; + private static final String PAYMENT_TYPE_ID_PARAM_NAME = "paymentTypeId"; + private static final String ENABLE_PAYMENT_TYPE_PARAM_NAME = "enablePaymentType"; + private static final String ENABLE_FREE_WITHDRAWAL_PARAM_NAME = "enableFreeWithdrawalCharge"; + private static final String FREE_WITHDRAWAL_FREQUENCY_PARAM_NAME = "freeWithdrawalFrequency"; + private static final String RESTART_COUNT_FREQUENCY_PARAM_NAME = "restartCountFrequency"; + private static final String COUNT_FREQUENCY_TYPE_PARAM_NAME = "countFrequencyType"; + + private final Validator validator; + private final ChargeRepository chargeRepository; + private final LoanProductRepository loanProductRepository; + private final JdbcTemplate jdbcTemplate; + private final FineractEntityAccessUtil fineractEntityAccessUtil; + private final GLAccountRepositoryWrapper glAccountRepository; + private final TaxGroupRepositoryWrapper taxGroupRepository; + private final PaymentTypeRepository paymentTypeRepository; + + @Transactional + @Override + @CacheEvict(value = "charges", key = "T(org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil).getTenant().getTenantIdentifier().concat('ch')") + public ChargeCreateResponse create(final ChargeCreateRequest request) { + try { + validate(request); + + TaxGroup taxGroup = null; + if (request.getTaxGroupId() != null) { + taxGroup = this.taxGroupRepository.findOneWithNotFoundDetection(request.getTaxGroupId()); + } + + PaymentType paymentType = null; + if (Boolean.TRUE.equals(request.getEnablePaymentType()) && request.getPaymentTypeId() != null) { + paymentType = findPaymentTypeWithNotFoundDetection(request.getPaymentTypeId()); + } + + final Charge charge = buildCharge(request, taxGroup, paymentType); + this.chargeRepository.saveAndFlush(charge); + + // check if the office specific products are enabled. If yes, then save this charge against a specific + // office i.e. this charge is specific for this office. + fineractEntityAccessUtil.checkConfigurationAndAddProductResrictionsForUserOffice( Review Comment: I'm not sure if we can solve this one here, but overall that utility class is bad practice. First we are doing the check very late in the execution path whereas this should happen as early as possible (e.g. in the REST API layer aka in `SecurityConfig.java`. Sometimes the information is dynamic and you can't just write a static authorization rule, but for that Spring Security has `AuthorizationManager`s. Also: the function name of the utility class suggests that this is a read only function, but in fact some data is stored to the database. Not only misleading, but too many concerns in one place are being dealt with. At least 3 different entities (charge, loan product, savings product) are involved here. Leave for now, because this seems to be more work for a different day. Just informational. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
