]> git.argeo.org Git - lgpl/argeo-commons.git/blob - search/BasicSearch.java
Prepare next development cycle
[lgpl/argeo-commons.git] / search / BasicSearch.java
1 package org.argeo.api.acr.search;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Objects;
8 import java.util.function.Consumer;
9
10 import javax.xml.namespace.QName;
11
12 import org.argeo.api.acr.QNamed;
13
14 /**
15 * A basic search mechanism modelled on WebDav basicsearch.
16 *
17 * @see http://www.webdav.org/specs/rfc5323.html
18 */
19 public class BasicSearch {
20
21 private List<QName> select = new ArrayList<>();
22 private List<Scope> from = new ArrayList<>();
23
24 private ContentFilter<? extends Composition> where;
25
26 public BasicSearch select(QNamed... attr) {
27 for (QNamed q : attr)
28 select.add(q.qName());
29 return this;
30 }
31
32 public BasicSearch select(QName... attr) {
33 select.addAll(Arrays.asList(attr));
34 return this;
35 }
36
37 public BasicSearch from(URI uri) {
38 return from(uri, Depth.INFINITTY);
39 }
40
41 public BasicSearch from(URI uri, Depth depth) {
42 Objects.requireNonNull(uri);
43 Objects.requireNonNull(depth);
44 Scope scope = new Scope(uri, depth);
45 from.add(scope);
46 return this;
47 }
48
49 public BasicSearch where(Consumer<AndFilter> and) {
50 if (where != null)
51 throw new IllegalStateException("A where clause is already set");
52 AndFilter subFilter = new AndFilter();
53 and.accept(subFilter);
54 where = subFilter;
55 return this;
56 }
57
58 public List<QName> getSelect() {
59 return select;
60 }
61
62 public List<Scope> getFrom() {
63 return from;
64 }
65
66 public ContentFilter<? extends Composition> getWhere() {
67 return where;
68 }
69
70 public static enum Depth {
71 ZERO, ONE, INFINITTY;
72 }
73
74 public static class Scope {
75
76 URI uri;
77 Depth depth;
78
79 public Scope(URI uri, Depth depth) {
80 this.uri = uri;
81 this.depth = depth;
82 }
83
84 public URI getUri() {
85 return uri;
86 }
87
88 public Depth getDepth() {
89 return depth;
90 }
91
92 }
93
94 // static void main(String[] args) {
95 // BasicSearch search = new BasicSearch();
96 // search.select(DName.creationdate.qName()) //
97 // .from(URI.create("/test")) //
98 // .where((f) -> f.eq(DName.creationdate.qName(), ""));
99 // }
100 }