All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.exist.xquery.modules.scheduler.GetScheduledJobs Maven / Gradle / Ivy

/*
 *  eXist Scheduler Module Extension GetSheduledJobs
 *  Copyright (C) 2006-09 Adam Retter 
 *  www.adamretter.co.uk
 *
 *  This program 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
 *  of the License, or (at your option) any later version.
 *
 *  This program 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 program; if not, write to the Free Software Foundation
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  $Id$
 */
package org.exist.xquery.modules.scheduler;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.exist.dom.QName;
import org.exist.scheduler.ScheduledJobInfo;
import org.exist.scheduler.Scheduler;
import org.exist.scheduler.UserJob;
import org.exist.security.Subject;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.modules.ModuleUtils;
import org.exist.xquery.value.DateTimeValue;
import org.exist.xquery.value.FunctionReturnSequenceType;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.Type;
import org.xml.sax.SAXException;


/**
 * eXist Scheduler Module Extension GetScheduledJobs.
 *
 * Retrieves details of Jobs that have been Scheduled
 *
 * @author Adam Retter
 * @author Loren Cahlander
 * @version  1.3
 * @see      org.exist.xquery.BasicFunction#BasicFunction(org.exist.xquery.XQueryContext, org.exist.xquery.FunctionSignature)
 * @serial   2007-12-04
 * @serial   2009-07-09
 */
public class GetScheduledJobs extends BasicFunction
{
    	
	public final static FunctionSignature signature =
		new FunctionSignature(
			new QName( "get-scheduled-jobs", SchedulerModule.NAMESPACE_URI, SchedulerModule.PREFIX ),
			"Gets the details of all scheduled jobs in the form: " +
			"" +
			"    " +
			"        " +
			"            " +
			"                " +
			"                " +
			"                " +
			"                " +
			"                " +
			"                " +
			"                " +
			"            " +
			"        " +
			"    " +
			"",
			null,
			new FunctionReturnSequenceType( Type.NODE, Cardinality.EXACTLY_ONE, "the XML containing the list of jobs" ) 
		);
	
	private Scheduler                     scheduler = null;

    /**
     * GetScheduledJobs Constructor.
     *
     * @param  context    The Context of the calling XQuery
     * @param  signature  DOCUMENT ME!
     */
    public GetScheduledJobs( XQueryContext context, FunctionSignature signature )
    {
        super( context, signature );

        scheduler = context.getBroker().getBrokerPool().getScheduler();
    }

    /**
     * evaluate the call to the xquery function, it is really the main entry point of this class.
     *
     * @param   args             arguments from the function call
     * @param   contextSequence  the Context Sequence to operate on (not used here internally!)
     *
     * @return  A sequence representing the result of the function call
     *
     * @throws  XPathException  DOCUMENT ME!
     *
     * @see     org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
     */
    @Override
    public Sequence eval( Sequence[] args, Sequence contextSequence ) throws XPathException
    {
        Subject               user           = context.getSubject();

        boolean            userhasDBARole = user.hasDbaRole();

        StringBuilder      xmlBuf         = new StringBuilder();

        int                iJobs          = 0;
        List           groups         = scheduler.getJobGroupNames();
        List scheduledJobs  = scheduler.getScheduledJobs();

        for (String group : groups) {

            if (userhasDBARole || group.equals(UserJob.JOB_GROUP)) {
                xmlBuf.append("<" + SchedulerModule.PREFIX + ":group name=\"").append(group).append("\">");

                for (ScheduledJobInfo scheduledJob : scheduledJobs) {

                    if (scheduledJob.getGroup().equals(group)) {
                        xmlBuf.append("<" + SchedulerModule.PREFIX + ":job name=\"").append(scheduledJob.getName()).append("\">");
                        xmlBuf.append("<" + SchedulerModule.PREFIX + ":trigger name=\"").append(scheduledJob.getTriggerName()).append("\">");
                        xmlBuf.append("");
                        xmlBuf.append(scheduledJob.getTriggerExpression());
                        xmlBuf.append("");
                        xmlBuf.append("");
                        xmlBuf.append(scheduledJob.getTriggerState());
                        xmlBuf.append("");
                        xmlBuf.append("");
                        xmlBuf.append(new DateTimeValue(scheduledJob.getStartTime()));
                        xmlBuf.append("");
                        xmlBuf.append("");

                        Date endTime = scheduledJob.getEndTime();

                        if (endTime != null) {
                            xmlBuf.append(new DateTimeValue(endTime));
                        }

                        xmlBuf.append("");
                        xmlBuf.append("");

                        Date previousTime = scheduledJob.getPreviousFireTime();

                        if (previousTime != null) {
                            xmlBuf.append(new DateTimeValue(scheduledJob.getPreviousFireTime()));
                        }

                        xmlBuf.append("");
                        xmlBuf.append("");

                        Date nextTime = scheduledJob.getNextFireTime();

                        if (nextTime != null) {
                            xmlBuf.append(new DateTimeValue());
                        }

                        xmlBuf.append("");
                        xmlBuf.append("");

                        Date finalTime = scheduledJob.getFinalFireTime();

                        if ((endTime != null) && (finalTime != null)) {
                            xmlBuf.append(new DateTimeValue());
                        }

                        xmlBuf.append("");
                        xmlBuf.append("");
                        xmlBuf.append("");
                        iJobs++;
                    }
                }

                xmlBuf.append("");
            }
        }

        xmlBuf.insert( 0, "<" + SchedulerModule.PREFIX + ":jobs xmlns:scheduler=\"" + SchedulerModule.NAMESPACE_URI + "\" count=\"" + iJobs + "\">" );
        xmlBuf.append( "" );

        try {
            return ModuleUtils.stringToXML( context, xmlBuf.toString());
        } catch(SAXException | IOException se) {
            throw new XPathException(this, se.getMessage(), se);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy