package com.jware.apps.scheduler.web.struts.schedule;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;

import com.jware.apps.scheduler.model.JobParameterValue;
import com.jware.apps.scheduler.util.JobSchedulerUtils;
import com.jware.base.web.struts.jWareBaseAction;

/**
 * @author Jrmt
*/
public final class JobTriggerList extends jWareBaseAction {

    /**
     * Commons Logging instance.
    */
    private Log log = LogFactory.getLog(this.getClass().getName());

	/** 
	 * Process the specified HTTP request, and create the corresponding HTTP
	 * response (or forward to another web component that will create it).
	 * Return an <code>ActionForward</code> instance describing where and how
	 * control should be forwarded, or <code>null</code> if the response has
	 * already been completed.
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param actionForm The optional ActionForm bean for this request (if any)
	 * @param request The HTTP request we are processing
	 * @param response The HTTP response we are creating
	 * @param errors The ActionErrors created in BaseAction
	 * @param mochFlag The flag using Mocha
	 *
	 * @exception Exception if an input/output error or servlet exception occurs
	 */
	public ActionForward doExecute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response,
				 ActionErrors errors,
				 boolean mochaFlag)
	throws Exception {

		// Extract attributes we will need
		HttpSession session = request.getSession();
		Locale locale = getLocale(request);
		
		JobScheduleViewForm info = (JobScheduleViewForm)form;
		
		MessageResources messages = getResources(request);
		
		if (log.isDebugEnabled()) {
			log.debug("JobTriggerList:  Processing actions");
		}
	
		if( info == null ){
			info = new JobScheduleViewForm();
		}

		Scheduler scheduler = JobSchedulerUtils.getCurrentScheduler(session);
		String[] triggerGroups = scheduler.getTriggerGroupNames();
		
		List triggerList = new LinkedList();
		for (int i = 0; i < triggerGroups.length; i++) {
			String groupName = triggerGroups[i];
			String[] triggerNames = scheduler.getTriggerNames(groupName);
			for (int j = 0; j < triggerNames.length; j++) {
				String triggerName = triggerNames[j];
				Trigger trigger = scheduler.getTrigger(triggerName, groupName);
	            JobTriggerForm tForm = new JobTriggerForm();
	            tForm.setDescription(trigger.getDescription());
	            tForm.setJobGroup(trigger.getJobGroup());
	            tForm.setJobName(trigger.getJobName());
	            tForm.setMisFireInstruction(trigger.getMisfireInstruction());
	            tForm.setStartTime(JobSchedulerUtils.getDateAsString(trigger.getStartTime()));
	            tForm.setStopTime(JobSchedulerUtils.getDateAsString(trigger.getEndTime()));
	            tForm.setTriggerGroup(trigger.getGroup());
	            tForm.setTriggerName(trigger.getName());
	            tForm.setNextFireTime(JobSchedulerUtils.getDateAsString(trigger
	                    .getNextFireTime()));
	            tForm.setPreviousFireTime(JobSchedulerUtils.getDateAsString(trigger
	                    .getPreviousFireTime()));
	            tForm.setType(JobSchedulerUtils.getTriggerType(trigger));
	            tForm.getParamMap().put("triggerName", trigger.getName());
	            tForm.getParamMap().put("groupName", trigger.getGroup());

	            tForm.getJobParamMap().put("jobName", trigger.getJobName());
	            tForm.getJobParamMap().put("groupName", trigger.getJobGroup());

	            triggerList.add( tForm );
			}
        }

        request.setAttribute( "jobTriggerList", triggerList );
		
		return mapping.findForward("success");
		
    }
	
}

