Search in sources :

Example 1 with RelatedTablesExtension

use of mil.nga.geopackage.extension.related.RelatedTablesExtension in project geopackage-java by ngageoint.

the class RelatedMediaUtils method testMedia.

/**
 * Test related media tables
 *
 * @param geoPackage
 * @throws SQLException
 */
public static void testMedia(GeoPackage geoPackage) throws Exception {
    // Create a related tables extension
    RelatedTablesExtension rte = new RelatedTablesExtension(geoPackage);
    if (rte.has()) {
        rte.removeExtension();
    }
    TestCase.assertFalse(rte.has());
    TestCase.assertTrue(rte.getRelationships().isEmpty());
    // Choose a random feature table
    List<String> featureTables = geoPackage.getFeatureTables();
    if (featureTables.isEmpty()) {
        // pass with no testing
        return;
    }
    final String baseTableName = featureTables.get((int) (Math.random() * featureTables.size()));
    // Populate and validate a media table
    List<UserCustomColumn> additionalMediaColumns = RelatedTablesUtils.createAdditionalUserColumns();
    MediaTable mediaTable = MediaTable.create(MediaTableMetadata.create("media_table", additionalMediaColumns));
    String[] mediaColumns = mediaTable.getColumnNames();
    TestCase.assertEquals(MediaTable.numRequiredColumns() + additionalMediaColumns.size(), mediaColumns.length);
    UserCustomColumn idColumn = mediaTable.getIdColumn();
    TestCase.assertNotNull(idColumn);
    TestCase.assertTrue(idColumn.isNamed(MediaTableMetadata.DEFAULT_ID_COLUMN_NAME));
    TestCase.assertEquals(GeoPackageDataType.INTEGER, idColumn.getDataType());
    TestCase.assertTrue(idColumn.isNotNull());
    TestCase.assertTrue(idColumn.isPrimaryKey());
    UserCustomColumn dataColumn = mediaTable.getDataColumn();
    TestCase.assertNotNull(dataColumn);
    TestCase.assertTrue(dataColumn.isNamed(MediaTable.COLUMN_DATA));
    TestCase.assertEquals(GeoPackageDataType.BLOB, dataColumn.getDataType());
    TestCase.assertTrue(dataColumn.isNotNull());
    TestCase.assertFalse(dataColumn.isPrimaryKey());
    UserCustomColumn contentTypeColumn = mediaTable.getContentTypeColumn();
    TestCase.assertNotNull(contentTypeColumn);
    TestCase.assertTrue(contentTypeColumn.isNamed(MediaTable.COLUMN_CONTENT_TYPE));
    TestCase.assertEquals(GeoPackageDataType.TEXT, contentTypeColumn.getDataType());
    TestCase.assertTrue(contentTypeColumn.isNotNull());
    TestCase.assertFalse(contentTypeColumn.isPrimaryKey());
    // Create and validate a mapping table
    List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils.createAdditionalUserColumns();
    final String mappingTableName = "features_media";
    UserMappingTable userMappingTable = UserMappingTable.create(mappingTableName, additionalMappingColumns);
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    TestCase.assertEquals(UserMappingTable.numRequiredColumns() + additionalMappingColumns.size(), userMappingTable.getColumns().size());
    UserCustomColumn baseIdColumn = userMappingTable.getBaseIdColumn();
    TestCase.assertNotNull(baseIdColumn);
    TestCase.assertTrue(baseIdColumn.isNamed(UserMappingTable.COLUMN_BASE_ID));
    TestCase.assertEquals(GeoPackageDataType.INTEGER, baseIdColumn.getDataType());
    TestCase.assertTrue(baseIdColumn.isNotNull());
    TestCase.assertFalse(baseIdColumn.isPrimaryKey());
    UserCustomColumn relatedIdColumn = userMappingTable.getRelatedIdColumn();
    TestCase.assertNotNull(relatedIdColumn);
    TestCase.assertTrue(relatedIdColumn.isNamed(UserMappingTable.COLUMN_RELATED_ID));
    TestCase.assertEquals(GeoPackageDataType.INTEGER, relatedIdColumn.getDataType());
    TestCase.assertTrue(relatedIdColumn.isNotNull());
    TestCase.assertFalse(relatedIdColumn.isPrimaryKey());
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    // Create the media table, content row, and relationship between the
    // feature table and media table
    ContentsDao contentsDao = geoPackage.getContentsDao();
    TestCase.assertFalse(contentsDao.getTables().contains(mediaTable.getTableName()));
    ExtendedRelation extendedRelation = rte.addMediaRelationship(baseTableName, mediaTable, userMappingTable);
    validateContents(mediaTable, mediaTable.getContents());
    TestCase.assertTrue(rte.has());
    TestCase.assertTrue(rte.has(userMappingTable.getTableName()));
    TestCase.assertNotNull(extendedRelation);
    List<ExtendedRelation> extendedRelations = rte.getRelationships();
    TestCase.assertEquals(1, extendedRelations.size());
    TestCase.assertTrue(geoPackage.isTable(mappingTableName));
    TestCase.assertTrue(geoPackage.isTable(mediaTable.getTableName()));
    TestCase.assertTrue(contentsDao.getTables().contains(mediaTable.getTableName()));
    validateContents(mediaTable, contentsDao.queryForId(mediaTable.getTableName()));
    TestCase.assertEquals(MediaTable.RELATION_TYPE.getDataType(), geoPackage.getTableType(mediaTable.getTableName()));
    TestCase.assertTrue(geoPackage.isTableType(mediaTable.getTableName(), MediaTable.RELATION_TYPE.getDataType()));
    // Validate the media DAO
    MediaDao mediaDao = rte.getMediaDao(mediaTable);
    TestCase.assertNotNull(mediaDao);
    mediaTable = mediaDao.getTable();
    TestCase.assertNotNull(mediaTable);
    validateContents(mediaTable, mediaTable.getContents());
    // Insert media table rows
    byte[] mediaData = TestUtils.getTileBytes();
    String contentType = "image/png";
    BufferedImage mediaImage = ImageUtils.getImage(mediaData);
    int imageWidth = mediaImage.getWidth();
    int imageHeight = mediaImage.getHeight();
    int mediaCount = 2 + (int) (Math.random() * 9);
    long mediaRowId = 0;
    // Create and insert the first mediaCount - 1 rows
    for (int i = 0; i < mediaCount - 1; i++) {
        MediaRow mediaRow = mediaDao.newRow();
        mediaRow.setData(mediaData);
        mediaRow.setContentType(contentType);
        RelatedTablesUtils.populateUserRow(mediaTable, mediaRow, MediaTable.requiredColumns());
        mediaRowId = mediaDao.create(mediaRow);
        TestCase.assertTrue(mediaRowId > 0);
    }
    // Copy the last row insert and insert the final media row
    MediaRow mediaRowToCopy = new MediaRow(mediaDao.queryForIdRow(mediaRowId));
    MediaRow mediaRowCopy = mediaRowToCopy.copy();
    long copyMediaRowId = mediaDao.create(mediaRowCopy);
    TestCase.assertTrue(copyMediaRowId > 0);
    TestCase.assertEquals(mediaRowId + 1, copyMediaRowId);
    TestCase.assertEquals(mediaCount, mediaDao.count());
    // Build the Feature ids
    FeatureDao featureDao = geoPackage.getFeatureDao(baseTableName);
    FeatureResultSet featureResultSet = featureDao.queryForAll();
    int featureCount = featureResultSet.getCount();
    List<Long> featureIds = new ArrayList<>();
    while (featureResultSet.moveToNext()) {
        featureIds.add(featureResultSet.getRow().getId());
    }
    featureResultSet.close();
    // Build the Media ids
    UserCustomResultSet mediaResultSet = mediaDao.queryForAll();
    mediaCount = mediaResultSet.getCount();
    List<Long> mediaIds = new ArrayList<>();
    while (mediaResultSet.moveToNext()) {
        mediaIds.add(mediaResultSet.getRow().getId());
    }
    mediaResultSet.close();
    // Insert user mapping rows between feature ids and media ids
    UserMappingDao dao = rte.getMappingDao(mappingTableName);
    UserMappingRow userMappingRow = null;
    for (int i = 0; i < 10; i++) {
        userMappingRow = dao.newRow();
        userMappingRow.setBaseId(featureIds.get((int) (Math.random() * featureCount)));
        userMappingRow.setRelatedId(mediaIds.get((int) (Math.random() * mediaCount)));
        RelatedTablesUtils.populateUserRow(userMappingTable, userMappingRow, UserMappingTable.requiredColumns());
        TestCase.assertTrue(dao.create(userMappingRow) > 0);
    }
    TestCase.assertEquals(10, dao.count());
    // Validate the user mapping rows
    userMappingTable = dao.getTable();
    String[] mappingColumns = userMappingTable.getColumnNames();
    UserCustomResultSet resultSet = dao.queryForAll();
    int count = resultSet.getCount();
    TestCase.assertEquals(10, count);
    int manualCount = 0;
    while (resultSet.moveToNext()) {
        UserMappingRow resultRow = dao.getRow(resultSet);
        TestCase.assertFalse(resultRow.hasId());
        TestCase.assertTrue(featureIds.contains(resultRow.getBaseId()));
        TestCase.assertTrue(mediaIds.contains(resultRow.getRelatedId()));
        RelatedTablesUtils.validateUserRow(mappingColumns, resultRow);
        RelatedTablesUtils.validateDublinCoreColumns(resultRow);
        manualCount++;
    }
    TestCase.assertEquals(count, manualCount);
    resultSet.close();
    ExtendedRelationsDao extendedRelationsDao = rte.getExtendedRelationsDao();
    // Get the relations starting from the feature table
    List<ExtendedRelation> featureExtendedRelations = extendedRelationsDao.getBaseTableRelations(featureDao.getTableName());
    List<ExtendedRelation> featureExtendedRelations2 = extendedRelationsDao.getTableRelations(featureDao.getTableName());
    TestCase.assertEquals(1, featureExtendedRelations.size());
    TestCase.assertEquals(1, featureExtendedRelations2.size());
    TestCase.assertEquals(featureExtendedRelations.get(0).getId(), featureExtendedRelations2.get(0).getId());
    TestCase.assertTrue(extendedRelationsDao.getRelatedTableRelations(featureDao.getTableName()).isEmpty());
    // Test the feature table relations
    for (ExtendedRelation featureRelation : featureExtendedRelations) {
        // Test the relation
        TestCase.assertTrue(featureRelation.getId() >= 0);
        TestCase.assertEquals(featureDao.getTableName(), featureRelation.getBaseTableName());
        TestCase.assertEquals(featureDao.getPkColumnName(), featureRelation.getBasePrimaryColumn());
        TestCase.assertEquals(mediaDao.getTableName(), featureRelation.getRelatedTableName());
        TestCase.assertEquals(mediaDao.getPkColumnName(), featureRelation.getRelatedPrimaryColumn());
        TestCase.assertEquals(MediaTable.RELATION_TYPE.getName(), featureRelation.getRelationName());
        TestCase.assertEquals(mappingTableName, featureRelation.getMappingTableName());
        // Test the user mappings from the relation
        UserMappingDao userMappingDao = rte.getMappingDao(featureRelation);
        int totalMappedCount = userMappingDao.count();
        UserCustomResultSet mappingResultSet = userMappingDao.queryForAll();
        while (mappingResultSet.moveToNext()) {
            userMappingRow = userMappingDao.getRow(mappingResultSet);
            TestCase.assertTrue(featureIds.contains(userMappingRow.getBaseId()));
            TestCase.assertTrue(mediaIds.contains(userMappingRow.getRelatedId()));
            RelatedTablesUtils.validateUserRow(mappingColumns, userMappingRow);
            RelatedTablesUtils.validateDublinCoreColumns(userMappingRow);
        }
        mappingResultSet.close();
        // Get and test the media DAO
        mediaDao = rte.getMediaDao(featureRelation);
        TestCase.assertNotNull(mediaDao);
        mediaTable = mediaDao.getTable();
        TestCase.assertNotNull(mediaTable);
        validateContents(mediaTable, mediaTable.getContents());
        // Get and test the Media Rows mapped to each Feature Row
        featureResultSet = featureDao.queryForAll();
        int totalMapped = 0;
        while (featureResultSet.moveToNext()) {
            FeatureRow featureRow = featureResultSet.getRow();
            List<Long> mappedIds = rte.getMappingsForBase(featureRelation, featureRow.getId());
            List<MediaRow> mediaRows = mediaDao.getRows(mappedIds);
            TestCase.assertEquals(mappedIds.size(), mediaRows.size());
            for (MediaRow mediaRow : mediaRows) {
                TestCase.assertTrue(mediaRow.hasId());
                TestCase.assertTrue(mediaRow.getId() >= 0);
                TestCase.assertTrue(mediaIds.contains(mediaRow.getId()));
                TestCase.assertTrue(mappedIds.contains(mediaRow.getId()));
                GeoPackageGeometryDataUtils.compareByteArrays(mediaData, mediaRow.getData());
                TestCase.assertEquals(contentType, mediaRow.getContentType());
                RelatedTablesUtils.validateUserRow(mediaColumns, mediaRow);
                RelatedTablesUtils.validateDublinCoreColumns(mediaRow);
                validateDublinCoreColumns(mediaRow);
                BufferedImage image = mediaRow.getDataImage();
                TestCase.assertNotNull(image);
                TestCase.assertEquals(imageWidth, image.getWidth());
                TestCase.assertEquals(imageHeight, image.getHeight());
            }
            totalMapped += mappedIds.size();
        }
        featureResultSet.close();
        TestCase.assertEquals(totalMappedCount, totalMapped);
    }
    // Get the relations starting from the media table
    List<ExtendedRelation> mediaExtendedRelations = extendedRelationsDao.getRelatedTableRelations(mediaTable.getTableName());
    List<ExtendedRelation> mediaExtendedRelations2 = extendedRelationsDao.getTableRelations(mediaTable.getTableName());
    TestCase.assertEquals(1, mediaExtendedRelations.size());
    TestCase.assertEquals(1, mediaExtendedRelations2.size());
    TestCase.assertEquals(mediaExtendedRelations.get(0).getId(), mediaExtendedRelations2.get(0).getId());
    TestCase.assertTrue(extendedRelationsDao.getBaseTableRelations(mediaTable.getTableName()).isEmpty());
    // Test the media table relations
    for (ExtendedRelation mediaRelation : mediaExtendedRelations) {
        // Test the relation
        TestCase.assertTrue(mediaRelation.getId() >= 0);
        TestCase.assertEquals(featureDao.getTableName(), mediaRelation.getBaseTableName());
        TestCase.assertEquals(featureDao.getPkColumnName(), mediaRelation.getBasePrimaryColumn());
        TestCase.assertEquals(mediaDao.getTableName(), mediaRelation.getRelatedTableName());
        TestCase.assertEquals(mediaDao.getPkColumnName(), mediaRelation.getRelatedPrimaryColumn());
        TestCase.assertEquals(MediaTable.RELATION_TYPE.getName(), mediaRelation.getRelationName());
        TestCase.assertEquals(mappingTableName, mediaRelation.getMappingTableName());
        // Test the user mappings from the relation
        UserMappingDao userMappingDao = rte.getMappingDao(mediaRelation);
        int totalMappedCount = userMappingDao.count();
        UserCustomResultSet mappingResultSet = userMappingDao.queryForAll();
        while (mappingResultSet.moveToNext()) {
            userMappingRow = userMappingDao.getRow(mappingResultSet);
            TestCase.assertTrue(featureIds.contains(userMappingRow.getBaseId()));
            TestCase.assertTrue(mediaIds.contains(userMappingRow.getRelatedId()));
            RelatedTablesUtils.validateUserRow(mappingColumns, userMappingRow);
            RelatedTablesUtils.validateDublinCoreColumns(userMappingRow);
        }
        mappingResultSet.close();
        // Get and test the feature DAO
        featureDao = geoPackage.getFeatureDao(featureDao.getTableName());
        TestCase.assertNotNull(featureDao);
        FeatureTable featureTable = featureDao.getTable();
        TestCase.assertNotNull(featureTable);
        Contents featureContents = featureDao.getContents();
        TestCase.assertNotNull(featureContents);
        TestCase.assertEquals(ContentsDataType.FEATURES, featureContents.getDataType());
        TestCase.assertEquals(ContentsDataType.FEATURES.getName(), featureContents.getDataTypeName());
        TestCase.assertEquals(featureTable.getTableName(), featureContents.getTableName());
        TestCase.assertNotNull(featureContents.getLastChange());
        // Get and test the Feature Rows mapped to each Media Row
        mediaResultSet = mediaDao.queryForAll();
        int totalMapped = 0;
        while (mediaResultSet.moveToNext()) {
            MediaRow mediaRow = mediaDao.getRow(mediaResultSet);
            List<Long> mappedIds = rte.getMappingsForRelated(mediaRelation, mediaRow.getId());
            for (long mappedId : mappedIds) {
                FeatureRow featureRow = featureDao.queryForIdRow(mappedId);
                TestCase.assertNotNull(featureRow);
                TestCase.assertTrue(featureRow.hasId());
                TestCase.assertTrue(featureRow.getId() >= 0);
                TestCase.assertTrue(featureIds.contains(featureRow.getId()));
                TestCase.assertTrue(mappedIds.contains(featureRow.getId()));
                if (featureRow.getValue(featureRow.getGeometryColumnIndex()) != null) {
                    GeoPackageGeometryData geometryData = featureRow.getGeometry();
                    TestCase.assertNotNull(geometryData);
                    if (!geometryData.isEmpty()) {
                        TestCase.assertNotNull(geometryData.getGeometry());
                    }
                }
            }
            totalMapped += mappedIds.size();
        }
        mediaResultSet.close();
        TestCase.assertEquals(totalMappedCount, totalMapped);
    }
    // Add more columns to the media table
    int existingColumns = mediaTable.getColumns().size();
    UserCustomColumn mediaIdColumn = mediaTable.getIdColumn();
    UserCustomColumn mediaDataColumn = mediaTable.getDataColumn();
    UserCustomColumn mediaContentTypeColumn = mediaTable.getContentTypeColumn();
    int newColumns = 0;
    String newColumnName = "new_column";
    mediaDao.addColumn(UserCustomColumn.createColumn(newColumnName + ++newColumns, GeoPackageDataType.TEXT));
    mediaDao.addColumn(UserCustomColumn.createColumn(newColumnName + ++newColumns, GeoPackageDataType.BLOB));
    TestCase.assertEquals(existingColumns + 2, mediaTable.getColumns().size());
    for (int index = existingColumns; index < mediaTable.getColumns().size(); index++) {
        String name = newColumnName + (index - existingColumns + 1);
        TestCase.assertEquals(name, mediaTable.getColumnName(index));
        TestCase.assertEquals(index, mediaTable.getColumnIndex(name));
        TestCase.assertEquals(name, mediaTable.getColumn(index).getName());
        TestCase.assertEquals(index, mediaTable.getColumn(index).getIndex());
        TestCase.assertEquals(name, mediaTable.getColumnNames()[index]);
        TestCase.assertEquals(name, mediaTable.getColumns().get(index).getName());
        try {
            mediaTable.getColumn(index).setIndex(index - 1);
            TestCase.fail("Changed index on a created table column");
        } catch (Exception e) {
        }
        mediaTable.getColumn(index).setIndex(index);
    }
    TestCase.assertEquals(mediaIdColumn, mediaTable.getIdColumn());
    TestCase.assertEquals(mediaDataColumn, mediaTable.getDataColumn());
    TestCase.assertEquals(mediaContentTypeColumn, mediaTable.getContentTypeColumn());
    // Add another row with the new columns and read it
    MediaRow mediaRow = mediaDao.newRow();
    mediaRow.setData(mediaData);
    mediaRow.setContentType(contentType);
    RelatedTablesUtils.populateUserRow(mediaTable, mediaRow, MediaTable.requiredColumns());
    String newValue = UUID.randomUUID().toString();
    mediaRow.setValue(existingColumns, newValue);
    mediaRow.setValue(existingColumns + 1, mediaRow.getData());
    mediaRowId = mediaDao.create(mediaRow);
    TestCase.assertTrue(mediaRowId > 0);
    MediaRow newMediaRow = mediaDao.getRow(mediaDao.queryForIdRow(mediaRowId));
    TestCase.assertNotNull(newMediaRow);
    TestCase.assertEquals(newValue, newMediaRow.getValue(existingColumns));
    GeoPackageGeometryDataUtils.compareByteArrays(mediaRow.getData(), (byte[]) newMediaRow.getValue(existingColumns + 1));
    // Delete a single mapping
    int countOfIds = dao.countByIds(userMappingRow);
    TestCase.assertEquals(countOfIds, dao.deleteByIds(userMappingRow));
    TestCase.assertEquals(10 - countOfIds, dao.count());
    // Delete the relationship and user mapping table
    rte.removeRelationship(extendedRelation);
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    extendedRelations = rte.getRelationships();
    TestCase.assertEquals(0, extendedRelations.size());
    TestCase.assertFalse(geoPackage.isTable(mappingTableName));
    // Delete the media table and contents row
    TestCase.assertTrue(geoPackage.isTable(mediaTable.getTableName()));
    TestCase.assertNotNull(contentsDao.queryForId(mediaTable.getTableName()));
    geoPackage.deleteTable(mediaTable.getTableName());
    TestCase.assertFalse(geoPackage.isTable(mediaTable.getTableName()));
    TestCase.assertNull(contentsDao.queryForId(mediaTable.getTableName()));
    // Delete the related tables extension
    rte.removeExtension();
    TestCase.assertFalse(rte.has());
}
Also used : ArrayList(java.util.ArrayList) ExtendedRelationsDao(mil.nga.geopackage.extension.related.ExtendedRelationsDao) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) UserCustomResultSet(mil.nga.geopackage.user.custom.UserCustomResultSet) BufferedImage(java.awt.image.BufferedImage) FeatureResultSet(mil.nga.geopackage.features.user.FeatureResultSet) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) GeoPackageGeometryData(mil.nga.geopackage.geom.GeoPackageGeometryData) FeatureTable(mil.nga.geopackage.features.user.FeatureTable) RelatedTablesExtension(mil.nga.geopackage.extension.related.RelatedTablesExtension) UserMappingRow(mil.nga.geopackage.extension.related.UserMappingRow) SQLException(java.sql.SQLException) UserMappingTable(mil.nga.geopackage.extension.related.UserMappingTable) FeatureRow(mil.nga.geopackage.features.user.FeatureRow) Contents(mil.nga.geopackage.contents.Contents) UserMappingDao(mil.nga.geopackage.extension.related.UserMappingDao) ExtendedRelation(mil.nga.geopackage.extension.related.ExtendedRelation) ContentsDao(mil.nga.geopackage.contents.ContentsDao)

Example 2 with RelatedTablesExtension

use of mil.nga.geopackage.extension.related.RelatedTablesExtension in project geopackage-java by ngageoint.

the class GeoPackageExample method createRelatedTablesFeaturesExtension.

private static void createRelatedTablesFeaturesExtension(GeoPackage geoPackage, String tableName1, String tableName2) {
    RelatedTablesExtension relatedTables = new RelatedTablesExtension(geoPackage);
    List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils.createAdditionalUserColumns();
    UserMappingTable userMappingTable = UserMappingTable.create(tableName1 + "_" + tableName2, additionalMappingColumns);
    ExtendedRelation relation = relatedTables.addFeaturesRelationship(tableName1, tableName2, userMappingTable);
    insertRelatedTablesFeaturesExtensionRows(geoPackage, relation);
}
Also used : UserMappingTable(mil.nga.geopackage.extension.related.UserMappingTable) RelatedTablesExtension(mil.nga.geopackage.extension.related.RelatedTablesExtension) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) ExtendedRelation(mil.nga.geopackage.extension.related.ExtendedRelation)

Example 3 with RelatedTablesExtension

use of mil.nga.geopackage.extension.related.RelatedTablesExtension in project geopackage-java by ngageoint.

the class AlterTableUtils method testCopyFeatureTable.

/**
 * Test copy feature table
 *
 * @param geoPackage
 *            GeoPackage
 * @throws SQLException
 *             upon error
 * @throws IOException
 *             upon error
 */
public static void testCopyFeatureTable(GeoPackage geoPackage) throws SQLException, IOException {
    GeometryColumnsDao geometryColumnsDao = geoPackage.getGeometryColumnsDao();
    if (geometryColumnsDao.isTableExists()) {
        List<GeometryColumns> results = geometryColumnsDao.queryForAll();
        int viewNameCount = 0;
        for (GeometryColumns geometryColumns : results) {
            GeoPackageConnection db = geoPackage.getConnection();
            FeatureDao dao = geoPackage.getFeatureDao(geometryColumns);
            FeatureTable table = dao.getTable();
            String tableName = table.getTableName();
            String newTableName = tableName + "_copy";
            int existingColumns = table.columnCount();
            FeatureColumn pk = table.getPkColumn();
            FeatureColumn geometry = table.getGeometryColumn();
            FeatureIndexManager indexManager = new FeatureIndexManager(geoPackage, dao);
            indexManager.setContinueOnError(false);
            int indexGeoPackageCount = 0;
            if (indexManager.isIndexed(FeatureIndexType.GEOPACKAGE)) {
                indexManager.prioritizeQueryLocation(FeatureIndexType.GEOPACKAGE);
                indexGeoPackageCount = (int) indexManager.count();
            }
            int indexRTreeCount = 0;
            if (indexManager.isIndexed(FeatureIndexType.RTREE)) {
                indexManager.prioritizeQueryLocation(FeatureIndexType.RTREE);
                indexRTreeCount = (int) indexManager.count();
            }
            FeatureTileTableLinker linker = new FeatureTileTableLinker(geoPackage);
            List<String> tileTables = linker.getTileTablesForFeatureTable(tableName);
            ContentsIdExtension contentsIdExtension = new ContentsIdExtension(geoPackage);
            ContentsId contentsId = contentsIdExtension.get(tableName);
            List<MetadataReference> metadataReference = null;
            MetadataReferenceDao metadataReferenceDao = MetadataExtension.getMetadataReferenceDao(geoPackage);
            if (metadataReferenceDao.isTableExists()) {
                metadataReference = metadataReferenceDao.queryByTable(tableName);
            }
            List<DataColumns> dataColumns = null;
            DataColumnsDao dataColumnsDao = (new SchemaExtension(geoPackage)).getDataColumnsDao();
            if (dataColumnsDao.isTableExists()) {
                dataColumns = dataColumnsDao.queryByTable(tableName);
            }
            List<ExtendedRelation> extendedRelations = null;
            RelatedTablesExtension relatedTablesExtension = new RelatedTablesExtension(geoPackage);
            if (relatedTablesExtension.has()) {
                extendedRelations = relatedTablesExtension.getExtendedRelationsDao().getBaseTableRelations(tableName);
            }
            FeatureTableStyles featureTableStyles = new FeatureTableStyles(geoPackage, table);
            boolean featureStyle = featureTableStyles.has();
            List<Long> styleIds = null;
            List<Long> iconIds = null;
            List<Long> tableStyleIds = null;
            List<Long> tableIconIds = null;
            if (featureStyle) {
                styleIds = featureTableStyles.getAllStyleIds();
                iconIds = featureTableStyles.getAllIconIds();
                tableStyleIds = featureTableStyles.getAllTableStyleIds();
                tableIconIds = featureTableStyles.getAllTableIconIds();
            }
            String viewName = "v_my_" + (++viewNameCount) + "_view";
            createViewWithName(db, table, viewName, true);
            createViewWithName(db, table, viewName + "_2", false);
            int rowCount = dao.count();
            int tableCount = SQLiteMaster.count(geoPackage.getDatabase(), SQLiteMasterType.TABLE, tableName);
            int indexCount = indexCount(geoPackage.getDatabase(), tableName);
            int triggerCount = SQLiteMaster.count(geoPackage.getDatabase(), SQLiteMasterType.TRIGGER, tableName);
            int viewCount = SQLiteMaster.countViewsOnTable(geoPackage.getDatabase(), tableName);
            geoPackage.copyTable(tableName, newTableName);
            FeatureUtils.testUpdate(dao);
            FeatureDao copyDao = geoPackage.getFeatureDao(newTableName);
            GeometryColumns copyGeometryColumns = copyDao.getGeometryColumns();
            FeatureUtils.testUpdate(copyDao);
            FeatureTable copyTable = copyDao.getTable();
            TestCase.assertEquals(existingColumns, table.columnCount());
            TestCase.assertEquals(existingColumns, copyTable.columnCount());
            TestCase.assertEquals(rowCount, dao.count());
            TestCase.assertEquals(rowCount, copyDao.count());
            testTableCounts(db, tableName, tableCount, indexCount, triggerCount, viewCount);
            testTableCounts(db, newTableName, tableCount, indexCount, triggerCount, viewCount);
            TestCase.assertEquals(geometryColumns.getTableName(), table.getTableName());
            TestCase.assertEquals(newTableName, copyGeometryColumns.getTableName());
            TestCase.assertEquals(newTableName, copyTable.getTableName());
            TestCase.assertEquals(pk, table.getPkColumn());
            TestCase.assertEquals(pk.getName(), copyTable.getPkColumn().getName());
            TestCase.assertEquals(pk.getIndex(), copyTable.getPkColumn().getIndex());
            TestCase.assertEquals(geometry, table.getGeometryColumn());
            TestCase.assertEquals(geometry.getName(), copyTable.getGeometryColumnName());
            TestCase.assertEquals(geometry.getIndex(), copyTable.getGeometryColumn().getIndex());
            FeatureIndexManager copyIndexManager = new FeatureIndexManager(geoPackage, copyDao);
            copyIndexManager.setContinueOnError(false);
            if (indexManager.isIndexed(FeatureIndexType.GEOPACKAGE)) {
                indexManager.prioritizeQueryLocation(FeatureIndexType.GEOPACKAGE);
                TestCase.assertEquals(indexGeoPackageCount, indexManager.count());
                TestCase.assertTrue(copyIndexManager.isIndexed(FeatureIndexType.GEOPACKAGE));
                copyIndexManager.prioritizeQueryLocation(FeatureIndexType.GEOPACKAGE);
                TestCase.assertEquals(indexGeoPackageCount, copyIndexManager.count());
            } else {
                TestCase.assertFalse(copyIndexManager.isIndexed(FeatureIndexType.GEOPACKAGE));
            }
            if (indexManager.isIndexed(FeatureIndexType.RTREE)) {
                indexManager.prioritizeQueryLocation(FeatureIndexType.RTREE);
                TestCase.assertEquals(indexRTreeCount, indexManager.count());
                TestCase.assertTrue(copyIndexManager.isIndexed(FeatureIndexType.RTREE));
                copyIndexManager.prioritizeQueryLocation(FeatureIndexType.RTREE);
                TestCase.assertEquals(indexRTreeCount, copyIndexManager.count());
            } else {
                TestCase.assertFalse(copyIndexManager.isIndexed(FeatureIndexType.RTREE));
            }
            List<String> copyTileTables = linker.getTileTablesForFeatureTable(newTableName);
            TestCase.assertEquals(tileTables.size(), copyTileTables.size());
            for (String tileTable : tileTables) {
                TestCase.assertTrue(copyTileTables.contains(tileTable));
            }
            ContentsId copyContentsId = contentsIdExtension.get(newTableName);
            if (contentsId != null) {
                TestCase.assertNotNull(copyContentsId);
                TestCase.assertEquals(newTableName, copyContentsId.getTableName());
                TestCase.assertTrue(copyContentsId.getId() >= 0);
                TestCase.assertTrue(copyContentsId.getId() > contentsId.getId());
            } else {
                TestCase.assertNull(copyContentsId);
            }
            if (metadataReference != null) {
                List<MetadataReference> copyMetadataReference = metadataReferenceDao.queryByTable(newTableName);
                TestCase.assertEquals(metadataReference.size(), copyMetadataReference.size());
                for (int i = 0; i < metadataReference.size(); i++) {
                    TestCase.assertEquals(tableName, metadataReference.get(i).getTableName());
                    TestCase.assertEquals(newTableName, copyMetadataReference.get(i).getTableName());
                }
            }
            if (dataColumns != null) {
                List<DataColumns> copyDataColumns = dataColumnsDao.queryByTable(newTableName);
                TestCase.assertEquals(dataColumns.size(), copyDataColumns.size());
                for (int i = 0; i < dataColumns.size(); i++) {
                    TestCase.assertEquals(tableName, dataColumns.get(i).getTableName());
                    TestCase.assertEquals(newTableName, copyDataColumns.get(i).getTableName());
                }
            }
            if (extendedRelations != null) {
                List<ExtendedRelation> copyExtendedRelations = relatedTablesExtension.getExtendedRelationsDao().getBaseTableRelations(newTableName);
                TestCase.assertEquals(extendedRelations.size(), copyExtendedRelations.size());
                Map<String, ExtendedRelation> mappingTableToRelations = new HashMap<>();
                for (ExtendedRelation copyExtendedRelation : copyExtendedRelations) {
                    mappingTableToRelations.put(copyExtendedRelation.getMappingTableName(), copyExtendedRelation);
                }
                for (ExtendedRelation extendedRelation : extendedRelations) {
                    String mappingTableName = extendedRelation.getMappingTableName();
                    String copyMappingTableName = CoreSQLUtils.createName(geoPackage.getDatabase(), mappingTableName, tableName, newTableName);
                    ExtendedRelation copyExtendedRelation = mappingTableToRelations.get(copyMappingTableName);
                    TestCase.assertNotNull(copyExtendedRelation);
                    TestCase.assertTrue(extendedRelation.getId() < copyExtendedRelation.getId());
                    TestCase.assertEquals(tableName, extendedRelation.getBaseTableName());
                    TestCase.assertEquals(newTableName, copyExtendedRelation.getBaseTableName());
                    TestCase.assertEquals(extendedRelation.getBasePrimaryColumn(), copyExtendedRelation.getBasePrimaryColumn());
                    TestCase.assertEquals(extendedRelation.getRelatedTableName(), copyExtendedRelation.getRelatedTableName());
                    TestCase.assertEquals(extendedRelation.getRelatedPrimaryColumn(), copyExtendedRelation.getRelatedPrimaryColumn());
                    TestCase.assertEquals(extendedRelation.getRelationName(), copyExtendedRelation.getRelationName());
                    TestCase.assertTrue(geoPackage.isTable(mappingTableName));
                    TestCase.assertTrue(geoPackage.isTable(copyMappingTableName));
                    int mappingTableCount = geoPackage.getConnection().count(mappingTableName);
                    int copyMappingTableCount = geoPackage.getConnection().count(copyMappingTableName);
                    TestCase.assertEquals(mappingTableCount, copyMappingTableCount);
                }
            }
            FeatureTableStyles copyFeatureTableStyles = new FeatureTableStyles(geoPackage, copyTable);
            TestCase.assertEquals(featureStyle, copyFeatureTableStyles.has());
            if (featureStyle) {
                compareIds(styleIds, copyFeatureTableStyles.getAllStyleIds());
                compareIds(iconIds, copyFeatureTableStyles.getAllIconIds());
                compareIds(tableStyleIds, copyFeatureTableStyles.getAllTableStyleIds());
                compareIds(tableIconIds, copyFeatureTableStyles.getAllTableIconIds());
                if (featureTableStyles.hasStyleRelationship()) {
                    StyleMappingDao styleMappingDao = featureTableStyles.getStyleMappingDao();
                    StyleMappingDao copyStyleMappingDao = copyFeatureTableStyles.getStyleMappingDao();
                    TestCase.assertEquals(styleMappingDao.count(), copyStyleMappingDao.count());
                }
                if (featureTableStyles.hasIconRelationship()) {
                    StyleMappingDao iconMappingDao = featureTableStyles.getIconMappingDao();
                    StyleMappingDao copyIconMappingDao = copyFeatureTableStyles.getIconMappingDao();
                    TestCase.assertEquals(iconMappingDao.count(), copyIconMappingDao.count());
                }
                if (featureTableStyles.hasTableStyleRelationship()) {
                    StyleMappingDao tableStyleMappingDao = featureTableStyles.getTableStyleMappingDao();
                    StyleMappingDao copyTableStyleMappingDao = copyFeatureTableStyles.getTableStyleMappingDao();
                    TestCase.assertEquals(tableStyleMappingDao.count(), copyTableStyleMappingDao.count());
                }
                if (featureTableStyles.hasTableIconRelationship()) {
                    StyleMappingDao tableIconMappingDao = featureTableStyles.getTableIconMappingDao();
                    StyleMappingDao copyTableIconMappingDao = copyFeatureTableStyles.getTableIconMappingDao();
                    TestCase.assertEquals(tableIconMappingDao.count(), copyTableIconMappingDao.count());
                }
            }
            indexManager.close();
            copyIndexManager.close();
            String newTableName2 = tableName + "_copy2";
            geoPackage.copyTableAsEmpty(tableName, newTableName2);
            FeatureDao copyDao2 = geoPackage.getFeatureDao(newTableName2);
            TestCase.assertEquals(0, copyDao2.count());
        }
    }
}
Also used : MetadataReferenceDao(mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao) ContentsIdExtension(mil.nga.geopackage.extension.nga.contents.ContentsIdExtension) HashMap(java.util.HashMap) DataColumns(mil.nga.geopackage.extension.schema.columns.DataColumns) DataColumnsDao(mil.nga.geopackage.extension.schema.columns.DataColumnsDao) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) FeatureColumn(mil.nga.geopackage.features.user.FeatureColumn) FeatureTableStyles(mil.nga.geopackage.extension.nga.style.FeatureTableStyles) GeometryColumns(mil.nga.geopackage.features.columns.GeometryColumns) FeatureTable(mil.nga.geopackage.features.user.FeatureTable) SchemaExtension(mil.nga.geopackage.extension.schema.SchemaExtension) FeatureTileTableLinker(mil.nga.geopackage.extension.nga.link.FeatureTileTableLinker) ContentsId(mil.nga.geopackage.extension.nga.contents.ContentsId) MetadataReference(mil.nga.geopackage.extension.metadata.reference.MetadataReference) RelatedTablesExtension(mil.nga.geopackage.extension.related.RelatedTablesExtension) UniqueConstraint(mil.nga.geopackage.db.table.UniqueConstraint) RawConstraint(mil.nga.geopackage.db.table.RawConstraint) FeatureIndexManager(mil.nga.geopackage.features.index.FeatureIndexManager) ExtendedRelation(mil.nga.geopackage.extension.related.ExtendedRelation) GeometryColumnsDao(mil.nga.geopackage.features.columns.GeometryColumnsDao) StyleMappingDao(mil.nga.geopackage.extension.nga.style.StyleMappingDao)

Example 4 with RelatedTablesExtension

use of mil.nga.geopackage.extension.related.RelatedTablesExtension in project geopackage-java by ngageoint.

the class RelatedSimpleAttributesUtils method testSimpleAttributes.

/**
 * Test related simple attributes tables
 *
 * @param geoPackage
 * @throws SQLException
 */
public static void testSimpleAttributes(GeoPackage geoPackage) throws Exception {
    // Create a related tables extension
    RelatedTablesExtension rte = new RelatedTablesExtension(geoPackage);
    if (rte.has()) {
        rte.removeExtension();
    }
    TestCase.assertFalse(rte.has());
    TestCase.assertTrue(rte.getRelationships().isEmpty());
    // Choose a random attributes table
    List<String> attributesTables = geoPackage.getAttributesTables();
    if (attributesTables.isEmpty()) {
        // pass with no testing
        return;
    }
    final String baseTableName = attributesTables.get((int) (Math.random() * attributesTables.size()));
    // Validate nullable non simple columns
    try {
        SimpleAttributesTable.create(SimpleAttributesTableMetadata.create("simple_table", RelatedTablesUtils.createAdditionalUserColumns()));
        TestCase.fail("Simple Attributes Table created with nullable non simple columns");
    } catch (Exception e) {
    // pass
    }
    // Validate non nullable non simple columns
    try {
        SimpleAttributesTable.create(SimpleAttributesTableMetadata.create("simple_table", RelatedTablesUtils.createAdditionalUserColumns(true)));
        TestCase.fail("Simple Attributes Table created with non nullable non simple columns");
    } catch (Exception e) {
    // pass
    }
    // Validate nullable simple columns
    try {
        SimpleAttributesTable.create(SimpleAttributesTableMetadata.create("simple_table", RelatedTablesUtils.createSimpleUserColumns(false)));
        TestCase.fail("Simple Attributes Table created with nullable simple columns");
    } catch (Exception e) {
    // pass
    }
    // Populate and validate a simple attributes table
    List<UserCustomColumn> simpleUserColumns = RelatedTablesUtils.createSimpleUserColumns();
    SimpleAttributesTable simpleTable = SimpleAttributesTable.create(SimpleAttributesTableMetadata.create("simple_table", simpleUserColumns));
    String[] simpleColumns = simpleTable.getColumnNames();
    TestCase.assertEquals(SimpleAttributesTable.numRequiredColumns() + simpleUserColumns.size(), simpleColumns.length);
    UserCustomColumn idColumn = simpleTable.getIdColumn();
    TestCase.assertNotNull(idColumn);
    TestCase.assertTrue(idColumn.isNamed(SimpleAttributesTable.COLUMN_ID));
    TestCase.assertEquals(GeoPackageDataType.INTEGER, idColumn.getDataType());
    TestCase.assertTrue(idColumn.isNotNull());
    TestCase.assertTrue(idColumn.isPrimaryKey());
    // Create and validate a mapping table
    List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils.createAdditionalUserColumns();
    final String mappingTableName = "attributes_simple_attributes";
    UserMappingTable userMappingTable = UserMappingTable.create(mappingTableName, additionalMappingColumns);
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    TestCase.assertEquals(UserMappingTable.numRequiredColumns() + additionalMappingColumns.size(), userMappingTable.getColumns().size());
    UserCustomColumn baseIdColumn = userMappingTable.getBaseIdColumn();
    TestCase.assertNotNull(baseIdColumn);
    TestCase.assertTrue(baseIdColumn.isNamed(UserMappingTable.COLUMN_BASE_ID));
    TestCase.assertEquals(GeoPackageDataType.INTEGER, baseIdColumn.getDataType());
    TestCase.assertTrue(baseIdColumn.isNotNull());
    TestCase.assertFalse(baseIdColumn.isPrimaryKey());
    UserCustomColumn relatedIdColumn = userMappingTable.getRelatedIdColumn();
    TestCase.assertNotNull(relatedIdColumn);
    TestCase.assertTrue(relatedIdColumn.isNamed(UserMappingTable.COLUMN_RELATED_ID));
    TestCase.assertEquals(GeoPackageDataType.INTEGER, relatedIdColumn.getDataType());
    TestCase.assertTrue(relatedIdColumn.isNotNull());
    TestCase.assertFalse(relatedIdColumn.isPrimaryKey());
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    // Create the simple attributes table, content row, and relationship
    // between the attributes table and simple attributes table
    ContentsDao contentsDao = geoPackage.getContentsDao();
    TestCase.assertFalse(contentsDao.getTables().contains(simpleTable.getTableName()));
    ExtendedRelation extendedRelation = rte.addSimpleAttributesRelationship(baseTableName, simpleTable, userMappingTable);
    validateContents(simpleTable, simpleTable.getContents());
    TestCase.assertTrue(rte.has());
    TestCase.assertTrue(rte.has(userMappingTable.getTableName()));
    TestCase.assertNotNull(extendedRelation);
    List<ExtendedRelation> extendedRelations = rte.getRelationships();
    TestCase.assertEquals(1, extendedRelations.size());
    TestCase.assertTrue(geoPackage.isTable(mappingTableName));
    TestCase.assertTrue(geoPackage.isTable(simpleTable.getTableName()));
    TestCase.assertTrue(contentsDao.getTables().contains(simpleTable.getTableName()));
    validateContents(simpleTable, contentsDao.queryForId(simpleTable.getTableName()));
    TestCase.assertEquals(SimpleAttributesTable.RELATION_TYPE.getDataType(), geoPackage.getTableType(simpleTable.getTableName()));
    TestCase.assertTrue(geoPackage.isTableType(simpleTable.getTableName(), SimpleAttributesTable.RELATION_TYPE.getDataType()));
    // Validate the simple attributes DAO
    SimpleAttributesDao simpleDao = rte.getSimpleAttributesDao(simpleTable);
    TestCase.assertNotNull(simpleDao);
    simpleTable = simpleDao.getTable();
    TestCase.assertNotNull(simpleTable);
    validateContents(simpleTable, simpleTable.getContents());
    // Insert simple attributes table rows
    int simpleCount = 2 + (int) (Math.random() * 9);
    long simpleRowId = 0;
    // Create and insert the first simpleCount - 1 rows
    for (int i = 0; i < simpleCount - 1; i++) {
        SimpleAttributesRow simpleRow = simpleDao.newRow();
        RelatedTablesUtils.populateUserRow(simpleTable, simpleRow, SimpleAttributesTable.requiredColumns());
        simpleRowId = simpleDao.create(simpleRow);
        TestCase.assertTrue(simpleRowId > 0);
    }
    // Copy the last row insert and insert the final simple attributes row
    SimpleAttributesRow simpleRowToCopy = new SimpleAttributesRow(simpleDao.queryForIdRow(simpleRowId));
    SimpleAttributesRow simpleRowCopy = simpleRowToCopy.copy();
    long copySimpleRowId = simpleDao.create(simpleRowCopy);
    TestCase.assertTrue(copySimpleRowId > 0);
    TestCase.assertEquals(simpleRowId + 1, copySimpleRowId);
    TestCase.assertEquals(simpleCount, simpleDao.count());
    // Build the Attributes ids
    AttributesDao attributesDao = geoPackage.getAttributesDao(baseTableName);
    AttributesResultSet attributesResultSet = attributesDao.queryForAll();
    int attributesCount = attributesResultSet.getCount();
    List<Long> attributeIds = new ArrayList<>();
    while (attributesResultSet.moveToNext()) {
        attributeIds.add(attributesResultSet.getRow().getId());
    }
    attributesResultSet.close();
    // Build the Simple Attribute ids
    UserCustomResultSet simpleResultSet = simpleDao.queryForAll();
    simpleCount = simpleResultSet.getCount();
    List<Long> simpleIds = new ArrayList<>();
    while (simpleResultSet.moveToNext()) {
        simpleIds.add(simpleResultSet.getRow().getId());
    }
    simpleResultSet.close();
    // Insert user mapping rows between attribute ids and simple attribute
    // ids
    UserMappingDao dao = rte.getMappingDao(mappingTableName);
    UserMappingRow userMappingRow = null;
    for (int i = 0; i < 10; i++) {
        userMappingRow = dao.newRow();
        userMappingRow.setBaseId(attributeIds.get((int) (Math.random() * attributesCount)));
        userMappingRow.setRelatedId(simpleIds.get((int) (Math.random() * simpleCount)));
        RelatedTablesUtils.populateUserRow(userMappingTable, userMappingRow, UserMappingTable.requiredColumns());
        TestCase.assertTrue(dao.create(userMappingRow) > 0);
    }
    TestCase.assertEquals(10, dao.count());
    // Validate the user mapping rows
    userMappingTable = dao.getTable();
    String[] mappingColumns = userMappingTable.getColumnNames();
    UserCustomResultSet resultSet = dao.queryForAll();
    int count = resultSet.getCount();
    TestCase.assertEquals(10, count);
    int manualCount = 0;
    while (resultSet.moveToNext()) {
        UserMappingRow resultRow = dao.getRow(resultSet);
        TestCase.assertFalse(resultRow.hasId());
        TestCase.assertTrue(attributeIds.contains(resultRow.getBaseId()));
        TestCase.assertTrue(simpleIds.contains(resultRow.getRelatedId()));
        RelatedTablesUtils.validateUserRow(mappingColumns, resultRow);
        RelatedTablesUtils.validateDublinCoreColumns(resultRow);
        manualCount++;
    }
    TestCase.assertEquals(count, manualCount);
    resultSet.close();
    ExtendedRelationsDao extendedRelationsDao = rte.getExtendedRelationsDao();
    // Get the relations starting from the attributes table
    List<ExtendedRelation> attributesExtendedRelations = extendedRelationsDao.getBaseTableRelations(attributesDao.getTableName());
    List<ExtendedRelation> attributesExtendedRelations2 = extendedRelationsDao.getTableRelations(attributesDao.getTableName());
    TestCase.assertEquals(1, attributesExtendedRelations.size());
    TestCase.assertEquals(1, attributesExtendedRelations2.size());
    TestCase.assertEquals(attributesExtendedRelations.get(0).getId(), attributesExtendedRelations2.get(0).getId());
    TestCase.assertTrue(extendedRelationsDao.getRelatedTableRelations(attributesDao.getTableName()).isEmpty());
    // Test the attributes table relations
    for (ExtendedRelation attributesRelation : attributesExtendedRelations) {
        // Test the relation
        TestCase.assertTrue(attributesRelation.getId() >= 0);
        TestCase.assertEquals(attributesDao.getTableName(), attributesRelation.getBaseTableName());
        TestCase.assertEquals(attributesDao.getPkColumnName(), attributesRelation.getBasePrimaryColumn());
        TestCase.assertEquals(simpleDao.getTableName(), attributesRelation.getRelatedTableName());
        TestCase.assertEquals(simpleDao.getPkColumnName(), attributesRelation.getRelatedPrimaryColumn());
        TestCase.assertEquals(SimpleAttributesTable.RELATION_TYPE.getName(), attributesRelation.getRelationName());
        TestCase.assertEquals(mappingTableName, attributesRelation.getMappingTableName());
        // Test the user mappings from the relation
        UserMappingDao userMappingDao = rte.getMappingDao(attributesRelation);
        int totalMappedCount = userMappingDao.count();
        UserCustomResultSet mappingResultSet = userMappingDao.queryForAll();
        while (mappingResultSet.moveToNext()) {
            userMappingRow = userMappingDao.getRow(mappingResultSet);
            TestCase.assertTrue(attributeIds.contains(userMappingRow.getBaseId()));
            TestCase.assertTrue(simpleIds.contains(userMappingRow.getRelatedId()));
            RelatedTablesUtils.validateUserRow(mappingColumns, userMappingRow);
            RelatedTablesUtils.validateDublinCoreColumns(userMappingRow);
        }
        mappingResultSet.close();
        // Get and test the simple attributes DAO
        simpleDao = rte.getSimpleAttributesDao(attributesRelation);
        TestCase.assertNotNull(simpleDao);
        simpleTable = simpleDao.getTable();
        TestCase.assertNotNull(simpleTable);
        validateContents(simpleTable, simpleTable.getContents());
        // Get and test the Simple Attributes Rows mapped to each
        // Attributes Row
        attributesResultSet = attributesDao.queryForAll();
        int totalMapped = 0;
        while (attributesResultSet.moveToNext()) {
            AttributesRow attributesRow = attributesResultSet.getRow();
            List<Long> mappedIds = rte.getMappingsForBase(attributesRelation, attributesRow.getId());
            List<SimpleAttributesRow> simpleRows = simpleDao.getRows(mappedIds);
            TestCase.assertEquals(mappedIds.size(), simpleRows.size());
            for (SimpleAttributesRow simpleRow : simpleRows) {
                TestCase.assertTrue(simpleRow.hasId());
                TestCase.assertTrue(simpleRow.getId() >= 0);
                TestCase.assertTrue(simpleIds.contains(simpleRow.getId()));
                TestCase.assertTrue(mappedIds.contains(simpleRow.getId()));
                RelatedTablesUtils.validateUserRow(simpleColumns, simpleRow);
                RelatedTablesUtils.validateSimpleDublinCoreColumns(simpleRow);
            }
            totalMapped += mappedIds.size();
        }
        attributesResultSet.close();
        TestCase.assertEquals(totalMappedCount, totalMapped);
    }
    // Get the relations starting from the simple attributes table
    List<ExtendedRelation> simpleExtendedRelations = extendedRelationsDao.getRelatedTableRelations(simpleTable.getTableName());
    List<ExtendedRelation> simpleExtendedRelations2 = extendedRelationsDao.getTableRelations(simpleTable.getTableName());
    TestCase.assertEquals(1, simpleExtendedRelations.size());
    TestCase.assertEquals(1, simpleExtendedRelations2.size());
    TestCase.assertEquals(simpleExtendedRelations.get(0).getId(), simpleExtendedRelations2.get(0).getId());
    TestCase.assertTrue(extendedRelationsDao.getBaseTableRelations(simpleTable.getTableName()).isEmpty());
    // Test the simple attributes table relations
    for (ExtendedRelation simpleRelation : simpleExtendedRelations) {
        // Test the relation
        TestCase.assertTrue(simpleRelation.getId() >= 0);
        TestCase.assertEquals(attributesDao.getTableName(), simpleRelation.getBaseTableName());
        TestCase.assertEquals(attributesDao.getPkColumnName(), simpleRelation.getBasePrimaryColumn());
        TestCase.assertEquals(simpleDao.getTableName(), simpleRelation.getRelatedTableName());
        TestCase.assertEquals(simpleDao.getPkColumnName(), simpleRelation.getRelatedPrimaryColumn());
        TestCase.assertEquals(SimpleAttributesTable.RELATION_TYPE.getName(), simpleRelation.getRelationName());
        TestCase.assertEquals(mappingTableName, simpleRelation.getMappingTableName());
        // Test the user mappings from the relation
        UserMappingDao userMappingDao = rte.getMappingDao(simpleRelation);
        int totalMappedCount = userMappingDao.count();
        UserCustomResultSet mappingResultSet = userMappingDao.queryForAll();
        while (mappingResultSet.moveToNext()) {
            userMappingRow = userMappingDao.getRow(mappingResultSet);
            TestCase.assertTrue(attributeIds.contains(userMappingRow.getBaseId()));
            TestCase.assertTrue(simpleIds.contains(userMappingRow.getRelatedId()));
            RelatedTablesUtils.validateUserRow(mappingColumns, userMappingRow);
            RelatedTablesUtils.validateDublinCoreColumns(userMappingRow);
        }
        mappingResultSet.close();
        // Get and test the attributes DAO
        attributesDao = geoPackage.getAttributesDao(attributesDao.getTableName());
        TestCase.assertNotNull(attributesDao);
        AttributesTable attributesTable = attributesDao.getTable();
        TestCase.assertNotNull(attributesTable);
        Contents attributesContents = attributesTable.getContents();
        TestCase.assertNotNull(attributesContents);
        TestCase.assertEquals(ContentsDataType.ATTRIBUTES, attributesContents.getDataType());
        TestCase.assertEquals(ContentsDataType.ATTRIBUTES.getName(), attributesContents.getDataTypeName());
        TestCase.assertEquals(attributesTable.getTableName(), attributesContents.getTableName());
        TestCase.assertNotNull(attributesContents.getLastChange());
        // Get and test the Attributes Rows mapped to each Simple Attributes
        // Row
        simpleResultSet = simpleDao.queryForAll();
        int totalMapped = 0;
        while (simpleResultSet.moveToNext()) {
            SimpleAttributesRow simpleRow = simpleDao.getRow(simpleResultSet);
            List<Long> mappedIds = rte.getMappingsForRelated(simpleRelation, simpleRow.getId());
            for (long mappedId : mappedIds) {
                AttributesRow attributesRow = attributesDao.queryForIdRow(mappedId);
                TestCase.assertNotNull(attributesRow);
                TestCase.assertTrue(attributesRow.hasId());
                TestCase.assertTrue(attributesRow.getId() >= 0);
                TestCase.assertTrue(attributeIds.contains(attributesRow.getId()));
                TestCase.assertTrue(mappedIds.contains(attributesRow.getId()));
            }
            totalMapped += mappedIds.size();
        }
        simpleResultSet.close();
        TestCase.assertEquals(totalMappedCount, totalMapped);
    }
    // Delete a single mapping
    int countOfIds = dao.countByIds(userMappingRow);
    TestCase.assertEquals(countOfIds, dao.deleteByIds(userMappingRow));
    TestCase.assertEquals(10 - countOfIds, dao.count());
    // Delete the relationship and user mapping table
    rte.removeRelationship(extendedRelation);
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    extendedRelations = rte.getRelationships();
    TestCase.assertEquals(0, extendedRelations.size());
    TestCase.assertFalse(geoPackage.isTable(mappingTableName));
    // Delete the simple attributes table and contents row
    TestCase.assertTrue(geoPackage.isTable(simpleTable.getTableName()));
    TestCase.assertNotNull(contentsDao.queryForId(simpleTable.getTableName()));
    geoPackage.deleteTable(simpleTable.getTableName());
    TestCase.assertFalse(geoPackage.isTable(simpleTable.getTableName()));
    TestCase.assertNull(contentsDao.queryForId(simpleTable.getTableName()));
    // Delete the related tables extension
    rte.removeExtension();
    TestCase.assertFalse(rte.has());
}
Also used : ArrayList(java.util.ArrayList) ExtendedRelationsDao(mil.nga.geopackage.extension.related.ExtendedRelationsDao) AttributesResultSet(mil.nga.geopackage.attributes.AttributesResultSet) UserCustomResultSet(mil.nga.geopackage.user.custom.UserCustomResultSet) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) AttributesTable(mil.nga.geopackage.attributes.AttributesTable) RelatedTablesExtension(mil.nga.geopackage.extension.related.RelatedTablesExtension) UserMappingRow(mil.nga.geopackage.extension.related.UserMappingRow) SQLException(java.sql.SQLException) AttributesRow(mil.nga.geopackage.attributes.AttributesRow) UserMappingTable(mil.nga.geopackage.extension.related.UserMappingTable) Contents(mil.nga.geopackage.contents.Contents) UserMappingDao(mil.nga.geopackage.extension.related.UserMappingDao) ExtendedRelation(mil.nga.geopackage.extension.related.ExtendedRelation) ContentsDao(mil.nga.geopackage.contents.ContentsDao)

Example 5 with RelatedTablesExtension

use of mil.nga.geopackage.extension.related.RelatedTablesExtension in project geopackage-java by ngageoint.

the class GeoPackageExample method createRelatedTablesMediaExtension.

private static void createRelatedTablesMediaExtension(GeoPackage geoPackage) throws IOException {
    RelatedTablesExtension relatedTables = new RelatedTablesExtension(geoPackage);
    List<UserCustomColumn> additionalMediaColumns = RelatedTablesUtils.createAdditionalUserColumns();
    MediaTable mediaTable = MediaTable.create(MediaTableMetadata.create("media", additionalMediaColumns));
    List<UserCustomColumn> additionalMappingColumns = RelatedTablesUtils.createAdditionalUserColumns();
    String tableName1 = "geometry1";
    UserMappingTable userMappingTable1 = UserMappingTable.create(tableName1 + "_" + mediaTable.getTableName(), additionalMappingColumns);
    ExtendedRelation relation1 = relatedTables.addMediaRelationship(tableName1, mediaTable, userMappingTable1);
    insertRelatedTablesMediaExtensionRows(geoPackage, relation1, "BIT Systems%", "BIT Systems", "BITSystems_Logo.png", "image/png", "BIT Systems Logo", "https://www.caci.com/bit-systems");
    String tableName2 = "geometry2";
    UserMappingTable userMappingTable2 = UserMappingTable.create(tableName2 + "_" + mediaTable.getTableName(), additionalMappingColumns);
    ExtendedRelation relation2 = relatedTables.addMediaRelationship(tableName2, mediaTable, userMappingTable2);
    insertRelatedTablesMediaExtensionRows(geoPackage, relation2, "NGA%", "NGA", "NGA_Logo.png", "image/png", "NGA Logo", "http://www.nga.mil");
    insertRelatedTablesMediaExtensionRows(geoPackage, relation2, "NGA", "NGA", "NGA.jpg", "image/jpeg", "Aerial View of NGA East", "http://www.nga.mil");
    if (CONTENTS_ID) {
        insertRelatedTablesMediaPreviewExtensionRows(geoPackage, relatedTables);
    }
}
Also used : UserMappingTable(mil.nga.geopackage.extension.related.UserMappingTable) MediaTable(mil.nga.geopackage.extension.related.media.MediaTable) RelatedTablesExtension(mil.nga.geopackage.extension.related.RelatedTablesExtension) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) MultiLineString(mil.nga.sf.MultiLineString) LineString(mil.nga.sf.LineString) CircularString(mil.nga.sf.CircularString) ExtendedRelation(mil.nga.geopackage.extension.related.ExtendedRelation)

Aggregations

RelatedTablesExtension (mil.nga.geopackage.extension.related.RelatedTablesExtension)12 ExtendedRelation (mil.nga.geopackage.extension.related.ExtendedRelation)9 UserMappingDao (mil.nga.geopackage.extension.related.UserMappingDao)6 UserMappingRow (mil.nga.geopackage.extension.related.UserMappingRow)6 UserMappingTable (mil.nga.geopackage.extension.related.UserMappingTable)6 UserCustomColumn (mil.nga.geopackage.user.custom.UserCustomColumn)6 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)5 CircularString (mil.nga.sf.CircularString)5 LineString (mil.nga.sf.LineString)5 MultiLineString (mil.nga.sf.MultiLineString)5 SchemaExtension (mil.nga.geopackage.extension.schema.SchemaExtension)4 FeatureResultSet (mil.nga.geopackage.features.user.FeatureResultSet)4 FeatureRow (mil.nga.geopackage.features.user.FeatureRow)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 AttributesDao (mil.nga.geopackage.attributes.AttributesDao)3 RawConstraint (mil.nga.geopackage.db.table.RawConstraint)3 UniqueConstraint (mil.nga.geopackage.db.table.UniqueConstraint)3 MetadataReference (mil.nga.geopackage.extension.metadata.reference.MetadataReference)3 MetadataReferenceDao (mil.nga.geopackage.extension.metadata.reference.MetadataReferenceDao)3