fed1eb7fe92aa69da3514ead3d7e290c5c78a816
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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.openecomp.sdc.be.components.impl;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
30 import java.io.IOException;
31 import java.lang.reflect.Type;
32 import org.onap.sdc.gab.GABService;
33 import org.onap.sdc.gab.GABServiceImpl;
34 import org.onap.sdc.gab.model.GABQuery;
35 import org.onap.sdc.gab.model.GABResult;
36 import org.onap.sdc.gab.model.GABResults;
37
38 @org.springframework.stereotype.Component
39 public class GenericArtifactBrowserBusinessLogic extends BaseBusinessLogic {
40
41     private GABService gabService;
42
43     public GenericArtifactBrowserBusinessLogic() {
44         gabService = new GABServiceImpl();
45     }
46
47     public String searchFor(GABQuery gabQuery) throws IOException {
48         GABResults gabResults = gabService.searchFor(gabQuery);
49         return createGsonForGABResult().toJson(gabResults);
50     }
51
52     private Gson createGsonForGABResult(){
53         return new GsonBuilder().setPrettyPrinting()
54             .registerTypeAdapter(GABResult.class, new GABResultSerializer())
55             .registerTypeAdapter(GABResults.class, new GABResultsSerializer())
56             .create();
57     }
58
59     private class GABResultsSerializer implements JsonSerializer<GABResults> {
60         @Override
61         public JsonElement serialize(GABResults gabResults, Type type,
62             JsonSerializationContext jsonSerializationContext) {
63             JsonObject result = new JsonObject();
64             JsonArray jsonArray = new JsonArray();
65             gabResults.getRows().stream().map(jsonSerializationContext::serialize).forEach(jsonArray::add);
66             result.add("data", jsonArray);
67             return result;
68         }
69     }
70
71     private class GABResultSerializer implements JsonSerializer<GABResult> {
72         @Override
73         public JsonElement serialize(GABResult gabResult, Type type, JsonSerializationContext jsonSerializationContext) {
74             JsonObject result = new JsonObject();
75             gabResult.getEntries().forEach(entry -> result.addProperty(entry.getPath(), String.valueOf(entry.getData())));
76             return result;
77         }
78     }
79
80 }