Organise imports to ONAP Java standards
[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)
33     throws JsonProcessingException {
34
35     // Obtain the set of aggregation names
36     Set keySet = aggregations.keySet();
37     AggregationResult[] aggResults = new AggregationResult[keySet.size()];
38
39     int index = 0;
40     for (Iterator it = keySet.iterator(); it.hasNext(); ) {
41       String key = (String) it.next();
42       AggregationResult aggResult = new AggregationResult();
43       aggResult.setName(key);
44
45       JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
46       Object buckets = bucketsOrNested.get("buckets");
47       if (buckets == null) {
48         // we have a nested
49         Number count = (Number) bucketsOrNested.remove("doc_count");
50         aggResult.setCount(count);
51         AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
52         aggResult.setNestedAggregations(nestedResults);
53       } else {
54         AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
55         aggResult.setBuckets(aggBuckets);
56       }
57
58       aggResults[index] = aggResult;
59       index++;
60     }
61
62     return aggResults;
63
64   }
65
66   private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets)
67     throws JsonProcessingException {
68     AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
69     for (int i = 0; i < buckets.size(); i++) {
70       AggregationBucket aggBucket = new AggregationBucket();
71       JSONObject bucketContent = (JSONObject) buckets.get(i);
72       Object key = bucketContent.remove("key");
73       aggBucket.setKey(key);
74       Object formatted = bucketContent.remove("key_as_string");
75       if (formatted != null) {
76         aggBucket.setFormattedKey((String) formatted);
77       }
78       Object count = bucketContent.remove("doc_count");
79       if (count != null) {
80         aggBucket.setCount((Number) count);
81       }
82       bucketContent.remove("from");
83       bucketContent.remove("from_as_string");
84       bucketContent.remove("to");
85       bucketContent.remove("to_as_string");
86
87
88       if (!bucketContent.entrySet().isEmpty()) {
89         // we have results from sub-aggregation
90         AggregationResult[] subResult = parseAggregationResults(bucketContent);
91         if (subResult != null) {
92           aggBucket.setSubAggregationResult(subResult);
93         }
94       }
95       aggBuckets[i] = aggBucket;
96     }
97
98     return aggBuckets;
99   }
100
101 }