dc7d5cb40f41f4269e793db60d2bc148a7f093e8
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.core.tools.store;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.mapping.Mapper;
25 import com.datastax.driver.mapping.Result;
26 import com.datastax.driver.mapping.annotations.Accessor;
27 import com.datastax.driver.mapping.annotations.Query;
28 import com.datastax.driver.mapping.annotations.QueryParameters;
29 import com.google.common.collect.Sets;
30 import org.openecomp.core.nosqldb.api.NoSqlDb;
31 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
32 import org.openecomp.core.tools.store.zusammen.datatypes.ElementEntity;
33 import org.openecomp.core.tools.store.zusammen.datatypes.VersionEntity;
34
35 import java.util.Date;
36 import java.util.Set;
37
38 public class VersionCassandraLoader {
39
40     private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
41     private static Mapper<VersionEntity> mapper = noSqlDb.getMappingManager().mapper(VersionEntity.class);
42     private static VersionAccessor accessor = noSqlDb.getMappingManager().createAccessor(VersionAccessor.class);
43
44     public void insertElementToVersion(ElementEntity elementEntity) {
45         accessor.addElements(Sets.newHashSet(elementEntity.getElementId()), elementEntity.getSpace(), elementEntity.getItemId(), elementEntity.getVersionId());
46     }
47
48     public void insertVersion(VersionEntity versionEntity) {
49         accessor.insertVersion(               versionEntity.getSpace(),
50                 versionEntity.getItemId(),
51                 versionEntity.getVersionId(),
52                 versionEntity.getBaseVersionId(),
53                 versionEntity.getCreationTime(),
54                 versionEntity.getInfo(),
55                 versionEntity.getModificationTime(),
56                 versionEntity.getRelations());
57     }
58
59
60     public Result<VersionEntity> list() {
61         return accessor.getAll();
62     }
63
64     public ResultSet listItemVersion() { return accessor.getAllItemVersion();}
65
66     @Accessor
67     interface VersionAccessor {
68
69         @Query("UPDATE zusammen_dox.version_elements SET element_ids=element_ids+? " +
70                 "WHERE space=? AND item_id=? AND version_id=?")
71         void addElements(Set<String> elementIds, String space, String itemId, String versionId);
72
73         @Query("insert into zusammen_dox.version (space,item_id,version_id,base_version_id,creation_time,info,modification_time,relations) values (?,?,?,?,?,?,?,?)")
74         void insertVersion(String space, String itemId, String versionId, String baseVersionId, Date createTime, String info, Date modificationTime, String relations);
75
76
77         @Query("select * from zusammen_dox.version ")
78         @QueryParameters(fetchSize = 400)
79         Result<VersionEntity> getAll();
80
81         @Query("select space,item_id,version_id from zusammen_dox.version ")
82         ResultSet getAllItemVersion();
83     }
84 }