package org.treesched.ant;

import java.util.Date;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DynamicConfigurator;
import org.treesched.Interval;
import org.treesched.Schedule;
import org.treesched.ScheduleFactory;
import org.treesched.ScheduleList;

/**
 * The class is the represents the top element of a schedule
 * in an Ant build file.
 */
public class TopScheduleElement implements DynamicConfigurator, Schedule {

    /** The list of schedules that make up this schedule element. */
	private ScheduleList childSchedules = new ScheduleList();
	
	/*
	 *  (non-Javadoc)
	 * @see org.treesched.Schedule#setLimits(org.treesched.Interval)
	 */
	public void setLimits(Interval limits) {
	   childSchedules.setLimits(limits); 
	}
	
	/*
	 *  (non-Javadoc)
	 * @see org.treesched.Schedule#nextDue(java.util.Date)
	 */
	public Interval nextDue(Date now) {
		return childSchedules.nextDue(now);
	}

	/*
	 *  (non-Javadoc)
	 * @see org.treesched.Schedule#reset()
	 */
	public void reset() {
		childSchedules.reset();
	}
	
	/* (non-Javadoc)
	 * @see org.oddjob.arooa.DynamicConfigurator#setDynamicAttribute(java.lang.String, java.lang.Object)
	 */
	public void setDynamicAttribute(String name, String value)
		throws BuildException {
			throw new BuildException("No such attribute " + name);
	}

	/* (non-Javadoc)
	 * @see org.oddjob.arooa.DynamicConfigurator#createDynamicElement(java.lang.String)
	 */
	public Object createDynamicElement(String name) throws BuildException {

		ScheduleFactory factory = ScheduleFactory.getInstance();
		Schedule newChild = factory.createSchedule(name);
		childSchedules.addChildSchedule(newChild);
		NestedScheduleElement se = new NestedScheduleElement(newChild);
		return se;
	}

}
