|
| 1 | +package tech.artcoded.websitev2.peppol; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.util.Date; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.apache.camel.Body; |
| 10 | +import org.apache.camel.Exchange; |
| 11 | +import org.apache.camel.Header; |
| 12 | +import org.apache.camel.builder.RouteBuilder; |
| 13 | +import org.springframework.beans.factory.annotation.Value; |
| 14 | +import org.springframework.stereotype.Service; |
| 15 | + |
| 16 | +import lombok.SneakyThrows; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | +import tech.artcoded.websitev2.pages.fee.FeeService; |
| 19 | +import tech.artcoded.websitev2.pages.invoice.InvoiceGeneration; |
| 20 | +import tech.artcoded.websitev2.pages.invoice.InvoiceGenerationRepository; |
| 21 | +import tech.artcoded.websitev2.rest.util.MockMultipartFile; |
| 22 | +import tech.artcoded.websitev2.upload.FileUploadService; |
| 23 | + |
| 24 | +@Service |
| 25 | +@Slf4j |
| 26 | +public class PeppolService extends RouteBuilder { |
| 27 | + |
| 28 | + @Value("${application.upload.pathToPeppolExpenditure}") |
| 29 | + private String pathToPeppolExpenditure; |
| 30 | + |
| 31 | + @Value("${application.upload.pathToPeppolInvoice}") |
| 32 | + private String pathToPeppolInvoice; |
| 33 | + |
| 34 | + private final FileUploadService uploadService; |
| 35 | + private final FeeService feeService; |
| 36 | + private final InvoiceGenerationRepository invoiceRepository; |
| 37 | + |
| 38 | + public PeppolService(FileUploadService uploadService, |
| 39 | + FeeService feeService, |
| 40 | + InvoiceGenerationRepository invoiceRepository) { |
| 41 | + this.uploadService = uploadService; |
| 42 | + this.invoiceRepository = invoiceRepository; |
| 43 | + this.feeService = feeService; |
| 44 | + } |
| 45 | + |
| 46 | + @SneakyThrows |
| 47 | + public void addInvoice(InvoiceGeneration invoice) { |
| 48 | + log.info("receiving invoice {}. copying it to {} and set status to processing...", invoice.getId(), |
| 49 | + pathToPeppolInvoice); |
| 50 | + var ubl = this.uploadService.findOneById(invoice.getInvoiceUBLId()) |
| 51 | + .orElseThrow(() -> new RuntimeException("file with id %s not found".formatted(invoice.getInvoiceUBLId()))); |
| 52 | + |
| 53 | + var file = this.uploadService.getFile(ubl); |
| 54 | + var out = new File(pathToPeppolInvoice, "%s.xml".formatted(invoice.getId())); |
| 55 | + Files.copy(file.toPath(), out.toPath()); |
| 56 | + this.invoiceRepository.findById(invoice.getId()) |
| 57 | + .map(i -> i.toBuilder().peppolStatus(PeppolStatus.PROCESSING).build()) |
| 58 | + .ifPresent(invoiceRepository::save); |
| 59 | + } |
| 60 | + |
| 61 | + void updatePeppolStatus(@Header(Exchange.FILE_NAME) String fileName) throws IOException { |
| 62 | + var invoiceId = fileName.replace(".xml", ""); |
| 63 | + invoiceRepository.findById(invoiceId).map(i -> i.toBuilder().peppolStatus(PeppolStatus.SUCCESS).build()) |
| 64 | + .ifPresentOrElse(invoiceRepository::save, |
| 65 | + () -> log.error(fileName + ": cannot extract invoice id or invoice doesn't exist.")); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @SneakyThrows |
| 70 | + void pushFee(@Body File file, @Header(Exchange.FILE_NAME) String fileName, |
| 71 | + @Header(Exchange.FILE_CONTENT_TYPE) String contentType) { |
| 72 | + |
| 73 | + this.feeService.save("[PEPPOL]: " + fileName, "New peppol expense", new Date(), List.of(MockMultipartFile.builder() |
| 74 | + .originalFilename(fileName) |
| 75 | + .contentType(contentType) |
| 76 | + .name(fileName) |
| 77 | + .bytes(Files.readAllBytes(file.toPath())) |
| 78 | + .build())); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void configure() throws Exception { |
| 84 | + onException(Exception.class) |
| 85 | + .handled(true) |
| 86 | + .transform().simple("Exception occurred due: ${exception.message}") |
| 87 | + .log("${body}"); |
| 88 | + |
| 89 | + from("file:%s/success?noop=true&idempotent=true&idempotentRepository=#fileIdempotentRepository" |
| 90 | + .formatted(pathToPeppolInvoice)) |
| 91 | + .routeId("Peppol::UpdateProcessedInvoices") |
| 92 | + .log("receiving file '${headers.%s}', will update peppol status".formatted(Exchange.FILE_NAME)) |
| 93 | + .bean(() -> this, "updatePeppolStatus"); |
| 94 | + |
| 95 | + from("file:" + pathToPeppolExpenditure) |
| 96 | + .routeId("Peppol::PeppolExpenseToFee") |
| 97 | + .log("receiving file '${headers.%s}', will convert it to fee".formatted(Exchange.FILE_NAME)) |
| 98 | + .bean(() -> this, "pushFee"); |
| 99 | + } |
| 100 | +} |
0 commit comments