Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / JanusGraphDBEngine.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.serialization.engines;
22
23 import org.apache.tinkerpop.gremlin.structure.Vertex;
24 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
25 import org.onap.aai.introspection.Loader;
26 import org.onap.aai.serialization.db.JanusGraphSingleton;
27
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 public class JanusGraphDBEngine extends TransactionalGraphEngine {
33
34     /**
35      * Instantiates a new JanusGraph DB engine.
36      *
37      * @param style the style
38      * @param loader the loader
39      */
40     public JanusGraphDBEngine(QueryStyle style, Loader loader) {
41         super(style, loader, JanusGraphSingleton.getInstance());
42     }
43
44     /**
45      * Instantiates a new JanusGraph DB engine.
46      *
47      * @param style the style
48      * @param loader the loader
49      * @param connect the connect
50      */
51     public JanusGraphDBEngine(QueryStyle style, Loader loader, boolean connect) {
52         super(style, loader);
53         if (connect) {
54             this.singleton = JanusGraphSingleton.getInstance();
55         }
56     }
57
58     /**
59      * {@inheritDoc}
60      */
61     @Override
62     public boolean setListProperty(Vertex v, String name, List<?> objs) {
63
64         // clear out list full replace style
65
66         Iterator<VertexProperty<Object>> iterator = v.properties(name);
67         while (iterator.hasNext()) {
68             iterator.next().remove();
69         }
70         if (objs != null) {
71             for (Object obj : objs) {
72                 v.property(name, obj);
73             }
74         }
75         return true;
76     }
77
78     /**
79      * {@inheritDoc}
80      */
81     @Override
82     public List<Object> getListProperty(Vertex v, String name) {
83
84         List<Object> result = new ArrayList<>();
85
86         Iterator<VertexProperty<Object>> iterator = v.properties(name);
87
88         while (iterator.hasNext()) {
89             result.add(iterator.next().value());
90         }
91
92         if (result.isEmpty()) {
93             result = null;
94         }
95
96         return result;
97
98     }
99
100 }