package org.wyona.yanel.impl.scheduler;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

import java.util.Date;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 */
public class QuartzSchedulerUtil {

    private static Logger log = Logger.getLogger(QuartzSchedulerUtil.class);

    /**
     * Schedule jobs based on XML configuration
     * @param scheduler Scheduler
     * @param doc XML document containing jobs configuration
     * @param groupName Group name, e.g. realm ID
     */
    public static void schedule(Scheduler scheduler, Document doc, String groupName) throws Exception {
        log.info("Add jobs for group '" + groupName + "' to scheduler.");

        Element jobsElement = (Element) doc.getDocumentElement().getElementsByTagName("jobs").item(0);
        String enabled = jobsElement.getAttribute("enabled");
        if (enabled != null && enabled.equals("false")) {
            log.warn("Jobs of group/realm '" + groupName + "' have been disabled.");
            return;
        }

        NodeList jobElements = doc.getDocumentElement().getElementsByTagName("job");
   
        for (int i = 0; i < jobElements.getLength(); i++) {
            Element jobE = (Element) jobElements.item(i);
            log.info("Add job with class: " + jobE.getAttribute("class"));
            String jobName = jobE.getAttribute("name");
            JobDetail jobDetail = new JobDetail(jobName, groupName, Class.forName(jobE.getAttribute("class")));

            Element triggerElement = (Element) jobE.getElementsByTagName("trigger").item(0);

            Date startDate = new Date();
            String startDateA = triggerElement.getAttribute("startDate");
            if (startDateA != null && startDateA.length() > 0) {
                try {
                    startDate = new java.text.SimpleDateFormat("yyyy.MM.dd'T'HH:mm:ss").parse(startDateA);
                } catch(java.text.ParseException e) {
                    log.error("Could not parse startDate: " + e.getMessage());
                }
            }

            Date endDate = null;
            String endDateA = triggerElement.getAttribute("endDate");
            if (endDateA != null && endDateA.length() > 0) {
                try {
                    endDate = new java.text.SimpleDateFormat("yyyy.MM.dd'T'HH:mm:ss").parse(endDateA);
                } catch(java.text.ParseException e) {
                    log.error("Could not parse endDate: " + e.getMessage());
                }
            }

            // TODO: Implement repeat count and interval
            Trigger trigger = new SimpleTrigger(jobName + "Trigger", groupName, startDate, endDate, SimpleTrigger.REPEAT_INDEFINITELY, 60L * 1000L);
            scheduler.scheduleJob(jobDetail, trigger);
        }
    }
}

@@ -1866,32 +1866,153 @@
 length() %3E 0) %7B%0A
+                if (startDateA.equals(%22NOW%22)) %7B%0A                    startDate = new Date();%0A                %7D else %7B%0A    
                 
@@ -2009,32 +2009,36 @@
           try %7B%0A
+    
                 
@@ -2136,32 +2136,36 @@
                 
+  
+  
 %7D catch(java.tex
@@ -2178,32 +2178,36 @@
 eException e) %7B%0A
+    
                 
@@ -2258,34 +2258,85 @@
 + e.getMessage()
-);
+ + %22 (Use NOW as start date)%22);%0A                    %7D
 %0A               
@@ -2688,32 +2688,32 @@
 eException e) %7B%0A
-
                 
@@ -2770,16 +2770,39 @@
 essage()
+ + %22 (No end date set)%22
 );%0A     

check if start date it set to NOW