a19bd3f60066dd7237d8c7b28904b81c60a3b870
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / Formatter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.queryformats;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.stream.Stream;
32
33 import org.onap.aai.logging.LogFormatTools;
34 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
35 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
36
37 public class Formatter {
38
39     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Formatter.class);
40
41     protected JsonParser parser = new JsonParser();
42     protected final FormatMapper format;
43
44     public Formatter(FormatMapper format) {
45         this.format = format;
46     }
47
48     public JsonObject output(List<Object> queryResults) {
49
50         Stream<Object> stream;
51         JsonObject result = new JsonObject();
52         JsonArray body = new JsonArray();
53
54         if (this.format instanceof Count) {
55             JsonObject countResult;
56             try {
57                 countResult = format.formatObject(queryResults).orElseThrow(() -> new AAIFormatVertexException(""));
58                 body.add(countResult);
59             } catch (Exception e) {
60                 LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
61             }
62         } else {
63             if (queryResults.size() >= format.parallelThreshold()) {
64                 stream = queryResults.parallelStream();
65             } else {
66                 stream = queryResults.stream();
67             }
68
69             final boolean isParallel = stream.isParallel();
70
71             stream.map(o -> {
72                 try {
73                     return format.formatObject(o);
74                 } catch (AAIFormatVertexException e) {
75                     LOGGER.warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e));
76                 } catch (AAIFormatQueryResultFormatNotSupported e) {
77                     LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e));
78                 }
79
80                 return Optional.<JsonObject>empty();
81             }).filter(Optional::isPresent).map(Optional::get).forEach(json -> {
82                 if (isParallel) {
83                     synchronized (body) {
84                         body.add(json);
85                     }
86                 } else {
87                     body.add(json);
88                 }
89             });
90
91         }
92         result.add("results", body);
93         return result.getAsJsonObject();
94     }
95
96 }