11package tech .artcoded .websitev2 .peppol ;
22
33import java .io .File ;
4- import java .io .IOException ;
54import 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 ;
135import org .springframework .beans .factory .annotation .Value ;
146import org .springframework .stereotype .Service ;
157
168import lombok .SneakyThrows ;
179import lombok .extern .slf4j .Slf4j ;
18- import tech .artcoded .websitev2 .pages .fee .FeeService ;
1910import tech .artcoded .websitev2 .pages .invoice .InvoiceGeneration ;
2011import tech .artcoded .websitev2 .pages .invoice .InvoiceGenerationRepository ;
21- import tech .artcoded .websitev2 .rest .util .MockMultipartFile ;
2212import tech .artcoded .websitev2 .upload .FileUploadService ;
2313
2414@ Service
2515@ Slf4j
26- public class PeppolService extends RouteBuilder {
16+ public class PeppolService {
2717
2818 @ Value ("${application.upload.pathToPeppolExpenditure}" )
2919 private String pathToPeppolExpenditure ;
@@ -32,15 +22,12 @@ public class PeppolService extends RouteBuilder {
3222 private String pathToPeppolInvoice ;
3323
3424 private final FileUploadService uploadService ;
35- private final FeeService feeService ;
3625 private final InvoiceGenerationRepository invoiceRepository ;
3726
3827 public PeppolService (FileUploadService uploadService ,
39- FeeService feeService ,
4028 InvoiceGenerationRepository invoiceRepository ) {
4129 this .uploadService = uploadService ;
4230 this .invoiceRepository = invoiceRepository ;
43- this .feeService = feeService ;
4431 }
4532
4633 @ SneakyThrows
@@ -58,43 +45,4 @@ public void addInvoice(InvoiceGeneration invoice) {
5845 .ifPresent (invoiceRepository ::save );
5946 }
6047
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- }
10048}
0 commit comments