Format Java code to ONAP standard
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / util / AggregationParsingUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.util;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import java.util.Iterator;
25 import java.util.Set;
26 import org.json.simple.JSONArray;
27 import org.json.simple.JSONObject;
28 import org.onap.aai.sa.searchdbabstraction.entity.AggregationBucket;
29 import org.onap.aai.sa.searchdbabstraction.entity.AggregationResult;
30
31 public class AggregationParsingUtil {
32     public static AggregationResult[] parseAggregationResults(JSONObject aggregations) throws JsonProcessingException {
33
34         // Obtain the set of aggregation names
35         Set keySet = aggregations.keySet();
36         AggregationResult[] aggResults = new AggregationResult[keySet.size()];
37
38         int index = 0;
39         for (Iterator it = keySet.iterator(); it.hasNext();) {
40             String key = (String) it.next();
41             AggregationResult aggResult = new AggregationResult();
42             aggResult.setName(key);
43
44             JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
45             Object buckets = bucketsOrNested.get("buckets");
46             if (buckets == null) {
47                 // we have a nested
48                 Number count = (Number) bucketsOrNested.remove("doc_count");
49                 aggResult.setCount(count);
50                 AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
51                 aggResult.setNestedAggregations(nestedResults);
52             } else {
53                 AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
54                 aggResult.setBuckets(aggBuckets);
55             }
56
57             aggResults[index] = aggResult;
58             index++;
59         }
60
61         return aggResults;
62
63     }
64
65     private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets) throws JsonProcessingException {
66         AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
67         for (int i = 0; i < buckets.size(); i++) {
68             AggregationBucket aggBucket = new AggregationBucket();
69             JSONObject bucketContent = (JSONObject) buckets.get(i);
70             Object key = bucketContent.remove("key");
71             aggBucket.setKey(key);
72             Object formatted = bucketContent.remove("key_as_string");
73             if (formatted != null) {
74                 aggBucket.setFormattedKey((String) formatted);
75             }
76             Object count = bucketContent.remove("doc_count");
77             if (count != null) {
78                 aggBucket.setCount((Number) count);
79             }
80             bucketContent.remove("from");
81             bucketContent.remove("from_as_string");
82             bucketContent.remove("to");
83             bucketContent.remove("to_as_string");
84
85
86             if (!bucketContent.entrySet().isEmpty()) {
87                 // we have results from sub-aggregation
88                 AggregationResult[] subResult = parseAggregationResults(bucketContent);
89                 if (subResult != null) {
90                     aggBucket.setSubAggregationResult(subResult);
91                 }
92             }
93             aggBuckets[i] = aggBucket;
94         }
95
96         return aggBuckets;
97     }
98
99 }