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=========================================================
20 package org.onap.aai.serialization.queryformats;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import org.onap.aai.logging.LogFormatTools;
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.stream.Stream;
35 public class Formatter {
37 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Formatter.class);
39 protected JsonParser parser = new JsonParser();
40 protected final FormatMapper format;
42 public Formatter(FormatMapper format) {
46 public JsonObject output(List<Object> queryResults) {
48 Stream<Object> stream;
49 JsonObject result = new JsonObject();
50 JsonArray body = new JsonArray();
52 if (this.format instanceof Count) {
53 JsonObject countResult;
55 countResult = format.formatObject(queryResults).orElseThrow(() -> new AAIFormatVertexException(""));
56 body.add(countResult);
57 } catch (Exception e) {
58 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
61 if (queryResults.size() >= format.parallelThreshold()) {
62 stream = queryResults.parallelStream();
64 stream = queryResults.stream();
67 final boolean isParallel = stream.isParallel();
71 return format.formatObject(o);
72 } catch (AAIFormatVertexException e) {
73 LOGGER.warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
74 } catch (AAIFormatQueryResultFormatNotSupported e) {
75 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
78 return Optional.<JsonObject>empty();
80 .filter(Optional::isPresent)
93 result.add("results", body);
94 return result.getAsJsonObject();