diff --git a/src/main/java/mil/nga/geopackage/extension/schema/columns/DataColumnsDao.java b/src/main/java/mil/nga/geopackage/extension/schema/columns/DataColumnsDao.java index c5a1fbc5..2aee66e6 100644 --- a/src/main/java/mil/nga/geopackage/extension/schema/columns/DataColumnsDao.java +++ b/src/main/java/mil/nga/geopackage/extension/schema/columns/DataColumnsDao.java @@ -13,9 +13,13 @@ import com.j256.ormlite.support.ConnectionSource; import mil.nga.geopackage.GeoPackageCore; +import mil.nga.geopackage.contents.Contents; import mil.nga.geopackage.db.GeoPackageCoreConnection; import mil.nga.geopackage.db.GeoPackageDao; import mil.nga.geopackage.db.TableColumnKey; +import mil.nga.geopackage.user.UserColumn; +import mil.nga.geopackage.user.UserColumns; +import mil.nga.geopackage.user.UserTable; /** * Data Columns Data Access Object @@ -265,4 +269,194 @@ public int deleteByTableName(String tableName) throws SQLException { return deleted; } + /** + * Save the column titles as data columns + * + * @param table + * user table + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void saveColumnTitles(UserTable table) + throws SQLException { + saveColumnTitles(table.getContents(), table.getUserColumns()); + } + + /** + * Save the column titles as data columns + * + * @param contents + * user table contents + * @param columns + * user columns + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void saveColumnTitles(Contents contents, + UserColumns columns) throws SQLException { + saveColumnTitles(contents, columns.getColumns()); + } + + /** + * Save the column titles as data columns + * + * @param contents + * user table contents + * @param columns + * user columns + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void saveColumnTitles(Contents contents, + List columns) throws SQLException { + + for (UserColumn column : columns) { + + saveColumnTitle(contents, column); + + } + + } + + /** + * Save the column title as a data column + * + * @param contents + * user table contents + * @param column + * user column + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void saveColumnTitle(Contents contents, UserColumn column) + throws SQLException { + + String table = contents.getTableName(); + String name = column.getName(); + String title = column.getTitle(); + + DataColumns dataColumns = getDataColumn(table, name); + if (dataColumns != null) { + dataColumns.setName(title); + dataColumns.setTitle(title); + update(dataColumns); + } else if (title != null) { + dataColumns = new DataColumns(); + dataColumns.setContents(contents); + dataColumns.setColumnName(name); + dataColumns.setName(title); + dataColumns.setTitle(title); + create(dataColumns); + } + + } + + /** + * Load the column titles from data columns + * + * @param table + * user table + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void loadColumnTitles(UserTable table) + throws SQLException { + loadColumnTitles(table.getUserColumns()); + } + + /** + * Load the column titles from data columns + * + * @param columns + * user columns + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void loadColumnTitles(UserColumns columns) + throws SQLException { + loadColumnTitles(columns.getTableName(), columns.getColumns()); + } + + /** + * Load the column titles from data columns + * + * @param table + * table name + * @param columns + * user columns + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void loadColumnTitles(String table, + List columns) throws SQLException { + + if (isTableExists()) { + + for (UserColumn column : columns) { + + loadColumnTitle(table, column); + + } + + } + + } + + /** + * Load the column title from a data column + * + * @param table + * table name + * @param column + * user column + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public void loadColumnTitle(String table, UserColumn column) + throws SQLException { + + column.setTitle(getColumnTitle(table, column.getName())); + + } + + /** + * Get the column title from a data column + * + * @param table + * table name + * @param column + * column name + * @return column title or null + * @throws SQLException + * upon failure + * @since 6.6.7 + */ + public String getColumnTitle(String table, String column) + throws SQLException { + + String title = null; + + if (isTableExists()) { + + DataColumns dataColumns = getDataColumn(table, column); + if (dataColumns != null) { + title = dataColumns.getName(); + if (title == null) { + title = dataColumns.getTitle(); + } + } + + } + + return title; + } + } diff --git a/src/main/java/mil/nga/geopackage/user/UserColumn.java b/src/main/java/mil/nga/geopackage/user/UserColumn.java index daf10c19..99a57a9e 100644 --- a/src/main/java/mil/nga/geopackage/user/UserColumn.java +++ b/src/main/java/mil/nga/geopackage/user/UserColumn.java @@ -14,6 +14,7 @@ import mil.nga.geopackage.db.table.Constraints; import mil.nga.geopackage.db.table.RawConstraint; import mil.nga.geopackage.db.table.TableColumn; +import mil.nga.geopackage.extension.schema.columns.DataColumnsDao; /** * Metadata about a single column from a user table @@ -115,6 +116,14 @@ public abstract class UserColumn implements Comparable { */ private final Constraints constraints; + /** + * Column title, not saved as part of the column, saved using + * {@link DataColumnsDao} + * + * @since 6.6.7 + */ + private String title; + /** * Constructor * @@ -220,6 +229,7 @@ protected UserColumn(UserColumn userColumn) { this.type = userColumn.type; this.dataType = userColumn.dataType; this.constraints = userColumn.constraints.copy(); + this.title = userColumn.title; } /** @@ -948,6 +958,29 @@ public String buildConstraintSql(Constraint constraint) { return sql; } + /** + * Get the column title. The title is not saved as part of the column but + * can be loaded using {@link DataColumnsDao}. + * + * @return column title or null + * @since 6.6.7 + */ + public String getTitle() { + return title; + } + + /** + * Set the column title. The title is not saved as part of the column but + * can be saved using {@link DataColumnsDao}. + * + * @param title + * column title + * @since 6.6.7 + */ + public void setTitle(String title) { + this.title = title; + } + /** * {@inheritDoc} *