2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Modifications Copyright (C) 2018 IBM.
8 * ===========================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END====================================================
23 package org.onap.aaf.auth.batch.helpers;
25 import java.util.Date;
26 import java.util.Iterator;
29 import java.util.TreeMap;
31 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
33 import com.datastax.driver.core.ResultSet;
34 import com.datastax.driver.core.Row;
35 import com.datastax.driver.core.Session;
37 public class LastNotified {
38 private Map<String,Date> lastNotified = new TreeMap<>();
39 private Session session;
40 public static final Date never = new Date(0);
42 public LastNotified(Session session) {
43 this.session = session;
46 public void add(Set<String> users) {
47 StringBuilder query = new StringBuilder();
50 for(String user : users) {
59 add(session.execute(query.toString()),lastNotified);
67 add(session.execute(query.toString()),lastNotified);
72 * Note: target_key CAN also contain a Pipe.
79 public Date lastNotified(String user, String target, String target_key) {
80 String key = user + '|' + target + '|' + target_key;
81 return lastNotified(key);
84 public Date lastNotified(String key) {
85 Date rv = lastNotified.get(key);
88 lastNotified.put(key, rv);
93 private Date add(ResultSet result, Map<String, Date> lastNotified) {
95 for(Iterator<Row> iter = result.iterator(); iter.hasNext();) {
97 String key = r.getString(0) + '|' +
98 r.getString(1) + '|' +
101 lastNotified.put(key, last=r.getTimestamp(3));
106 private void startQuery(StringBuilder query) {
107 query.append("SELECT user,target,key,last FROM authz.notified WHERE user in (");
110 private void endQuery(StringBuilder query) {
114 public void update(StringBuilder query,String user, String target, String key) {
115 query.append("UPDATE authz.notified SET last=dateof(now()) WHERE user='");
117 query.append("' AND target='");
118 query.append(target);
119 query.append("' AND key='");
124 public static String newKey(UserRoleDAO.Data urdd) {
125 return urdd.user + "|ur|" + urdd.role;