/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2002-2006, GeoTools Project Managment Committee (PMC)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.data.jdbc;

import java.net.URLDecoder;
import java.sql.ResultSet;


/**
 * Primary key of a table.
 *
 * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
 *
 */
public class PrimaryKey {
    /**
     * The columns making up the primary key
     */
    public Column[] columns;

    /**
     * Creates a new primary key.
     */
    public PrimaryKey(Column[] columns) {
        this.columns = columns;
    }

    /**
     * Decodes a featureId into an array of objects which map to the columns
     * of the primary key.
     *
     * @param fid The featureId.
     *
     * @return An array of values which map the primary key columns making up
     * the featureId.
     *
     * @throws Exception
     */
    public Object[] decode(String fid) throws Exception {
        Object[] values = new Object[columns.length];
        String[] tokens = fid.split("&");

        if (tokens.length != columns.length) {
            throw new RuntimeException("fid: " + fid + " does not map to primary key with "
                + columns.length + " elements");
        }

        for (int i = 0; i < tokens.length; i++) {
            values[i] = URLDecoder.decode(tokens[i], "UTF-8");
        }

        return values;
    }

    /**
     * Encodes a table row into a featureId by obtaining the primary key values
     * from the row.
     *
     * @param rs A result set pointing to a paritcular table row.
     *
     * @return A featureid for the row.
     *
     * @throws Exception
     */
    public String encode(ResultSet rs) throws Exception {
        StringBuffer fid = new StringBuffer();

        for (int i = 0; i < columns.length; i++) {
            //TODO: run column[i].type through converter to string
            Object value = rs.getObject(columns[i].name);
            fid.append(value.toString());

            if (i < (columns.length - 1)) {
                fid.append("&");
            }
        }

        return fid.toString();
    }

    /**
     * A column in a primary key.
     *
     */
    public static class Column {
        /**
         * THe column name;
         */
        public String name;

        /**
         * The column type.
         */
        public Class type;

        /**
         * Flag indicating wether value for the columns is auto-generated by the
         * database
         */
        public boolean isAutoGenerated = false;

        /**
         * Name of a sequence which generates values for the column.
         */
        public String sequence = null;

        public Column(String name, Class type) {
            this.name = name;
            this.type = type;
        }
    }
}
