/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jetspeed.maven.plugins.db.ddlutils.model;

import java.sql.DatabaseMetaData;

/**
 * ForeignKey extension to allow overriding ddlutils 1.0 behavior at runtime for 
 * https://issues.apache.org/jira/browse/DDLUTILS-75 which is fixed in ddlutils 1.1 but hasn't been released yet.
 * @version $Id: ForeignKey.java 1079016 2011-03-07 23:09:49Z ate $
 *
 */
public class ForeignKey extends org.apache.ddlutils.model.ForeignKey
{
    /** The DELETE_RULE type {@link java.sql.DatabaseMetaData#getImportedKeys} */
    private int _deleteRuleCode;
    /** The attribute for the onDelete clause in the schema XML */
    private String _onDelete;
	
	public ForeignKey()
	{
		super();
	}

	public ForeignKey(String name)
	{
		super(name);
        // If the _deleteRuleCode not set explicitly, default is no action
        _deleteRuleCode = DatabaseMetaData.importedKeyNoAction;
        _onDelete = "none";
	}

    /**
     * Sets the action to take on a delete of the referenced primary key. Maps
     * the code from DatabaseMetaData constant to the Torque database.dtd.
     * 
     * @param deleteRuleCode
     *            The action to take.
     * @see java.sql.DatabaseMetaData#getImportedKeys
     */
    public void setDeleteRuleCode(int deleteRuleCode)
    {
        _deleteRuleCode = deleteRuleCode;
        switch (_deleteRuleCode)
        {
            case DatabaseMetaData.importedKeyCascade:
                _onDelete = "cascade";
                break;
            case DatabaseMetaData.importedKeyRestrict:
                _onDelete = "restrict";
                break;
            case DatabaseMetaData.importedKeySetNull:
                _onDelete = "setnull";
                break;
            case DatabaseMetaData.importedKeyNoAction: // fall-through
            default:
                _onDelete = "none";
        }
    }
    

    /**
     * The action to take when a delete occurs of the parent key in a this
     * foreign key. Constant form
     * 
     * @return The delete rule action to take.
     * @see java.sql.DatabaseMetaData#getImportedKeys
     */
    public int getDeleteRuleCode()
    {
        return _deleteRuleCode;
    }

    /**
     * The action to take when a delete occurs of the parent key in a this
     * foreign key.
     * 
     * @return The value for the "onDelete" attribute of the <foreign-key> tag.
     */
    public String getOnDelete()
    {
        return _onDelete;
    }

    /**
     * Set the action to take when a delete occurs of the parent key in a this
     * foreign key.
     * 
     * @param onDelete
     *            The value for the "onDelete" attribute of the <foreign-key>
     *            tag.
     */
    public void setOnDelete(String onDelete)
    {
        _onDelete = onDelete;
        if (onDelete.equals("none"))
        {
            _deleteRuleCode = DatabaseMetaData.importedKeyNoAction;
        }
        else if (onDelete.equals("cascade"))
        {
            _deleteRuleCode = DatabaseMetaData.importedKeyCascade;
        }
        else if (onDelete.equals("restrict"))
        {
            _deleteRuleCode = DatabaseMetaData.importedKeyRestrict;
        }
        else if (onDelete.equals("setnull"))
        {
            _deleteRuleCode = DatabaseMetaData.importedKeySetNull;
        }
    }
}
