<commit_before>/*
 * $Log: SchedulerAdapter.java,v $
 * Revision 1.5  2008-08-27 16:22:06  europe\L190409
 * fixed scheduler client
 *
 * Revision 1.4  2007/02/12 14:08:01  <NAME> <<EMAIL>>
 * Logger from LogUtil
 *
 */
package nl.nn.adapterframework.scheduler;

import java.util.Date;

import nl.nn.adapterframework.util.DateUtils;
import nl.nn.adapterframework.util.LogUtil;
import nl.nn.adapterframework.util.XmlBuilder;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
/**
 * The SchedulerAdapter is an adapter for the <a href="http://quartz.sourceforge.net">Quartz scheduler</a> <br/>
 * It transforms the information from the scheduler to XML.
 * @version Id
 * @author  <NAME>
 * @since 4.0
  */
public class SchedulerAdapter {
	public static final String version = "$RCSfile: SchedulerAdapter.java,v $ $Revision: 1.5 $ $Date: 2008-08-27 16:22:06 $";
	protected Logger log=LogUtil.getLogger(this);
	
    /**
     * Get all jobgroups, jobs within this group, the jobdetail and the
     * associated triggers in XML format.
     */
    public XmlBuilder getJobGroupNamesWithJobsToXml(Scheduler theScheduler) {
        XmlBuilder xbRoot = new XmlBuilder("jobGroups");

        try {
            // process groups
            String[] jgnames = theScheduler.getJobGroupNames();

            for (int i = 0; i < jgnames.length; i++) {
                XmlBuilder el = new XmlBuilder("jobGroup");
                el.addAttribute("name", jgnames[i]);

                // process jobs within group
                XmlBuilder jb = new XmlBuilder("jobs");
                String[] jobNames = theScheduler.getJobNames(jgnames[i]);

                for (int j = 0; j < jobNames.length; j++) {
                    XmlBuilder jn = new XmlBuilder("job");
                    jn.addAttribute("name", jobNames[j]);

                    // details for job
                    XmlBuilder jd = jobDetailToXmlBuilder(theScheduler, jobNames[j], jgnames[i]);
                    jn.addSubElement(jd);

                    // get the triggers for this job
                    XmlBuilder tr= getJobTriggers(theScheduler, jobNames[j], jgnames[i]);
                    jn.addSubElement(tr);

                    XmlBuilder datamap = jobDataMapToXmlBuilder(theScheduler, jobNames[j], jgnames[i]);
                    jn.addSubElement(datamap);
                    jb.addSubElement(jn);
                }
                el.addSubElement(jb);
                xbRoot.addSubElement(el);
            }
        } catch (org.quartz.SchedulerException se) {
           log.error(se);
        }
        return xbRoot;
    }
    
    public XmlBuilder getJobTriggers(Scheduler theScheduler, String jobName, String groupName) {

        XmlBuilder xbRoot = new XmlBuilder("triggersForJob");

        xbRoot.addAttribute("jobName", jobName);
        xbRoot.addAttribute("groupName", groupName);
        try {
            String[] tgnames = theScheduler.getTriggerGroupNames();

            for (int i = 0; i < tgnames.length; i++) {
                String[] triggerNames = theScheduler.getTriggerNames(tgnames[i]);

                for (int s = 0; s < triggerNames.length; s++) {
                    Trigger trigger = theScheduler.getTrigger(triggerNames[s], tgnames[i]);

                    if ((trigger.getJobName().equals(jobName)) && (trigger.getJobGroup().equals(groupName))) {
                        XmlBuilder tr = triggerToXmlBuilder(theScheduler, triggerNames[s], tgnames[i]);

                        xbRoot.addSubElement(tr);
                    }
                }
            }
        } catch (org.quartz.SchedulerException se) {
            log.error(se);

        }

        return xbRoot;
    }

    public XmlBuilder getSchedulerCalendarNamesToXml(Scheduler theScheduler) {
        XmlBuilder xbRoot = new XmlBuilder("schedulerCalendars");

        try {
            String[] names = theScheduler.getCalendarNames();

            for (int i = 0; i < names.length; i++) {
                XmlBuilder el = new XmlBuilder("calendar");

                el.setValue(names[i]);
                xbRoot.addSubElement(el);
            }
        } catch (org.quartz.SchedulerException se) {
            log.error(se.toString());
        }
        return xbRoot;
    }
 
    public XmlBuilder getSchedulerMetaDataToXml(Scheduler theScheduler) {
        XmlBuilder xbRoot = new XmlBuilder("schedulerMetaData");

        try {
            SchedulerMetaData smd = theScheduler.getMetaData();
<commit_msg>added getJobMessages method<commit_after>/*
 * $Log: SchedulerAdapter.java,v $
 * Revision 1.6  2009-03-17 10:34:29  m168309
 * added getJobMessages method
 *
 * Revision 1.5  2008/08/27 16:22:06  <NAME> <<EMAIL>>
 * fixed scheduler client
 *
 * Revision 1.4  2007/02/12 14:08:01  <NAME> <<EMAIL>>
 * Logger from LogUtil
 *
 */
package nl.nn.adapterframework.scheduler;

import java.util.Date;

import nl.nn.adapterframework.configuration.Configuration;
import nl.nn.adapterframework.util.DateUtils;
import nl.nn.adapterframework.util.LogUtil;
import nl.nn.adapterframework.util.XmlBuilder;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
/**
 * The SchedulerAdapter is an adapter for the <a href="http://quartz.sourceforge.net">Quartz scheduler</a> <br/>
 * It transforms the information from the scheduler to XML.
 * @version Id
 * @author  <NAME>
 * @since 4.0
  */
public class SchedulerAdapter {
	public static final String version = "$RCSfile: SchedulerAdapter.java,v $ $Revision: 1.6 $ $Date: 2009-03-17 10:34:29 $";
	protected Logger log=LogUtil.getLogger(this);
	
    /**
     * Get all jobgroups, jobs within this group, the jobdetail and the
     * associated triggers in XML format.
     */
    public XmlBuilder getJobGroupNamesWithJobsToXml(Scheduler theScheduler, Configuration config) {
        XmlBuilder xbRoot = new XmlBuilder("jobGroups");

        try {
            // process groups
            String[] jgnames = theScheduler.getJobGroupNames();

            for (int i = 0; i < jgnames.length; i++) {
                XmlBuilder el = new XmlBuilder("jobGroup");
                el.addAttribute("name", jgnames[i]);

                // process jobs within group
                XmlBuilder jb = new XmlBuilder("jobs");
                String[] jobNames = theScheduler.getJobNames(jgnames[i]);

                for (int j = 0; j < jobNames.length; j++) {
                    XmlBuilder jn = new XmlBuilder("job");
                    jn.addAttribute("name", jobNames[j]);

                    // details for job
                    XmlBuilder jd = jobDetailToXmlBuilder(theScheduler, jobNames[j], jgnames[i]);
                    jn.addSubElement(jd);

                    // get the triggers for this job
                    XmlBuilder tr= getJobTriggers(theScheduler, jobNames[j], jgnames[i]);
                    jn.addSubElement(tr);

                    XmlBuilder datamap = jobDataMapToXmlBuilder(theScheduler, jobNames[j], jgnames[i]);
                    jn.addSubElement(datamap);
                    jb.addSubElement(jn);

					XmlBuilder ms= getJobMessages(config.getScheduledJob(jobNames[j]));
					jn.addSubElement(ms);
                }
                el.addSubElement(jb);
                xbRoot.addSubElement(el);
            }
        } catch (org.quartz.SchedulerException se) {
           log.error(se);
        }
        return xbRoot;
    }
    
    public XmlBuilder getJobTriggers(Scheduler theScheduler, String jobName, String groupName) {

        XmlBuilder xbRoot = new XmlBuilder("triggersForJob");

        xbRoot.addAttribute("jobName", jobName);
        xbRoot.addAttribute("groupName", groupName);
        try {
            String[] tgnames = theScheduler.getTriggerGroupNames();

            for (int i = 0; i < tgnames.length; i++) {
                String[] triggerNames = theScheduler.getTriggerNames(tgnames[i]);

                for (int s = 0; s < triggerNames.length; s++) {
                    Trigger trigger = theScheduler.getTrigger(triggerNames[s], tgnames[i]);

                    if ((trigger.getJobName().equals(jobName)) && (trigger.getJobGroup().equals(groupName))) {
                        XmlBuilder tr = triggerToXmlBuilder(theScheduler, triggerNames[s], tgnames[i]);

                        xbRoot.addSubElement(tr);
                    }
                }
            }
        } catch (org.quartz.SchedulerException se) {
            log.error(se);

        }

        return xbRoot;
    }

	public XmlBuilder getJobMessages(JobDef jobdef) {
		XmlBuilder jobMessages = new XmlBuilder("jobMessages");
		for (int t=0; t<jobdef.getMessageKeeper().size(); t++) {
			XmlBuilder jobMessage=new XmlBuilder("jobMessage");
			jobMessage.setValue(jobdef.getMessageKeeper().getMessage(t).getMessageText(),true);
			jobMessage.addAttribute("date", DateUtils.format(jobdef.getMessageKeeper().getMessage(t).getMessageDate(), DateUtils.FORMAT_FULL_GENERIC));
			jobMessages.addSubElement(jobMessage);
		}
		return jobMessages;
	}

    public XmlBuilder getSchedulerCalendarNamesToXml(Scheduler theScheduler) {
        XmlBuilder xbRoot = new XmlBuilder("schedulerCalendars");

        try {
            String[] names = theScheduler.getCalendarNames();

            for (int i = 0; i < names.length; i++) {
                XmlBuilder el = new XmlBuilder("calendar");

                el.setValue(names[i]);
                xbRoot.addSubElement(el);
            }
        } catch (org.quartz.SchedulerException se) {
            log.error(se.toString());
        }
        return xbRoot;
    }
 
    public XmlBuilder getSchedulerMetaDataToXml(Scheduler theScheduler) {
        XmlBuilder xbRoot = new XmlBuilder("schedulerMetaData");

        try {
            SchedulerMetaData smd = theScheduler.getMetaData();

{'fraction_non_alphanumeric': 0.11028200407016184, 'fraction_numerical': 0.013664114739800368, 'mean_word_length': 2.3408870184525736, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '532285', 'n_tokens_mistral': 3237, 'n_tokens_neox': 2929, 'n_words': 634}