Moving Historical Queries to spring boot
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / query / ChameleonResponseBuiler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.datarouter.query;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response.Status;
28
29 import org.apache.camel.Exchange;
30 import org.onap.aai.datarouter.exception.DataRouterException;
31
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34 import com.google.gson.JsonParser;
35
36 public class ChameleonResponseBuiler  {
37
38   private static final String SOURCE = "source";
39   private static final String TARGET = "target";
40   private static final String TYPE = "type";
41
42   public static String buildEntity(String chameleonResponse, String id) throws DataRouterException{
43     
44     JsonParser parser = new JsonParser();
45     JsonObject root = parser.parse(chameleonResponse).getAsJsonObject();
46     JsonObject champResponse = new JsonObject();
47     if (!root.has(TYPE)) {
48       throw new DataRouterException("Chameloen response does not have type : "+chameleonResponse , Status.BAD_REQUEST);
49     }
50     champResponse.addProperty("key", id);
51     champResponse.addProperty(TYPE, root.get(TYPE).getAsString());
52     if (root.has(SOURCE)) {
53       champResponse.add(SOURCE, root.get(SOURCE));
54     }
55     if (root.has(TARGET)) {
56       champResponse.add(TARGET, root.get(TARGET));
57     }
58
59     JsonObject props = new JsonObject();
60     List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
61         root.getAsJsonObject().entrySet());
62     for (Map.Entry<String, JsonElement> e : entries) {
63       if (!TYPE.equals(e.getKey()) && !SOURCE.equals(e.getKey()) && !TARGET.equals(e.getKey())) {
64         props.addProperty(e.getKey(), e.getValue().getAsString());
65       }
66
67     }
68     
69     champResponse.add("properties", props);
70
71     return champResponse.toString();
72     
73   }
74   
75  
76   public static String buildObjectRelationship(String chameleonResponse, String id){
77     //TODO: implement when chameleon supports this query     
78     return "[]";
79   }
80   public static String buildCollection(String chameleonResponse){
81     //TODO: implement when chameleon supports this query   
82     return "[]";
83     
84   }
85   
86  
87 }