/*
 * 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.
 *
 * $Header:$
 */

package org.apache.beehive.controls.system.jdbc.parser;

import org.apache.beehive.controls.api.context.ControlBeanContext;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * The abstract base class for fragment's which may contain child fragments.
 */
public abstract class SqlFragmentContainer extends SqlFragment {

    /** Child fragments of this container. */
    protected ArrayList<SqlFragment> _children;

    /**
     * Construct a new SqlFragmentContainer instance.
     */
    SqlFragmentContainer() {
        _children = new ArrayList<SqlFragment>();
    }

    /**
     * Does this fragment contain a parameter value for a prepared statement?
     * @return true If this fragment contains a parameter value for a PreparedStatement.
     */
    boolean hasParamValue() {
        for (SqlFragment f : _children) {
            if (f.hasParamValue()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Add a child.
     * @param child Child to add.
     */
    void addChild(SqlFragment child) {
        _children.add(child);
    }

    /**
     * Return the array of children.
     * @return An array of SqlFragments.
     */
    SqlFragment[] getChildren() {
        SqlFragment[] fragments = new SqlFragment[_children.size()];
        return _children.toArray(fragments);
    }

    /**
     * Must be implemented for JUnit testing.
     * @return The String value of this fragment and all of its child fragments.
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (SqlFragment f : _children) {
            s.append(f.toString());
        }
        return s.toString();
    }

    /**
     * builds the text of the prepared statement
     *
     * @param context A ControlBeanContext instance.
     * @param m The annotated method.
     * @param args The method's parameters.
     * @return The PreparedStatement text generated by this fragment and its children.
     */
    String getPreparedStatementText(ControlBeanContext context, Method m, Object[] args) {
        StringBuilder sb = new StringBuilder();
        for (SqlFragment sf : _children) {
            sb.append(sf.getPreparedStatementText(context, m, args));
        }
        return sb.toString();
    }
}
