- private Map<String,Date> lastNotified = new TreeMap<>();
- private Session session;
-
- public LastNotified(Session session) {
- this.session = session;
- }
-
- public void add(Set<String> users) {
- StringBuilder query = new StringBuilder();
- startNotifyQuery(query);
- int cnt = 0;
- for(String user : users) {
- if(++cnt>1) {
- query.append(',');
- }
- query.append('\'');
- query.append(user);
- query.append('\'');
- if(cnt>=30) {
- endNotifyQuery(query, Notification.TYPE.OA);
- add(session.execute(query.toString()),lastNotified);
- query.setLength(0);
- startNotifyQuery(query);
- cnt=0;
- }
- }
- if(cnt>0) {
- endNotifyQuery(query, Notification.TYPE.OA);
- add(session.execute(query.toString()),lastNotified);
- }
- }
-
- public Date lastNotified(String user) {
- return lastNotified.get(user);
- }
-
- private void add(ResultSet result, Map<String, Date> lastNotified) {
- for(Iterator<Row> iter = result.iterator(); iter.hasNext();) {
- Row r = iter.next();
- lastNotified.put(r.getString(0), r.getTimestamp(1));
- }
- }
-
- private void startNotifyQuery(StringBuilder query) {
- query.append("SELECT user,last FROM authz.notify WHERE user in (");
- }
-
- private void endNotifyQuery(StringBuilder query, TYPE oa) {
- query.append(") AND type=");
- query.append(oa.idx());
- query.append(';');
+ private Map<String,Date> lastNotifiedVar = new TreeMap<>();
+ private Session session;
+ public static final Date NEVER = new Date(0);
+ private static final String SELECT = "SELECT user,target,key,last FROM authz.notified";
+
+ public LastNotified(Session session) {
+ this.session = session;
+ }
+
+ public void add(Set<String> users) {
+ StringBuilder query = new StringBuilder();
+ startQuery(query);
+ int cnt = 0;
+ for(String user : users) {
+ if(++cnt > 1) {
+ query.append(',');
+ }
+ query.append('\'');
+ query.append(user);
+ query.append('\'');
+ if(cnt >= 30) {
+ endQuery(query);
+ add(session.execute(query.toString()),lastNotifiedVar, (x,y) -> false);
+ query.setLength(0);
+ startQuery(query);
+ cnt = 0;
+ }
+ }
+ if(cnt>0) {
+ endQuery(query);
+ add(session.execute(query.toString()),lastNotifiedVar, (x,y) -> false);
+ }
+ }
+
+ /**
+ * Note: target_key CAN also contain a Pipe.
+ *
+ * @param user
+ * @param target
+ * @param targetkey
+ * @return
+ */
+ public Date lastNotified(String user, String target, String targetkey) {
+ String key = user + '|' + target + '|' + (targetkey==null?"":targetkey);
+ return lastNotified(key);
+ }
+
+ public Date lastNotified(String key) {
+ Date d = lastNotifiedVar.get(key);
+ return d==null?NEVER:d;
+ }
+
+ private Date add(ResultSet result, Map<String, Date> lastNotified, MarkDelete md) {
+ Date last = null;
+ Row r;
+ for(Iterator<Row> iter = result.iterator(); iter.hasNext();) {
+ r = iter.next();
+ String ttKey = r.getString(1) + '|' +
+ r.getString(2);
+
+ String fullKey = r.getString(0) + '|' +
+ ttKey;
+ last=r.getTimestamp(3);
+ if(!md.process(fullKey, last)) {
+ lastNotified.put(fullKey, last);
+ Date d = lastNotified.get(ttKey);
+ if(d==null || d.after(last)) { // put most recent, if different
+ lastNotified.put(ttKey, last);
+ }
+ }
+ }
+ return last;
+ }
+
+ @FunctionalInterface
+ private interface MarkDelete {
+ boolean process(String fullKey, Date last);
+ }
+
+ private void startQuery(StringBuilder query) {
+ query.append(SELECT + " WHERE user in (");