Merge "Fix technical debt and reduce lines"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / daoImp / CommonClassDaoImpl.java
index 05bf50f..170c308 100644 (file)
 package org.onap.policy.daoImp;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+
+import javax.script.SimpleBindings;
 
 import org.hibernate.Criteria;
+import org.hibernate.HibernateException;
 import org.hibernate.Query;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
@@ -38,15 +43,34 @@ import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.PolicyRoles;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Service;
 
 @Service("CommonClassDao")
+@Primary
 public class CommonClassDaoImpl implements CommonClassDao{
 
        private static final Logger LOGGER = FlexLogger.getLogger(CommonClassDaoImpl.class);
+       private static SessionFactory sessionfactory;
+    
+    public static SessionFactory getSessionfactory() {
+          return sessionfactory;
+    }
+
+    public static void setSessionfactory(SessionFactory sessionfactory) {
+          CommonClassDaoImpl.sessionfactory = sessionfactory;
+    }
+
+    @Autowired
+    private CommonClassDaoImpl(SessionFactory sessionfactory){
+          CommonClassDaoImpl.sessionfactory = sessionfactory;
+    }
+    
+    public CommonClassDaoImpl(){
+          //Default Constructor
+    }
+
        
-       @Autowired
-       SessionFactory sessionfactory;
        
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
@@ -230,30 +254,39 @@ public class CommonClassDaoImpl implements CommonClassDao{
 
        @Override
        public List<Object> checkExistingGroupListforUpdate(String arg0, String arg1) {
-               return null;
+               return Collections.emptyList();
        }
 
 
        @Override
        public void deleteAll() {}
 
-
+       
        @SuppressWarnings("unchecked")
        @Override
-       public List<Object> getDataByQuery(String query) {
+       public List<Object> getDataByQuery(String query, SimpleBindings params) {
                Session session = sessionfactory.openSession();
                Transaction tx = session.beginTransaction();
                List<Object> data = null;
                try {
                        Query hbquery = session.createQuery(query);
+                       for (Map.Entry<String, Object> paramPair : params.entrySet()) {
+                               if(paramPair.getValue() instanceof java.lang.Long){
+                                       hbquery.setLong(paramPair.getKey(), (long) paramPair.getValue());
+                               }
+                               else{
+                                       hbquery.setParameter(paramPair.getKey(), paramPair.getValue());
+                               }
+                       }
                        data = hbquery.list();
                        tx.commit();
                } catch (Exception e) {
-                       LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e); 
+                       LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Database Table"+e);
+                       throw e;
                }finally{
                        try{
                                session.close();
-                       }catch(Exception e1){
+                       }catch(HibernateException e1){
                                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
                        }
                }