]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.acr/src/org/argeo/api/acr/search/BasicSearch.java
Merge tag 'v2.3.18' into testing
[lgpl/argeo-commons.git] / org.argeo.api.acr / src / org / argeo / api / acr / 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 /**
38 * Convenience method, to search below this absolute path, with depth
39 * {@link Depth#INFINITTY}.
40 */
41 public BasicSearch from(String path) {
42 return from(URI.create(path), Depth.INFINITTY);
43 }
44
45 /** Search below this URI, with depth {@link Depth#INFINITTY}. */
46 public BasicSearch from(URI uri) {
47 return from(uri, Depth.INFINITTY);
48 }
49
50 /** Search below this URI, with this {@link Depth}. */
51 public BasicSearch from(URI uri, Depth depth) {
52 Objects.requireNonNull(uri);
53 Objects.requireNonNull(depth);
54 Scope scope = new Scope(uri, depth);
55 from.add(scope);
56 return this;
57 }
58
59 public BasicSearch where(Consumer<AndFilter> and) {
60 if (where != null)
61 throw new IllegalStateException("A where clause is already set");
62 AndFilter subFilter = new AndFilter();
63 and.accept(subFilter);
64 where = subFilter;
65 return this;
66 }
67
68 public List<QName> getSelect() {
69 return select;
70 }
71
72 public List<Scope> getFrom() {
73 return from;
74 }
75
76 public ContentFilter<? extends Composition> getWhere() {
77 return where;
78 }
79
80 public static enum Depth {
81 ZERO, ONE, INFINITTY;
82 }
83
84 public static class Scope {
85
86 URI uri;
87 Depth depth;
88
89 public Scope(URI uri, Depth depth) {
90 this.uri = uri;
91 this.depth = depth;
92 }
93
94 public URI getUri() {
95 return uri;
96 }
97
98 public Depth getDepth() {
99 return depth;
100 }
101
102 }
103
104 // static void main(String[] args) {
105 // BasicSearch search = new BasicSearch();
106 // search.select(DName.creationdate.qName()) //
107 // .from(URI.create("/test")) //
108 // .where((f) -> f.eq(DName.creationdate.qName(), ""));
109 // }
110 }