use of mil.nga.geopackage.extension.related.UserMappingDao 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());
}
use of mil.nga.geopackage.extension.related.UserMappingDao 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());
}
use of mil.nga.geopackage.extension.related.UserMappingDao in project geopackage-java by ngageoint.
the class GeoPackageExample method insertRelatedTablesMediaPreviewExtensionRows.
private static void insertRelatedTablesMediaPreviewExtensionRows(GeoPackage geoPackage, RelatedTablesExtension relatedTables) throws IOException {
ContentsIdExtension contentsId = new ContentsIdExtension(geoPackage);
MediaTable mediaTable = MediaTable.create(MediaTableMetadata.create("preview"));
UserMappingTable userMappingTable = UserMappingTable.create("features_" + mediaTable.getTableName());
ExtendedRelation relation = relatedTables.addMediaRelationship(ContentsId.TABLE_NAME, mediaTable, userMappingTable);
MediaDao mediaDao = relatedTables.getMediaDao(relation);
UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);
for (String featureTable : geoPackage.getFeatureTables()) {
long featureContentsId = contentsId.getOrCreateId(featureTable);
FeaturePreview preview = new FeaturePreview(geoPackage, featureTable);
preview.setManual(true);
preview.setBufferPercentage(0.1);
BufferedImage previewImage = preview.draw();
byte[] previewBytes = ImageUtils.writeImageToBytes(previewImage, ImageUtils.IMAGE_FORMAT_PNG);
MediaRow mediaRow = mediaDao.newRow();
mediaRow.setData(previewBytes);
mediaRow.setContentType("image/png");
long mediaRowId = mediaDao.create(mediaRow);
UserMappingRow userMappingRow = userMappingDao.newRow();
userMappingRow.setBaseId(featureContentsId);
userMappingRow.setRelatedId(mediaRowId);
userMappingDao.create(userMappingRow);
}
}
use of mil.nga.geopackage.extension.related.UserMappingDao in project geopackage-java by ngageoint.
the class GeoPackageExample method insertRelatedTablesMediaExtensionRows.
private static void insertRelatedTablesMediaExtensionRows(GeoPackage geoPackage, ExtendedRelation relation, String query, String name, String file, String contentType, String description, String source) {
RelatedTablesExtension relatedTables = new RelatedTablesExtension(geoPackage);
FeatureDao featureDao = geoPackage.getFeatureDao(relation.getBaseTableName());
MediaDao mediaDao = relatedTables.getMediaDao(relation);
UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);
MediaRow mediaRow = mediaDao.newRow();
mediaRow.setData(TestUtils.getTestFileBytes(file));
mediaRow.setContentType(contentType);
RelatedTablesUtils.populateUserRow(mediaDao.getTable(), mediaRow, MediaTable.requiredColumns());
DublinCoreMetadata.setValue(mediaRow, DublinCoreType.TITLE, name);
DublinCoreMetadata.setValue(mediaRow, DublinCoreType.DESCRIPTION, description);
DublinCoreMetadata.setValue(mediaRow, DublinCoreType.SOURCE, source);
long mediaRowId = mediaDao.create(mediaRow);
FeatureResultSet featureResultSet = featureDao.queryForLike(TEXT_COLUMN, query);
for (FeatureRow featureRow : featureResultSet) {
UserMappingRow userMappingRow = userMappingDao.newRow();
userMappingRow.setBaseId(featureRow.getId());
userMappingRow.setRelatedId(mediaRowId);
RelatedTablesUtils.populateUserRow(userMappingDao.getTable(), userMappingRow, UserMappingTable.requiredColumns());
String featureName = featureRow.getValue(TEXT_COLUMN).toString();
DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.TITLE, featureName + " - " + name);
DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.DESCRIPTION, featureName + " - " + description);
DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.SOURCE, source);
userMappingDao.create(userMappingRow);
}
featureResultSet.close();
}
use of mil.nga.geopackage.extension.related.UserMappingDao in project geopackage-java by ngageoint.
the class GeoPackageExample method insertRelatedTablesFeaturesExtensionRows.
private static void insertRelatedTablesFeaturesExtensionRows(GeoPackage geoPackage, ExtendedRelation relation) {
RelatedTablesExtension relatedTables = new RelatedTablesExtension(geoPackage);
UserMappingDao userMappingDao = relatedTables.getMappingDao(relation);
FeatureDao featureDao1 = geoPackage.getFeatureDao(relation.getBaseTableName());
FeatureDao featureDao2 = geoPackage.getFeatureDao(relation.getRelatedTableName());
FeatureResultSet featureResultSet1 = featureDao1.queryForAll();
for (FeatureRow featureRow1 : featureResultSet1) {
String featureName = featureRow1.getValue(TEXT_COLUMN).toString();
FeatureResultSet featureResultSet2 = featureDao2.queryForEq(TEXT_COLUMN, featureName);
for (FeatureRow featureRow2 : featureResultSet2) {
UserMappingRow userMappingRow = userMappingDao.newRow();
userMappingRow.setBaseId(featureRow1.getId());
userMappingRow.setRelatedId(featureRow2.getId());
RelatedTablesUtils.populateUserRow(userMappingDao.getTable(), userMappingRow, UserMappingTable.requiredColumns());
DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.TITLE, featureName);
DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.DESCRIPTION, featureName);
DublinCoreMetadata.setValue(userMappingRow, DublinCoreType.SOURCE, featureName);
userMappingDao.create(userMappingRow);
}
featureResultSet2.close();
}
featureResultSet1.close();
}
Aggregations