Organization defined users whose user roles do not expire will also not have their...
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / CassDAOImpl.java
index 58c4386..bb4e210 100644 (file)
@@ -7,9 +7,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.
@@ -48,19 +48,30 @@ import com.datastax.driver.core.ResultSetFuture;
  */
 public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS, DATA> implements DAO<TRANS,DATA> {
     public static final String USER_NAME = "__USER_NAME__";
+    public static final String CASS_READ_CONSISTENCY="cassandra.readConsistency";
+    public static final String CASS_WRITE_CONSISTENCY="cassandra.writeConsistency";
     protected static final String CREATE_SP = "CREATE ";
     protected static final String UPDATE_SP = "UPDATE ";
     protected static final String DELETE_SP = "DELETE ";
     protected static final String SELECT_SP = "SELECT ";
+    private static final String WHERE = " WHERE ";
+    private static final String READ_IS_DISABLED = "Read is disabled for %s";
 
     protected final String C_TEXT = getClass().getSimpleName() + " CREATE";
     protected final String R_TEXT = getClass().getSimpleName() + " READ";
     protected final String U_TEXT = getClass().getSimpleName() + " UPDATE";
     protected final String D_TEXT = getClass().getSimpleName() + " DELETE";
     private String table;
-    
-    protected final ConsistencyLevel readConsistency,writeConsistency;
-    
+
+    protected final ConsistencyLevel readConsistency;
+    protected final ConsistencyLevel writeConsistency;
+
+    protected PSInfo createPS;
+    protected PSInfo readPS;
+    protected PSInfo updatePS;
+    protected PSInfo deletePS;
+    protected boolean async=false;
+
     // Setteable only by CachedDAO
     protected Cached<?, ?> cache;
 
@@ -78,7 +89,7 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
         readConsistency = read;
         writeConsistency = write;
     }
-    
+
     /**
      * A Constructor to share Session with other DAOs.
      *
@@ -95,12 +106,6 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
         writeConsistency = write;
     }
 
-    protected PSInfo createPS;
-    protected PSInfo readPS;
-    protected PSInfo updatePS;
-    protected PSInfo deletePS;
-    protected boolean async=false;
-
     public void async(boolean bool) {
         async = bool;
     }
@@ -108,18 +113,18 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
     public final String[] setCRUD(TRANS trans, String table, Class<?> dc,Loader<DATA> loader) {
         return setCRUD(trans, table, dc, loader, -1);
     }
-    
+
     public final String[] setCRUD(TRANS trans, String table, Class<?> dc,Loader<DATA> loader, int max) {
                 Field[] fields = dc.getDeclaredFields();
-                int end = max>=0 & max<fields.length?max:fields.length;
+                int end = max>=0 && max<fields.length?max:fields.length;
                 // get keylimit from a non-null Loader
                 int keylimit = loader.keylimit();
-            
+
                 StringBuilder sbfc = new StringBuilder();
                 StringBuilder sbq = new StringBuilder();
                 StringBuilder sbwc = new StringBuilder();
                 StringBuilder sbup = new StringBuilder();
-            
+
                 if (keylimit>0) {
                     for (int i=0;i<end;++i) {
                         if (i>0) {
@@ -143,42 +148,50 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
                             sbwc.append("=?");
                         }
                     }
-            
+
                     createPS = new PSInfo(trans, "INSERT INTO " + table + " ("+ sbfc +") VALUES ("+ sbq +");",loader,writeConsistency);
-            
-                    readPS = new PSInfo(trans, "SELECT " + sbfc + " FROM " + table + " WHERE " + sbwc + ';',loader,readConsistency);
-            
+
+                    readPS = new PSInfo(trans, SELECT_SP + sbfc + " FROM " + table + WHERE + sbwc + ';',loader,readConsistency);
+
                     // Note: UPDATES can't compile if there are no fields besides keys... Use "Insert"
                     if (sbup.length()==0) {
                         updatePS = createPS; // the same as an insert
                     } else {
-                        updatePS = new PSInfo(trans, "UPDATE " + table + " SET " + sbup + " WHERE " + sbwc + ';',loader,writeConsistency);
+                        updatePS = new PSInfo(trans, UPDATE_SP + table + " SET " + sbup + WHERE + sbwc + ';',loader,writeConsistency);
                     }
-            
-                    deletePS = new PSInfo(trans, "DELETE FROM " + table + " WHERE " + sbwc + ';',loader,writeConsistency);
+
+                    deletePS = new PSInfo(trans, "DELETE FROM " + table + WHERE + sbwc + ';',loader,writeConsistency);
                 }
                 return new String[] {sbfc.toString(), sbq.toString(), sbup.toString(), sbwc.toString()};
             }
 
     public void replace(CRUD crud, PSInfo psInfo) {
         switch(crud) {
-            case create: createPS = psInfo; break;
-            case read:   readPS = psInfo; break;
-            case update: updatePS = psInfo; break;
-            case delete: deletePS = psInfo; break;
+            case create: createPS = psInfo;
+            break;
+            case read:   readPS = psInfo;
+            break;
+            case update: updatePS = psInfo;
+            break;
+            case delete: deletePS = psInfo;
+            break;
         }
     }
 
     public void disable(CRUD crud) {
         switch(crud) {
-            case create: createPS = null; break;
-            case read:   readPS = null; break;
-            case update: updatePS = null; break;
-            case delete: deletePS = null; break;
+            case create: createPS = null;
+            break;
+            case read:   readPS = null;
+            break;
+            case update: updatePS = null;
+            break;
+            case delete: deletePS = null;
+            break;
         }
     }
 
-    
+
     /**
      * Given a DATA object, extract the individual elements from the Data into an Object Array for the
      * execute element.
@@ -207,21 +220,21 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
      */
     public Result<List<DATA>> read(TRANS trans, DATA data) {
         if (readPS==null) {
-            return Result.err(Result.ERR_NotImplemented,"Read is disabled for %s",getClass().getSimpleName());
+            return Result.err(Result.ERR_NotImplemented,READ_IS_DISABLED,getClass().getSimpleName());
         }
         return readPS.read(trans, R_TEXT, data);
     }
 
     public Result<List<DATA>> read(TRANS trans, Object ... key) {
         if (readPS==null) {
-            return Result.err(Result.ERR_NotImplemented,"Read is disabled for %s",getClass().getSimpleName());
+            return Result.err(Result.ERR_NotImplemented,READ_IS_DISABLED,getClass().getSimpleName());
         }
         return readPS.read(trans, R_TEXT, key);
     }
-    
+
     public Result<DATA> readPrimKey(TRANS trans, Object ... key) {
         if (readPS==null) {
-            return Result.err(Result.ERR_NotImplemented,"Read is disabled for %s",getClass().getSimpleName());
+            return Result.err(Result.ERR_NotImplemented,READ_IS_DISABLED,getClass().getSimpleName());
         }
         Result<List<DATA>> rld = readPS.read(trans, R_TEXT, key);
         if (rld.isOK()) {
@@ -254,7 +267,7 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
                 return Result.err(rs);
             }
         }
-        
+
         wasModified(trans, CRUD.update, data);
         return Result.ok();
     }
@@ -273,7 +286,7 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
             if (rd.isEmpty()) {
                 return Result.err(Status.ERR_NotFound,"Not Found");
             }
-            for (DATA d : rd.value) { 
+            for (DATA d : rd.value) {
                 if (async) {
                     Result<ResultSetFuture> rs = deletePS.execAsync(trans, D_TEXT, d);
                     if (rs.notOK()) {
@@ -303,7 +316,7 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
         }
         return Result.ok();
     }
-    
+
     public final Object[] keyFrom(DATA data) {
         return createPS.keyFrom(data);
     }
@@ -312,9 +325,7 @@ public class CassDAOImpl<TRANS extends TransStore,DATA> extends AbsCassDAO<TRANS
     public String table() {
         return table;
     }
-    
-    public static final String CASS_READ_CONSISTENCY="cassandra.readConsistency";
-    public static final String CASS_WRITE_CONSISTENCY="cassandra.writeConsistency";
+
     protected static ConsistencyLevel readConsistency(AuthzTrans trans, String table) {
         String prop = trans.getProperty(CASS_READ_CONSISTENCY+'.'+table);
         if (prop==null) {