From f640c790c3b9fd66fa5c18086d0e976b3fdcd60c Mon Sep 17 00:00:00 2001 From: Adam Saghy Date: Tue, 1 Aug 2023 12:53:58 +0200 Subject: [PATCH] FINERACT-1724: Fix `update last cob date of loan update` query --- .../CustomLoanAccountLockRepository.java | 24 +++++++++ .../CustomLoanAccountLockRepositoryImpl.java | 52 +++++++++++++++++++ .../cob/domain/LoanAccountLockRepository.java | 17 +----- 3 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepository.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepositoryImpl.java diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepository.java b/fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepository.java new file mode 100644 index 00000000000..765db8e28ca --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepository.java @@ -0,0 +1,24 @@ +/** + * 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.cob.domain; + +public interface CustomLoanAccountLockRepository { + + void updateLoanFromAccountLocks(); +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepositoryImpl.java new file mode 100644 index 00000000000..1a4a3e90853 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/domain/CustomLoanAccountLockRepositoryImpl.java @@ -0,0 +1,52 @@ +/** + * 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.cob.domain; + +import jakarta.persistence.EntityManager; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class CustomLoanAccountLockRepositoryImpl implements CustomLoanAccountLockRepository { + + private final EntityManager entityManager; + private final DatabaseSpecificSQLGenerator databaseSpecificSQLGenerator; + + @Override + public void updateLoanFromAccountLocks() { + String sql = "UPDATE m_loan SET last_closed_business_date = (select " + + databaseSpecificSQLGenerator.subDate("lck.lock_placed_on_cob_business_date", "1", "DAY") + + """ + from m_loan_account_locks lck + where lck.loan_id = id + and lck.lock_placed_on_cob_business_date is not null + and lck.error is not null + and lck.lock_owner in ('LOAN_COB_CHUNK_PROCESSING','LOAN_INLINE_COB_PROCESSING')) + where last_closed_business_date is null and exists (select lck.loan_id + from m_loan_account_locks lck where lck.loan_id = id + and lck.lock_placed_on_cob_business_date is not null and lck.error is not null + and lck.lock_owner in ('LOAN_COB_CHUNK_PROCESSING','LOAN_INLINE_COB_PROCESSING')) + """; + + entityManager.createNativeQuery(sql).executeUpdate(); + entityManager.flush(); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/domain/LoanAccountLockRepository.java b/fineract-provider/src/main/java/org/apache/fineract/cob/domain/LoanAccountLockRepository.java index 40550a51796..46d6dc9dd59 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/cob/domain/LoanAccountLockRepository.java +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/domain/LoanAccountLockRepository.java @@ -25,7 +25,8 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; -public interface LoanAccountLockRepository extends JpaRepository, JpaSpecificationExecutor { +public interface LoanAccountLockRepository + extends CustomLoanAccountLockRepository, JpaRepository, JpaSpecificationExecutor { Optional findByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner); @@ -35,20 +36,6 @@ public interface LoanAccountLockRepository extends JpaRepository