JUnit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / daoimpl / PolicyValidationDaoImpl.java
index 66401cc..f64b9ce 100644 (file)
@@ -3,13 +3,14 @@
  * ONAP Policy Engine
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * 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.
@@ -17,6 +18,7 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.policy.rest.daoimpl;
 
 import java.util.ArrayList;
@@ -45,28 +47,28 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 @Component
-public class PolicyValidationDaoImpl implements CommonClassDao{
+public class PolicyValidationDaoImpl implements CommonClassDao {
 
     private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationDaoImpl.class);
     private static final String DB_CONNECTION_CLOSING_ERROR = "Error While Closing Connection/Statement";
     private static final String DBTABLE_QUERY_ERROR = "Error While Querying Table";
     private static SessionFactory sessionfactory;
-    
-    public static SessionFactory getSessionfactory() {
-          return sessionfactory;
+
+    @Autowired
+    private PolicyValidationDaoImpl(SessionFactory sessionfactory) {
+        PolicyValidationDaoImpl.sessionfactory = sessionfactory;
     }
 
-    public static void setSessionfactory(SessionFactory sessionfactory) {
-          PolicyValidationDaoImpl.sessionfactory = sessionfactory;
+    public PolicyValidationDaoImpl() {
+        // Default Constructor
     }
 
-    @Autowired
-    private PolicyValidationDaoImpl(SessionFactory sessionfactory){
-          PolicyValidationDaoImpl.sessionfactory = sessionfactory;
+    public static SessionFactory getSessionfactory() {
+        return sessionfactory;
     }
-    
-    public PolicyValidationDaoImpl(){
-          //Default Constructor
+
+    public static void setSessionfactory(SessionFactory sessionfactory) {
+        PolicyValidationDaoImpl.sessionfactory = sessionfactory;
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
@@ -74,16 +76,16 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
     public List<Object> getData(Class className) {
         Session session = sessionfactory.openSession();
         List<Object> data = null;
-        try{
+        try {
             Criteria cr = session.createCriteria(className);
             data = cr.list();
-        }catch(Exception e){
+        } catch (Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
-        }finally{
-            try{
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e){
-                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR+e);
+            } catch (Exception e) {
+                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e);
             }
         }
         return data;
@@ -97,22 +99,22 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         List<Object> data = null;
         try {
             Criteria cr = session.createCriteria(className);
-            if(columnName.contains(":") && key.contains(":")){
+            if (columnName.contains(":") && key.contains(":")) {
                 String[] columns = columnName.split(":");
                 String[] keys = key.split(":");
-                for(int i=0; i < columns.length; i++){
+                for (int i = 0; i < columns.length; i++) {
                     cr.add(Restrictions.eq(columns[i], keys[i]));
                 }
-            }else{
+            } else {
                 cr.add(Restrictions.eq(columnName, key));
             }
             data = cr.list();
         } catch (Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
-        }finally{
-            try{
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -126,12 +128,12 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         try {
             session.persist(entity);
             tx.commit();
-        }catch(Exception e){
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table"+e);
-        }finally{
-            try{
+        } catch (Exception e) {
+            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving data to Table" + e);
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -145,12 +147,12 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         try {
             session.delete(entity);
             tx.commit();
-        }catch(Exception e){
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table"+e);
-        }finally{
-            try{
+        } catch (Exception e) {
+            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting data from Table" + e);
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -165,12 +167,12 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         try {
             session.update(entity);
             tx.commit();
-        }catch(Exception e){
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table"+e);
-        }finally{
-            try{
+        } catch (Exception e) {
+            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating data to Table" + e);
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -185,30 +187,31 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         Transaction tx = session.beginTransaction();
         List<Object> data = null;
         String[] columnNames = null;
-        if(columnName != null && columnName.contains(":")){
+        if (columnName != null && columnName.contains(":")) {
             columnNames = columnName.split(":");
         }
         String[] values = null;
-        if(value != null && value.contains(":")){
+        if (value != null && value.contains(":")) {
             values = value.split(":");
         }
         try {
             Criteria cr = session.createCriteria(className);
-            if(columnNames != null && values != null && columnNames.length == values.length){
-                for (int i = 0; i < columnNames.length; i++){
-                    cr.add(Restrictions.eq(columnNames[i],values[i]));
+            if (columnNames != null && values != null && columnNames.length == values.length) {
+                for (int i = 0; i < columnNames.length; i++) {
+                    cr.add(Restrictions.eq(columnNames[i], values[i]));
                 }
-            }else{
-                cr.add(Restrictions.eq(columnName,value));
+            } else {
+                cr.add(Restrictions.eq(columnName, value));
             }
             data = cr.list();
             tx.commit();
         } catch (Exception e) {
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"+e + className);
-        }finally{
-            try{
+            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying for Duplicate Entries for Table"
+                    + e + className);
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -223,13 +226,13 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         Transaction tx = session.beginTransaction();
         List<PolicyRoles> rolesData = null;
         try {
-            Criteria cr = session.createCriteria(PolicyRoles.class);
-            Disjunction disjunction = Restrictions.disjunction();
-            Conjunction conjunction1  = Restrictions.conjunction();
+            final Criteria cr = session.createCriteria(PolicyRoles.class);
+            final Disjunction disjunction = Restrictions.disjunction();
+            Conjunction conjunction1 = Restrictions.conjunction();
             conjunction1.add(Restrictions.eq("role", "admin"));
-            Conjunction conjunction2  = Restrictions.conjunction();
+            Conjunction conjunction2 = Restrictions.conjunction();
             conjunction2.add(Restrictions.eq("role", "editor"));
-            Conjunction conjunction3  = Restrictions.conjunction();
+            Conjunction conjunction3 = Restrictions.conjunction();
             conjunction3.add(Restrictions.eq("role", "guest"));
             disjunction.add(conjunction1);
             disjunction.add(conjunction2);
@@ -237,11 +240,11 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
             rolesData = cr.add(disjunction).list();
             tx.commit();
         } catch (Exception e) {
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);
-        }finally{
-            try{
+            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table" + e);
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -270,10 +273,9 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         try {
             Query hbquery = session.createQuery(query);
             for (Map.Entry<String, Object> paramPair : params.entrySet()) {
-                if(paramPair.getValue() instanceof java.lang.Long){
+                if (paramPair.getValue() instanceof java.lang.Long) {
                     hbquery.setLong(paramPair.getKey(), (long) paramPair.getValue());
-                }
-                else{
+                } else {
                     hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
                 }
             }
@@ -282,10 +284,10 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         } catch (Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
             throw e;
-        }finally{
-            try{
+        } finally {
+            try {
                 session.close();
-            }catch(HibernateException e1){
+            } catch (HibernateException e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -301,23 +303,23 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
         Object data = null;
         try {
             Criteria cr = session.createCriteria(className);
-            if(columnName.contains(":") && key.contains(":")){
+            if (columnName.contains(":") && key.contains(":")) {
                 String[] columns = columnName.split(":");
                 String[] keys = key.split(":");
-                for(int i=0; i < columns.length; i++){
+                for (int i = 0; i < columns.length; i++) {
                     cr.add(Restrictions.eq(columns[i], keys[i]));
                 }
-            }else{
+            } else {
                 cr.add(Restrictions.eq(columnName, key));
             }
             data = cr.list().get(0);
             tx.commit();
         } catch (Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
-        }finally{
-            try{
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -346,11 +348,11 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
             hbquery.executeUpdate();
             tx.commit();
         } catch (Exception e) {
-            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table"+e);
-        }finally{
-            try{
+            LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Database Table" + e);
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }
@@ -363,16 +365,16 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
     public List<String> getDataByColumn(Class className, String columnName) {
         Session session = sessionfactory.openSession();
         List<String> data = null;
-        try{
+        try {
             Criteria cr = session.createCriteria(className);
             cr.setProjection(Projections.property(columnName));
             data = cr.list();
-        }catch(Exception e){
+        } catch (Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + e);
-        }finally{
-            try{
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e){
+            } catch (Exception e) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e);
             }
         }
@@ -391,24 +393,24 @@ public class PolicyValidationDaoImpl implements CommonClassDao{
             Disjunction disjunction = Restrictions.disjunction();
             List<Conjunction> conjunctionList = new ArrayList<>();
             String[] columNames = columnName.split(":");
-            for(int i =0; i < data.size(); i++){
+            for (int i = 0; i < data.size(); i++) {
                 String[] entiySplit = data.get(i).split(":");
                 Conjunction conjunction = Restrictions.conjunction();
                 conjunction.add(Restrictions.eq(columNames[0], entiySplit[0]));
                 conjunction.add(Restrictions.eq(columNames[1], entiySplit[1]));
                 conjunctionList.add(conjunction);
             }
-            for(int j =0 ; j < conjunctionList.size(); j++){
+            for (int j = 0; j < conjunctionList.size(); j++) {
                 disjunction.add(conjunctionList.get(j));
             }
             entityData = cr.add(disjunction).list();
             tx.commit();
         } catch (Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DBTABLE_QUERY_ERROR + className + e);
-        }finally{
-            try{
+        } finally {
+            try {
                 session.close();
-            }catch(Exception e1){
+            } catch (Exception e1) {
                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + DB_CONNECTION_CLOSING_ERROR + e1);
             }
         }