package org.exist.xquery.functions.xmldb;

import org.exist.dom.persistent.DocumentImpl;
import org.exist.dom.persistent.NewArrayNodeSet;
import org.exist.dom.persistent.NodeProxy;
import org.exist.dom.persistent.NodeSet;
import org.exist.dom.persistent.NodeSetIterator;
import org.exist.dom.QName;
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.value.DateTimeValue;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.FunctionReturnSequenceType;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;

public class FindLastModified extends BasicFunction {

	public final static FunctionSignature signatures[] = new FunctionSignature[]{
			new FunctionSignature(
					new QName("find-last-modified-since", XMLDBModule.NAMESPACE_URI, XMLDBModule.PREFIX),
					"Filters the given node set to only include nodes from resources which were modified since the specified " +
							"date time.",
					new SequenceType[]{
							new FunctionParameterSequenceType("node-set", Type.NODE, Cardinality.ZERO_OR_MORE,
									"A node set"),
							new FunctionParameterSequenceType("since", Type.DATE_TIME, Cardinality.EXACTLY_ONE,
									"Date")
					},
					new FunctionReturnSequenceType(Type.NODE, Cardinality.ZERO_OR_MORE, "the filtered node set.")
			),
			new FunctionSignature(
					new QName("find-last-modified-until", XMLDBModule.NAMESPACE_URI, XMLDBModule.PREFIX),
					"Filters the given node set to only include nodes from resources which were modified until the specified " +
							"date time.",
					new SequenceType[]{
							new FunctionParameterSequenceType("node-set", Type.NODE, Cardinality.ZERO_OR_MORE,
									"A node set"),
							new FunctionParameterSequenceType("until", Type.DATE_TIME, Cardinality.EXACTLY_ONE,
									"Date")
					},
					new FunctionReturnSequenceType(Type.NODE, Cardinality.ZERO_OR_MORE, "the filtered node set.")
			)
	};

	public FindLastModified(XQueryContext context, FunctionSignature signature) {
		super(context, signature);
	}
	
	@Override
	public Sequence eval(Sequence[] args, Sequence contextSequence)
			throws XPathException {
		final NodeSet nodes = args[0].toNodeSet();
		if (nodes.isEmpty())
			{return Sequence.EMPTY_SEQUENCE;}
		
		final NodeSet result = new NewArrayNodeSet();
		final DateTimeValue dtv = (DateTimeValue) args[1].itemAt(0);
		final long lastModified = dtv.getDate().getTime();

		for (final NodeSetIterator i = nodes.iterator(); i.hasNext(); ) {
			final NodeProxy proxy = i.next();
			final DocumentImpl doc = proxy.getOwnerDocument();
			final long modified = doc.getMetadata().getLastModified();

			boolean matches;
			if (this.isCalledAs("find-last-modified-since")) {
				matches = modified > lastModified;
			} else {
				matches = modified <= lastModified;
			}

			if (matches)
				{result.add(proxy);}
		}
		return result;
	}

}

@@ -1,8 +1,863 @@
+/*%0A * eXist Open Source Native XML Database%0A * Copyright (C) 2001-2017 The eXist Project%0A * http://exist-db.org%0A *%0A * This program is free software; you can redistribute it and/or%0A * modify it under the terms of the GNU Lesser General Public License%0A * as published by the Free Software Foundation; either version 2%0A * of the License, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Lesser General Public License for more details.%0A *%0A * You should have received a copy of the GNU Lesser General Public%0A * License along with this library; if not, write to the Free Software%0A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA%0A */%0A%0A
 package 

add copyright header