diff --git a/README.md b/README.md index 508ee14..5076cce 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,62 @@ Tool export query in CSV and XLS / XLSX format ![Java runtime version](https://img.shields.io/badge/run%20on-java%208+-%23113366.svg?style=for-the-badge&logo=openjdk&logoColor=white) ![Java build version](https://img.shields.io/badge/build%20on-java%2011+-%23ED8B00.svg?style=for-the-badge&logo=openjdk&logoColor=white) ![Apache Maven](https://img.shields.io/badge/Apache%20Maven-3.9.0+-C71A36?style=for-the-badge&logo=Apache%20Maven&logoColor=white) + +## 1 Quickstart + +### 1.1 Create a sample query catalog + +``` + + + + + + + + + + + +``` + +### 1.2 Load and use the catalog + +``` + QueryConfigCatalog catalog = QueryConfigCatalog.loadQueryConfigCatalogSafe( "cl://sample/query-catalog-sample.xml" ); + try ( Connection conn = ... ) { + catalog.handle( conn , "main-catalog", "Q001"); + } +``` + +## 2 Formats + +### 2.1 HTML format + +HTML format is handled with core I/O API, no dependency needed. + +### 2.2 CSV format + +CSV Format needs *OpenCSV* dependency, which is automatically included by default when importing query-export-tool dependency. + +If needed it can be added in explicit way : + +``` + + com.opencsv + opencsv + ${opencsv-version} + +``` + +### 2.3 XLS/XLSX formats + +XLS/XLSX Formats needs *Apache POI* dependency, which is *NOT* automatically included by default when importing query-export-tool dependency : + +``` + + org.apache.poi + poi-ooxml + ${poi-version} + +``` \ No newline at end of file diff --git a/src/main/java/org/fugerit/java/query/export/catalog/QueryConfigCatalog.java b/src/main/java/org/fugerit/java/query/export/catalog/QueryConfigCatalog.java index 4e00344..7982040 100644 --- a/src/main/java/org/fugerit/java/query/export/catalog/QueryConfigCatalog.java +++ b/src/main/java/org/fugerit/java/query/export/catalog/QueryConfigCatalog.java @@ -3,12 +3,16 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.sql.Connection; import java.util.Properties; import org.fugerit.java.core.cfg.ConfigRuntimeException; import org.fugerit.java.core.cfg.xml.CustomListCatalogConfig; +import org.fugerit.java.core.cfg.xml.GenericListCatalogConfig; +import org.fugerit.java.core.function.SafeFunction; import org.fugerit.java.core.io.helper.HelperIOException; +import org.fugerit.java.core.io.helper.StreamHelper; import org.fugerit.java.core.lang.helpers.BooleanUtils; import org.fugerit.java.core.lang.helpers.StringUtils; import org.fugerit.java.core.util.collection.ListMapStringKey; @@ -30,6 +34,29 @@ public QueryConfigCatalog() { this.getGeneralProps().setProperty( ATT_TYPE , QueryConfig.class.getName() ); } + public static QueryConfigCatalog loadQueryConfigCatalogSafe( String path ) { + return SafeFunction.get( () -> { + QueryConfigCatalog catalog = new QueryConfigCatalog(); + try ( InputStream is = StreamHelper.resolveStream( path ) ) { + GenericListCatalogConfig.load( is ,catalog ); + } + return catalog; + } ); + } + + public void handle( Connection conn, String catalogId, String queryId ) throws IOException { + log.info( "handle catalogId : {}, queryId : {}", catalogId, queryId ); + ListMapStringKey catalog = this.getListMap( catalogId ); + if ( catalog == null ) { + throw new IOException( "Catalog not found : "+catalogId ); + } + QueryConfig queryConfig = catalog.get( queryId ); + if ( queryConfig == null ) { + throw new IOException( "Query not found : "+queryId+" in catalog : "+catalogId ); + } + handle(conn, queryConfig); + } + public static void handle( Connection conn, QueryConfig queryConfig ) throws IOException { HelperIOException.apply( () -> { QueryExportConfig config = null; diff --git a/src/test/java/test/org/fugerit/java/query/export/tool/TestCatalog.java b/src/test/java/test/org/fugerit/java/query/export/tool/TestCatalog.java index 35526ee..001b0c3 100644 --- a/src/test/java/test/org/fugerit/java/query/export/tool/TestCatalog.java +++ b/src/test/java/test/org/fugerit/java/query/export/tool/TestCatalog.java @@ -2,12 +2,11 @@ import java.io.File; import java.io.IOException; -import java.io.InputStream; +import java.sql.Connection; +import java.sql.SQLException; import org.fugerit.java.core.cfg.ConfigRuntimeException; -import org.fugerit.java.core.cfg.xml.GenericListCatalogConfig; import org.fugerit.java.core.function.SafeFunction; -import org.fugerit.java.core.lang.helpers.ClassHelper; import org.fugerit.java.query.export.catalog.QueryConfig; import org.fugerit.java.query.export.catalog.QueryConfigCatalog; import org.junit.Assert; @@ -19,20 +18,24 @@ private boolean testWorkerSingle( QueryConfig queryConfig ) { return SafeFunction.get( () -> { logger.info( "test start" ); File outputFile = new File( queryConfig.getOutputFile() ); - QueryConfigCatalog.handle(getConnection(), queryConfig); + try ( Connection conn = getConnection() ) { + QueryConfigCatalog.handle( conn, queryConfig); + } return outputFile.exists(); } ); } @Test - public void testCatalog() throws IOException { - QueryConfigCatalog catalog = new QueryConfigCatalog(); - try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/query-catalog-sample.xml" ) ) { - GenericListCatalogConfig.load(is, catalog); - catalog.getListMap( "sample-catalog" ).stream().forEach( c -> Assert.assertTrue( this.testWorkerSingle(c) ) ); - catalog.getListMap( "fail-catalog" ).stream().forEach( - c -> Assert.assertThrows( ConfigRuntimeException.class, () -> this.testWorkerSingle(c) ) - ); + public void testCatalog() throws IOException, SQLException { + QueryConfigCatalog catalog = QueryConfigCatalog.loadQueryConfigCatalogSafe( "cl://sample/query-catalog-sample.xml" ); + catalog.getListMap( "sample-catalog" ).stream().forEach( c -> Assert.assertTrue( this.testWorkerSingle(c) ) ); + catalog.getListMap( "fail-catalog" ).stream().forEach( + c -> Assert.assertThrows( ConfigRuntimeException.class, () -> this.testWorkerSingle(c) ) ); + // test specific + try ( Connection conn = getConnection() ) { + catalog.handle( conn , "main-catalog", "Q010"); + Assert.assertThrows( IOException.class , () -> catalog.handle(conn, "main-catalog", "not-exists" ) ); + Assert.assertThrows( IOException.class , () -> catalog.handle(conn, "not-exists", "not-exists" ) ); } } diff --git a/src/test/resources/sample/query-catalog-sample.xml b/src/test/resources/sample/query-catalog-sample.xml index 5a00828..5cee39b 100644 --- a/src/test/resources/sample/query-catalog-sample.xml +++ b/src/test/resources/sample/query-catalog-sample.xml @@ -1,15 +1,19 @@ - - - - - + + + + + - + + + + + \ No newline at end of file