2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.aai.serialization.queryformats;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
29 import org.json.JSONArray;
30 import org.json.simple.JSONObject;
31 import org.json.simple.parser.JSONParser;
32 import org.onap.aai.logging.LogFormatTools;
33 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
34 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36 import javax.ws.rs.core.MultivaluedMap;
37 import java.util.Iterator;
38 import java.util.List;
40 import java.util.Optional;
41 import java.util.stream.Stream;
43 public class Formatter {
45 private static final Logger LOGGER = LoggerFactory.getLogger(Formatter.class);
47 protected final FormatMapper format;
48 protected MultivaluedMap<String, String> params;
50 public Formatter(FormatMapper format) {
54 public Formatter(FormatMapper format, MultivaluedMap<String, String> params) {
59 public JsonObject output(List<Object> queryResults, Map<String, List<String>> properties) {
63 if (this.format instanceof Count) {
64 JsonObject countResult;
65 body = new JsonArray();
67 countResult = format.formatObject(queryResults).orElseThrow(() -> new AAIFormatVertexException(""));
68 body.add(countResult);
69 } catch (Exception e) {
70 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
72 } else if (this.format instanceof LifecycleFormat) {
73 LifecycleFormat lifecycleFormat = (LifecycleFormat) format;
74 body = lifecycleFormat.process(queryResults);
75 } else if (this.format instanceof Aggregate) {
76 Aggregate aggregateFormat = (Aggregate) format;
77 body = aggregateFormat.process(queryResults, properties);
78 JsonObject result = new JsonObject();
79 if (body != null && body.size() > 0) {
80 result.add("results", (body.get(0)).getAsJsonObject().get("results"));
85 body = new JsonArray();
86 Stream<Object> stream;
87 if (queryResults.size() >= format.parallelThreshold()) {
88 stream = queryResults.parallelStream();
90 stream = queryResults.stream();
93 final boolean isParallel = stream.isParallel();
97 if (properties!= null && !properties.isEmpty()){
98 return format.formatObject(o, properties);
100 return format.formatObject(o);
102 } catch (AAIFormatVertexException e) {
103 LOGGER.warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
104 } catch (AAIFormatQueryResultFormatNotSupported e) {
105 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
108 return Optional.<JsonObject>empty();
109 }).filter(Optional::isPresent)
113 synchronized (body) {
123 if (params !=null && params.containsKey("as-tree")) {
124 String isAsTree = params.get("as-tree").get(0);
125 if (isAsTree != null && isAsTree.equalsIgnoreCase("true")
126 && body != null && body.size() != 0) {
127 JsonObject jsonObjectBody = body.get(0).getAsJsonObject();
128 if (jsonObjectBody != null && jsonObjectBody.size() > 0) {
129 return body.get(0).getAsJsonObject();
133 JsonObject result = new JsonObject();
134 result.add("results", body);
135 return result.getAsJsonObject();
139 public JsonObject output(List<Object> queryResults) {
140 return output(queryResults, null);