]> git.argeo.org Git - lgpl/argeo-commons.git/blob - BasicSearch.java
8028f5d2033e8d5138bf23aaca90a029b2e2f4ce
[lgpl/argeo-commons.git] / 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.DName;
13 import org.argeo.api.acr.QNamed;
14
15 public class BasicSearch {
16
17 private List<QName> select = new ArrayList<>();
18 private List<Scope> from = new ArrayList<>();
19
20 private ContentFilter<? extends Composition> where;
21
22 public BasicSearch select(QNamed... attr) {
23 for (QNamed q : attr)
24 select.add(q.qName());
25 return this;
26 }
27
28 public BasicSearch select(QName... attr) {
29 select.addAll(Arrays.asList(attr));
30 return this;
31 }
32
33 public BasicSearch from(URI uri) {
34 return from(uri, Depth.INFINITTY);
35 }
36
37 public BasicSearch from(URI uri, Depth depth) {
38 Objects.requireNonNull(uri);
39 Objects.requireNonNull(depth);
40 Scope scope = new Scope(uri, depth);
41 from.add(scope);
42 return this;
43 }
44
45 public BasicSearch where(Consumer<AndFilter> and) {
46 if (where != null)
47 throw new IllegalStateException("A where clause is already set");
48 AndFilter subFilter = new AndFilter();
49 and.accept(subFilter);
50 where = subFilter;
51 return this;
52 }
53
54 public List<QName> getSelect() {
55 return select;
56 }
57
58 public List<Scope> getFrom() {
59 return from;
60 }
61
62 public ContentFilter<? extends Composition> getWhere() {
63 return where;
64 }
65
66 public static enum Depth {
67 ZERO, ONE, INFINITTY;
68 }
69
70 public static class Scope {
71
72 URI uri;
73 Depth depth;
74
75 public Scope(URI uri, Depth depth) {
76 this.uri = uri;
77 this.depth = depth;
78 }
79
80 public URI getUri() {
81 return uri;
82 }
83
84 public Depth getDepth() {
85 return depth;
86 }
87
88 }
89
90 static void main(String[] args) {
91 BasicSearch search = new BasicSearch();
92 search.select(DName.creationdate.qName()) //
93 .from(URI.create("/test")) //
94 .where((f) -> f.eq(DName.creationdate.qName(), ""));
95 }
96 }