Search in sources :

Example 1 with UserCustomColumn

use of mil.nga.geopackage.user.custom.UserCustomColumn in project geopackage-java by ngageoint.

the class RelatedTablesUtils method validateDublinCoreColumn.

/**
 * Validate a user row for expected Dublin Core Column
 *
 * @param userRow
 *            user custom row
 * @param type
 *            Dublin Core Type
 */
public static void validateDublinCoreColumn(UserCustomRow userRow, DublinCoreType type) {
    UserCustomTable customTable = userRow.getTable();
    TestCase.assertTrue(DublinCoreMetadata.hasColumn(userRow.getTable(), type));
    TestCase.assertTrue(DublinCoreMetadata.hasColumn(userRow, type));
    UserCustomColumn column1 = DublinCoreMetadata.getColumn(customTable, type);
    UserCustomColumn column2 = DublinCoreMetadata.getColumn(userRow, type);
    TestCase.assertNotNull(column1);
    TestCase.assertNotNull(column2);
    TestCase.assertEquals(column1, column2);
    Object value = DublinCoreMetadata.getValue(userRow, type);
    TestCase.assertNotNull(value);
}
Also used : UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) UserCustomTable(mil.nga.geopackage.user.custom.UserCustomTable)

Example 2 with UserCustomColumn

use of mil.nga.geopackage.user.custom.UserCustomColumn in project geopackage-java by ngageoint.

the class RelatedTablesUtils method validateUserRow.

/**
 * Validate a user row
 *
 * @param columns
 *            array of columns
 * @param userRow
 *            user custom row
 */
public static void validateUserRow(String[] columns, UserCustomRow userRow) {
    TestCase.assertEquals(columns.length, userRow.columnCount());
    for (int i = 0; i < userRow.columnCount(); i++) {
        UserCustomColumn column = userRow.getTable().getColumns().get(i);
        GeoPackageDataType dataType = column.getDataType();
        TestCase.assertEquals(i, column.getIndex());
        TestCase.assertEquals(columns[i], userRow.getColumnName(i));
        TestCase.assertEquals(i, userRow.getColumnIndex(columns[i]));
        int rowType = userRow.getRowColumnType(i);
        Object value = userRow.getValue(i);
        switch(rowType) {
            case ResultUtils.FIELD_TYPE_INTEGER:
                TestUtils.validateIntegerValue(value, column.getDataType());
                break;
            case ResultUtils.FIELD_TYPE_FLOAT:
                TestUtils.validateFloatValue(value, column.getDataType());
                break;
            case ResultUtils.FIELD_TYPE_STRING:
                if (dataType == GeoPackageDataType.DATE || dataType == GeoPackageDataType.DATETIME) {
                    TestCase.assertTrue(value instanceof Date);
                    Date date = (Date) value;
                    DateConverter converter = DateConverter.converter(dataType);
                    String dateString = converter.stringValue(date);
                    TestCase.assertEquals(date.getTime(), converter.dateValue(dateString).getTime());
                } else {
                    TestCase.assertTrue(value instanceof String);
                }
                break;
            case ResultUtils.FIELD_TYPE_BLOB:
                TestCase.assertTrue(value instanceof byte[]);
                break;
            case ResultUtils.FIELD_TYPE_NULL:
                TestCase.assertNull(value);
                break;
        }
    }
}
Also used : DateConverter(mil.nga.geopackage.db.DateConverter) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) GeoPackageDataType(mil.nga.geopackage.db.GeoPackageDataType) Date(java.util.Date)

Example 3 with UserCustomColumn

use of mil.nga.geopackage.user.custom.UserCustomColumn in project geopackage-java by ngageoint.

the class RelatedTablesUtils method populateUserRow.

/**
 * Populate the user row additional column values
 *
 * @param userTable
 *            user custom table
 * @param userRow
 *            user custom row
 * @param skipColumns
 *            columns to skip populating
 */
public static void populateUserRow(UserCustomTable userTable, UserCustomRow userRow, List<String> skipColumns) {
    Set<String> skipColumnsSet = new HashSet<>(skipColumns);
    for (UserCustomColumn column : userTable.getColumns()) {
        if (!skipColumnsSet.contains(column.getName())) {
            // Leave nullable columns null 20% of the time
            if (!column.isNotNull() && DublinCoreType.fromName(column.getName()) == null) {
                if (Math.random() < 0.2) {
                    continue;
                }
            }
            Object value = null;
            switch(column.getDataType()) {
                case TEXT:
                    String text = UUID.randomUUID().toString();
                    if (column.getMax() != null && text.length() > column.getMax()) {
                        text = text.substring(0, column.getMax().intValue());
                    }
                    value = text;
                    break;
                case REAL:
                case DOUBLE:
                    value = Math.random() * 5000.0;
                    break;
                case BOOLEAN:
                    value = Math.random() < .5 ? false : true;
                    break;
                case INTEGER:
                case INT:
                    value = (int) (Math.random() * 500);
                    break;
                case BLOB:
                    byte[] blob = UUID.randomUUID().toString().getBytes();
                    if (column.getMax() != null && blob.length > column.getMax()) {
                        byte[] blobLimited = new byte[column.getMax().intValue()];
                        ByteBuffer.wrap(blob, 0, column.getMax().intValue()).get(blobLimited);
                        blob = blobLimited;
                    }
                    value = blob;
                    break;
                case DATE:
                case DATETIME:
                    DateConverter converter = DateConverter.converter(column.getDataType());
                    Date date = new Date();
                    if (Math.random() < .5) {
                        value = date;
                    } else {
                        value = converter.stringValue(date);
                    }
                    break;
                default:
                    throw new UnsupportedOperationException("Not implemented for data type: " + column.getDataType());
            }
            userRow.setValue(column.getName(), value);
        }
    }
}
Also used : DateConverter(mil.nga.geopackage.db.DateConverter) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) Date(java.util.Date) HashSet(java.util.HashSet)

Example 4 with UserCustomColumn

use of mil.nga.geopackage.user.custom.UserCustomColumn in project geopackage-java by ngageoint.

the class RelatedTablesWriteTest method testWriteRelationshipsToAttributes.

/**
 * Test write relationships to attributes
 *
 * @throws SQLException
 * @throws IOException
 */
@Test
public void testWriteRelationshipsToAttributes() throws SQLException, IOException {
    RelatedTablesExtension rte = new RelatedTablesExtension(geoPackage);
    if (rte.has()) {
        rte.removeExtension();
    }
    // 1. Has extension
    TestCase.assertFalse(rte.has());
    // 4. Get relationships
    List<ExtendedRelation> extendedRelations = rte.getRelationships();
    TestCase.assertTrue(extendedRelations.isEmpty());
    // 2. Add extension
    // 5. Add relationship between "geometry2d" and "attributes"
    final String baseTableName = "geometry1";
    final String relatedTableName = "attributes";
    final String mappingTableName = "g1_a";
    List<UserCustomColumn> additionalColumns = RelatedTablesUtils.createAdditionalUserColumns();
    UserMappingTable userMappingTable = UserMappingTable.create(mappingTableName, additionalColumns);
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    TestCase.assertEquals(UserMappingTable.numRequiredColumns() + additionalColumns.size(), userMappingTable.getColumns().size());
    UserCustomColumn baseIdColumn = userMappingTable.getBaseIdColumn();
    TestCase.assertNotNull(baseIdColumn);
    TestCase.assertTrue(baseIdColumn.isNamed(UserMappingTable.COLUMN_BASE_ID));
    TestCase.assertTrue(baseIdColumn.isNotNull());
    TestCase.assertFalse(baseIdColumn.isPrimaryKey());
    UserCustomColumn relatedIdColumn = userMappingTable.getRelatedIdColumn();
    TestCase.assertNotNull(relatedIdColumn);
    TestCase.assertTrue(relatedIdColumn.isNamed(UserMappingTable.COLUMN_RELATED_ID));
    TestCase.assertTrue(relatedIdColumn.isNotNull());
    TestCase.assertFalse(relatedIdColumn.isPrimaryKey());
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    ExtendedRelation extendedRelation = rte.addAttributesRelationship(baseTableName, relatedTableName, userMappingTable);
    TestCase.assertTrue(rte.has());
    TestCase.assertTrue(rte.has(userMappingTable.getTableName()));
    TestCase.assertNotNull(extendedRelation);
    extendedRelations = rte.getRelationships();
    TestCase.assertEquals(1, extendedRelations.size());
    TestCase.assertTrue(geoPackage.isTable(mappingTableName));
    // 7. Add mappings
    FeatureDao baseDao = geoPackage.getFeatureDao(baseTableName);
    AttributesDao relatedDao = geoPackage.getAttributesDao(relatedTableName);
    FeatureResultSet baseFrs = baseDao.queryForAll();
    int baseCount = baseFrs.getCount();
    long[] baseIds = new long[baseCount];
    int inx = 0;
    while (baseFrs.moveToNext()) {
        baseIds[inx++] = baseFrs.getRow().getId();
    }
    baseFrs.close();
    AttributesResultSet relatedArs = relatedDao.queryForAll();
    int relatedCount = relatedArs.getCount();
    long[] relatedIds = new long[relatedCount];
    inx = 0;
    while (relatedArs.moveToNext()) {
        relatedIds[inx++] = relatedArs.getRow().getId();
    }
    relatedArs.close();
    UserMappingDao dao = rte.getMappingDao(mappingTableName);
    UserMappingRow userMappingRow = null;
    for (inx = 0; inx < 10; inx++) {
        userMappingRow = dao.newRow();
        userMappingRow.setBaseId(baseIds[(int) (Math.random() * baseCount)]);
        userMappingRow.setRelatedId(relatedIds[(int) (Math.random() * relatedCount)]);
        RelatedTablesUtils.populateUserRow(userMappingTable, userMappingRow, UserMappingTable.requiredColumns());
        TestCase.assertTrue(dao.create(userMappingRow) > 0);
    }
    TestCase.assertEquals(10, dao.count());
    userMappingTable = dao.getTable();
    String[] columns = 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());
        RelatedTablesUtils.validateUserRow(columns, resultRow);
        RelatedTablesUtils.validateDublinCoreColumns(resultRow);
        manualCount++;
    }
    TestCase.assertEquals(count, manualCount);
    resultSet.close();
    // 8. Remove mappings (note: it is plausible and allowed
    // to have duplicate entries)
    TestCase.assertTrue(dao.deleteByIds(userMappingRow) > 0);
    // 6. Remove relationship
    rte.removeRelationship(extendedRelation);
    TestCase.assertFalse(rte.has(userMappingTable.getTableName()));
    extendedRelations = rte.getRelationships();
    TestCase.assertEquals(0, extendedRelations.size());
    TestCase.assertFalse(geoPackage.isTable(mappingTableName));
    // 3. Remove extension
    rte.removeExtension();
    TestCase.assertFalse(rte.has());
}
Also used : AttributesResultSet(mil.nga.geopackage.attributes.AttributesResultSet) FeatureDao(mil.nga.geopackage.features.user.FeatureDao) UserCustomResultSet(mil.nga.geopackage.user.custom.UserCustomResultSet) FeatureResultSet(mil.nga.geopackage.features.user.FeatureResultSet) AttributesDao(mil.nga.geopackage.attributes.AttributesDao) UserCustomColumn(mil.nga.geopackage.user.custom.UserCustomColumn) Test(org.junit.Test)

Example 5 with UserCustomColumn

use of mil.nga.geopackage.user.custom.UserCustomColumn 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)

Aggregations

UserCustomColumn (mil.nga.geopackage.user.custom.UserCustomColumn)20 ArrayList (java.util.ArrayList)9 UserCustomResultSet (mil.nga.geopackage.user.custom.UserCustomResultSet)7 ExtendedRelation (mil.nga.geopackage.extension.related.ExtendedRelation)6 RelatedTablesExtension (mil.nga.geopackage.extension.related.RelatedTablesExtension)6 UserMappingTable (mil.nga.geopackage.extension.related.UserMappingTable)6 FeatureDao (mil.nga.geopackage.features.user.FeatureDao)6 FeatureResultSet (mil.nga.geopackage.features.user.FeatureResultSet)6 UserCustomTable (mil.nga.geopackage.user.custom.UserCustomTable)6 AttributesDao (mil.nga.geopackage.attributes.AttributesDao)4 AttributesResultSet (mil.nga.geopackage.attributes.AttributesResultSet)4 Contents (mil.nga.geopackage.contents.Contents)4 UserMappingDao (mil.nga.geopackage.extension.related.UserMappingDao)4 UserMappingRow (mil.nga.geopackage.extension.related.UserMappingRow)4 Test (org.junit.Test)4 SQLException (java.sql.SQLException)3 AttributesRow (mil.nga.geopackage.attributes.AttributesRow)3 FeatureRow (mil.nga.geopackage.features.user.FeatureRow)3 TileDao (mil.nga.geopackage.tiles.user.TileDao)3 TileResultSet (mil.nga.geopackage.tiles.user.TileResultSet)3