Sonar Fixes: Auth Batch Helpers
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / Approval.java
index a902962..83d9cd5 100644 (file)
@@ -9,9 +9,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -28,6 +28,7 @@ import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+import java.util.SortedMap;
 import java.util.TreeMap;
 import java.util.UUID;
 
@@ -35,6 +36,7 @@ import org.onap.aaf.auth.dao.cass.ApprovalDAO;
 import org.onap.aaf.auth.env.AuthzTrans;
 import org.onap.aaf.auth.layer.Result;
 import org.onap.aaf.cadi.util.CSV;
+import org.onap.aaf.misc.env.Env;
 import org.onap.aaf.misc.env.TimeTaken;
 import org.onap.aaf.misc.env.Trans;
 
@@ -50,16 +52,16 @@ public class Approval implements CacheChange.Data  {
     public static final String RE_VALIDATE_ADMIN = "Revalidate as Admin of AAF Namespace [";
     public static final String RE_VALIDATE_OWNER = "Revalidate as Owner of AAF Namespace [";
 
-    public static TreeMap<String,List<Approval>> byApprover = new TreeMap<>();
-    public static TreeMap<String,List<Approval>> byUser = new TreeMap<>();
-    public static TreeMap<UUID,List<Approval>> byTicket = new TreeMap<>();
-    public static List<Approval> list = new LinkedList<>();
+    public static final SortedMap<String,List<Approval>> byApprover = new TreeMap<>();
+    public static final SortedMap<String,List<Approval>> byUser = new TreeMap<>();
+    public static final SortedMap<UUID,List<Approval>> byTicket = new TreeMap<>();
+    public static final List<Approval> list = new LinkedList<>();
     private static final CacheChange<Approval> cache = new CacheChange<>();
-    
+
     public final ApprovalDAO.Data add;
     private String role;
 
-    public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
+    public static final Creator<Approval> v2_0_17 = new Creator<Approval>() {
         @Override
         public Approval create(Row row) {
             return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2),
@@ -73,7 +75,7 @@ public class Approval implements CacheChange.Data  {
         }
     };
 
-    public static Visitor<Approval> FullLoad = new Visitor<Approval>() {
+    public static final Visitor<Approval> FullLoad = new Visitor<Approval>() {
         @Override
         public void visit(Approval app) {
             List<Approval> ln;
@@ -109,8 +111,8 @@ public class Approval implements CacheChange.Data  {
             }
         }
     };
-    
-    public Approval(UUID id, UUID ticket, String approver,// Date last_notified, 
+
+    public Approval(UUID id, UUID ticket, String approver,// Date last_notified,
             String user, String memo, String operation, String status, String type, long updated) {
         add = new ApprovalDAO.Data();
         add.id = id;
@@ -124,7 +126,7 @@ public class Approval implements CacheChange.Data  {
         add.updated = new Date(updated);
         role = roleFromMemo(memo);
     }
-    
+
     public static String roleFromMemo(String memo) {
         if (memo==null) {
             return null;
@@ -134,19 +136,24 @@ public class Approval implements CacheChange.Data  {
             int second = memo.indexOf(']', ++first);
             if (second>=0) {
                 String role = memo.substring(first, second);
-                if (memo.startsWith(RE_VALIDATE_ADMIN)) {
-                    return role + ".admin";
-                } else if (memo.startsWith(RE_VALIDATE_OWNER)) {
-                    return role + ".owner";
-                } else {
-                    first = memo.indexOf('[',second);
-                    if(first>=0) {
-                        second = memo.indexOf(']', ++first);
-                        if(second>=0 && (memo.startsWith(RE_APPROVAL_IN_ROLE) ||
-                                memo.startsWith(ADD_USER_TO_ROLE))) {
-                                return  memo.substring(first, second);
-                        }
-                    }
+                return getRoleString(role, memo, second);
+            }
+        }
+        return null;
+    }
+
+    public static String getRoleString(String role, String memo, int second) {
+        if (memo.startsWith(RE_VALIDATE_ADMIN)) {
+            return role + ".admin";
+        } else if (memo.startsWith(RE_VALIDATE_OWNER)) {
+            return role + ".owner";
+        } else {
+            int secondString = memo.indexOf('[',second);
+            if(secondString>=0) {
+                second = memo.indexOf(']', ++secondString);
+                if(second>=0 && (memo.startsWith(RE_APPROVAL_IN_ROLE) ||
+                                         memo.startsWith(ADD_USER_TO_ROLE))) {
+                    return  memo.substring(secondString, second);
                 }
             }
         }
@@ -156,37 +163,37 @@ public class Approval implements CacheChange.Data  {
     public static int load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
         int count = 0;
         try {
-            count+=call(trans,session,creator.query(null), creator, visitor);
+            count += call(trans,session,creator.query(null), creator, visitor);
         } finally {
             trans.info().log("Found",count,"Approval Records");
         }
         return count;
     }
-    
+
     public static int load(Trans trans, Session session, Creator<Approval> creator ) {
         int count = 0;
         try {
-            count+=call(trans,session,creator.query(null), creator, FullLoad);
+            count += call(trans,session,creator.query(null), creator, FullLoad);
         } finally {
             trans.info().log("Found",count,"Approval Records");
         }
         return count;
     }
-    
+
     public static int loadUsers(Trans trans, Session session, Set<String> users, Visitor<Approval> visitor) {
         int total = 0;
         for(String user : users) {
-            total+=call(trans,session,String.format("%s WHERE user='%s';",v2_0_17.select(), user),v2_0_17,visitor);
+            total += call(trans,session,String.format("%s WHERE user='%s';",v2_0_17.select(), user),v2_0_17,visitor);
         }
         return total;
     }
-    
+
     public static void row(CSV.RowSetter crs, Approval app) {
         crs.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
     }
 
     private static int call(Trans trans, Session session, String query, Creator<Approval> creator, Visitor<Approval> visitor) {
-        TimeTaken tt = trans.start("DB Query", Trans.REMOTE);
+        TimeTaken tt = trans.start("DB Query", Env.REMOTE);
         ResultSet results;
         try {
             Statement stmt = new SimpleStatement( query );
@@ -201,14 +208,14 @@ public class Approval implements CacheChange.Data  {
             tt.done();
         }
     }
-    
+
     @Override
     public void expunge() {
         List<Approval> la = byApprover.get(getApprover());
         if (la!=null) {
             la.remove(this);
         }
-        
+
         la = byUser.get(getUser());
         if (la!=null) {
             la.remove(this);
@@ -288,11 +295,11 @@ public class Approval implements CacheChange.Data  {
         add.ticket=null;
         add.status="lapsed";
     }
-    
+
     public String getRole() {
         return role;
     }
-    
+
     public String toString() {
         return getUser() + ' ' + getMemo();
     }
@@ -310,12 +317,12 @@ public class Approval implements CacheChange.Data  {
             }
         }
     }
-    
+
 
     public static void resetLocalData() {
         cache.resetLocalData();
     }
-    
+
     public static int sizeForDeletion() {
         return cache.cacheSize();
     }