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