diff --git a/CHANGELOG.md b/CHANGELOG.md index 630c2ab6..510318a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Adheres to [Semantic Versioning](http://semver.org/). * geopackage-core version 6.6.7 * sqlite-jdbc version 3.45.1.0 +* Get abstract User DAO by table name +* Set User Custom DAO contents ## [6.6.4](https://github.com/ngageoint/geopackage-java/releases/tag/6.6.4) (11-29-2023) diff --git a/src/main/java/mil/nga/geopackage/GeoPackage.java b/src/main/java/mil/nga/geopackage/GeoPackage.java index 411d120b..dfe6a9c1 100644 --- a/src/main/java/mil/nga/geopackage/GeoPackage.java +++ b/src/main/java/mil/nga/geopackage/GeoPackage.java @@ -12,6 +12,7 @@ import mil.nga.geopackage.tiles.matrixset.TileMatrixSet; import mil.nga.geopackage.tiles.user.TileDao; import mil.nga.geopackage.tiles.user.TileTable; +import mil.nga.geopackage.user.UserDao; import mil.nga.geopackage.user.custom.UserCustomDao; import mil.nga.geopackage.user.custom.UserCustomTable; @@ -146,6 +147,16 @@ public interface GeoPackage extends GeoPackageCore { */ public UserCustomDao getUserCustomDao(UserCustomTable table); + /** + * Get a User DAO from a table + * + * @param tableName + * table name + * @return user dao + * @since 6.6.5 + */ + public UserDao getUserDao(String tableName); + /** * Perform a query on the database * diff --git a/src/main/java/mil/nga/geopackage/GeoPackageImpl.java b/src/main/java/mil/nga/geopackage/GeoPackageImpl.java index acb6671d..ed2e2caf 100644 --- a/src/main/java/mil/nga/geopackage/GeoPackageImpl.java +++ b/src/main/java/mil/nga/geopackage/GeoPackageImpl.java @@ -26,6 +26,7 @@ import mil.nga.geopackage.tiles.user.TileDao; import mil.nga.geopackage.tiles.user.TileTable; import mil.nga.geopackage.tiles.user.TileTableReader; +import mil.nga.geopackage.user.UserDao; import mil.nga.geopackage.user.custom.UserCustomDao; import mil.nga.geopackage.user.custom.UserCustomTable; import mil.nga.geopackage.user.custom.UserCustomTableReader; @@ -417,9 +418,58 @@ public UserCustomDao getUserCustomDao(String tableName) { */ @Override public UserCustomDao getUserCustomDao(UserCustomTable table) { + + if (table.getContents() == null) { + ContentsDao contentsDao = getContentsDao(); + Contents contents = null; + try { + contents = contentsDao.queryForId(table.getTableName()); + } catch (SQLException e) { + throw new GeoPackageException( + "Failed to retrieve " + Contents.class.getSimpleName() + + " for table name: " + table.getTableName(), + e); + } + table.setContents(contents); + } return new UserCustomDao(getName(), database, table); } + /** + * {@inheritDoc} + */ + @Override + public UserDao getUserDao(String tableName) { + + UserDao dao = null; + + if (!isContentsTable(tableName)) { + throw new GeoPackageException( + "No contents for user table: " + tableName); + } + ContentsDataType dataType = getTableCoreDataType(tableName); + if (dataType != null) { + switch (dataType) { + case ATTRIBUTES: + dao = getAttributesDao(tableName); + break; + case FEATURES: + dao = getFeatureDao(tableName); + break; + case TILES: + dao = getTileDao(tableName); + break; + default: + throw new GeoPackageException("Unsupported data type: " + + dataType + ", table: " + tableName); + } + } else { + dao = getUserCustomDao(tableName); + } + + return dao; + } + /** * {@inheritDoc} */ diff --git a/src/test/java/mil/nga/geopackage/GeoPackageCreateTest.java b/src/test/java/mil/nga/geopackage/GeoPackageCreateTest.java index 683fefbe..7d4d51ad 100644 --- a/src/test/java/mil/nga/geopackage/GeoPackageCreateTest.java +++ b/src/test/java/mil/nga/geopackage/GeoPackageCreateTest.java @@ -122,4 +122,17 @@ public void testTableTypes() throws SQLException { } + /** + * Test user daos + * + * @throws SQLException + * upon error + */ + @Test + public void testUserDao() throws SQLException { + + GeoPackageTestUtils.testUserDao(geoPackage); + + } + } diff --git a/src/test/java/mil/nga/geopackage/GeoPackageImportTest.java b/src/test/java/mil/nga/geopackage/GeoPackageImportTest.java index c35db366..3c1e9877 100644 --- a/src/test/java/mil/nga/geopackage/GeoPackageImportTest.java +++ b/src/test/java/mil/nga/geopackage/GeoPackageImportTest.java @@ -122,4 +122,17 @@ public void testTableTypes() throws SQLException { } + /** + * Test user daos + * + * @throws SQLException + * upon error + */ + @Test + public void testUserDao() throws SQLException { + + GeoPackageTestUtils.testUserDao(geoPackage); + + } + } diff --git a/src/test/java/mil/nga/geopackage/GeoPackageTestUtils.java b/src/test/java/mil/nga/geopackage/GeoPackageTestUtils.java index b0f23721..79f19723 100644 --- a/src/test/java/mil/nga/geopackage/GeoPackageTestUtils.java +++ b/src/test/java/mil/nga/geopackage/GeoPackageTestUtils.java @@ -1,5 +1,7 @@ package mil.nga.geopackage; +import static org.junit.Assert.fail; + import java.io.File; import java.sql.SQLException; import java.util.ArrayList; @@ -10,11 +12,13 @@ import java.util.Set; import junit.framework.TestCase; +import mil.nga.geopackage.attributes.AttributesDao; import mil.nga.geopackage.contents.Contents; import mil.nga.geopackage.contents.ContentsDao; import mil.nga.geopackage.contents.ContentsDataType; import mil.nga.geopackage.db.GeoPackageDataType; import mil.nga.geopackage.db.TableColumnKey; +import mil.nga.geopackage.db.table.TableInfo; import mil.nga.geopackage.extension.coverage.CoverageData; import mil.nga.geopackage.features.columns.GeometryColumns; import mil.nga.geopackage.features.columns.GeometryColumnsDao; @@ -29,6 +33,8 @@ import mil.nga.geopackage.tiles.matrixset.TileMatrixSet; import mil.nga.geopackage.tiles.matrixset.TileMatrixSetDao; import mil.nga.geopackage.tiles.user.TileDao; +import mil.nga.geopackage.user.UserDao; +import mil.nga.geopackage.user.custom.UserCustomDao; import mil.nga.proj.Projection; import mil.nga.proj.ProjectionConstants; import mil.nga.proj.ProjectionFactory; @@ -723,4 +729,52 @@ public static void testTableTypes(GeoPackage geoPackage) } + /** + * Test user daos + * + * @param geoPackage + * GeoPackage + * @throws SQLException + * upon error + */ + public static void testUserDao(GeoPackage geoPackage) throws SQLException { + + List tables = geoPackage.getTables(); + for (String table : tables) { + UserDao dao = geoPackage.getUserDao(table); + TestCase.assertNotNull(dao); + ContentsDataType dataType = geoPackage.getTableCoreDataType(table); + TableInfo tableInfo = TableInfo.info(geoPackage.getConnection(), + table); + TestCase.assertEquals(tableInfo.numColumns(), dao.columnCount()); + Contents contents = dao.getContents(); + TestCase.assertNotNull(contents); + if (dataType == null) { + TestCase.assertTrue(dao instanceof UserCustomDao); + TestCase.assertNotNull(contents.getDataTypeName()); + } else { + switch (dataType) { + case ATTRIBUTES: + TestCase.assertTrue(dao instanceof AttributesDao); + TestCase.assertEquals(ContentsDataType.ATTRIBUTES, + contents.getDataType()); + break; + case FEATURES: + TestCase.assertTrue(dao instanceof FeatureDao); + TestCase.assertEquals(ContentsDataType.FEATURES, + contents.getDataType()); + break; + case TILES: + TestCase.assertTrue(dao instanceof TileDao); + TestCase.assertEquals(ContentsDataType.TILES, + contents.getDataType()); + break; + default: + fail("Unsupported data type: " + dataType); + } + } + } + + } + }