Merge "Remove the declaration of thrown exception 'java.io.IOException', as it cannot...
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / UserRole.java
index b4e1a6d..dd24d7d 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.io.IOException;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Date;
@@ -32,6 +34,7 @@ import java.util.TreeMap;
 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
 import org.onap.aaf.auth.dao.cass.UserRoleDAO.Data;
 import org.onap.aaf.auth.env.AuthzTrans;
+import org.onap.aaf.cadi.CadiException;
 import org.onap.aaf.cadi.util.CSV;
 import org.onap.aaf.misc.env.Env;
 import org.onap.aaf.misc.env.TimeTaken;
@@ -49,7 +52,7 @@ public class UserRole implements Cloneable, CacheChange.Data  {
     public static final String UR = "ur";
     public static final String APPROVE_UR = "ur";
 
-       private static final String SEPARATOR = "\",\"";
+    private static final String SEPARATOR = "\",\"";
 
     // CACHE Calling
     private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
@@ -124,6 +127,14 @@ public class UserRole implements Cloneable, CacheChange.Data  {
         load(trans,session,creator,"user='"+ user +'\'',visitor);
     }
 
+    public static void load(Trans trans, CSV csv, Creator<UserRole> creator, Visitor<UserRole> visitor) throws IOException, CadiException {
+//        public UserRole(String user, String role, String ns, String rname, Date expires) {
+        csv.visit( row -> 
+            visitor.visit(new UserRole(row.get(1),row.get(2),row.get(3),row.get(4),
+                new Date(Long.parseLong(row.get(6)))))
+        );
+    }
+    
     private static void load(Trans trans, Session session, Creator<UserRole> creator, String where, Visitor<UserRole> visitor) {
         String query = creator.query(where);
         trans.debug().log( "query: " + query );
@@ -159,8 +170,8 @@ public class UserRole implements Cloneable, CacheChange.Data  {
     }
 
     public static class DataLoadVisitor implements Visitor<UserRole> {
-               @Override
-               public void visit(UserRole ur) {
+        @Override
+        public void visit(UserRole ur) {
             data.add(ur);
 
             List<UserRole> lur = byUser.get(ur.urdd.user);
@@ -176,7 +187,7 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                 byRole.put(ur.urdd.role, lur);
             }
             lur.add(ur);
-               }
+        }
     }
     
     public int totalLoaded() {
@@ -295,71 +306,81 @@ public class UserRole implements Cloneable, CacheChange.Data  {
     }
     
     public void row(final CSV.Writer csvw, String tag) {
-       csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime());
+        csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime());
     }
 
     public void row(final CSV.Writer csvw, String tag, String reason) {
-       csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime(),reason);
+        csvw.row(tag,user(),role(),ns(),rname(),Chrono.dateOnlyStamp(expires()),expires().getTime(),reason);
     }
     
     public static Data row(List<String> row) {
-               Data data = new Data();
-               data.user = row.get(1);
-               data.role = row.get(2);
-               data.ns = row.get(3);
-               data.rname = row.get(4);
-               data.expires = new Date(Long.parseLong(row.get(6)));
-               return data;
-       }
-
-       public static void batchDelete(StringBuilder sb, List<String> row) {
-       sb.append("DELETE from authz.user_role WHERE user='");
-       sb.append(row.get(1));
-       sb.append("' AND role='");
-       sb.append(row.get(2));
-       sb.append("';\n");
+        Data data = new Data();
+        data.user = row.get(1);
+        data.role = row.get(2);
+        data.ns = row.get(3);
+        data.rname = row.get(4);
+        data.expires = new Date(Long.parseLong(row.get(6)));
+        return data;
     }
 
-    public static void batchExtend(StringBuilder sb, List<String> row, Date newDate ) {
-       sb.append("UPDATE authz.user_role SET expires='");
-       sb.append(Chrono.dateTime(newDate));
-       sb.append("' WHERE user='");
-       sb.append(row.get(1));
-       sb.append("' AND role='");
-       sb.append(row.get(2));
-       sb.append("';\n");
+    public static void batchDelete(StringBuilder sb, List<String> row) {
+        sb.append("DELETE from authz.user_role WHERE user='");
+        sb.append(row.get(1));
+        sb.append("' AND role='");
+        sb.append(row.get(2));
+        sb.append("';\n");
     }
 
+    public static void batchExtend(StringBuilder sb, List<String> row, Date newDate ) {
+        sb.append("UPDATE authz.user_role SET expires='");
+        sb.append(Chrono.dateTime(newDate));
+        sb.append("' WHERE user='");
+        sb.append(row.get(1));
+        sb.append("' AND role='");
+        sb.append(row.get(2));
+        sb.append("';\n");
+    }
+
+    public void batchExtend(StringBuilder sb, Date newDate) {
+        sb.append("UPDATE authz.user_role SET expires='");
+        sb.append(Chrono.dateTime(newDate));
+        sb.append("' WHERE user='");
+        sb.append(user());
+        sb.append("' AND role='");
+        sb.append(role());
+        sb.append("';\n");
+    }
+    
     public void batchUpdateExpires(StringBuilder sb) {
-       sb.append("UPDATE authz.user_role SET expires='");
-       sb.append(Chrono.dateTime(expires()));
-       sb.append("' WHERE user='");
-       sb.append(user());
-       sb.append("' AND role='");
-       sb.append(role());
-       sb.append("';\n");
-    }
-
-       public static String histMemo(String fmt, List<String> row) {
-               String reason;
-               if(row.size()>7) { // Reason included
-                       reason = String.format("%s removed from %s because %s", 
-                                       row.get(1),row.get(2),row.get(7));
-               } else {
-                       reason = String.format(fmt, row.get(1),row.get(2), row.get(5));
-               }
-               return reason;
-       }
-
-       public static String histSubject(List<String> row) {
-               return row.get(1) + '|' + row.get(2);   
-       }
-
-       public static void clear() {
-               data.clear();
-               byUser.clear();
-               byRole.clear();
-               cache.resetLocalData();
-               
-       }
+        sb.append("UPDATE authz.user_role SET expires='");
+        sb.append(Chrono.dateTime(expires()));
+        sb.append("' WHERE user='");
+        sb.append(user());
+        sb.append("' AND role='");
+        sb.append(role());
+        sb.append("';\n");
+    }
+
+    public static String histMemo(String fmt, List<String> row) {
+        String reason;
+        if(row.size()>7) { // Reason included
+            reason = String.format("%s removed from %s because %s", 
+                    row.get(1),row.get(2),row.get(7));
+        } else {
+            reason = String.format(fmt, row.get(1),row.get(2), row.get(5));
+        }
+        return reason;
+    }
+
+    public static String histSubject(List<String> row) {
+        return row.get(1) + '|' + row.get(2);    
+    }
+
+    public static void clear() {
+        data.clear();
+        byUser.clear();
+        byRole.clear();
+        cache.resetLocalData();
+        
+    }
 }
\ No newline at end of file