77import org .apache .camel .ExchangePattern ;
88import org .apache .camel .Header ;
99import org .apache .camel .builder .RouteBuilder ;
10- import org .apache .commons .compress . utils . FileNameUtils ;
10+ import org .apache .commons .io . FilenameUtils ;
1111import org .apache .commons .io .IOUtils ;
1212import org .apache .jena .rdf .model .ModelFactory ;
1313import org .apache .jena .riot .Lang ;
2323import java .util .UUID ;
2424
2525import static java .util .Optional .ofNullable ;
26- import static org .apache .commons .compress .utils .FileNameUtils .getBaseName ;
2726import static tech .artcoded .triplestore .sparql .route .Constants .*;
2827
2928@ Component
@@ -34,9 +33,9 @@ public class MigrationRouteBuilder extends RouteBuilder {
3433 private String defaultGraph ;
3534
3635 private static final Cache <String , String > GRAPH_CACHE = Caffeine .newBuilder ()
37- .expireAfterAccess (Duration .ofMinutes (5 ))
38- .maximumSize (1000 )
39- .build ();
36+ .expireAfterAccess (Duration .ofMinutes (5 ))
37+ .maximumSize (1000 )
38+ .build ();
4039
4140 public MigrationRouteBuilder (TDBService tdbService ) {
4241 this .tdbService = tdbService ;
@@ -45,50 +44,49 @@ public MigrationRouteBuilder(TDBService tdbService) {
4544 record Migration (String uuid , long count ) {
4645 }
4746
48-
4947 @ Override
5048 public void configure () throws Exception {
5149 onException (Exception .class )
52- .handled (true )
53- .log ("Exception occurred due: ${exception.message}" )
54- ;
50+ .handled (true )
51+ .log ("Exception occurred due: ${exception.message}" );
5552
5653 from ("file:{{triplestore.migration.dir}}?sortBy=file:name;file:modified" )
57- .routeId ("MigrationRoute::Entrypoint" )
58- .log ("receiving file '${headers.%s}', will execute migration to the triplestore" .formatted (Exchange .FILE_NAME ))
59- .convertBodyTo (byte [].class )
60- .choice ()
61- .when (header (Exchange .FILE_NAME ).endsWith ("graph" ))
62- .bean (() -> this , "addGraphToCache" )
63- .otherwise ()
64- .setProperty (HEADER_TITLE , simple ("'${headers.%s}', has been executed to the triplestore" .formatted (Exchange .FILE_NAME )))
65- .setProperty (HEADER_TYPE , constant (SYNC_FILE_TRIPLESTORE ))
66- .to (ExchangePattern .InOnly , "direct:perform-migration" )
67- .endChoice ();
54+ .routeId ("MigrationRoute::Entrypoint" )
55+ .log ("receiving file '${headers.%s}', will execute migration to the triplestore" .formatted (Exchange .FILE_NAME ))
56+ .convertBodyTo (byte [].class )
57+ .choice ()
58+ .when (header (Exchange .FILE_NAME ).endsWith ("graph" ))
59+ .bean (() -> this , "addGraphToCache" )
60+ .otherwise ()
61+ .setProperty (HEADER_TITLE ,
62+ simple ("'${headers.%s}', has been executed to the triplestore" .formatted (Exchange .FILE_NAME )))
63+ .setProperty (HEADER_TYPE , constant (SYNC_FILE_TRIPLESTORE ))
64+ .to (ExchangePattern .InOnly , "direct:perform-migration" )
65+ .endChoice ();
6866
6967 from ("direct:perform-migration" )
70- .routeId ("MigrationRoute::PerformMigrationInternal" )
71- .bean (() -> this , "performMigration" )
72- .choice ()
73- .when (simple ("${body.count} > 0" ))
74- .transform (simple ("${body.uuid}" ))
75- .setHeader (CORRELATION_ID , body ())
76- .setHeader (HEADER_TITLE , exchangeProperty (HEADER_TITLE ))
77- .setHeader (HEADER_TYPE , exchangeProperty (HEADER_TYPE ))
78- .to (ExchangePattern .InOnly , NOTIFICATION_ENDPOINT )
79- .otherwise ()
80- .log ("No triples changed" )
81- .endChoice ();
68+ .routeId ("MigrationRoute::PerformMigrationInternal" )
69+ .bean (() -> this , "performMigration" )
70+ .choice ()
71+ .when (simple ("${body.count} > 0" ))
72+ .transform (simple ("${body.uuid}" ))
73+ .setHeader (CORRELATION_ID , body ())
74+ .setHeader (HEADER_TITLE , exchangeProperty (HEADER_TITLE ))
75+ .setHeader (HEADER_TYPE , exchangeProperty (HEADER_TYPE ))
76+ .to (ExchangePattern .InOnly , NOTIFICATION_ENDPOINT )
77+ .otherwise ()
78+ .log ("No triples changed" )
79+ .endChoice ();
8280 }
8381
8482 void addGraphToCache (@ Body byte [] file ,
85- @ Header (Exchange .FILE_NAME ) String fileName ) {
86- GRAPH_CACHE .put (getBaseName (fileName ), IOUtils .toString (file , StandardCharsets .UTF_8 .name ()));
83+ @ Header (Exchange .FILE_NAME ) String fileName ) {
84+ GRAPH_CACHE .put (FilenameUtils . getBaseName (fileName ), IOUtils .toString (file , StandardCharsets .UTF_8 .name ()));
8785 }
8886
8987 Migration performMigration (@ Body byte [] file ,
90- @ Header (Exchange .FILE_NAME ) String fileName ) {
91- String extension = FileNameUtils .getExtension (fileName );
88+ @ Header (Exchange .FILE_NAME ) String fileName ) {
89+ String extension = FilenameUtils .getExtension (fileName );
9290
9391 if ("sparql" .equalsIgnoreCase (extension )) {
9492 var summary = tdbService .executeUpdateQuery (IOUtils .toString (file , StandardCharsets .UTF_8 .name ()));
@@ -97,11 +95,11 @@ Migration performMigration(@Body byte[] file,
9795 Lang lang = RDFLanguages .filenameToLang (fileName );
9896 var model = ModelFactory .createDefaultModel ();
9997 RDFDataMgr .read (model , new ByteArrayInputStream (file ), lang );
100- String graph = ofNullable (GRAPH_CACHE .getIfPresent (getBaseName (fileName ))).orElseGet (() -> defaultGraph );
98+ String graph = ofNullable (GRAPH_CACHE .getIfPresent (FilenameUtils .getBaseName (fileName )))
99+ .orElseGet (() -> defaultGraph );
101100 var summary = tdbService .batchLoadData (graph , model );
102101 return new Migration (UUID .randomUUID ().toString (), summary );
103102 }
104103
105-
106104 }
107105}
0 commit comments