Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / LastNotified.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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====================================================
21  *
22  */
23 package org.onap.aaf.auth.batch.helpers;
24
25 import java.util.Date;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
30
31 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
32
33 import com.datastax.driver.core.ResultSet;
34 import com.datastax.driver.core.Row;
35 import com.datastax.driver.core.Session;
36
37 public class LastNotified {
38         private Map<String,Date> lastNotified = new TreeMap<>();
39         private Session session;
40         private static final Date never = new Date(0);
41         
42         public LastNotified(Session session) {
43                 this.session = session;
44         }
45         
46         public void add(Set<String> users) {
47                 StringBuilder query = new StringBuilder();
48                 startQuery(query);
49                 int cnt = 0;
50         for(String user : users) {
51                 if(++cnt>1) {
52                         query.append(',');
53                 }
54                 query.append('\'');
55                 query.append(user);
56                 query.append('\'');
57                 if(cnt>=30) {
58                         endQuery(query);
59                         add(session.execute(query.toString()),lastNotified);
60                         query.setLength(0);
61                         startQuery(query);
62                         cnt=0;
63                 }
64         }
65         if(cnt>0) {
66                 endQuery(query);
67                         add(session.execute(query.toString()),lastNotified);
68         }
69         }
70
71         /**
72          * Note: target_key CAN also contain a Pipe.
73          * 
74          * @param user
75          * @param target
76          * @param target_key
77          * @return
78          */
79         public Date lastNotified(String user, String target, String target_key) {
80                 String key = user + '|' + target + '|' + target_key;
81                 return lastNotified(key);
82         }
83         
84         public Date lastNotified(String key) {
85                 Date rv = lastNotified.get(key);
86                 if(rv==null) {
87                         rv = never;
88                         lastNotified.put(key, rv);
89                 }
90                 return rv;
91         }
92         
93         private Date add(ResultSet result, Map<String, Date> lastNotified) {
94                 Date last = null;
95         for(Iterator<Row> iter = result.iterator(); iter.hasNext();) {
96                 Row r = iter.next();
97                 String key = r.getString(0) + '|' +
98                                      r.getString(1) + '|' +
99                                      r.getString(2);
100                 
101                 lastNotified.put(key, last=r.getTimestamp(3));
102         }
103         return last;
104         }
105
106         private void startQuery(StringBuilder query) {
107                 query.append("SELECT user,target,key,last FROM authz.notified WHERE user in (");
108         }
109
110         private void endQuery(StringBuilder query) {
111                 query.append(");");
112         }
113
114         public void update(StringBuilder query,String user, String target, String key) {
115                 query.append("UPDATE authz.notified SET last=dateof(now()) WHERE user='");
116                 query.append(user);
117                 query.append("' AND target='");
118                 query.append(target);
119                 query.append("' AND key='");
120                 query.append(key);
121                 query.append("';\n");
122         }
123
124         public static String newKey(UserRoleDAO.Data urdd) {
125                 return urdd.user + "|ur|" + urdd.role;
126         }
127
128 }