Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-core / src / main / java / org / openecomp / sdc / notification / dao / impl / LastNotificationDaoCassandraImpl.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
21 package org.openecomp.sdc.notification.dao.impl;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Row;
25 import com.datastax.driver.mapping.Mapper;
26 import com.datastax.driver.mapping.Result;
27 import com.datastax.driver.mapping.annotations.Accessor;
28 import com.datastax.driver.mapping.annotations.Query;
29 import org.openecomp.core.dao.impl.CassandraBaseDao;
30 import org.openecomp.core.nosqldb.api.NoSqlDb;
31 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
32 import org.openecomp.sdc.notification.dao.LastNotificationDao;
33 import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity;
34
35 import java.util.Collection;
36 import java.util.UUID;
37
38
39 public class LastNotificationDaoCassandraImpl extends CassandraBaseDao<LastSeenNotificationEntity> implements LastNotificationDao {
40
41   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
42   private static final Mapper<LastSeenNotificationEntity> mapper =
43       noSqlDb.getMappingManager().mapper(LastSeenNotificationEntity.class);
44   private static final LastNotificationAccessor accessor =
45       noSqlDb.getMappingManager().createAccessor(LastNotificationAccessor.class);
46
47   @Override
48   protected Mapper<LastSeenNotificationEntity> getMapper() {
49     return mapper;
50   }
51
52   @Override
53   protected Object[] getKeys(LastSeenNotificationEntity entity) {
54     return new Object[]{entity.getOwnerId()};
55   }
56
57   @Override
58   public Collection<LastSeenNotificationEntity> list(LastSeenNotificationEntity entity) {
59     return accessor.list(entity.getOwnerId()).all();
60   }
61
62   @Override
63   public UUID getOwnerLastEventId(String ownerId) {
64     ResultSet ownerLastEventId = accessor.getOwnerLastEventId(ownerId);
65     Row one = ownerLastEventId.one();
66     return one != null ? one.getUUID("event_id") : null;
67   }
68
69   @Override
70   public void persistOwnerLastEventId(String ownerId, UUID eventId) {
71             accessor.updateOwnerLastEventId(eventId, ownerId);
72   }
73
74   @Accessor
75   interface LastNotificationAccessor {
76
77     @Query("select * from last_notification where owner_id=?")
78     Result<LastSeenNotificationEntity> list(String ownerId);
79
80     @Query("select event_id from last_notification where owner_id=?")
81     ResultSet getOwnerLastEventId(String ownerId);
82
83     @Query("update last_notification set event_id=? where owner_id=?")
84     ResultSet updateOwnerLastEventId(UUID eventId, String ownerId);
85   }
86
87 }