FINERACT-1694: External event storage integration

This commit is contained in:
Arnold Galovics
2022-09-02 10:03:09 +02:00
committed by Arnold Gálovics
parent 5a9ddab671
commit f46abaf9e2
29 changed files with 386 additions and 60 deletions
@@ -54,7 +54,7 @@ public class ExternalEvent extends AbstractPersistableCustom {
@Setter
private ExternalEventStatus status;
@Column(name = "sent_at", nullable = false)
@Column(name = "sent_at", nullable = true)
@Setter
private OffsetDateTime sentAt;
@@ -45,6 +45,14 @@ public class DelayedExternalEventService {
return !localEventStorage.get().isEmpty();
}
public void clearEnqueuedEvents() {
localEventStorage.get().clear();
}
public List<BusinessEvent<?>> getEnqueuedEvents() {
return List.copyOf(localEventStorage.get());
}
public void postEnqueuedEvents() {
List<BusinessEvent<?>> enqueuedEvents = localEventStorage.get();
if (enqueuedEvents.isEmpty()) {
@@ -1,43 +0,0 @@
/**
* 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.infrastructure.event.external.service.serialization;
import java.util.Comparator;
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.BusinessEventSerializer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
public class BusinessEventSerializerComparator implements Comparator<BusinessEventSerializer> {
@Override
public int compare(BusinessEventSerializer o1, BusinessEventSerializer o2) {
int o1Order = getOrderOrDefault(o1);
int o2Order = getOrderOrDefault(o2);
return Integer.compare(o1Order, o2Order);
}
private int getOrderOrDefault(BusinessEventSerializer serializer) {
Order orderAnnotation = serializer.getClass().getAnnotation(Order.class);
if (orderAnnotation != null) {
return orderAnnotation.value();
} else {
return Ordered.LOWEST_PRECEDENCE;
}
}
}
@@ -19,19 +19,17 @@
package org.apache.fineract.infrastructure.event.external.service.serialization;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.BusinessEventSerializer;
import org.apache.fineract.portfolio.businessevent.domain.BusinessEvent;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class BusinessEventSerializerFactory {
private final List<BusinessEventSerializer> serializers;
public BusinessEventSerializerFactory(List<BusinessEventSerializer> serializers) {
this.serializers = serializers.stream().sorted(new BusinessEventSerializerComparator()).toList();
}
public <T> BusinessEventSerializer create(BusinessEvent<T> event) {
for (BusinessEventSerializer serializer : serializers) {
if (serializer.canSerialize(event)) {
@@ -0,0 +1,29 @@
/**
* 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.infrastructure.event.external.service.serialization.mapper.savings;
import org.apache.fineract.avro.savings.v1.SavingsAccountTransactionDataV1;
import org.apache.fineract.portfolio.savings.data.SavingsAccountTransactionData;
import org.mapstruct.Mapper;
@Mapper(componentModel = "spring")
public interface SavingsAccountTransactionDataMapper {
SavingsAccountTransactionDataV1 map(SavingsAccountTransactionData source);
}
@@ -50,13 +50,18 @@ public class LoanAdjustTransactionBusinessEventSerializer implements BusinessEve
public <T> byte[] serialize(BusinessEvent<T> rawEvent) throws IOException {
LoanAdjustTransactionBusinessEvent event = (LoanAdjustTransactionBusinessEvent) rawEvent;
LoanTransaction transactionToAdjust = event.get().getTransactionToAdjust();
LoanTransaction newTransactionDetail = event.get().getNewTransactionDetail();
LoanTransactionData transactionToAdjustData = service.retrieveLoanTransaction(transactionToAdjust.getLoan().getId(),
transactionToAdjust.getId());
LoanTransactionData newTransactionDetailData = service.retrieveLoanTransaction(newTransactionDetail.getLoan().getId(),
newTransactionDetail.getId());
LoanTransactionDataV1 transactionToAdjustAvroDto = mapper.map(transactionToAdjustData);
LoanTransactionDataV1 newTransactionDetailAvroDto = mapper.map(newTransactionDetailData);
LoanTransaction newTransactionDetail = event.get().getNewTransactionDetail();
LoanTransactionDataV1 newTransactionDetailAvroDto = null;
if (newTransactionDetail != null) {
LoanTransactionData newTransactionDetailData = service.retrieveLoanTransaction(newTransactionDetail.getLoan().getId(),
newTransactionDetail.getId());
newTransactionDetailAvroDto = mapper.map(newTransactionDetailData);
}
LoanTransactionAdjustmentDataV1 avroDto = new LoanTransactionAdjustmentDataV1(transactionToAdjustAvroDto,
newTransactionDetailAvroDto);
ByteBuffer buffer = avroDto.toByteBuffer();
@@ -0,0 +1,58 @@
/**
* 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.infrastructure.event.external.service.serialization.serializer.savings;
import java.io.IOException;
import java.nio.ByteBuffer;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.avro.savings.v1.SavingsAccountTransactionDataV1;
import org.apache.fineract.infrastructure.event.external.service.serialization.mapper.savings.SavingsAccountTransactionDataMapper;
import org.apache.fineract.infrastructure.event.external.service.serialization.serializer.BusinessEventSerializer;
import org.apache.fineract.infrastructure.event.external.service.support.ByteBufferConverter;
import org.apache.fineract.portfolio.businessevent.domain.BusinessEvent;
import org.apache.fineract.portfolio.businessevent.domain.savings.transaction.SavingsAccountTransactionBusinessEvent;
import org.apache.fineract.portfolio.savings.data.SavingsAccountTransactionData;
import org.apache.fineract.portfolio.savings.domain.SavingsAccountTransaction;
import org.apache.fineract.portfolio.savings.service.SavingsAccountReadPlatformService;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class SavingsAccountTransactionBusinessEventSerializer implements BusinessEventSerializer {
private final SavingsAccountReadPlatformService service;
private final SavingsAccountTransactionDataMapper mapper;
private final ByteBufferConverter byteBufferConverter;
@Override
public <T> boolean canSerialize(BusinessEvent<T> event) {
return event instanceof SavingsAccountTransactionBusinessEvent;
}
@Override
public <T> byte[] serialize(BusinessEvent<T> rawEvent) throws IOException {
SavingsAccountTransactionBusinessEvent event = (SavingsAccountTransactionBusinessEvent) rawEvent;
SavingsAccountTransaction tx = event.get();
SavingsAccountTransactionData data = service.retrieveSavingsTransaction(tx.getSavingsAccount().getId(), tx.getId(),
tx.getSavingsAccount().depositAccountType());
SavingsAccountTransactionDataV1 avroDto = mapper.map(data);
ByteBuffer buffer = avroDto.toByteBuffer();
return byteBufferConverter.convert(buffer);
}
}
@@ -212,8 +212,7 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
public LoanAccountData retrieveOne(final Long loanId) {
try {
final AppUser currentUser = this.context.authenticatedUser();
final String hierarchy = currentUser.getOffice().getHierarchy();
final String hierarchy = getHierarchyString();
final String hierarchySearchString = hierarchy + "%";
final LoanMapper rm = new LoanMapper(sqlGenerator, delinquencyReadPlatformService);
@@ -231,6 +230,14 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService {
}
}
private String getHierarchyString() {
AppUser currentUser = null;
if (this.context != null) {
currentUser = this.context.getAuthenticatedUserIfPresent();
}
return Optional.ofNullable(currentUser).map(appUser -> appUser.getOffice().getHierarchy()).orElse(".");
}
@Override
public LoanAccountData retrieveLoanByLoanAccount(String loanAccountNumber) {
@@ -406,7 +406,6 @@ public final class SavingsAccountData implements Serializable {
return this.interestCompoundingPeriodType;
}
public Integer getInterestCompoundingPeriodTypeId() {
return this.interestCompoundingPeriodType.getId().intValue();
}
@@ -24,17 +24,16 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<changeSet author="fineract" id="1">
<delete tableName="m_external_event"/>
<dropColumn tableName="m_external_event" columnName="data"/>
</changeSet>
<changeSet author="fineract" id="2-mysql" context="mysql">
<dropColumn tableName="m_external_event" columnName="data"/>
<addColumn tableName="m_external_event">
<column name="data" type="VARBINARY">
<column name="data" type="BLOB">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
<changeSet author="fineract" id="2-postgresql" context="postgresql">
<dropColumn tableName="m_external_event" columnName="data"/>
<addColumn tableName="m_external_event">
<column name="data" type="BYTEA">
<constraints nullable="false"/>