package org.apache.torque.templates.transformer.om;

/*
 * 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.
 */

import java.util.ArrayList;
import java.util.List;

import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.templates.TorqueSchemaAttributeName;
import org.apache.torque.templates.TorqueSchemaElementName;

/**
 * Utility class containing methods to find certain elements
 * in the source tree.
 *
 * $Id: FindHelper.java 1331196 2012-04-27 02:56:12Z tfischer $
 */
final class FindHelper
{
    /**
     * Private constructor for utility class.
     */
    private FindHelper()
    {
    }

    /**
     * Finds a table in the given root element by name. The root element must
     * be a database element.
     *
     * @param root the database Source element, not null.
     * @param tableName the table name, not null.
     * @param searchExternalSchemata if the table should also be located
     *        in the external schemata.
     *
     * @return the column's source element, or null if no matching table
     *         exists.
     */
    public static SourceElement findTable(
            SourceElement root,
            String tableName,
            boolean searchExternalSchemata)
    {
        if (!TorqueSchemaElementName.DATABASE.getName().equals(
                root.getName()))
        {
            throw new IllegalArgumentException("Wrong source element "
                    + root.getName());
        }
        for (SourceElement table : root.getChildren(
                TorqueSchemaElementName.TABLE))
        {
            if (tableName.equals(table.getAttribute(
                    TorqueSchemaAttributeName.NAME)))
            {
                return table;
            }
        }
        if (searchExternalSchemata)
        {
            for (SourceElement externalSchema : root.getChildren(
                    TorqueSchemaElementName.EXTERNAL_SCHEMA))
            {
                SourceElement databaseElement
                    = externalSchema.getChild(TorqueSchemaElementName.DATABASE);
                SourceElement result = findTable(
                        databaseElement,
                        tableName,
                        searchExternalSchemata);
                if (result != null)
                {
                    return result;
                }
            }
        }
        return null;
    }

    /**
     * Finds a column in a table by name.
     *
     * @param table the table Source element, not null.
     * @param columnName the column name, not null.
     *
     * @return the column's source element, or null if no matching column
     *         exists.
     */
    public static SourceElement findColumn(
            SourceElement table,
            String columnName)
    {
        if (!TorqueSchemaElementName.TABLE.getName().equals(
                table.getName()))
        {
            throw new IllegalArgumentException("Wrong source element "
                    + table.getName());
        }
        for (SourceElement column : table.getChildren(
                TorqueSchemaElementName.COLUMN))
        {
            if (column.getAttribute(TorqueSchemaAttributeName.NAME)
                    .equals(columnName))
            {
                return column;
            }
        }
        return null;
    }

    /**
     * Finds all foreign key in one table that reference a certain other table.
     *
     * @param table the table containing the foreign keys to search, not null.
     * @param tableName the name of the foreign table in the foreign Key.
     *
     * @return the foreign keys referencing the given table name.
     */
    public static List<SourceElement> findForeignKeyByReferencedTable(
            SourceElement table,
            String tableName)
    {
        List<SourceElement> result = new ArrayList<SourceElement>();
        if (!TorqueSchemaElementName.TABLE.getName().equals(
                table.getName()))
        {
            throw new IllegalArgumentException("Wrong source element "
                    + table.getName());
        }
        for (SourceElement foreignKey : table.getChildren(
                TorqueSchemaElementName.FOREIGN_KEY))
        {
            if (foreignKey.getAttribute(
                        TorqueSchemaAttributeName.FOREIGN_TABLE)
                    .equals(tableName))
            {
                result.add(foreignKey);
            }
        }
        return result;
    }
}
