Skip to content

Commit

Permalink
FINERACT-1724: Fix update last cob date of loan update query
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsaghy committed Aug 1, 2023
1 parent 987d414 commit f640c79
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface LoanAccountLockRepository extends JpaRepository<LoanAccountLock, Long>, JpaSpecificationExecutor<LoanAccountLock> {
public interface LoanAccountLockRepository
extends CustomLoanAccountLockRepository, JpaRepository<LoanAccountLock, Long>, JpaSpecificationExecutor<LoanAccountLock> {

Optional<LoanAccountLock> findByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner);

Expand All @@ -35,20 +36,6 @@ public interface LoanAccountLockRepository extends JpaRepository<LoanAccountLock

boolean existsByLoanIdAndLockOwner(Long loanId, LockOwner lockOwner);

@Query(value = """
update m_loan set last_closed_business_date = (select lck.lock_placed_on_cob_business_date - 1
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'))""", nativeQuery = true)
@Modifying(flushAutomatically = true)
void updateLoanFromAccountLocks();

@Query("""
delete from LoanAccountLock lck where lck.lockPlacedOnCobBusinessDate is not null and lck.error is not null and
lck.lockOwner in (org.apache.fineract.cob.domain.LockOwner.LOAN_COB_CHUNK_PROCESSING,org.apache.fineract.cob.domain.LockOwner.LOAN_INLINE_COB_PROCESSING)
Expand Down

0 comments on commit f640c79

Please sign in to comment.