[SDC-29] Amdocs OnBoard 1707 initial commit.
[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  * ============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 package org.openecomp.sdc.activitylog.dao.impl;
21
22 import com.datastax.driver.mapping.Mapper;
23 import com.datastax.driver.mapping.Result;
24 import com.datastax.driver.mapping.annotations.Accessor;
25 import com.datastax.driver.mapping.annotations.Query;
26 import org.openecomp.core.dao.impl.CassandraBaseDao;
27 import org.openecomp.core.nosqldb.api.NoSqlDb;
28 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
29 import org.openecomp.sdc.activitylog.dao.ActivityLogDao;
30 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
31
32 import java.util.Collection;
33 import java.util.Date;
34
35 public class ActivityLogDaoCassandraImpl extends CassandraBaseDao<ActivityLogEntity> implements ActivityLogDao{
36     private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
37     private static final Mapper<ActivityLogEntity> mapper =
38             noSqlDb.getMappingManager().mapper(ActivityLogEntity.class);
39     private static final ActivityLogAccessor accessor =
40             noSqlDb.getMappingManager().createAccessor(ActivityLogAccessor.class);
41
42     @Override
43     protected Mapper<ActivityLogEntity> getMapper() {
44         return mapper;
45     }
46
47     @Override
48     protected Object[] getKeys(ActivityLogEntity entity) {
49         return new Object[0];
50     }
51
52     @Override
53     public Collection<ActivityLogEntity> list(ActivityLogEntity entity) {
54         return accessor.list().all();
55     }
56
57     @Override
58     public void create(ActivityLogEntity activityLogEntity) {
59         accessor.create(activityLogEntity.getItemId(), activityLogEntity.getVersionId(),activityLogEntity.getId(),
60                 activityLogEntity.getType(),activityLogEntity.getUser(), activityLogEntity.getTimestamp(), activityLogEntity.isSuccess(),
61                 activityLogEntity.getMessage(), activityLogEntity.getComment());
62     }
63
64     @Override
65     public Collection<ActivityLogEntity> getActivityLogListForItem(String itemId, String versionId) {
66         return accessor.getForItem(itemId, versionId).all();
67     }
68
69
70     @Accessor
71     interface ActivityLogAccessor {
72         @Query("select item_id, version_id, activity_id, type, user, timestamp, success, message, comment"
73                         + " from activity_log")
74         Result<ActivityLogEntity> list();
75
76         @Query("select item_id, version_id, activity_id, type, user, timestamp, success, message, comment"
77                         + " from activity_log where item_id=? and version_id=?")
78         Result<ActivityLogEntity> getForItem(String itemId, String versionId);
79
80         @Query("insert into activity_log " +
81                 " (item_id, version_id, activity_id, type, user, timestamp, success, message, comment)" +
82                 " values (?,?,?,?,?,?,?,?,?)")
83         Result<ActivityLogEntity> create(String itemId, String versionId, String id, String type,
84                                          String user, Date timestamp, boolean success,
85                                          String message, String comment);
86     }
87 }