Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / DataCopy.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
21 package org.onap.aai.introspection.sideeffect;
22
23 import org.apache.tinkerpop.gremlin.structure.Vertex;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.introspection.Introspector;
26 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
27 import org.onap.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException;
28 import org.onap.aai.parsers.query.QueryParser;
29 import org.onap.aai.restcore.util.URITools;
30 import org.onap.aai.schema.enums.PropertyMetadata;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
33
34 import javax.ws.rs.core.MultivaluedMap;
35 import java.io.UnsupportedEncodingException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map.Entry;
41 import java.util.Objects;
42 import java.util.Optional;
43
44 public class DataCopy extends SideEffect {
45
46     public DataCopy(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
47         super(obj, self, dbEngine, serializer);
48     }
49
50     @Override
51     protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
52             throws URISyntaxException, UnsupportedEncodingException, AAIException {
53         if (completeUri.isPresent()) {
54             URI uri = new URI(completeUri.get());
55             MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
56             QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
57             List<Vertex> results = uriQuery.getQueryBuilder().toList();
58             Introspector resultObj = this.latestLoader.introspectorFromName(uriQuery.getResultType());
59             if (results.size() == 1) {
60                 serializer.dbToObject(Collections.singletonList(results.get(0)), resultObj, 0, true, "false");
61                 try {
62                     obj.setValue(entry.getKey(), Objects.requireNonNull(resultObj.getValue(uri.getFragment()),
63                             uri.getFragment() + " was null"));
64                 } catch (NullPointerException e) {
65                     throw new AAIMissingRequiredPropertyException(
66                             "property " + uri.getFragment() + " not found at " + uri);
67                 }
68             } else {
69                 if (results.isEmpty()) {
70                     throw new AAIException("AAI_6114", "object located at " + uri + " not found");
71                 } else {
72                     throw new AAIMultiplePropertiesException(
73                             "multiple values of " + entry.getKey() + " found when searching " + uri);
74                 }
75             }
76         } //else skip processing because no required properties were specified
77     }
78
79     @Override
80     protected PropertyMetadata getPropertyMetadata() {
81         return PropertyMetadata.DATA_COPY;
82     }
83
84     @Override
85     protected boolean replaceWithWildcard() {
86         return false;
87     }
88
89 }