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