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