Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: P4ADEV-1636 WF ClassifyIuvActivity: define classification labels #72

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import it.gov.pagopa.payhub.activities.dao.PaymentsReportingDao;
import it.gov.pagopa.payhub.activities.dao.TransferDao;
import it.gov.pagopa.payhub.activities.dao.TreasuryDao;
import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import it.gov.pagopa.payhub.activities.exception.ClassificationException;
import it.gov.pagopa.payhub.activities.service.classifications.ClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

@Lazy
@Slf4j
@Component
Expand All @@ -19,15 +24,18 @@ public class TransferClassificationActivityImpl implements TransferClassificatio
private final TransferDao transferDao;
private final PaymentsReportingDao paymentsReportingDao;
private final TreasuryDao treasuryDao;
private final ClassificationService classificationService;

public TransferClassificationActivityImpl(ClassificationDao classificationDao,
TransferDao transferDao,
PaymentsReportingDao paymentsReportingDao,
TreasuryDao treasuryDao) {
TransferDao transferDao,
PaymentsReportingDao paymentsReportingDao,
TreasuryDao treasuryDao,
ClassificationService classificationService) {
this.classificationDao = classificationDao;
this.transferDao = transferDao;
this.paymentsReportingDao = paymentsReportingDao;
this.treasuryDao = treasuryDao;
this.classificationService = classificationService;
}

@Override
Expand All @@ -36,14 +44,22 @@ public void classify(Long orgId, String iuv, String iur, int transferIndex) {
if (!classificationDao.deleteTransferClassification(orgId, iuv, iur, transferIndex)) {
throw new ClassificationException("Error occurred while clean up current processing Requests due to failed deletion");
}
transferDao.findBySemanticKey(orgId, iuv, iur, transferIndex);
TransferDTO transferDTO = transferDao.findBySemanticKey(orgId, iuv, iur, transferIndex);

log.info("Retrieve payment reporting for organization id: {} and iuv: {} and iur {} and transfer index: {}", orgId, iuv, iur, transferIndex);
PaymentsReportingDTO paymentsReportingDTO = paymentsReportingDao.findBySemanticKey(orgId, iuv, iur, transferIndex);
if (paymentsReportingDTO!=null) {
TreasuryDTO treasuryDTO = retrieveTreasury(orgId, paymentsReportingDTO);
List<ClassificationsEnum> classifications = classificationService.defineLabels(transferDTO, paymentsReportingDTO, treasuryDTO);
log.info("Labels defined for organization id: {} and iuv: {} and iur {} and transfer index: {} are: {}",
orgId, iuv, iur, transferIndex, String.join(", ", classifications.stream().map(String::valueOf).toList()));
}

private TreasuryDTO retrieveTreasury(Long orgId, PaymentsReportingDTO paymentsReportingDTO) {
if (paymentsReportingDTO != null) {
String iuf = paymentsReportingDTO.getIuf();
log.info("Retrieve treasury for organization id: {} and iuf {}", orgId, iuf);
treasuryDao.getByOrganizationIdAndIuf(orgId, iuf);
return treasuryDao.getByOrganizationIdAndIuf(orgId, iuf);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import it.gov.pagopa.payhub.activities.dto.TransferDTO;

import java.util.List;

/**
* Data Access Object (DAO) interface for handling operations related to
* `Transfer` objects.
Expand All @@ -12,5 +10,14 @@
*/
public interface TransferDao {

/**
* find transfer by semantic key
*
* @param orgId organization id
* @param iuv payment identifier
* @param iur reporting identifier
* @param transferIndex transfer index
* @return TransferDTO object returned
*/
TransferDTO findBySemanticKey(Long orgId, String iuv, String iur, int transferIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;


public interface TreasuryDao {

Long insert(TreasuryDTO treasuryDto);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import it.gov.pagopa.payhub.activities.exception.ClassificationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* Service for classifying a transfer based on receipt, payment reporting, and treasury data.
* It uses a list of {@link LabelClassifier} to determine the corresponding labels.
*/
@Lazy
@Slf4j
@Service
public class ClassificationService {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
private final List<LabelClassifier> classifiers;

public ClassificationService(List<LabelClassifier> classifiers) {
this.classifiers = classifiers;
}

/**
* Determines a list of labels ({@link ClassificationsEnum}) based on the provided data.
*
* For each classifier in the list, the {@code define} method is executed with the given parameters.
* Non-{@code null} labels are collected into a list. If no classifier produces a valid label,
* a {@link ClassificationException} is thrown.
*
* @param transferDTO the transfer data.
* @param paymentsReportingDTO the payment reporting data.
* @param treasuryDTO the treasury data.
* @return a list of valid labels generated by the classifiers.
* @throws ClassificationException if no valid label is determined.
*/
public List<ClassificationsEnum> defineLabels(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
List<ClassificationsEnum> labels = classifiers.stream()
.map(classifier -> classifier.define(transferDTO, paymentsReportingDTO, treasuryDTO))
.filter(Objects::nonNull)
.toList();

if (labels.isEmpty()) {
throw new ClassificationException("Cannot define classification");
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
}
return labels;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.gov.pagopa.payhub.activities.service.classifications;
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Lazy
@Component
public class IufNoTesClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
if(paymentsReportingDTO != null && treasuryDTO == null) {
return ClassificationsEnum.IUF_NO_TES;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Lazy
@Component
public class IufTesDivImpClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
if (paymentsReportingDTO != null && treasuryDTO != null &&
Objects.equals(paymentsReportingDTO.getTotalAmountCents(), treasuryDTO.getBillIpNumber().movePointRight(2).longValueExact())) {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
return ClassificationsEnum.IUF_TES_DIV_IMP;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Lazy
@Component
public class IuvNoRtClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
if(paymentsReportingDTO != null && transferDTO == null) {
return ClassificationsEnum.IUV_NO_RT;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;

/**
* Interface for defining classification logic for transfers based on provided data.
*
* Implementations of this interface contain specific rules to evaluate
* and determine a classification label ({@link ClassificationsEnum}). Each implementation
* should handle a particular condition or set of conditions to decide the appropriate label.
*/
public interface LabelClassifier {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Defines a classification label ({@link ClassificationsEnum}) based on the given data.
*
* @param transferDTO the transfer data.
* @param paymentsReportingDTO the payment reporting data.
* @param treasuryDTO the treasury data.
* @return the classification label if conditions are met, or {@code null} if no label is applicable.
*/
ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO);
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Lazy
@Component
public class RtIufClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
if (transferDTO != null && paymentsReportingDTO != null && Objects.equals(transferDTO.getAmount(), paymentsReportingDTO.getAmountPaidCents())) {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
return ClassificationsEnum.RT_IUF;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Lazy
@Component
public class RtIufTesClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
if(transferDTO != null && paymentsReportingDTO != null && treasuryDTO != null &&
Objects.equals(transferDTO.getAmount(), paymentsReportingDTO.getAmountPaidCents()) &&
Objects.equals(paymentsReportingDTO.getAmountPaidCents(), treasuryDTO.getBillIpNumber().movePointRight(2).longValueExact())) {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
return ClassificationsEnum.RT_IUF_TES;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Lazy
@Component
public class RtNoIufClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
if(transferDTO != null && paymentsReportingDTO == null && treasuryDTO == null) {
return ClassificationsEnum.RT_NO_IUF;
}
if (transferDTO != null && paymentsReportingDTO != null && treasuryDTO == null && Objects.equals(transferDTO.getAmount(), paymentsReportingDTO.getAmountPaidCents())) {
return ClassificationsEnum.RT_NO_IUF;
}
if (transferDTO != null && treasuryDTO != null && paymentsReportingDTO == null && Objects.equals(transferDTO.getAmount(), treasuryDTO.getBillIpNumber().movePointRight(2).longValueExact())) {
return ClassificationsEnum.RT_NO_IUF;
}
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package it.gov.pagopa.payhub.activities.service.classifications;

import it.gov.pagopa.payhub.activities.dto.TransferDTO;
import it.gov.pagopa.payhub.activities.dto.paymentsreporting.PaymentsReportingDTO;
import it.gov.pagopa.payhub.activities.dto.treasury.TreasuryDTO;
import it.gov.pagopa.payhub.activities.enums.ClassificationsEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Lazy
@Component
public class RtTesClassifier implements LabelClassifier {

@Override
public ClassificationsEnum define(TransferDTO transferDTO, PaymentsReportingDTO paymentsReportingDTO, TreasuryDTO treasuryDTO) {
if(transferDTO != null && treasuryDTO != null && paymentsReportingDTO == null &&
Objects.equals(transferDTO.getAmount(), treasuryDTO.getBillIpNumber().movePointRight(2).longValueExact())) {
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved
return ClassificationsEnum.RT_TES;
}
return null;
}
}
Loading
Loading