3a47c38444bf483e30c9837437ce98c3cc0d8a66
[so.git] / common / src / main / java / org / onap / so / 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.so.client.graphinventory.entities;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28 import org.onap.so.client.aai.entities.QueryStep;
29 import org.onap.so.client.graphinventory.GraphInventoryObjectName;
30
31 import com.google.common.base.Joiner;
32
33
34 public class DSLQueryBuilder<S, E> implements QueryStep {
35
36         private List<QueryStep> steps = new ArrayList<>();
37         private String suffix = "";
38         
39         public DSLQueryBuilder() {
40                 
41         }
42         public DSLQueryBuilder(DSLNode node) {
43                 steps.add(node);
44         }
45         
46         public DSLQueryBuilder<S, DSLNode> node(DSLNode node) {
47                 steps.add(node);
48                 
49                 return (DSLQueryBuilder<S, DSLNode>) this;
50         }
51         public DSLQueryBuilder<S, E> output() {
52                 if (steps.get(steps.size() -1) instanceof DSLNode) {
53                         ((DSLNode)steps.get(steps.size() -1)).output();
54                 }
55                 return this;
56         }
57         
58         public <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) {
59                 
60                 List<DSLQueryBuilder<?, ?>> unions = Arrays.asList(union);
61                 steps.add(() -> {
62                         StringBuilder query = new StringBuilder();
63                 
64                         query.append("> [ ").append(
65                                         Joiner.on(", ").join(
66                                                 unions.stream().map(item -> item.build()).collect(Collectors.toList())))
67                                         .append(" ]");
68                         return query.toString();
69                 });
70                 
71                 return (DSLQueryBuilder<S, E2>) this;
72         }
73         
74         public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) {
75
76                 steps.add(() -> {
77                         StringBuilder query = new StringBuilder();
78                         query.append(where.build()).append(")");
79                         String result = query.toString();
80                         if (!result.startsWith(">")) {
81                                 result = "> " + result;
82                         }
83                         return "(" + result;
84                 });
85                 return this;
86         }
87         
88         public DSLQueryBuilder<S, E> to(DSLQueryBuilder<?, ?> to) {
89                 steps.add(() -> {
90                         StringBuilder query = new StringBuilder();
91                         
92                         query.append("> ").append(to.build());
93                         return query.toString();
94                 });
95                 return this;
96         }
97         
98         public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) {
99                 return to(__.node(name));
100         }
101         
102         public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) {
103                 return to(__.node(name, key));
104         }
105         
106         public DSLQueryBuilder<S, E> limit(int limit) {
107                 suffix = " LIMIT " + limit;
108                 return this;
109         }
110         
111         @Override
112         public String build() {
113                 return compile();
114         }
115         
116         @Override
117         public String toString() {
118                 return build();
119         }
120         
121         @Override
122         public boolean equals(Object o) {
123                 if (o != null) {
124                         if (o instanceof QueryStep) {
125                                 return ((QueryStep)o).build().equals(this.build());
126                         } else if (o instanceof String) {
127                                 return o.equals(this.build());
128                         }
129                 }
130                 return false;
131         }
132         
133         @Override
134         public int hashCode() {
135                 
136                 return build().hashCode();
137         }
138         
139         private String compile() {
140                 return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
141         }
142         
143         protected QueryStep getFirst() {
144                 return steps.get(0);
145         }
146 }