Improve Batches
[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.batch.helpers.Notification.TYPE;
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         
41         public LastNotified(Session session) {
42                 this.session = session;
43         }
44         
45         public void add(Set<String> users) {
46                 StringBuilder query = new StringBuilder();
47                 startNotifyQuery(query);
48                 int cnt = 0;
49         for(String user : users) {
50                 if(++cnt>1) {
51                         query.append(',');
52                 }
53                 query.append('\'');
54                 query.append(user);
55                 query.append('\'');
56                 if(cnt>=30) {
57                         endNotifyQuery(query, Notification.TYPE.OA);
58                         add(session.execute(query.toString()),lastNotified);
59                         query.setLength(0);
60                         startNotifyQuery(query);
61                         cnt=0;
62                 }
63         }
64         if(cnt>0) {
65                 endNotifyQuery(query, Notification.TYPE.OA);
66                         add(session.execute(query.toString()),lastNotified);
67         }
68         }
69
70         public Date lastNotified(String user) {
71                 return lastNotified.get(user);
72         }
73         
74         private void add(ResultSet result, Map<String, Date> lastNotified) {
75         for(Iterator<Row> iter = result.iterator(); iter.hasNext();) {
76                 Row r = iter.next();
77                 lastNotified.put(r.getString(0), r.getTimestamp(1));
78         }
79         }
80
81         private void startNotifyQuery(StringBuilder query) {
82                 query.append("SELECT user,last FROM authz.notify WHERE user in (");
83         }
84     
85     private void endNotifyQuery(StringBuilder query, TYPE oa) {
86         query.append(") AND type=");
87         query.append(oa.idx());
88         query.append(';');
89     }
90 }