/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2011 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * 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; either
 * version 2.1 of the License, or any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.ow2.jonas.antmodular.jonasbase.bootstrap;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.ow2.jonas.antmodular.bootstrap.BootstrapTask;
import org.ow2.jonas.antmodular.bootstrap.JOnASAntTool;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

/**
 * Abstract jonas base task
 */
public abstract class AbstractJOnASBaseAntTask extends Task {

    /**
     * Source directory (JOnAS root).
     */
    protected File jonasRoot = null;

    /**
     * Destination directory (Where create the jonasBase).
     */
    protected File destDir = null;

    /**
     * List of tasks to execute
     */
    protected List tasks;

    /**
     * Configuration directory of JOnAS
     */
    protected static final String CONF_DIR = "/conf";

    /**
     * Default constructor
     */
    protected AbstractJOnASBaseAntTask() {
        this.tasks = new ArrayList();
    }

    /**
     * Run this task.
     * @see org.apache.tools.ant.Task#execute()
     */
    @Override
    public void execute() {

        Hashtable<String,String> properties = getProject().getProperties();
        Iterator it = properties.keySet().iterator();

        this.jonasRoot = new File(getProject().getProperty("jonas.root"));
        this.destDir = new File(getProject().getProperty("jonas.base"));

        if (this.jonasRoot == null) {
            throw new BuildException("jonasRoot element is not set");
        }

        if (this.destDir == null) {
            throw new BuildException("destDir element is not set");
        }

        if (this.jonasRoot.getPath().equals(this.destDir.getPath())) {
            throw new BuildException("jonasRoot and destDir is the same path !");
        }


    }

    /**
     * Execute all tasks
     */
    public void executeAllTask() {

        Iterator it = this.tasks.iterator();
        while (it.hasNext()) {
            Object o = it.next();

            if (o instanceof BaseTaskItf) {
                BaseTaskItf task = (BaseTaskItf) o;
                task.setDestDir(this.destDir);
                task.setJonasRoot(this.jonasRoot);
                if (task instanceof BootstrapTask) {
                    ((BootstrapTask) task).setJonasBase(this.destDir);
                }
                JOnASAntTool.configure(this, (Task) task);
                String info = task.getLogInfo();
                if (info != null) {
                    log(info, Project.MSG_INFO);
                }
                task.execute();
            } else {
                Task task = (Task) o;
                JOnASAntTool.configure(this, task);
                task.execute();
            }
        }
    }


    /**
     * Add tasks for configured object.
     * @param subTasks some tasks to do on files
     */
    public void addTasks(final Tasks subTasks) {
        if (subTasks != null) {
            for (Iterator it = subTasks.getTasks().iterator(); it.hasNext();) {
                tasks.add(it.next());
            }
        }
    }

    /**
     * Add a task for configure some objects.
     * @param task the task to do
     */
    public void addTask(final BaseTaskItf task) {
        if (task != null) {
            tasks.add(task);
        }
    }

    /**
     * Return Jms tasks
     * @return all tasks
     */
    public List getTasks() {
        return tasks;
    }

    /**
     * Create a JReplace Task for changing service classname in jonas.properties.
     *
     * @param serviceName service classname to use.
     * @return Returns a JReplace Task.
     */
    protected JTask createServiceNameReplace(final String serviceName, final String info, final String confDir, String property) {
        // Replace the service in jonas.properties file
        JTask jtask = new JTask();
        jtask.setDestDir(destDir);
        jtask.changeValueForKey(info, confDir, JOnASBaseTask.JONAS_CONF_FILE, property, serviceName, false);
        jtask.setLogInfo(info + "Setting service to : " + serviceName);
        return jtask;
    }

    /**
     * @param destDir Destination directory to set
     */
    public void setDestDir(File destDir) {
        this.destDir = destDir;
    }
}
