package org.argeo.api.acr.search; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.function.Consumer; import javax.xml.namespace.QName; import org.argeo.api.acr.QNamed; /** * A basic search mechanism modelled on WebDav basicsearch. * * @see http://www.webdav.org/specs/rfc5323.html */ public class BasicSearch { private List select = new ArrayList<>(); private List from = new ArrayList<>(); private ContentFilter where; public BasicSearch select(QNamed... attr) { for (QNamed q : attr) select.add(q.qName()); return this; } public BasicSearch select(QName... attr) { select.addAll(Arrays.asList(attr)); return this; } /** * Convenience method, to search below this absolute path, with depth * {@link Depth#INFINITTY}. */ public BasicSearch from(String path) { return from(URI.create(path), Depth.INFINITTY); } /** Search below this URI, with depth {@link Depth#INFINITTY}. */ public BasicSearch from(URI uri) { return from(uri, Depth.INFINITTY); } /** Search below this URI, with this {@link Depth}. */ public BasicSearch from(URI uri, Depth depth) { Objects.requireNonNull(uri); Objects.requireNonNull(depth); Scope scope = new Scope(uri, depth); from.add(scope); return this; } public BasicSearch where(Consumer and) { // if (where != null) // throw new IllegalStateException("A where clause is already set"); // AndFilter subFilter = new AndFilter(); and.accept((AndFilter) getWhere()); // where = subFilter; return this; } public List getSelect() { return select; } public List getFrom() { return from; } public ContentFilter getWhere() { if (where == null) where = new AndFilter(); return where; } public static enum Depth { ZERO, ONE, INFINITTY; } public static class Scope { URI uri; Depth depth; public Scope(URI uri, Depth depth) { this.uri = uri; this.depth = depth; } public URI getUri() { return uri; } public Depth getDepth() { return depth; } } // static void main(String[] args) { // BasicSearch search = new BasicSearch(); // search.select(DName.creationdate.qName()) // // .from(URI.create("/test")) // // .where((f) -> f.eq(DName.creationdate.qName(), "")); // } }