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