Last active
November 6, 2020 10:12
spring EventListener example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.forleven.finance.audit.controller; | |
import com.forleven.finance.audit.event.InvoiceAuditEvent; | |
import com.forleven.finance.audit.form.InvoiceAuditObjectForm; | |
import lombok.extern.slf4j.Slf4j; | |
import org.joda.time.DateTime; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationEventPublisher; | |
import org.springframework.core.task.TaskExecutor; | |
import org.springframework.data.jpa.domain.Specification; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.web.bind.annotation.*; | |
import com.forleven.finance.invoice.model.Invoice; | |
import com.forleven.finance.invoice.specifications.InvoiceSpecification; | |
import com.forleven.finance.invoice.util.Situation; | |
import springfox.documentation.annotations.ApiIgnore; | |
import java.util.*; | |
import static org.springframework.data.jpa.domain.Specification.where; | |
@Slf4j | |
@RestController | |
@RequestMapping("/audit/invoice") | |
@ApiIgnore | |
public class InvoiceAuditController { | |
@Autowired | |
private TaskExecutor asyncExecutor; | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@PostMapping | |
@ResponseStatus(HttpStatus.ACCEPTED) | |
public void publishInvoiceAudit( | |
@RequestBody InvoiceAuditObjectForm invoiceAuditObjectForm | |
) { | |
log.info("Initial audit to payment gateway not send payments"); | |
Specification<Invoice> spec = InvoiceSpecification.withSituation(Situation.EXPIRED) | |
.and(InvoiceSpecification.withoutPayment()) | |
.and(InvoiceSpecification.withPaymentGatewayProvider()) | |
.and(InvoiceSpecification.withInstitutionWalletBank(invoiceAuditObjectForm.getIdInstitutionWallet())) | |
// .and(InvoiceSpecification.withIdInstitutionWallet(invoiceAuditObjectForm.getIdInstitutionWallet())) | |
.and(InvoiceSpecification.withDueDate(new DateTime(invoiceAuditObjectForm.getDueDate()).toDate())) | |
.and(InvoiceSpecification.hasStatusActive()); | |
InvoiceAuditEvent invoiceAuditEvent = InvoiceAuditEvent.builder() | |
.spec(spec) | |
.build(); | |
log.info("event"); | |
publisher.publishEvent(invoiceAuditEvent); | |
log.info("Publish event"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.forleven.finance.audit.event; | |
import com.forleven.finance.invoice.model.Invoice; | |
import lombok.Builder; | |
import lombok.Data; | |
import lombok.experimental.Wither; | |
import org.springframework.data.jpa.domain.Specification; | |
@Builder | |
@Wither | |
@Data | |
public class InvoiceAuditEvent { | |
private Specification<Invoice> spec; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.forleven.finance.audit.listener; | |
import com.forleven.finance.audit.event.InvoiceAuditEvent; | |
import com.forleven.finance.invoice.form.InvoicePaymentForm; | |
import com.forleven.finance.invoice.form.InvoicePaymentMethodForm; | |
import com.forleven.finance.invoice.model.Invoice; | |
import com.forleven.finance.invoice.model.InvoicePaymentDetails; | |
import com.forleven.finance.invoice.service.InvoicePaymentDetailsService; | |
import com.forleven.finance.invoice.service.InvoicePaymentService; | |
import com.forleven.finance.invoice.service.InvoiceService; | |
import com.forleven.finance.invoice.util.InvoicePaymentDetailsType; | |
import com.forleven.finance.invoice.util.Operation; | |
import com.forleven.finance.payment.gateway.core.PaymentGateway; | |
import com.forleven.finance.payment.gateway.invoice.PaymentGatewayInvoice; | |
import com.forleven.finance.util.GeneralSpecification; | |
import com.forleven.finance.wallet.model.InstitutionWalletDetails; | |
import com.forleven.finance.wallet.model.InstitutionWalletPaymentGatewayProvider; | |
import com.forleven.finance.wallet.service.InstitutionWalletDetailsService; | |
import com.forleven.finance.wallet.service.InstitutionWalletTokenService; | |
import com.forleven.finance.wallet.specifications.InstitutionWalletPaymentGatewayProviderSpecification; | |
import com.forleven.finance.wallet.util.PaymentGatewayTokenType; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationEventPublisher; | |
import org.springframework.context.ApplicationListener; | |
import org.springframework.context.event.EventListener; | |
import org.springframework.core.task.TaskExecutor; | |
import org.springframework.data.jpa.domain.Specification; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.stereotype.Component; | |
import java.math.BigDecimal; | |
import java.util.*; | |
import java.util.concurrent.CompletableFuture; | |
import static org.springframework.data.jpa.domain.Specification.where; | |
@Slf4j | |
@Component | |
public class InvoiceAuditListener { | |
@Autowired | |
private TaskExecutor asyncExecutor; | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@Autowired | |
private InvoiceService invoiceService; | |
@Autowired | |
private InstitutionWalletTokenService institutionWalletTokenService; | |
@Autowired | |
private PaymentGateway paymentGateway; | |
@Autowired | |
private InvoicePaymentService invoicePaymentService; | |
@Autowired | |
private InvoicePaymentDetailsService invoicePaymentDetailsService; | |
@Autowired | |
private InstitutionWalletDetailsService institutionWalletDetailsService; | |
@Async | |
@EventListener | |
public void handleEvent(InvoiceAuditEvent spec) { | |
log.info("Deu '{}' invoices para ver ", invoiceService.getAllInvoice(spec.getSpec()).orElse(Collections.emptyList()).size()); | |
invoiceService.getAllInvoice(spec.getSpec()) | |
.orElse(Collections.emptyList()) | |
.forEach(invoice -> { | |
log.info("!!!!!!!!!!!! INVOICE '{}' TO CHECK !!!!!!!!!!!! ", invoice.getIdInvoice()); | |
Specification<InstitutionWalletPaymentGatewayProvider> specWallet = | |
where(InstitutionWalletPaymentGatewayProviderSpecification.withCustomerInstallment(invoice.getIdInstitutionCustomerInstallment())) | |
.and(GeneralSpecification.hasStatusActive()); | |
institutionWalletTokenService.getWalletToken(specWallet, PaymentGatewayTokenType.USER_TOKEN).forEach(walletToken -> { | |
PaymentGatewayInvoice gatewayInvoice = PaymentGatewayInvoice.builder() | |
.id(invoice.getProvider().get(0).getIdentifierInvoicePaymentGatewayProvider()) | |
.userToken(walletToken.getTokenProvider().getValue()) | |
.build(); | |
CompletableFuture<Optional<PaymentGatewayInvoice>> gatewayResponse = | |
PaymentGateway.run(paymentGateway.retrieve(gatewayInvoice), asyncExecutor); | |
gatewayResponse.thenAccept(paymentGatewayInvoice -> { | |
Optional<PaymentGatewayInvoice> paymentGatewayInvoice1 = paymentGatewayInvoice.filter(invoiceRetrieved -> invoiceRetrieved.getDateOfPayment() != null); | |
paymentGatewayInvoice1.ifPresent(invoiceToUpdate -> { | |
String paymentMethod = invoiceToUpdate.getPaymentGatewayInvoicePayment().getPaymentMethod(); | |
BigDecimal totalPaid = invoiceToUpdate.getPaymentGatewayInvoicePayment().getAmount(); | |
Date dateOfPayment = invoiceToUpdate.getPaymentGatewayInvoicePayment().getPaymentDate(); | |
log.info("Total Paid Value '{}'", totalPaid); | |
InvoicePaymentForm.InvoicePaymentFormBuilder paymentFormBuilder = InvoicePaymentForm.builder() | |
.isGatewayPayment(true) | |
.idInvoice(invoice.getIdInvoice()) | |
.amount(totalPaid) | |
.paymentDate(dateOfPayment) | |
.ignoreLessAmount(true) | |
.createDate(invoiceToUpdate.getDateOfPayment()) | |
.invoicePaymentMethods(Collections.singletonList( | |
InvoicePaymentMethodForm.builder() | |
.idInvoicePayment(invoice.getIdInvoice()) | |
.paymentMethod(paymentMethod) | |
.value(totalPaid) | |
.build())); | |
InvoicePaymentForm paymentForm = paymentFormBuilder.build(); | |
invoicePaymentService.saveInvoicePayment(paymentForm).forEach(invoicePayment -> { | |
log.info("Invoice Payment '{}' Saved !", invoicePayment.getIdInvoicePayment()); | |
List<InvoicePaymentDetails> paymentDetails = Arrays.asList( | |
InvoicePaymentDetails.builder() | |
.idInvoicePayment(invoicePayment.getIdInvoicePayment()) | |
.detailsType(InvoicePaymentDetailsType.COMMISSION) | |
.value(invoiceToUpdate.getCommission()) | |
.operation(Operation.SUB) | |
.build(), | |
InvoicePaymentDetails.builder() | |
.idInvoicePayment(invoicePayment.getIdInvoicePayment()) | |
.detailsType(InvoicePaymentDetailsType.TAX) | |
.value(invoiceToUpdate.getTax()) | |
.operation(Operation.SUB) | |
.build() | |
); | |
log.info("Now trying to save invoice Details"); | |
invoicePaymentDetailsService.saveInvoicePaymentDetails(paymentDetails); | |
log.info("Now mark wallet '{}' with payments in progress", walletToken.getGatewayProvider().getIdInstitutionWallet()); | |
InstitutionWalletDetails details = InstitutionWalletDetails.builder() | |
.idInstitutionWallet(walletToken.getGatewayProvider().getIdInstitutionWallet()) | |
.name("payments_in_progress") | |
.value("1") | |
.build(); | |
institutionWalletDetailsService.saveWalletDetails(details); | |
}); | |
}); | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment