Merge "major-Extract the assignment out of this expression"
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / Cred.java
index f566933..6f23c51 100644 (file)
@@ -3,6 +3,7 @@
  * org.onap.aaf
  * ===========================================================================
  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 IBM.
  * ===========================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +22,7 @@
 
 package org.onap.aaf.auth.batch.helpers;
 
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.GregorianCalendar;
@@ -79,29 +81,29 @@ public class Cred  {
          * @return
          */
         public List<Note> notes() {
-               return notes;
+            return notes;
         }
         
         public void addNote(int level, String note) {
-               if(notes==null) {
-                       notes=new ArrayList<>();
-               
-               notes.add(new Note(level,note));
+            if(notes==null) {
+                notes=new ArrayList<>();
+            } 
+            notes.add(new Note(level,note));
         }
         
         public String toString() {
-               return expires.toString() + ": " + type + ' ' + tag;
+            return expires.toString() + ": " + type + ' ' + tag;
         }
     }
     
     public static class Note {
-       public final int level;
-       public final String note;
-       
-       public Note(int level, String note) {
-               this.level = level;
-               this.note = note;
-       }
+        public final int level;
+        public final String note;
+        
+        public Note(int level, String note) {
+            this.level = level;
+            this.note = note;
+        }
     }
     public Date last(final int ... types) {
         Date last = null;
@@ -139,7 +141,7 @@ public class Cred  {
     }
 
     public static void loadOneNS(Trans trans, Session session, String ns,int ... types ) {
-        load(trans, session,"select id, type, expires, other, writetime(cred), tag from authz.cred WHERE ns='" + ns + "';");
+        load(trans, session,"select id, type, expires, other, writetime(cred), tag from authz.cred WHERE ns='" + ns + "';", types);
     }
 
     private static void load(Trans trans, Session session, String query, int ...types) {
@@ -168,7 +170,7 @@ public class Cred  {
                         boolean hastype = false;
                         for (int t : types) {
                             if (t==type) {
-                               hastype=true;
+                                hastype=true;
                                 break;
                             }
                         }
@@ -177,7 +179,7 @@ public class Cred  {
                         }
                     }
                     add(row.getString(0), row.getInt(1),row.getTimestamp(2),row.getInt(3),row.getLong(4),
-                               row.getString(5));
+                            row.getString(5));
                 }
             } finally {
                 tt.done();
@@ -188,13 +190,13 @@ public class Cred  {
     }
 
     public static void add(
-               final String id, 
-               final int type,
-               final Date timestamp,
-               final int other,
-               final long written,
-               final String tag
-               ) {
+            final String id, 
+            final int type,
+            final Date timestamp,
+            final int other,
+            final long written,
+            final String tag
+            ) {
         Cred cred = data.get(id);
         if (cred==null) {
             cred = new Cred(id);
@@ -202,9 +204,10 @@ public class Cred  {
         }
         cred.instances.add(new Instance(type, timestamp, other, written/1000,tag));
         
-        List<Cred> lscd = byNS.get(cred.ns);
+        List<Cred> lscd = byNS.get(cred.ns);   
         if (lscd==null) {
-            byNS.put(cred.ns, (lscd=new ArrayList<>()));
+               lscd=new ArrayList<>();
+            byNS.put(cred.ns,lscd);
         }
         boolean found = false;
         for (Cred c : lscd) {
@@ -216,10 +219,10 @@ public class Cred  {
         if (!found) {
             lscd.add(cred);
         }
-       }
+    }
 
 
-       /** 
+    /** 
      * Count entries in Cred data.
      * Note, as opposed to other methods, need to load the whole cred table for the Types.
      * @param numbuckets 
@@ -270,11 +273,9 @@ public class Cred  {
         public void inc(int type, Date start, Date expires) {
             for (int i=0;i<dates.length-1;++i) {
                 if (start.before(dates[i])) {
-                    if (type==CredDAO.CERT_SHA256_RSA) {
-                        if (start.after(dates[i+1])) {
+                    if ((type==CredDAO.CERT_SHA256_RSA)&&(start.after(dates[i+1]))) {
                             ++x509Added[i];
-                        }
-                    }
+                         }
                     if (expires.after(dates[i])) {
                         switch(type) {
                             case CredDAO.RAW:
@@ -306,28 +307,34 @@ public class Cred  {
     }
     
     public void row(final CSV.Writer csvw, final Instance inst) {
-       csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
-                       inst.expires.getTime(),inst.tag);
+        csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
+                inst.expires.getTime(),inst.tag);
     }
 
     public void row(final CSV.Writer csvw, final Instance inst, final String reason) {
-       csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
-                       inst.expires.getTime(),inst.tag,reason);
+        csvw.row("cred",id,ns,Integer.toString(inst.type),Chrono.dateOnlyStamp(inst.expires),
+                inst.expires.getTime(),inst.tag,reason);
     }
 
-
+    static SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+SSSS");
     public static void batchDelete(StringBuilder sb, List<String> row) {
-       sb.append("DELETE from authz.cred WHERE id='");
-       sb.append(row.get(1));
-       sb.append("' AND type=");
-       sb.append(Integer.parseInt(row.get(3)));
-       // Note: We have to work with long, because Expires is part of Key... can't easily do date.
-       sb.append(" AND expires=dateof(maxtimeuuid(");
-       sb.append(row.get(5));
-       sb.append("));\n");
-       }
+        Long l = Long.parseLong(row.get(5));
+        String date = sdf.format(new Date(l));
+        sb.append("DELETE from authz.cred WHERE id='");
+        sb.append(row.get(1));
+        sb.append("' AND type=");
+        sb.append(Integer.parseInt(row.get(3)));
+        // Note: We have to work with long, because Expires is part of Key... can't easily do date.
+        sb.append(" AND expires='");
+        sb.append(date);
+        sb.append("';\n");
+//        sb.append(" AND expires=dateof(maxtimeuuid(");
+//        sb.append(row.get(5));
+//        sb.append("));\n");
+        
+    }
 
-       public String toString() {
+    public String toString() {
         StringBuilder sb = new StringBuilder(id);
         sb.append('[');
         for (Instance i : instances) {
@@ -358,24 +365,24 @@ public class Cred  {
     }
 
 
-       public static String histSubject(List<String> row) {
-               return row.get(1);
-       }
+    public static String histSubject(List<String> row) {
+        return row.get(1);
+    }
 
 
-       public static String histMemo(String fmt, String orgName, List<String> row) {
-               String reason;
-               if(row.size()>5) { // Reason included
-                       reason = row.get(5);
-               } else {
-                       reason = String.format(fmt, row.get(1),orgName,row.get(4));
-               }
-               return reason;
-       }
+    public static String histMemo(String fmt, String orgName, List<String> row) {
+        String reason;
+        if(row.size()>5) { // Reason included
+            reason = row.get(5);
+        } else {
+            reason = String.format(fmt, row.get(1),orgName,row.get(4));
+        }
+        return reason;
+    }
 
 
-       public static void clear() {
-               data.clear();
-               byNS.clear();
-       }
+    public static void clear() {
+        data.clear();
+        byNS.clear();
+    }
 }
\ No newline at end of file