package mil.nga.geopackage.features.user;

import android.content.ContentValues;

import java.io.IOException;

import mil.nga.geopackage.GeoPackageException;
import mil.nga.geopackage.geom.GeoPackageGeometryData;
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.3.3
     */
    public FeatureRow(FeatureRow featureRow) {
        super(featureRow);
    }

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

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

    /**
     * Get the geometry
     *
     * @return
     */
    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}
     * <p>
     * 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);
        }

    }

}

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

handle geometry data copy in feature rows