Add new query format count and
[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 java.util.List;
25 import java.util.Optional;
26 import java.util.stream.Stream;
27
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonObject;
35 import com.google.gson.JsonParser;
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 = null;
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);
58                                 body.add(countResult);                  
59                         } catch (Exception e) {
60                                 LOGGER.warn("Failed to format result type of the query", 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 Optional.<JsonObject>of(format.formatObject(o));
74                                 } catch (AAIFormatVertexException e) {
75                                         LOGGER.warn("Failed to format vertex, returning a partial list", e);
76                                 } catch (AAIFormatQueryResultFormatNotSupported e) {
77                                         LOGGER.warn("Failed to format result type of the query", e);
78                                 }
79
80                                 return Optional.<JsonObject>empty();
81                         })
82                         .filter(Optional<JsonObject>::isPresent)
83                         .map(Optional<JsonObject>::get)
84                         .forEach(json -> {
85                                         if (isParallel) {
86                                                 synchronized (body) {
87                                                         body.add(json);
88                                                 }
89                                         } else {
90                                                 body.add(json);
91                                         }
92                         });
93
94                 }
95                 result.add("results", body);
96                 return result.getAsJsonObject();
97         }
98
99 }