09383c9af74e1fb771b17ec285cc10ceadf0793c
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / PathedURL.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.serialization.queryformats;
22
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import org.apache.tinkerpop.gremlin.structure.Vertex;
26 import org.openecomp.aai.db.props.AAIProperties;
27 import org.openecomp.aai.exceptions.AAIException;
28 import org.openecomp.aai.introspection.Introspector;
29 import org.openecomp.aai.introspection.Loader;
30 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
31 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
32 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
33
34 public final class PathedURL implements FormatMapper {
35
36         private final UrlBuilder urlBuilder;
37         private final JsonParser parser;
38         private final Loader loader;
39         
40         public PathedURL (Loader loader, UrlBuilder urlBuilder) throws AAIException {
41                 this.urlBuilder = urlBuilder;
42                 this.parser = new JsonParser();
43                 this.loader = loader;
44         }
45
46         @Override
47         public int parallelThreshold() {
48                 return 20;
49         }
50
51         @Override
52         public JsonObject formatObject(Object input) throws AAIFormatVertexException {
53                 Vertex v = (Vertex)input;
54                 try {
55                         final Introspector searchResult = this.loader.introspectorFromName("result-data");
56
57                         searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE));
58
59                         searchResult.setValue("resource-link", this.urlBuilder.pathed(v));
60                         final String json = searchResult.marshal(false);
61                         return this.parser.parse(json).getAsJsonObject();
62
63                 } catch (AAIUnknownObjectException e) {
64                         throw new RuntimeException("Fatal error - result-data does not exist!", e);
65                 }
66
67         }
68
69 }