728471491b36be95178c0cc652b666292bc98a29
[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 package org.onap.aai.serialization.queryformats;
21
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;
30
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.stream.Stream;
34
35 public class Formatter {
36
37         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Formatter.class);
38
39         protected JsonParser parser = new JsonParser();
40         protected final FormatMapper format;
41
42         public Formatter(FormatMapper format) {
43                 this.format = format;
44         }
45
46         public JsonObject output(List<Object> queryResults) {
47
48                 Stream<Object> stream;
49                 JsonObject result = new JsonObject();
50                 JsonArray body = new JsonArray();
51
52                 if (this.format instanceof Count) {
53                         JsonObject countResult;
54                         try {
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));
59                         }
60                 } else {
61                         if (queryResults.size() >= format.parallelThreshold()) {
62                                 stream = queryResults.parallelStream();
63                         } else {
64                                 stream = queryResults.stream();
65                         }
66
67                         final boolean isParallel = stream.isParallel();
68
69                         stream.map(o -> {
70                                 try {
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));
76                                 }
77
78                                 return Optional.<JsonObject>empty();
79                         })
80                         .filter(Optional::isPresent)
81                         .map(Optional::get)
82                         .forEach(json -> {
83                                         if (isParallel) {
84                                                 synchronized (body) {
85                                                         body.add(json);
86                                                 }
87                                         } else {
88                                                 body.add(json);
89                                         }
90                         });
91
92                 }
93                 result.add("results", body);
94                 return result.getAsJsonObject();
95         }
96
97 }