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;
28 import org.onap.aai.logging.LogFormatTools;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
30 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.stream.Stream;
36 public class Formatter {
38 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Formatter.class);
40 protected JsonParser parser = new JsonParser();
41 protected final FormatMapper format;
43 public Formatter(FormatMapper format) {
47 public JsonObject output(List<Object> queryResults) {
49 Stream<Object> stream = null;
50 JsonObject result = new JsonObject();
51 JsonArray body = new JsonArray();
53 if (this.format instanceof Count) {
54 JsonObject countResult;
56 countResult = format.formatObject(queryResults);
57 body.add(countResult);
58 } catch (Exception e) {
59 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
62 if (queryResults.size() >= format.parallelThreshold()) {
63 stream = queryResults.parallelStream();
65 stream = queryResults.stream();
68 final boolean isParallel = stream.isParallel();
72 return Optional.<JsonObject>of(format.formatObject(o));
73 } catch (AAIFormatVertexException e) {
74 LOGGER.warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
75 } catch (AAIFormatQueryResultFormatNotSupported e) {
76 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
79 return Optional.<JsonObject>empty();
81 .filter(Optional<JsonObject>::isPresent)
82 .map(Optional<JsonObject>::get)
94 result.add("results", body);
95 return result.getAsJsonObject();