package mil.nga.geopackage.features.user;

import java.io.IOException;

import mil.nga.geopackage.GeoPackageException;
import mil.nga.geopackage.geom.GeoPackageGeometryData;
import mil.nga.geopackage.user.ContentValues;
import mil.nga.geopackage.user.UserRow;

/**
 * Feature Row containing the values from a single cursor row
 * 
 * @author osbornb
 */
public class FeatureRow extends UserRow<FeatureColumn, FeatureTable> {

	/**
	 * Constructor
	 * 
	 * @param table
	 * @param columnTypes
	 * @param values
	 */
	FeatureRow(FeatureTable table, int[] columnTypes, Object[] values) {
		super(table, columnTypes, values);
	}

	/**
	 * Constructor to create an empty row
	 * 
	 * @param table
	 */
	FeatureRow(FeatureTable table) {
		super(table);
	}

	/**
	 * Copy Constructor
	 * 
	 * @param featureRow
	 *            feature row to copy
	 * @since 1.2.3
	 */
	public FeatureRow(FeatureRow featureRow) {
		super(featureRow);
	}

	/**
	 * Get the geometry column index
	 * 
	 * @return geometry column index
	 */
	public int getGeometryColumnIndex() {
		return getTable().getGeometryColumnIndex();
	}

	/**
	 * Get the geometry feature column
	 * 
	 * @return geometry feature column
	 */
	public FeatureColumn getGeometryColumn() {
		return getTable().getGeometryColumn();
	}

	/**
	 * Get the geometry
	 * 
	 * @return geometry data
	 */
	public GeoPackageGeometryData getGeometry() {
		GeoPackageGeometryData geometryData = null;
		Object value = getValue(getGeometryColumnIndex());
		if (value != null) {
			geometryData = (GeoPackageGeometryData) value;
		}
		return geometryData;
	}

	/**
	 * Set the geometry data
	 * 
	 * @param geometryData
	 */
	public void setGeometry(GeoPackageGeometryData geometryData) {
		setValue(getGeometryColumnIndex(), geometryData);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Handles geometry columns
	 */
	@Override
	protected void columnToContentValue(ContentValues contentValues,
			FeatureColumn column, Object value) {

		if (column.isGeometry()) {

			String columnName = column.getName();

			if (value instanceof GeoPackageGeometryData) {
				GeoPackageGeometryData geometryData = (GeoPackageGeometryData) value;
				try {
					contentValues.put(columnName, geometryData.toBytes());
				} catch (IOException e) {
					throw new GeoPackageException(
							"Failed to write Geometry Data bytes. column: "
									+ columnName, e);
				}
			} else if (value instanceof byte[]) {
				contentValues.put(columnName, (byte[]) value);
			} else {
				throw new GeoPackageException(
						"Unsupported update geometry column value type. column: "
								+ columnName + ", value type: "
								+ value.getClass().getName());
			}
		} else {
			super.columnToContentValue(contentValues, column, value);
		}

	}

}

@@ -63,16 +63,41 @@
 ception;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -1789,24 +1789,746 @@
 yData);%0A%09%7D%0A%0A
+%09/**%0A%09 * %7B@inheritDoc%7D%0A%09 * %0A%09 * Handles geometry columns%0A%09 */%0A%09@Override%0A%09protected Object copyValue(FeatureColumn column, Object value) %7B%0A%0A%09%09Object copyValue = null;%0A%0A%09%09if (column.isGeometry() && value instanceof GeoPackageGeometryData) %7B%0A%0A%09%09%09GeoPackageGeometryData geometryData = (GeoPackageGeometryData) value;%0A%09%09%09try %7B%0A%09%09%09%09byte%5B%5D bytes = geometryData.toBytes();%0A%09%09%09%09byte%5B%5D copyBytes = Arrays.copyOf(bytes, bytes.length);%0A%09%09%09%09copyValue = new GeoPackageGeometryData(copyBytes);%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09throw new GeoPackageException(%0A%09%09%09%09%09%09%22Failed to copy Geometry Data bytes. column: %22%0A%09%09%09%09%09%09%09%09+ column.getName(), e);%0A%09%09%09%7D%0A%0A%09%09%7D else %7B%0A%09%09%09copyValue = super.copyValue(column, value);%0A%09%09%7D%0A%0A%09%09return copyValue;%0A%09%7D%0A%0A
 %09/**%0A%09 * %7B@i

handle geometry data copy in feature rows