2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aaiclient.client.graphinventory.entities;
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.function.Consumer;
28 import java.util.stream.Collectors;
29 import org.onap.aaiclient.client.aai.entities.QueryStep;
30 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName;
31 import com.google.common.base.Joiner;
33 public class DSLQueryBuilder<S, E> {
35 private List<QueryStep> steps = new ArrayList<>();
36 private String suffix = "";
38 protected DSLQueryBuilder() {
42 protected DSLQueryBuilder(QueryStep node) {
46 public <T> DSLQueryBuilder<S, DSLNodeBase<?>> node(DSLNodeBase<?> node) {
49 return (DSLQueryBuilder<S, DSLNodeBase<?>>) this;
52 public DSLQueryBuilder<S, Node> output() {
53 callOnLambda(item -> item.output());
54 return (DSLQueryBuilder<S, Node>) this;
57 public DSLQueryBuilder<S, Node> output(String... fields) {
58 callOnLambda(item -> item.output(fields));
59 return (DSLQueryBuilder<S, Node>) this;
62 protected void callOnLambda(Consumer<DSLNodeBase> consumer) {
64 Object obj = steps.get(steps.size() - 1);
65 if (obj instanceof DSLNodeBase) {
66 ((DSLNodeBase) steps.get(steps.size() - 1)).output();
67 } else if (obj.getClass().getName().contains("$$Lambda$")) {
68 // process lambda expressions
69 for (Field f : obj.getClass().getDeclaredFields()) {
70 f.setAccessible(true);
74 if (o instanceof DSLQueryBuilder && ((DSLQueryBuilder) o).steps.get(0) instanceof DSLNodeBase) {
75 consumer.accept(((DSLNodeBase) ((DSLQueryBuilder) o).steps.get(0)));
77 } catch (IllegalArgumentException | IllegalAccessException e) {
79 f.setAccessible(false);
86 public final <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) {
88 List<DSLQueryBuilder<?, ?>> unions = Arrays.asList(union);
90 StringBuilder query = new StringBuilder();
93 .append(Joiner.on(", ")
94 .join(unions.stream().map(item -> item.compile()).collect(Collectors.toList())))
96 return query.toString();
99 return (DSLQueryBuilder<S, E2>) this;
102 public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) {
105 StringBuilder query = new StringBuilder();
106 query.append(where.compile()).append(")");
107 String result = query.toString();
108 if (!result.startsWith(">")) {
109 result = "> " + result;
116 public <E2> DSLQueryBuilder<S, E2> to(DSLQueryBuilder<?, E2> to) {
118 StringBuilder query = new StringBuilder();
120 query.append("> ").append(to.compile());
121 return query.toString();
123 return (DSLQueryBuilder<S, E2>) this;
126 public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) {
127 return (DSLQueryBuilder<S, E>) to(__.node(name));
130 public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) {
131 return (DSLQueryBuilder<S, E>) to(__.node(name, key));
134 public DSLQueryBuilder<S, E> limit(int limit) {
135 suffix = " LIMIT " + limit;
139 public DSLTraversal<E> build() {
140 return new DSLTraversal<>(compile());
144 public String toString() {
145 return build().get();
149 public boolean equals(Object o) {
151 return o.toString().equals(toString());
157 public int hashCode() {
159 return compile().hashCode();
162 private String compile() {
163 return String.join(" ", steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
166 protected QueryStep getFirst() {