X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsa%2Fsearchdbabstraction%2Futil%2FAggregationParsingUtil.java;h=20fd027072fdf369ff19a34561a05131d7338375;hb=f637a36c9df966c341727910e30241b63cc49c06;hp=53e02bf8e25f20830232370cdcc8df55c33f12c1;hpb=d6348739c632fb69c8833078effbb902adc7f702;p=aai%2Fsearch-data-service.git diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/util/AggregationParsingUtil.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/util/AggregationParsingUtil.java index 53e02bf..20fd027 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/util/AggregationParsingUtil.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/util/AggregationParsingUtil.java @@ -1,4 +1,4 @@ -/** +/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ @@ -21,82 +21,83 @@ package org.onap.aai.sa.searchdbabstraction.util; import com.fasterxml.jackson.core.JsonProcessingException; +import java.util.Iterator; +import java.util.Set; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.onap.aai.sa.searchdbabstraction.entity.AggregationBucket; import org.onap.aai.sa.searchdbabstraction.entity.AggregationResult; -import java.util.Iterator; -import java.util.Set; - public class AggregationParsingUtil { - public static AggregationResult[] parseAggregationResults(JSONObject aggregations) - throws JsonProcessingException { - // Obtain the set of aggregation names - Set keySet = aggregations.keySet(); - AggregationResult[] aggResults = new AggregationResult[keySet.size()]; + private AggregationParsingUtil() { // Do not instantiate + } - int index = 0; - for (Iterator it = keySet.iterator(); it.hasNext(); ) { - String key = (String) it.next(); - AggregationResult aggResult = new AggregationResult(); - aggResult.setName(key); + public static AggregationResult[] parseAggregationResults(JSONObject aggregations) throws JsonProcessingException { - JSONObject bucketsOrNested = (JSONObject) aggregations.get(key); - Object buckets = bucketsOrNested.get("buckets"); - if (buckets == null) { - // we have a nested - Number count = (Number) bucketsOrNested.remove("doc_count"); - aggResult.setCount(count); - AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested); - aggResult.setNestedAggregations(nestedResults); - } else { - AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets); - aggResult.setBuckets(aggBuckets); - } + // Obtain the set of aggregation names + Set keySet = aggregations.keySet(); + AggregationResult[] aggResults = new AggregationResult[keySet.size()]; - aggResults[index] = aggResult; - index++; - } + int index = 0; + for (Iterator it = keySet.iterator(); it.hasNext();) { + String key = (String) it.next(); + AggregationResult aggResult = new AggregationResult(); + aggResult.setName(key); - return aggResults; + JSONObject bucketsOrNested = (JSONObject) aggregations.get(key); + Object buckets = bucketsOrNested.get("buckets"); + if (buckets == null) { + // we have a nested + Number count = (Number) bucketsOrNested.remove("doc_count"); + aggResult.setCount(count); + AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested); + aggResult.setNestedAggregations(nestedResults); + } else { + AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets); + aggResult.setBuckets(aggBuckets); + } - } + aggResults[index] = aggResult; + index++; + } + + return aggResults; + + } - private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets) - throws JsonProcessingException { - AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()]; - for (int i = 0; i < buckets.size(); i++) { - AggregationBucket aggBucket = new AggregationBucket(); - JSONObject bucketContent = (JSONObject) buckets.get(i); - Object key = bucketContent.remove("key"); - aggBucket.setKey(key); - Object formatted = bucketContent.remove("key_as_string"); - if (formatted != null) { - aggBucket.setFormattedKey((String) formatted); - } - Object count = bucketContent.remove("doc_count"); - if (count != null) { - aggBucket.setCount((Number) count); - } - bucketContent.remove("from"); - bucketContent.remove("from_as_string"); - bucketContent.remove("to"); - bucketContent.remove("to_as_string"); + private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets) throws JsonProcessingException { + AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()]; + for (int i = 0; i < buckets.size(); i++) { + AggregationBucket aggBucket = new AggregationBucket(); + JSONObject bucketContent = (JSONObject) buckets.get(i); + Object key = bucketContent.remove("key"); + aggBucket.setKey(key); + Object formatted = bucketContent.remove("key_as_string"); + if (formatted != null) { + aggBucket.setFormattedKey((String) formatted); + } + Object count = bucketContent.remove("doc_count"); + if (count != null) { + aggBucket.setCount((Number) count); + } + bucketContent.remove("from"); + bucketContent.remove("from_as_string"); + bucketContent.remove("to"); + bucketContent.remove("to_as_string"); - if (!bucketContent.entrySet().isEmpty()) { - // we have results from sub-aggregation - AggregationResult[] subResult = parseAggregationResults(bucketContent); - if (subResult != null) { - aggBucket.setSubAggregationResult(subResult); + if (!bucketContent.entrySet().isEmpty()) { + // we have results from sub-aggregation + AggregationResult[] subResult = parseAggregationResults(bucketContent); + if (subResult != null) { + aggBucket.setSubAggregationResult(subResult); + } + } + aggBuckets[i] = aggBucket; } - } - aggBuckets[i] = aggBucket; - } - return aggBuckets; - } + return aggBuckets; + } }