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