Merge "move aai-client into its own project"
[so.git] / graph-inventory / aai-client / src / main / java / org / onap / aaiclient / client / graphinventory / entities / DSLQueryBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaiclient.client.graphinventory.entities;
22
23 import java.lang.reflect.Field;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.stream.Collectors;
28 import org.onap.aaiclient.client.aai.entities.QueryStep;
29 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName;
30 import com.google.common.base.Joiner;
31
32 public class DSLQueryBuilder<S, E> {
33
34     private List<QueryStep> steps = new ArrayList<>();
35     private String suffix = "";
36
37     protected DSLQueryBuilder() {
38
39     }
40
41     protected DSLQueryBuilder(QueryStep node) {
42         steps.add(node);
43     }
44
45     public <T> DSLQueryBuilder<S, DSLNodeBase<?>> node(DSLNodeBase<?> node) {
46         steps.add(node);
47
48         return (DSLQueryBuilder<S, DSLNodeBase<?>>) this;
49     }
50
51     public DSLQueryBuilder<S, Node> output() {
52         Object obj = steps.get(steps.size() - 1);
53         if (obj instanceof DSLNodeBase) {
54             ((DSLNodeBase) steps.get(steps.size() - 1)).output();
55         } else if (obj.getClass().getName().contains("$$Lambda$")) {
56             // process lambda expressions
57             for (Field f : obj.getClass().getDeclaredFields()) {
58                 f.setAccessible(true);
59                 Object o;
60                 try {
61                     o = f.get(obj);
62                     if (o instanceof DSLQueryBuilder && ((DSLQueryBuilder) o).steps.get(0) instanceof DSLNodeBase) {
63                         ((DSLNodeBase) ((DSLQueryBuilder) o).steps.get(0)).output();
64                     }
65                 } catch (IllegalArgumentException | IllegalAccessException e) {
66                 }
67                 f.setAccessible(false);
68                 break;
69             }
70         }
71         return (DSLQueryBuilder<S, Node>) this;
72     }
73
74     @SafeVarargs
75     public final <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) {
76
77         List<DSLQueryBuilder<?, ?>> unions = Arrays.asList(union);
78         steps.add(() -> {
79             StringBuilder query = new StringBuilder();
80
81             query.append("> [ ")
82                     .append(Joiner.on(", ")
83                             .join(unions.stream().map(item -> item.compile()).collect(Collectors.toList())))
84                     .append(" ]");
85             return query.toString();
86         });
87
88         return (DSLQueryBuilder<S, E2>) this;
89     }
90
91     public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) {
92
93         steps.add(() -> {
94             StringBuilder query = new StringBuilder();
95             query.append(where.compile()).append(")");
96             String result = query.toString();
97             if (!result.startsWith(">")) {
98                 result = "> " + result;
99             }
100             return "(" + result;
101         });
102         return this;
103     }
104
105     public <E2> DSLQueryBuilder<S, E2> to(DSLQueryBuilder<?, E2> to) {
106         steps.add(() -> {
107             StringBuilder query = new StringBuilder();
108
109             query.append("> ").append(to.compile());
110             return query.toString();
111         });
112         return (DSLQueryBuilder<S, E2>) this;
113     }
114
115     public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) {
116         return (DSLQueryBuilder<S, E>) to(__.node(name));
117     }
118
119     public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) {
120         return (DSLQueryBuilder<S, E>) to(__.node(name, key));
121     }
122
123     public DSLQueryBuilder<S, E> limit(int limit) {
124         suffix = " LIMIT " + limit;
125         return this;
126     }
127
128     public DSLTraversal<E> build() {
129         return new DSLTraversal<>(compile());
130     }
131
132     @Override
133     public String toString() {
134         return build().get();
135     }
136
137     @Override
138     public boolean equals(Object o) {
139         if (o != null) {
140             return o.toString().equals(toString());
141         }
142         return false;
143     }
144
145     @Override
146     public int hashCode() {
147
148         return compile().hashCode();
149     }
150
151     private String compile() {
152         return String.join(" ", steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
153     }
154
155     protected QueryStep getFirst() {
156         return steps.get(0);
157     }
158 }