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