package org.apache.maven.hibernate.beans;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed 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.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.ant.DirectoryScanner;

/**
 * 
 * @author <a href="as851@columbia.edu">Alex Shneyderman</a>
 * @since  $Id$
 */
public class CommonOperationsBean {
	
    private String includes = null;
    private String excludes = null;
    private String basedir  = null;
    
    private static final Log LOG = LogFactory.getLog(CommonOperationsBean.class);    

    public String getBasedir()
    {
        return basedir;
    }

    public void setBasedir(String basedir)
    {
        this.basedir = basedir;
    }

    public String getExcludes()
    {
        return excludes;
    }

    public void setExcludes(String excludes)
    {
        this.excludes = excludes;
    }

    public String getIncludes()
    {
        return includes;
    }

    public void setIncludes(String includes)
    {
        this.includes = includes;
    }

    protected String[] getBaseDirNames()
    {
        LOG.debug("Bases string: " + getBasedir());
        StringTokenizer st = new StringTokenizer(getBasedir(), ",");
        String r[] = new String[st.countTokens()];
        for(int i = 0; st.hasMoreTokens(); i++)
            r[i] = st.nextToken();

        return r;
    }

    protected File[] getBaseDirs()
    {
        String bases[] = getBaseDirNames();
        List l = new ArrayList();
        for(int i = 0; i < bases.length; i++)
        {
            File t = new File(bases[i]);
            if(t.isDirectory())
            {
                l.add(t);
                LOG.info("  Adding base dir: " + t.getAbsolutePath());
            }
        }

        return (File[])l.toArray(new File[0]);
    }

    protected String[] getFileNames()
    {
        List files = new LinkedList();
        File dirs[] = getBaseDirs();
        for(int i = 0; i < dirs.length; i++)
        {
            DirectoryScanner directoryScanner = new DirectoryScanner();
            directoryScanner.setBasedir(dirs[i]);
            LOG.debug("Base dir:" + dirs[i].getAbsolutePath());
            LOG.debug("Excludes:" + getExcludes());
            LOG.debug("Includes:" + getIncludes());
            directoryScanner.setExcludes(StringUtils.split(getExcludes(), ","));
            directoryScanner.setIncludes(StringUtils.split(getIncludes(), ","));
            directoryScanner.scan();
            String includesFiles[] = directoryScanner.getIncludedFiles();
            for(int j = 0; j < includesFiles.length; j++)
            {
                File file = new File(includesFiles[j]);
                if(!file.isFile())
                    file = new File(directoryScanner.getBasedir(), includesFiles[j]);
                files.add(file.getAbsolutePath());
                LOG.debug("work with file:" + file.getAbsolutePath());
            }

        }

        return (String[])files.toArray(new String[0]);
    }

    protected File[] getFileDescriptors()
    {
        String names[] = getFileNames();
        List files = new ArrayList();
        for(int i = 0; i < names.length; i++)
        {
            File t = new File(names[i]);
            if(t.isFile())
                files.add(t);
        }

        return (File[])files.toArray(new File[0]);
    }



	
}
