re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-activity-log-lib / openecomp-sdc-activity-log-core / src / main / java / org / openecomp / sdc / activitylog / dao / impl / ActivityLogDaoCassandraImpl.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdc.activitylog.dao.impl;
17
18 import com.datastax.driver.extras.codecs.enums.EnumNameCodec;
19 import com.datastax.driver.mapping.Mapper;
20 import com.datastax.driver.mapping.MappingManager;
21 import com.datastax.driver.mapping.Result;
22 import com.datastax.driver.mapping.annotations.Accessor;
23 import com.datastax.driver.mapping.annotations.Query;
24 import org.openecomp.core.dao.impl.CassandraBaseDao;
25 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
26 import org.openecomp.sdc.activitylog.dao.ActivityLogDao;
27 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
28 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
29
30 import java.util.Collection;
31
32 public class ActivityLogDaoCassandraImpl extends CassandraBaseDao<ActivityLogEntity>
33     implements ActivityLogDao {
34
35   private static final Mapper<ActivityLogEntity> mapper;
36   private static final ActivityLogAccessor accessor;
37
38   static {
39     MappingManager mappingManager = NoSqlDbFactory.getInstance().createInterface().getMappingManager();
40     mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry()
41                   .register(new EnumNameCodec<>(ActivityType.class));
42
43     mapper = mappingManager.mapper(ActivityLogEntity.class);
44     accessor = mappingManager.createAccessor(ActivityLogAccessor.class);
45   }
46
47   @Override
48   protected Mapper<ActivityLogEntity> getMapper() {
49     return mapper;
50   }
51
52   @Override
53   protected Object[] getKeys(ActivityLogEntity entity) {
54     return new Object[]{entity.getItemId(), entity.getVersionId(), entity.getId()};
55   }
56
57   @Override
58   public Collection<ActivityLogEntity> list(ActivityLogEntity entity) {
59     return accessor.listByItemVersion(entity.getItemId(), entity.getVersionId()).all();
60   }
61
62   @Accessor
63   interface ActivityLogAccessor {
64
65     @Query("select * from activity_log where item_id=? and version_id=?")
66     Result<ActivityLogEntity> listByItemVersion(String itemId, String versionId);
67   }
68 }