Java Code Examples for mil.nga.geopackage.metadata.reference.MetadataReferenceDao

The following examples show how to use mil.nga.geopackage.metadata.reference.MetadataReferenceDao. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: geopackage-core-java   Source File: GeoPackageExtensions.java    License: MIT License 6 votes vote down vote up
/**
 * Delete the Metadata extensions for the table
 * 
 * @param geoPackage
 *            GeoPackage
 * @param table
 *            table name
 * @since 3.2.0
 */
public static void deleteMetadata(GeoPackageCore geoPackage, String table) {

	MetadataReferenceDao metadataReferenceDao = geoPackage
			.getMetadataReferenceDao();
	try {
		if (metadataReferenceDao.isTableExists()) {
			metadataReferenceDao.deleteByTableName(table);
		}
	} catch (SQLException e) {
		throw new GeoPackageException(
				"Failed to delete Metadata extension. GeoPackage: "
						+ geoPackage.getName() + ", Table: " + table,
				e);
	}

}
 
Example 2
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean createMetadataReferenceTable() {
	verifyWritable();

	boolean created = false;
	MetadataReferenceDao dao = getMetadataReferenceDao();
	try {
		if (!dao.isTableExists()) {
			created = tableCreator.createMetadataReference() > 0;
		}
	} catch (SQLException e) {
		throw new GeoPackageException("Failed to check if "
				+ MetadataReference.class.getSimpleName()
				+ " table exists and create it", e);
	}
	return created;
}
 
Example 3
Source Project: geopackage-core-java   Source File: MetadataDao.java    License: MIT License 6 votes vote down vote up
/**
 * Delete the Metadata, cascading
 * 
 * @param metadata
 *            metadata
 * @return deleted count
 * @throws SQLException
 *             upon failure
 */
public int deleteCascade(Metadata metadata) throws SQLException {
	int count = 0;

	if (metadata != null) {

		// Delete Metadata References and remove parent references
		MetadataReferenceDao dao = getMetadataReferenceDao();
		dao.deleteByMetadata(metadata.getId());
		dao.removeMetadataParent(metadata.getId());

		// Delete
		count = delete(metadata);
	}
	return count;
}
 
Example 4
Source Project: geopackage-core-java   Source File: MetadataDao.java    License: MIT License 5 votes vote down vote up
/**
 * Get or create a Metadata Reference DAO
 * 
 * @return metadata reference dao
 * @throws SQLException
 */
private MetadataReferenceDao getMetadataReferenceDao() throws SQLException {
	if (metadataReferenceDao == null) {
		metadataReferenceDao = DaoManager.createDao(connectionSource,
				MetadataReference.class);
	}
	return metadataReferenceDao;
}
 
Example 5
Source Project: geopackage-android   Source File: MetadataReferenceUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Test create
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testCreate(GeoPackage geoPackage) throws SQLException {

	MetadataReferenceDao dao = geoPackage.getMetadataReferenceDao();
	MetadataDao metadataDao = geoPackage.getMetadataDao();

	if (dao.isTableExists()) {
		// Get current count
		long count = dao.countOf();

		long fileId = 5555;
		long parentId = 5556;

		// Create new metadata
		Metadata metadata = new Metadata();
		metadata.setId(fileId);
		metadata.setMetadataScope(MetadataScopeType.FEATURE);
		metadata.setStandardUri("https://www.nga.mil");
		metadata.setMimeType("text/xml");
		metadata.setMetadata("Create metadata text");
		metadataDao.create(metadata);

		Metadata metadata2 = new Metadata();
		metadata2.setId(parentId);
		metadata2.setMetadataScope(MetadataScopeType.FEATURE_TYPE);
		metadata2.setStandardUri("https://www.nga.mil");
		metadata2.setMimeType("text/xml");
		metadata2.setMetadata("Create metadata text 2");
		metadataDao.create(metadata2);

		ReferenceScopeType scopeType = ReferenceScopeType.ROW;
		String tableName = "CREATE_TABLE_NAME";
		long rowIdValue = 50;
		Date timestamp = new Date();

		MetadataReference reference = new MetadataReference();
		reference.setReferenceScope(scopeType);
		reference.setTableName(tableName);
		reference.setRowIdValue(rowIdValue);
		reference.setTimestamp(timestamp);
		reference.setMetadata(metadata);
		reference.setParentMetadata(metadata2);
		dao.create(reference);

		// Verify count
		long newCount = dao.countOf();
		TestCase.assertEquals(count + 1, newCount);

		// Verify saved metadata
		List<MetadataReference> queryMetadataReferenceList = dao
				.queryByMetadata(fileId, parentId);
		TestCase.assertNotNull(queryMetadataReferenceList);
		TestCase.assertEquals(1, queryMetadataReferenceList.size());
		MetadataReference queryMetadataReference = queryMetadataReferenceList
				.get(0);
		TestCase.assertEquals(scopeType,
				queryMetadataReference.getReferenceScope());
		TestCase.assertEquals(tableName,
				queryMetadataReference.getTableName());
		TestCase.assertNull(queryMetadataReference.getColumnName());
		TestCase.assertEquals(rowIdValue, queryMetadataReference
				.getRowIdValue().longValue());
		TestCase.assertEquals(timestamp,
				queryMetadataReference.getTimestamp());
		TestCase.assertEquals(fileId, queryMetadataReference.getFileId());
		TestCase.assertEquals(parentId, queryMetadataReference
				.getParentId().longValue());
		TestCase.assertNotNull(queryMetadataReference.getMetadata());
		TestCase.assertNotNull(queryMetadataReference.getParentMetadata());
	}

}
 
Example 6
Source Project: geopackage-android   Source File: MetadataReferenceUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

	MetadataReferenceDao dao = geoPackage.getMetadataReferenceDao();

	if (dao.isTableExists()) {
		List<MetadataReference> results = dao.queryForAll();

		if (!results.isEmpty()) {

			// Choose random metadata
			int random = (int) (Math.random() * results.size());
			MetadataReference metadataReference = results.get(random);

			// Delete the metadata reference
			dao.delete(metadataReference);

			// Verify deleted
			List<MetadataReference> queryMetadataReferenceList = dao
					.queryByMetadata(metadataReference.getFileId(),
							metadataReference.getParentId());
			TestCase.assertTrue(queryMetadataReferenceList.isEmpty());

			// Choose prepared deleted
			results = dao.queryForAll();
			if (!results.isEmpty()) {

				// Choose random metadata
				random = (int) (Math.random() * results.size());
				metadataReference = results.get(random);

				// Find which metadata to delete
				QueryBuilder<MetadataReference, Void> qb = dao
						.queryBuilder();
				qb.where().eq(MetadataReference.COLUMN_FILE_ID,
						metadataReference.getFileId());
				PreparedQuery<MetadataReference> query = qb.prepare();
				List<MetadataReference> queryResults = dao.query(query);
				int count = queryResults.size();

				// Delete
				DeleteBuilder<MetadataReference, Void> db = dao
						.deleteBuilder();
				db.where().eq(MetadataReference.COLUMN_FILE_ID,
						metadataReference.getFileId());
				PreparedDelete<MetadataReference> deleteQuery = db
						.prepare();
				int deleted = dao.delete(deleteQuery);

				TestCase.assertEquals(count, deleted);

			}
		}
	}
}
 
Example 7
Source Project: geopackage-core-java   Source File: GeoPackageCoreImpl.java    License: MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public MetadataReferenceDao getMetadataReferenceDao() {
	return createDao(MetadataReference.class);
}
 
Example 8
Source Project: geopackage-java   Source File: MetadataReferenceUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Test create
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testCreate(GeoPackage geoPackage) throws SQLException {

	MetadataReferenceDao dao = geoPackage.getMetadataReferenceDao();
	MetadataDao metadataDao = geoPackage.getMetadataDao();

	if (dao.isTableExists()) {
		// Get current count
		long count = dao.countOf();

		long fileId = 5555;
		long parentId = 5556;

		// Create new metadata
		Metadata metadata = new Metadata();
		metadata.setId(fileId);
		metadata.setMetadataScope(MetadataScopeType.FEATURE);
		metadata.setStandardUri("https://www.nga.mil");
		metadata.setMimeType("text/xml");
		metadata.setMetadata("Create metadata text");
		metadataDao.create(metadata);

		Metadata metadata2 = new Metadata();
		metadata2.setId(parentId);
		metadata2.setMetadataScope(MetadataScopeType.FEATURE_TYPE);
		metadata2.setStandardUri("https://www.nga.mil");
		metadata2.setMimeType("text/xml");
		metadata2.setMetadata("Create metadata text 2");
		metadataDao.create(metadata2);

		ReferenceScopeType scopeType = ReferenceScopeType.ROW;
		String tableName = "CREATE_TABLE_NAME";
		long rowIdValue = 50;
		Date timestamp = new Date();

		MetadataReference reference = new MetadataReference();
		reference.setReferenceScope(scopeType);
		reference.setTableName(tableName);
		reference.setRowIdValue(rowIdValue);
		reference.setTimestamp(timestamp);
		reference.setMetadata(metadata);
		reference.setParentMetadata(metadata2);
		dao.create(reference);

		// Verify count
		long newCount = dao.countOf();
		TestCase.assertEquals(count + 1, newCount);

		// Verify saved metadata
		List<MetadataReference> queryMetadataReferenceList = dao
				.queryByMetadata(fileId, parentId);
		TestCase.assertNotNull(queryMetadataReferenceList);
		TestCase.assertEquals(1, queryMetadataReferenceList.size());
		MetadataReference queryMetadataReference = queryMetadataReferenceList
				.get(0);
		TestCase.assertEquals(scopeType,
				queryMetadataReference.getReferenceScope());
		TestCase.assertEquals(tableName,
				queryMetadataReference.getTableName());
		TestCase.assertNull(queryMetadataReference.getColumnName());
		TestCase.assertEquals(rowIdValue, queryMetadataReference
				.getRowIdValue().longValue());
		TestCase.assertEquals(timestamp,
				queryMetadataReference.getTimestamp());
		TestCase.assertEquals(fileId, queryMetadataReference.getFileId());
		TestCase.assertEquals(parentId, queryMetadataReference
				.getParentId().longValue());
		TestCase.assertNotNull(queryMetadataReference.getMetadata());
		TestCase.assertNotNull(queryMetadataReference.getParentMetadata());
	}

}
 
Example 9
Source Project: geopackage-java   Source File: MetadataReferenceUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Test delete
 * 
 * @param geoPackage
 * @throws SQLException
 */
public static void testDelete(GeoPackage geoPackage) throws SQLException {

	MetadataReferenceDao dao = geoPackage.getMetadataReferenceDao();

	if (dao.isTableExists()) {
		List<MetadataReference> results = dao.queryForAll();

		if (!results.isEmpty()) {

			// Choose random metadata
			int random = (int) (Math.random() * results.size());
			MetadataReference metadataReference = results.get(random);

			// Delete the metadata reference
			dao.delete(metadataReference);

			// Verify deleted
			List<MetadataReference> queryMetadataReferenceList = dao
					.queryByMetadata(metadataReference.getFileId(),
							metadataReference.getParentId());
			TestCase.assertTrue(queryMetadataReferenceList.isEmpty());

			// Choose prepared deleted
			results = dao.queryForAll();
			if (!results.isEmpty()) {

				// Choose random metadata
				random = (int) (Math.random() * results.size());
				metadataReference = results.get(random);

				// Find which metadata to delete
				QueryBuilder<MetadataReference, Void> qb = dao
						.queryBuilder();
				qb.where().eq(MetadataReference.COLUMN_FILE_ID,
						metadataReference.getFileId());
				PreparedQuery<MetadataReference> query = qb.prepare();
				List<MetadataReference> queryResults = dao.query(query);
				int count = queryResults.size();

				// Delete
				DeleteBuilder<MetadataReference, Void> db = dao
						.deleteBuilder();
				db.where().eq(MetadataReference.COLUMN_FILE_ID,
						metadataReference.getFileId());
				PreparedDelete<MetadataReference> deleteQuery = db
						.prepare();
				int deleted = dao.delete(deleteQuery);

				TestCase.assertEquals(count, deleted);

			}
		}
	}
}
 
Example 10
Source Project: geopackage-core-java   Source File: GeoPackageCore.java    License: MIT License 2 votes vote down vote up
/**
 * Get a Metadata Reference DAO
 * 
 * @return Metadata Reference DAO
 */
public MetadataReferenceDao getMetadataReferenceDao();