http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java 
b/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
index 3234be6..793ddf4 100644
--- a/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
@@ -20,15 +20,14 @@
 package org.apache.ranger.biz;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.ranger.authorization.utils.JsonUtils;
 import org.apache.ranger.common.GUIDUtil;
 import org.apache.ranger.common.MessageEnums;
 import org.apache.ranger.common.RESTErrorUtil;
@@ -36,20 +35,12 @@ import org.apache.ranger.common.RangerAdminTagEnricher;
 import org.apache.ranger.common.RangerServiceTagsCache;
 import org.apache.ranger.db.RangerDaoManager;
 import org.apache.ranger.entity.XXDBBase;
-import org.apache.ranger.entity.XXResourceDef;
 import org.apache.ranger.entity.XXService;
-import org.apache.ranger.entity.XXServiceDef;
 import org.apache.ranger.entity.XXServiceResource;
 import org.apache.ranger.entity.XXServiceVersionInfo;
 import org.apache.ranger.entity.XXTag;
-import org.apache.ranger.entity.XXTagAttribute;
-import org.apache.ranger.entity.XXTagAttributeDef;
-import org.apache.ranger.entity.XXServiceResourceElement;
-import org.apache.ranger.entity.XXServiceResourceElementValue;
 import org.apache.ranger.entity.XXTagResourceMap;
 import org.apache.ranger.plugin.model.*;
-import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource;
-import org.apache.ranger.plugin.model.RangerTagDef.RangerTagAttributeDef;
 import org.apache.ranger.plugin.store.AbstractTagStore;
 import org.apache.ranger.plugin.store.PList;
 import org.apache.ranger.plugin.store.RangerServiceResourceSignature;
@@ -117,8 +108,6 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerTagDef ret = rangerTagDefService.create(tagDef);
 
-               createTagAttributeDefs(ret.getId(), tagDef.getAttributeDefs());
-
                ret = rangerTagDefService.read(ret.getId());
 
                if (LOG.isDebugEnabled()) {
@@ -138,6 +127,8 @@ public class TagDBStore extends AbstractTagStore {
 
                if (existing == null) {
                        throw errorUtil.createRESTException("failed to update 
tag-def [" + tagDef.getName() + "], Reason: No TagDef found with id: [" + 
tagDef.getId() + "]", MessageEnums.DATA_NOT_UPDATABLE);
+               } else if (!existing.getName().equals(tagDef.getName())) {
+                       throw errorUtil.createRESTException("Cannot change 
tag-def name; existing-name:[" + existing.getName() + "], new-name:[" + 
tagDef.getName() + "]", MessageEnums.DATA_NOT_UPDATABLE);
                }
 
                tagDef.setCreatedBy(existing.getCreatedBy());
@@ -147,10 +138,6 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerTagDef ret = rangerTagDefService.update(tagDef);
 
-               // TODO: delete attributes might fail; so instead of 
delete+create, following should be updated to deal with only attributes that 
changed
-               deleteTagAttributeDefs(ret.getId());
-               createTagAttributeDefs(ret.getId(), tagDef.getAttributeDefs());
-
                ret = rangerTagDefService.read(ret.getId());
 
                if (LOG.isDebugEnabled()) {
@@ -174,7 +161,6 @@ public class TagDBStore extends AbstractTagStore {
                                        LOG.debug("Deleting tag-def [name=" + 
name + "; id=" + tagDef.getId() + "]");
                                }
 
-                               deleteTagAttributeDefs(tagDef.getId());
                                rangerTagDefService.delete(tagDef);
                        }
                }
@@ -194,7 +180,6 @@ public class TagDBStore extends AbstractTagStore {
                        RangerTagDef tagDef = rangerTagDefService.read(id);
 
                        if(tagDef != null) {
-                               deleteTagAttributeDefs(tagDef.getId());
                                rangerTagDefService.delete(tagDef);
                        }
                }
@@ -307,8 +292,6 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerTag ret = rangerTagService.create(tag);
 
-               createTagAttributes(ret.getId(), tag.getAttributes());
-
                ret = rangerTagService.read(ret.getId());
 
                if (LOG.isDebugEnabled()) {
@@ -337,9 +320,6 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerTag ret = rangerTagService.update(tag);
 
-               deleteTagAttributes(existing.getId());
-               createTagAttributes(existing.getId(), tag.getAttributes());
-
                ret = rangerTagService.read(ret.getId());
 
                if (LOG.isDebugEnabled()) {
@@ -357,8 +337,6 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerTag tag = rangerTagService.read(id);
 
-               deleteTagAttributes(id);
-
                rangerTagService.delete(tag);
 
                if (LOG.isDebugEnabled()) {
@@ -498,8 +476,6 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerServiceResource ret = 
rangerServiceResourceService.create(resource);
 
-               createResourceForServiceResource(ret.getId(), resource);
-
                ret = rangerServiceResourceService.read(ret.getId());
 
                if (LOG.isDebugEnabled()) {
@@ -527,9 +503,6 @@ public class TagDBStore extends AbstractTagStore {
                        
resource.setResourceSignature(serializer.getSignature());
                }
 
-               boolean serviceResourceElementUpdateNeeded =
-                               
!StringUtils.equals(existing.getResourceSignature(), 
resource.getResourceSignature());
-
                resource.setCreatedBy(existing.getCreatedBy());
                resource.setCreateTime(existing.getCreateTime());
                resource.setGuid(existing.getGuid());
@@ -537,11 +510,6 @@ public class TagDBStore extends AbstractTagStore {
 
                rangerServiceResourceService.update(resource);
 
-               if (serviceResourceElementUpdateNeeded) {
-                       deleteResourceForServiceResource(existing.getId());
-                       createResourceForServiceResource(existing.getId(), 
resource);
-               }
-
                RangerServiceResource ret = 
rangerServiceResourceService.read(existing.getId());
 
                if (LOG.isDebugEnabled()) {
@@ -551,6 +519,24 @@ public class TagDBStore extends AbstractTagStore {
                return ret;
        }
 
+
+       @Override
+       public void refreshServiceResource(Long resourceId) throws Exception {
+               XXServiceResource serviceResourceEntity = 
daoManager.getXXServiceResource().getById(resourceId);
+               String tagsText = null;
+
+               List<RangerTagResourceMap> tagResourceMaps = 
getTagResourceMapsForResourceId(resourceId);
+               if (tagResourceMaps != null) {
+                       List<RangerTag> associatedTags = new ArrayList<>();
+                       for (RangerTagResourceMap element : tagResourceMaps) {
+                               associatedTags.add(getTag(element.getTagId()));
+                       }
+                       tagsText = JsonUtils.listToJson(associatedTags);
+               }
+               serviceResourceEntity.setTags(tagsText);
+               daoManager.getXXServiceResource().update(serviceResourceEntity);
+       }
+
        @Override
        public void deleteServiceResource(Long id) throws Exception {
                if (LOG.isDebugEnabled()) {
@@ -560,7 +546,6 @@ public class TagDBStore extends AbstractTagStore {
                RangerServiceResource resource = getServiceResource(id);
 
                if(resource != null) {
-                       deleteResourceForServiceResource(resource.getId());
                        rangerServiceResourceService.delete(resource);
                }
 
@@ -578,7 +563,6 @@ public class TagDBStore extends AbstractTagStore {
                RangerServiceResource resource = getServiceResourceByGuid(guid);
 
                if(resource != null) {
-                       deleteResourceForServiceResource(resource.getId());
                        rangerServiceResourceService.delete(resource);
                }
 
@@ -719,6 +703,9 @@ public class TagDBStore extends AbstractTagStore {
 
                RangerTagResourceMap ret = 
rangerTagResourceMapService.create(tagResourceMap);
 
+               // We also need to update tags stored with the resource
+               refreshServiceResource(tagResourceMap.getResourceId());
+
                if (LOG.isDebugEnabled()) {
                        LOG.debug("<== TagDBStore.createTagResourceMap(" + 
tagResourceMap + "): " + ret);
                }
@@ -741,6 +728,8 @@ public class TagDBStore extends AbstractTagStore {
                if (tag.getOwner() == RangerTag.OWNER_SERVICERESOURCE) {
                        deleteTag(tagId);
                }
+               // We also need to update tags stored with the resource
+               refreshServiceResource(tagResourceMap.getResourceId());
 
                if (LOG.isDebugEnabled()) {
                        LOG.debug("<== TagDBStore.deleteTagResourceMap(" + id + 
")");
@@ -996,31 +985,7 @@ public class TagDBStore extends AbstractTagStore {
                Map<Long, RangerTagDef> tagDefMap = tagDBRetriever.getTagDefs();
                Map<Long, RangerTag> tagMap = tagDBRetriever.getTags();
                List<RangerServiceResource> resources = 
tagDBRetriever.getServiceResources();
-               List<RangerTagResourceMap> tagResourceMaps = 
tagDBRetriever.getTagResourceMaps();
-
-               Map<Long, List<Long>> resourceToTagIds = new HashMap<Long, 
List<Long>>();
-
-               if (CollectionUtils.isNotEmpty(tagResourceMaps)) {
-                       Long resourceId = null;
-                       List<Long> tagIds = null;
-
-                       for (RangerTagResourceMap tagResourceMap : 
tagResourceMaps) {
-                               if 
(!tagResourceMap.getResourceId().equals(resourceId)) {
-                                       if (resourceId != null) {
-                                               
resourceToTagIds.put(resourceId, tagIds);
-                                       }
-
-                                       resourceId = 
tagResourceMap.getResourceId();
-                                       tagIds = new ArrayList<Long>();
-                               }
-
-                               tagIds.add(tagResourceMap.getTagId());
-                       }
-
-                       if (resourceId != null) {
-                               resourceToTagIds.put(resourceId, tagIds);
-                       }
-               }
+               Map<Long, List<Long>> resourceToTagIds = 
tagDBRetriever.getResourceToTagIds();
 
                ret = new ServiceTags();
 
@@ -1039,159 +1004,6 @@ public class TagDBStore extends AbstractTagStore {
 
        }
 
-       private List<XXTagAttributeDef> createTagAttributeDefs(Long tagDefId, 
List<RangerTagAttributeDef> tagAttrDefList) {
-               if (LOG.isDebugEnabled()) {
-                       LOG.debug("==> TagDBStore.createTagAttributeDefs(" + 
tagDefId + ", attributeDefCount=" + (tagAttrDefList == null ? 0 : 
tagAttrDefList.size()) + ")");
-               }
-
-               if (tagDefId == null) {
-                       throw 
errorUtil.createRESTException("TagDBStore.createTagAttributeDefs(): Error 
creating tag-attr def. tagDefId can not be null.", 
MessageEnums.ERROR_CREATING_OBJECT);
-               }
-
-               List<XXTagAttributeDef> ret = new 
ArrayList<XXTagAttributeDef>();
-
-               if (CollectionUtils.isNotEmpty(tagAttrDefList)) {
-                       for (RangerTagDef.RangerTagAttributeDef attrDef : 
tagAttrDefList) {
-                               XXTagAttributeDef xAttrDef = new 
XXTagAttributeDef();
-
-                               xAttrDef.setTagDefId(tagDefId);
-                               xAttrDef.setName(attrDef.getName());
-                               xAttrDef.setType(attrDef.getType());
-                               xAttrDef = (XXTagAttributeDef) 
rangerAuditFields.populateAuditFieldsForCreate(xAttrDef);
-
-                               xAttrDef = 
daoManager.getXXTagAttributeDef().create(xAttrDef);
-
-                               ret.add(xAttrDef);
-                       }
-               }
-
-               if (LOG.isDebugEnabled()) {
-                       LOG.debug("<== TagDBStore.createTagAttributeDefs(" + 
tagDefId + ", attributeDefCount=" + (tagAttrDefList == null ? 0 : 
tagAttrDefList.size()) + "): retCount=" + ret.size());
-               }
-
-               return ret;
-       }
-
-       private void deleteTagAttributeDefs(Long tagDefId) {
-               if (LOG.isDebugEnabled()) {
-                       LOG.debug("==> TagDBStore.deleteTagAttributeDefs(" + 
tagDefId + ")");
-               }
-
-               if (tagDefId != null) {
-                       List<XXTagAttributeDef> tagAttrDefList = 
daoManager.getXXTagAttributeDef().findByTagDefId(tagDefId);
-
-                       if (CollectionUtils.isNotEmpty(tagAttrDefList)) {
-                               for (XXTagAttributeDef xAttrDef : 
tagAttrDefList) {
-                                       if (LOG.isDebugEnabled()) {
-                                               LOG.debug("Deleting 
tag-attribute def [name=" + xAttrDef.getName() + "; id=" + xAttrDef.getId() + 
"]");
-                                       }
-                                       
daoManager.getXXTagAttributeDef().remove(xAttrDef);
-                               }
-                       }
-               }
-
-               if (LOG.isDebugEnabled()) {
-                       LOG.debug("<== TagDBStore.deleteTagAttributeDefs(" + 
tagDefId + ")");
-               }
-       }
-
-       private List<XXTagAttribute> createTagAttributes(Long tagId, 
Map<String, String> attributes) {
-               List<XXTagAttribute> ret = new ArrayList<XXTagAttribute>();
-
-               if(MapUtils.isNotEmpty(attributes)) {
-                       for (Map.Entry<String, String> attr : 
attributes.entrySet()) {
-                               XXTagAttribute xTagAttr = new XXTagAttribute();
-
-                               xTagAttr.setTagId(tagId);
-                               xTagAttr.setName(attr.getKey());
-                               xTagAttr.setValue(attr.getValue());
-                               xTagAttr = (XXTagAttribute) 
rangerAuditFields.populateAuditFieldsForCreate(xTagAttr);
-
-                               xTagAttr = 
daoManager.getXXTagAttribute().create(xTagAttr);
-
-                               ret.add(xTagAttr);
-                       }
-               }
-
-               return ret;
-       }
-
-       private void deleteTagAttributes(Long tagId) {
-               List<XXTagAttribute> tagAttrList = 
daoManager.getXXTagAttribute().findByTagId(tagId);
-               for (XXTagAttribute tagAttr : tagAttrList) {
-                       daoManager.getXXTagAttribute().remove(tagAttr);
-               }
-       }
-
-       private void deleteResourceForServiceResource(Long resourceId) {
-               List<XXServiceResourceElement> resElements = 
daoManager.getXXServiceResourceElement().findByResourceId(resourceId);
-               
-               if(CollectionUtils.isNotEmpty(resElements)) {
-                       for(XXServiceResourceElement resElement : resElements) {
-                               List<XXServiceResourceElementValue> 
elementValues = 
daoManager.getXXServiceResourceElementValue().findByResValueId(resElement.getId());
-                               
-                               if(CollectionUtils.isNotEmpty(elementValues)) {
-                                       for(XXServiceResourceElementValue 
elementValue : elementValues) {
-                                               
daoManager.getXXServiceResourceElementValue().remove(elementValue.getId());
-                                       }
-                               }
-                               
-                               
daoManager.getXXServiceResourceElement().remove(resElement.getId());
-                       }
-               }
-       }
-
-       private void createResourceForServiceResource(Long resourceId, 
RangerServiceResource serviceResource) {
-               String serviceName = serviceResource.getServiceName();
-
-               XXService xService = 
daoManager.getXXService().findByName(serviceName);
-
-               if (xService == null) {
-                       throw errorUtil.createRESTException("No Service found 
with name: " + serviceName, MessageEnums.ERROR_CREATING_OBJECT);
-               }
-
-               XXServiceDef xServiceDef = 
daoManager.getXXServiceDef().getById(xService.getType());
-
-               if (xServiceDef == null) {
-                       throw errorUtil.createRESTException("No Service-Def 
found with ID: " + xService.getType(), MessageEnums.ERROR_CREATING_OBJECT);
-               }
-
-               Map<String, RangerPolicy.RangerPolicyResource> resElements = 
serviceResource.getResourceElements();
-
-               for (Map.Entry<String, RangerPolicyResource> resElement : 
resElements.entrySet()) {
-                       XXResourceDef xResDef = 
daoManager.getXXResourceDef().findByNameAndServiceDefId(resElement.getKey(), 
xServiceDef.getId());
-
-                       if (xResDef == null) {
-                               LOG.error("TagDBStore.createResource: 
ResourceType is not valid [" + resElement.getKey() + "]");
-                               throw errorUtil.createRESTException("Resource 
Type is not valid [" + resElement.getKey() + "]", MessageEnums.DATA_NOT_FOUND);
-                       }
-
-                       RangerPolicyResource policyRes = resElement.getValue();
-
-                       XXServiceResourceElement resourceElement = new 
XXServiceResourceElement();
-                       
resourceElement.setIsExcludes(policyRes.getIsExcludes());
-                       
resourceElement.setIsRecursive(policyRes.getIsRecursive());
-                       resourceElement.setResDefId(xResDef.getId());
-                       resourceElement.setResourceId(resourceId);
-
-                       resourceElement = (XXServiceResourceElement) 
rangerAuditFields.populateAuditFieldsForCreate(resourceElement);
-
-                       resourceElement = 
daoManager.getXXServiceResourceElement().create(resourceElement);
-
-                       int sortOrder = 1;
-                       for (String resVal : policyRes.getValues()) {
-                               XXServiceResourceElementValue 
resourceElementValue = new XXServiceResourceElementValue();
-                               
resourceElementValue.setResElementId(resourceElement.getId());
-                               resourceElementValue.setValue(resVal);
-                               resourceElementValue.setSortOrder(sortOrder);
-                               resourceElementValue = 
(XXServiceResourceElementValue) 
rangerAuditFields.populateAuditFieldsForCreate(resourceElementValue);
-
-                               resourceElementValue = 
daoManager.getXXServiceResourceElementValue().create(resourceElementValue);
-                               sortOrder++;
-                       }
-               }
-       }
-
        @Override
        public void deleteAllTagObjectsForService(String serviceName) throws 
Exception {
 
@@ -1204,8 +1016,6 @@ public class TagDBStore extends AbstractTagStore {
                if (service != null) {
                        Long serviceId = service.getId();
 
-                       List<XXTagAttribute> xxTagAttributes = 
daoManager.getXXTagAttribute().findByServiceIdAndOwner(serviceId, 
RangerTag.OWNER_SERVICERESOURCE);
-
                        List<XXTag> xxTags = 
daoManager.getXXTag().findByServiceIdAndOwner(serviceId, 
RangerTag.OWNER_SERVICERESOURCE);
 
                        List<XXTagResourceMap> xxTagResourceMaps = 
daoManager.getXXTagResourceMap().findByServiceId(serviceId);
@@ -1221,17 +1031,6 @@ public class TagDBStore extends AbstractTagStore {
                                }
                        }
 
-                       if (CollectionUtils.isNotEmpty(xxTagAttributes)) {
-                               for (XXTagAttribute xxTagAttribute : 
xxTagAttributes) {
-                                       try {
-                                               
daoManager.getXXTagAttribute().remove(xxTagAttribute);
-                                       } catch (Exception e) {
-                                               LOG.error("Error deleting 
RangerTagAttribute with id=" + xxTagAttribute.getId(), e);
-                                               throw e;
-                                       }
-                               }
-                       }
-
                        if (CollectionUtils.isNotEmpty(xxTags)) {
                                for (XXTag xxTag : xxTags) {
                                        try {
@@ -1243,32 +1042,6 @@ public class TagDBStore extends AbstractTagStore {
                                }
                        }
 
-                       List<XXServiceResourceElementValue> 
xxServiceResourceElementValues = 
daoManager.getXXServiceResourceElementValue().findByServiceId(serviceId);
-
-                       if 
(CollectionUtils.isNotEmpty(xxServiceResourceElementValues)) {
-                               for (XXServiceResourceElementValue 
xxServiceResourceElementValue : xxServiceResourceElementValues) {
-                                       try {
-                                               
daoManager.getXXServiceResourceElementValue().remove(xxServiceResourceElementValue);
-                                       } catch (Exception e) {
-                                               LOG.error("Error deleting 
ServiceResourceElementValue with id=" + xxServiceResourceElementValue.getId(), 
e);
-                                               throw e;
-                                       }
-                               }
-                       }
-
-                       List<XXServiceResourceElement> 
xxServiceResourceElements = 
daoManager.getXXServiceResourceElement().findByServiceId(serviceId);
-
-                       if 
(CollectionUtils.isNotEmpty(xxServiceResourceElements)) {
-                               for (XXServiceResourceElement 
xxServiceResourceElement : xxServiceResourceElements) {
-                                       try {
-                                               
daoManager.getXXServiceResourceElement().remove(xxServiceResourceElement);
-                                       } catch (Exception e) {
-                                               LOG.error("Error deleting 
ServiceResourceElement with id=" + xxServiceResourceElement.getId(), e);
-                                               throw e;
-                                       }
-                               }
-                       }
-
                        List<XXServiceResource> xxServiceResources = 
daoManager.getXXServiceResource().findByServiceId(serviceId);
 
                        if (CollectionUtils.isNotEmpty(xxServiceResources)) {

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
 
b/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
index 2a62fb4..536ca29 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/common/db/RangerTransactionSynchronizationAdapter.java
@@ -22,6 +22,7 @@ package org.apache.ranger.common.db;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -46,6 +47,7 @@ public class RangerTransactionSynchronizationAdapter extends 
TransactionSynchron
     private static final Log LOG = 
LogFactory.getLog(RangerTransactionSynchronizationAdapter.class);
 
     private static final ThreadLocal<List<Runnable>> RUNNABLES = new 
ThreadLocal<List<Runnable>>();
+    private static final ThreadLocal<List<Runnable>> RUNNABLES_AFTER_COMMIT = 
new ThreadLocal<List<Runnable>>();
 
     public void executeOnTransactionCompletion(Runnable runnable) {
         if (LOG.isDebugEnabled()) {
@@ -64,7 +66,7 @@ public class RangerTransactionSynchronizationAdapter extends 
TransactionSynchron
         TransactionSynchronizationAdapter
         */
 
-        if (!TransactionSynchronizationManager.isSynchronizationActive()) {
+        if (!registerSynchronization()) {
             LOG.info("Transaction synchronization is NOT ACTIVE. Executing 
right now runnable {" + runnable + "}");
             runnable.run();
             return;
@@ -73,9 +75,36 @@ public class RangerTransactionSynchronizationAdapter extends 
TransactionSynchron
         if (threadRunnables == null) {
             threadRunnables = new ArrayList<Runnable>();
             RUNNABLES.set(threadRunnables);
-            // Register a new transaction synchronization for the current 
thread.
-            // TransactionSynchronizationManage will call afterCompletion() 
when current transaction completes.
-            TransactionSynchronizationManager.registerSynchronization(this);
+        }
+        threadRunnables.add(runnable);
+    }
+
+    public void executeOnTransactionCommit(Runnable runnable) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Submitting new runnable {" + runnable + "} to run after 
transaction is committed");
+        }
+
+        /*
+        From TransactionSynchronizationManager documentation:
+        TransactionSynchronizationManager is a central helper that manages 
resources and transaction synchronizations per thread.
+        Resource management code should only register synchronizations when 
this manager is active,
+        which can be checked via isSynchronizationActive(); it should perform 
immediate resource cleanup else.
+        If transaction synchronization isn't active, there is either no 
current transaction,
+        or the transaction manager doesn't support transaction synchronization.
+
+        Note: Synchronization is an Interface for transaction synchronization 
callbacks which is implemented by
+        TransactionSynchronizationAdapter
+        */
+
+        if (!registerSynchronization()) {
+            LOG.info("Transaction synchronization is NOT ACTIVE. Executing 
right now runnable {" + runnable + "}");
+            runnable.run();
+            return;
+        }
+        List<Runnable> threadRunnables = RUNNABLES_AFTER_COMMIT.get();
+        if (threadRunnables == null) {
+            threadRunnables = new ArrayList<Runnable>();
+            RUNNABLES_AFTER_COMMIT.set(threadRunnables);
         }
         threadRunnables.add(runnable);
     }
@@ -83,48 +112,93 @@ public class RangerTransactionSynchronizationAdapter 
extends TransactionSynchron
     @Override
     public void afterCompletion(int status) {
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Transaction completed with status {" + (status == 
STATUS_COMMITTED ? "COMMITTED" : "ROLLED_BACK") + "}");
+            LOG.debug("==> 
RangerTransactionSynchronizationAdapter.afterCompletion(status=" + (status == 
STATUS_COMMITTED ? "COMMITTED" : "ROLLED_BACK") + ")");
         }
-        /* Thread runnables are expected to be executed only when the status 
is STATUS_ROLLED_BACK. Currently, executeOnTransactionCompletion()
-         * is called only for those changes that are going to be rolled-back 
by TransactionSynchronizationManager - such
-         * as when the operation returns HttpServletResponse.SC_NOT_MODIFIED 
status.
-         */
-        //if (status == STATUS_ROLLED_BACK) {
-            final List<Runnable> threadRunnables = RUNNABLES.get();
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Transaction completed, executing {" + 
threadRunnables.size() + "} runnables");
+
+        List<Runnable> allRunnables = null;
+
+        if (status == STATUS_COMMITTED) {
+            final List<Runnable> postCommitRunnables = 
RUNNABLES_AFTER_COMMIT.get();
+            if (CollectionUtils.isNotEmpty(postCommitRunnables)) {
+                allRunnables = postCommitRunnables;
             }
-            if (threadRunnables != null) {
-                try {
-                    //Create new  transaction
-                    TransactionTemplate txTemplate = new 
TransactionTemplate(txManager);
-                    
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
-
-                    txTemplate.execute(new TransactionCallback<Object>() {
-                        public Object doInTransaction(TransactionStatus 
status) {
-                            for (Runnable runnable : threadRunnables) {
-                                if (LOG.isDebugEnabled()) {
-                                    LOG.debug("Executing runnable {" + 
runnable + "}");
-                                }
-                                try {
-                                    runnable.run();
-                                } catch (RuntimeException e) {
-                                    LOG.error("Failed to execute runnable " + 
runnable, e);
-                                    break;
-                                }
-                            }
+        }
 
-                            return null;
-                        }
-                    });
-                } catch (Exception e) {
-                    LOG.error("Failed to commit TransactionService 
transaction", e);
-                    LOG.error("Ignoring...");
-                }
+        final List<Runnable> postCompletionRunnables = RUNNABLES.get();
+
+        if (CollectionUtils.isNotEmpty(postCompletionRunnables)) {
+            if (allRunnables == null) {
+                allRunnables = postCompletionRunnables;
+            } else {
+                allRunnables.addAll(postCompletionRunnables);
             }
+        }
 
-        //}
+        runRunnables(allRunnables);
+
+        RUNNABLES_AFTER_COMMIT.remove();
         RUNNABLES.remove();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== 
RangerTransactionSynchronizationAdapter.afterCompletion(status=" + (status == 
STATUS_COMMITTED ? "COMMITTED" : "ROLLED_BACK") + ")");
+        }
     }
 
+    private boolean registerSynchronization() {
+        final boolean ret = 
TransactionSynchronizationManager.isSynchronizationActive();
+        if (ret) {
+            List<Runnable> threadRunnablesOnCompletion = RUNNABLES.get();
+            List<Runnable> threadRunnablesOnCommit = 
RUNNABLES_AFTER_COMMIT.get();
+            if (threadRunnablesOnCompletion == null && threadRunnablesOnCommit 
== null) {
+                
TransactionSynchronizationManager.registerSynchronization(this);
+            }
+        }
+        return ret;
+    }
+
+    private void runRunnables(final List<Runnable> runnables) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("==> 
RangerTransactionSynchronizationAdapter.runRunnables()");
+        }
+
+        if (runnables != null) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Executing {" + runnables.size() + "} runnables");
+            }
+            try {
+                //Create new  transaction
+                TransactionTemplate txTemplate = new 
TransactionTemplate(txManager);
+                
txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
+
+                txTemplate.execute(new TransactionCallback<Object>() {
+                    public Object doInTransaction(TransactionStatus status) {
+                        for (Runnable runnable : runnables) {
+                            if (LOG.isDebugEnabled()) {
+                                LOG.debug("Executing runnable {" + runnable + 
"}");
+                            }
+                            try {
+                                runnable.run();
+                            } catch (RuntimeException e) {
+                                LOG.error("Failed to execute runnable " + 
runnable, e);
+                                break;
+                            }
+                        }
+
+                        return null;
+                    }
+                });
+            } catch (Exception e) {
+                LOG.error("Failed to commit TransactionService transaction", 
e);
+                LOG.error("Ignoring...");
+            }
+        } else {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("No runnables to execute");
+            }
+        }
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("<== 
RangerTransactionSynchronizationAdapter.runRunnables()");
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManager.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManager.java 
b/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManager.java
index e3b878b..80cb4be 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManager.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManager.java
@@ -28,6 +28,7 @@ import org.apache.log4j.Logger;
 import org.apache.ranger.common.RangerConstants;
 import org.apache.ranger.common.StringUtil;
 import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.common.db.RangerTransactionSynchronizationAdapter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -44,6 +45,9 @@ public class RangerDaoManager extends RangerDaoManagerBase {
        @Autowired
        StringUtil stringUtil;
 
+       @Autowired
+       RangerTransactionSynchronizationAdapter 
transactionSynchronizationAdapter;
+
        @Override
        public EntityManager getEntityManager() {
                return em;
@@ -68,15 +72,19 @@ public class RangerDaoManager extends RangerDaoManagerBase {
                return stringUtil;
        }
 
-       /*
-        * (non-Javadoc)
-        */
-       @Override
-       public BaseDao<?> getDaoForClassType(int classType) {
-               if (classType == RangerConstants.CLASS_TYPE_NONE) {
-                       return null;
-               }
-               return super.getDaoForClassType(classType);
+        /**
+         * (non-Javadoc)
+         */
+        @Override
+        public BaseDao<?> getDaoForClassType(int classType) {
+                if (classType == RangerConstants.CLASS_TYPE_NONE) {
+                      return null;
+                }
+                return super.getDaoForClassType(classType);
+        }
+
+       public RangerTransactionSynchronizationAdapter 
getRangerTransactionSynchronizationAdapter() {
+               return transactionSynchronizationAdapter;
        }
 
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManagerBase.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManagerBase.java 
b/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManagerBase.java
index d718441..1886aae 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManagerBase.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/RangerDaoManagerBase.java
@@ -24,19 +24,13 @@
  */
 
 import javax.persistence.EntityManager;
-
 import org.apache.log4j.Logger;
 import org.apache.ranger.common.AppConstants;
-import org.apache.ranger.common.RESTErrorUtil;
 import org.apache.ranger.common.db.BaseDao;
-import org.springframework.beans.factory.annotation.Autowired;
-
 
 public abstract class RangerDaoManagerBase {
        private static final Logger logger = 
Logger.getLogger(RangerDaoManagerBase.class);
 
-       @Autowired
-       protected RESTErrorUtil restErrorUtil;
        abstract public EntityManager getEntityManager();
 
        public RangerDaoManagerBase() {
@@ -366,6 +360,24 @@ public abstract class RangerDaoManagerBase {
                if (className.equals("XXPluginInfo")) {
                        return getXXPluginInfo();
                }
+               if (className.equals("XXPolicyRefCondition")) {
+                       return getXXPolicyRefCondition();
+               }
+               if (className.equals("XXPolicyRefGroup")) {
+                       return getXXPolicyRefGroup();
+               }
+               if (className.equals("XXPolicyRefDataMaskType")) {
+                       return getXXPolicyRefDataMaskType();
+               }
+               if (className.equals("XXPolicyRefResource")) {
+                       return getXXPolicyRefResource();
+               }
+               if (className.equals("XXPolicyRefUser")) {
+                       return getXXPolicyRefUser();
+               }
+               if (className.equals("XXPolicyRefAccessType")) {
+                       return getXXPolicyRefAccessType();
+               }
                logger.error("No DaoManager found for className=" + className, 
new Throwable());
                return null;
        }
@@ -590,5 +602,29 @@ public abstract class RangerDaoManagerBase {
        public XXPluginInfoDao getXXPluginInfo() {
                return new XXPluginInfoDao(this);
        }
+
+       public XXPolicyRefConditionDao getXXPolicyRefCondition() {
+               return new XXPolicyRefConditionDao(this);
+       }
+
+       public XXPolicyRefGroupDao getXXPolicyRefGroup() {
+               return new XXPolicyRefGroupDao(this);
+       }
+
+       public XXPolicyRefDataMaskTypeDao getXXPolicyRefDataMaskType() {
+               return new XXPolicyRefDataMaskTypeDao(this);
+       }
+
+       public XXPolicyRefResourceDao getXXPolicyRefResource() {
+               return new XXPolicyRefResourceDao(this);
+       }
+
+       public XXPolicyRefUserDao getXXPolicyRefUser() {
+               return new XXPolicyRefUserDao(this);
+       }
+
+       public XXPolicyRefAccessTypeDao getXXPolicyRefAccessType() {
+               return new XXPolicyRefAccessTypeDao(this);
+       }
 }
 

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXAccessTypeDefDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXAccessTypeDefDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXAccessTypeDefDao.java
index 8f3a506..8db657f 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/XXAccessTypeDefDao.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXAccessTypeDefDao.java
@@ -58,5 +58,4 @@ public class XXAccessTypeDefDao extends 
BaseDao<XXAccessTypeDef> {
                        return null;
                }
        }
-
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXDataMaskTypeDefDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXDataMaskTypeDefDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXDataMaskTypeDefDao.java
index f6e1aff..7e43db4 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXDataMaskTypeDefDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXDataMaskTypeDefDao.java
@@ -58,5 +58,4 @@ public class XXDataMaskTypeDefDao extends 
BaseDao<XXDataMaskTypeDef> {
                        return null;
                }
        }
-
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXGroupDao.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/db/XXGroupDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXGroupDao.java
index 19e2e11..acc8700 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/XXGroupDao.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXGroupDao.java
@@ -23,8 +23,6 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import javax.persistence.NoResultException;
-
 import org.apache.ranger.common.db.BaseDao;
 import org.apache.ranger.entity.XXGroup;
 
@@ -66,19 +64,5 @@ public class XXGroupDao extends BaseDao<XXGroup> {
                }
                return null;
        }
-       
-       @SuppressWarnings("unchecked")
-       public List<String> findByPolicyItemId(Long polItemId) {
-               if (polItemId == null) {
-                       return null;
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXGroup.findByPolicyItemId")
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return null;
-               }
-       }
 
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyConditionDefDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyConditionDefDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyConditionDefDao.java
index 21afcac..e2048ae 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyConditionDefDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyConditionDefDao.java
@@ -59,33 +59,5 @@ public class XXPolicyConditionDefDao extends 
BaseDao<XXPolicyConditionDef> {
                        return null;
                }
        }
-
-       public List<XXPolicyConditionDef> findByPolicyItemId(Long polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyConditionDef>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyConditionDef.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyConditionDef>();
-               }
-       }
-       
-       public XXPolicyConditionDef findByPolicyItemIdAndName(Long polItemId, 
String name) {
-               if(polItemId == null || name == null) {
-                       return null;
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyConditionDef.findByPolicyItemIdAndName", tClass)
-                                       .setParameter("polItemId", polItemId)
-                                       .setParameter("name", 
name).getSingleResult();
-               } catch (NoResultException e) {
-                       return null;
-               }
-       }
-
        
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemAccessDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemAccessDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemAccessDao.java
index de37e10..aeafc8a 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemAccessDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemAccessDao.java
@@ -30,19 +30,6 @@ public class XXPolicyItemAccessDao extends 
BaseDao<XXPolicyItemAccess> {
        public XXPolicyItemAccessDao(RangerDaoManagerBase daoManager) {
                super(daoManager);
        }
-       
-       public List<XXPolicyItemAccess> findByPolicyItemId(Long polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyItemAccess>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemAccess.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemAccess>();
-               }
-       }
 
        public List<XXPolicyItemAccess> findByPolicyId(Long policyId) {
                if(policyId == null) {
@@ -70,16 +57,4 @@ public class XXPolicyItemAccessDao extends 
BaseDao<XXPolicyItemAccess> {
                }
        }
 
-       public List<XXPolicyItemAccess> findByType(Long type) {
-               if (type == null) {
-                       return new ArrayList<XXPolicyItemAccess>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXPolicyItemAccess.findByType", tClass)
-                                       .setParameter("type", 
type).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemAccess>();
-               }
-       }
-
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemConditionDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemConditionDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemConditionDao.java
index 11596ef..4b974a7 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemConditionDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemConditionDao.java
@@ -30,19 +30,6 @@ public class XXPolicyItemConditionDao extends 
BaseDao<XXPolicyItemCondition> {
        public XXPolicyItemConditionDao(RangerDaoManagerBase daoManager) {
                super(daoManager);
        }
-       
-       public List<XXPolicyItemCondition> findByPolicyItemId(Long polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyItemCondition>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemCondition.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemCondition>();
-               }
-       }
 
        public List<XXPolicyItemCondition> findByPolicyId(Long policyId) {
                if(policyId == null) {
@@ -70,31 +57,4 @@ public class XXPolicyItemConditionDao extends 
BaseDao<XXPolicyItemCondition> {
                }
        }
 
-       public List<XXPolicyItemCondition> findByPolicyItemAndDefId(Long 
polItemId,
-                       Long polCondDefId) {
-               if(polItemId == null || polCondDefId == null) {
-                       return new ArrayList<XXPolicyItemCondition>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemCondition.findByPolicyItemAndDefId", tClass)
-                                       .setParameter("polItemId", polItemId)
-                                       .setParameter("polCondDefId", 
polCondDefId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemCondition>();
-               }
-       }
-
-       public List<XXPolicyItemCondition> findByPolicyConditionDefId(Long 
polCondDefId) {
-               if (polCondDefId == null) {
-                       return new ArrayList<XXPolicyItemCondition>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXPolicyItemCondition.findByPolicyConditionDefId",
 tClass)
-                                       .setParameter("polCondDefId", 
polCondDefId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemCondition>();
-               }
-       }
-
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemDataMaskInfoDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemDataMaskInfoDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemDataMaskInfoDao.java
index a8418c6..7deda62 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemDataMaskInfoDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemDataMaskInfoDao.java
@@ -30,19 +30,6 @@ public class XXPolicyItemDataMaskInfoDao extends 
BaseDao<XXPolicyItemDataMaskInf
        public XXPolicyItemDataMaskInfoDao(RangerDaoManagerBase daoManager) {
                super(daoManager);
        }
-       
-       public List<XXPolicyItemDataMaskInfo> findByPolicyItemId(Long 
polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyItemDataMaskInfo>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemDataMaskInfo.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemDataMaskInfo>();
-               }
-       }
 
        public List<XXPolicyItemDataMaskInfo> findByPolicyId(Long policyId) {
                if(policyId == null) {
@@ -70,16 +57,4 @@ public class XXPolicyItemDataMaskInfoDao extends 
BaseDao<XXPolicyItemDataMaskInf
                }
        }
 
-       public List<XXPolicyItemDataMaskInfo> findByType(Long type) {
-               if (type == null) {
-                       return new ArrayList<XXPolicyItemDataMaskInfo>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXPolicyItemDataMaskInfo.findByType", 
tClass)
-                                       .setParameter("type", 
type).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemDataMaskInfo>();
-               }
-       }
-
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemGroupPermDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemGroupPermDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemGroupPermDao.java
index 8c05699..aa4a349 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemGroupPermDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemGroupPermDao.java
@@ -31,19 +31,6 @@ public class XXPolicyItemGroupPermDao extends 
BaseDao<XXPolicyItemGroupPerm> {
                super(daoManager);
        }
 
-       public List<XXPolicyItemGroupPerm> findByPolicyItemId(Long polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyItemGroupPerm>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemGroupPerm.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemGroupPerm>();
-               }
-       }
-
        public List<XXPolicyItemGroupPerm> findByPolicyId(Long policyId) {
                if(policyId == null) {
                        return new ArrayList<XXPolicyItemGroupPerm>();

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemRowFilterInfoDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemRowFilterInfoDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemRowFilterInfoDao.java
index 4618e7d..ff889e7 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemRowFilterInfoDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemRowFilterInfoDao.java
@@ -29,19 +29,6 @@ public class XXPolicyItemRowFilterInfoDao extends 
BaseDao<XXPolicyItemRowFilterI
        public XXPolicyItemRowFilterInfoDao(RangerDaoManagerBase daoManager) {
                super(daoManager);
        }
-       
-       public List<XXPolicyItemRowFilterInfo> findByPolicyItemId(Long 
polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyItemRowFilterInfo>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemRowFilterInfo.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemRowFilterInfo>();
-               }
-       }
 
        public List<XXPolicyItemRowFilterInfo> findByPolicyId(Long policyId) {
                if(policyId == null) {

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemUserPermDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemUserPermDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemUserPermDao.java
index 40a0da1..66f1566 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemUserPermDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyItemUserPermDao.java
@@ -31,19 +31,6 @@ public class XXPolicyItemUserPermDao extends 
BaseDao<XXPolicyItemUserPerm> {
                super(daoManager);
        }
 
-       public List<XXPolicyItemUserPerm> findByPolicyItemId(Long polItemId) {
-               if(polItemId == null) {
-                       return new ArrayList<XXPolicyItemUserPerm>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyItemUserPerm.findByPolicyItemId", tClass)
-                                       .setParameter("polItemId", 
polItemId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyItemUserPerm>();
-               }
-       }
-
        public List<XXPolicyItemUserPerm> findByPolicyId(Long policyId) {
                if(policyId == null) {
                        return new ArrayList<XXPolicyItemUserPerm>();

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefAccessTypeDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefAccessTypeDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefAccessTypeDao.java
new file mode 100644
index 0000000..1ef01bb
--- /dev/null
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefAccessTypeDao.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.db;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.NoResultException;
+
+import org.apache.ranger.biz.RangerPolicyRetriever;
+import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.entity.XXPolicyRefAccessType;
+import org.springframework.stereotype.Service;
+
+@Service
+public class XXPolicyRefAccessTypeDao extends BaseDao<XXPolicyRefAccessType> {
+
+       public XXPolicyRefAccessTypeDao(RangerDaoManagerBase daoManager)  {
+               super(daoManager);
+       }
+
+       public List<XXPolicyRefAccessType> findByPolicyId(Long polId) {
+               if(polId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return getEntityManager()
+                                       
.createNamedQuery("XXPolicyRefAccessType.findByPolicyId", tClass)
+                                       .setParameter("policyId", 
polId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       public List<XXPolicyRefAccessType> findByAccessTypeDefId(Long 
accessTypeDefId) {
+               if (accessTypeDefId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefAccessType.findByAccessTypeDefId",
 tClass)
+                                       .setParameter("accessDefId", 
accessTypeDefId)
+                                       .getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+    public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedAccessNamesByPolicy(Long policyId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (policyId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefAccessType.findUpdatedAccessNamesByPolicy")
+                    .setParameter("policy", policyId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+       @SuppressWarnings("unchecked")
+       public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedAccessNamesByService(Long serviceId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (serviceId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefAccessType.findUpdatedAccessNamesByService")
+                    .setParameter("service", serviceId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefConditionDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefConditionDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefConditionDao.java
new file mode 100644
index 0000000..2c04ab4
--- /dev/null
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefConditionDao.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.db;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.NoResultException;
+
+import org.apache.ranger.biz.RangerPolicyRetriever;
+import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.entity.XXPolicyRefCondition;
+import org.springframework.stereotype.Service;
+
+@Service
+public class XXPolicyRefConditionDao extends BaseDao<XXPolicyRefCondition>  {
+
+       public XXPolicyRefConditionDao(RangerDaoManagerBase daoManager) {
+               super(daoManager);
+       }
+
+       public List<XXPolicyRefCondition> findByPolicyId(Long polId) {
+               if(polId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return getEntityManager()
+                                       
.createNamedQuery("XXPolicyRefCondition.findByPolicyId", tClass)
+                                       .setParameter("policyId", 
polId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+       public List<XXPolicyRefCondition> findByConditionName(String 
conditionName) {
+               if (conditionName == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefCondition.findByConditionName", 
tClass)
+                                       .setParameter("conditionName", 
conditionName).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       public List<XXPolicyRefCondition> findByConditionDefId(Long 
conditionDefId) {
+               if (conditionDefId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefCondition.findByConditionDefId",
 tClass)
+                                       .setParameter("conditionDefId", 
conditionDefId)
+                                       .getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+    public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedConditionNamesByPolicy(Long policyId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (policyId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefCondition.findUpdatedConditionNamesByPolicy")
+                    .setParameter("policy", policyId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+       @SuppressWarnings("unchecked")
+       public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedConditionNamesByService(Long serviceId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (serviceId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefCondition.findUpdatedConditionNamesByService")
+                    .setParameter("service", serviceId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefDataMaskTypeDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefDataMaskTypeDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefDataMaskTypeDao.java
new file mode 100644
index 0000000..258e3b0
--- /dev/null
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefDataMaskTypeDao.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.db;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.NoResultException;
+
+import org.apache.ranger.biz.RangerPolicyRetriever;
+import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.entity.XXPolicyRefDataMaskType;
+import org.springframework.stereotype.Service;
+
+@Service
+public class XXPolicyRefDataMaskTypeDao extends 
BaseDao<XXPolicyRefDataMaskType>{
+
+       public XXPolicyRefDataMaskTypeDao(RangerDaoManagerBase daoManager)  {
+               super(daoManager);
+       }
+
+       public List<XXPolicyRefDataMaskType> findByPolicyId(Long policyId) {
+               if(policyId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return getEntityManager()
+                                       
.createNamedQuery("XXPolicyRefDataMaskType.findByPolicyId", tClass)
+                                       .setParameter("policyId", 
policyId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+    public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedDataMaskNamesByPolicy(Long policyId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (policyId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefDataMaskType.findUpdatedDataMaskNamesByPolicy")
+                    .setParameter("policy", policyId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+       @SuppressWarnings("unchecked")
+       public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedDataMaskNamesByService(Long serviceId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (serviceId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefDataMaskType.findUpdatedDataMaskNamesByService")
+                    .setParameter("service", serviceId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefGroupDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefGroupDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefGroupDao.java
new file mode 100644
index 0000000..08829d4
--- /dev/null
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefGroupDao.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.db;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.NoResultException;
+
+import org.apache.ranger.biz.RangerPolicyRetriever;
+import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.entity.XXPolicyRefGroup;
+import org.springframework.stereotype.Service;
+
+@Service
+public class XXPolicyRefGroupDao extends BaseDao<XXPolicyRefGroup>{
+
+
+       public XXPolicyRefGroupDao(RangerDaoManagerBase daoManager)  {
+               super(daoManager);
+       }
+
+       public List<XXPolicyRefGroup> findByPolicyId(Long policyId) {
+               if(policyId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return getEntityManager()
+                                       
.createNamedQuery("XXPolicyRefGroup.findByPolicyId", tClass)
+                                       .setParameter("policyId", 
policyId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+       public List<XXPolicyRefGroup> findByGroupName(String groupName) {
+               if (groupName == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefGroup.findByGroupName", tClass)
+                                       .setParameter("groupName", 
groupName).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+    @SuppressWarnings("unchecked")
+    public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedGroupNamesByPolicy(Long policyId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (policyId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefGroup.findUpdatedGroupNamesByPolicy")
+                    .setParameter("policy", policyId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+       @SuppressWarnings("unchecked")
+       public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedGroupNamesByService(Long serviceId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (serviceId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefGroup.findUpdatedGroupNamesByService")
+                    .setParameter("service", serviceId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefResourceDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefResourceDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefResourceDao.java
new file mode 100644
index 0000000..e259ee8
--- /dev/null
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefResourceDao.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.db;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.NoResultException;
+
+import org.apache.ranger.biz.RangerPolicyRetriever;
+import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.entity.XXPolicyRefResource;
+import org.springframework.stereotype.Service;
+
+@Service
+public class XXPolicyRefResourceDao extends BaseDao<XXPolicyRefResource>{
+
+       public XXPolicyRefResourceDao(RangerDaoManagerBase daoManager)  {
+               super(daoManager);
+       }
+
+       public List<XXPolicyRefResource> findByPolicyId(Long policyId) {
+               if(policyId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return getEntityManager()
+                                       
.createNamedQuery("XXPolicyRefResource.findByPolicyId", tClass)
+                                       .setParameter("policyId", 
policyId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       public List<XXPolicyRefResource> findByResourceDefID(Long 
resourceDefId) {
+               if (resourceDefId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefResource.findByResourceDefId", 
tClass)
+                                       .setParameter("resourceDefId", 
resourceDefId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+        @SuppressWarnings("unchecked")
+           public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedResourceNamesByPolicy(Long policyId) {
+               List<RangerPolicyRetriever.PolicyTextNameMap> ret = new 
ArrayList<>();
+               if (policyId != null) {
+                   List<Object[]> rows = (List<Object[]>) getEntityManager()
+                           
.createNamedQuery("XXPolicyRefResource.findUpdatedResourceNamesByPolicy")
+                           .setParameter("policy", policyId)
+                           .getResultList();
+                   if (rows != null) {
+                       for (Object[] row : rows) {
+                           ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                       }
+                   }
+               }
+               return ret;
+           }
+
+               @SuppressWarnings("unchecked")
+               public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedResourceNamesByService(Long serviceId) {
+               List<RangerPolicyRetriever.PolicyTextNameMap> ret = new 
ArrayList<>();
+               if (serviceId != null) {
+                   List<Object[]> rows = (List<Object[]>) getEntityManager()
+                           
.createNamedQuery("XXPolicyRefResource.findUpdatedResourceNamesByService")
+                           .setParameter("service", serviceId)
+                           .getResultList();
+                   if (rows != null) {
+                       for (Object[] row : rows) {
+                           ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                       }
+                   }
+               }
+               return ret;
+           }
+}

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefUserDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefUserDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefUserDao.java
new file mode 100644
index 0000000..f7b6131
--- /dev/null
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyRefUserDao.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.ranger.db;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.persistence.NoResultException;
+
+import org.apache.ranger.biz.RangerPolicyRetriever;
+import org.apache.ranger.common.db.BaseDao;
+import org.apache.ranger.entity.XXPolicyRefUser;
+import org.springframework.stereotype.Service;
+
+@Service
+public class XXPolicyRefUserDao extends BaseDao<XXPolicyRefUser>{
+
+
+       public XXPolicyRefUserDao(RangerDaoManagerBase daoManager)  {
+               super(daoManager);
+       }
+
+       public List<XXPolicyRefUser> findByPolicyId(Long policyId) {
+               if(policyId == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return getEntityManager()
+                                       
.createNamedQuery("XXPolicyRefUser.findByPolicyId", tClass)
+                                       .setParameter("policyId", 
policyId).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+       public List<XXPolicyRefUser> findByUserName(String userName) {
+               if (userName == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefUser.findByUserName", tClass)
+                                       .setParameter("userName", 
userName).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       public List<XXPolicyRefUser> findByUserId(String userID) {
+               if (userID == null) {
+                       return Collections.EMPTY_LIST;
+               }
+               try {
+                       return 
getEntityManager().createNamedQuery("XXPolicyRefUser.findByUserId", tClass)
+                                       .setParameter("userID", 
userID).getResultList();
+               } catch (NoResultException e) {
+                       return Collections.EMPTY_LIST;
+               }
+       }
+
+       @SuppressWarnings("unchecked")
+    public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedUserNamesByPolicy(Long policyId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (policyId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefUser.findUpdatedUserNamesByPolicy")
+                    .setParameter("policy", policyId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+       @SuppressWarnings("unchecked")
+       public List<RangerPolicyRetriever.PolicyTextNameMap> 
findUpdatedUserNamesByService(Long serviceId) {
+        List<RangerPolicyRetriever.PolicyTextNameMap> ret = new ArrayList<>();
+        if (serviceId != null) {
+            List<Object[]> rows = (List<Object[]>) getEntityManager()
+                    
.createNamedQuery("XXPolicyRefUser.findUpdatedUserNamesByService")
+                    .setParameter("service", serviceId)
+                    .getResultList();
+            if (rows != null) {
+                for (Object[] row : rows) {
+                    ret.add(new 
RangerPolicyRetriever.PolicyTextNameMap((Long)row[0], (String)row[1], 
(String)row[2]));
+                }
+            }
+        }
+        return ret;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceDao.java
index 4b04b96..c34e44e 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceDao.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceDao.java
@@ -30,20 +30,6 @@ public class XXPolicyResourceDao extends 
BaseDao<XXPolicyResource> {
        public XXPolicyResourceDao(RangerDaoManagerBase daoManager) {
                super(daoManager);
        }
-       
-       public XXPolicyResource findByResDefIdAndPolicyId(Long resDefId, Long 
polId) {
-               if(resDefId == null || polId == null) {
-                       return null;
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyResource.findByResDefIdAndPolicyId", tClass)
-                                       .setParameter("resDefId", 
resDefId).setParameter("polId", polId)
-                                       .getSingleResult();
-               } catch (NoResultException e) {
-                       return null;
-               }
-       }
 
        public List<XXPolicyResource> findByPolicyId(Long policyId) {
                if(policyId == null) {

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceMapDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceMapDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceMapDao.java
index 7065737..edda109 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceMapDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXPolicyResourceMapDao.java
@@ -30,19 +30,6 @@ public class XXPolicyResourceMapDao extends 
BaseDao<XXPolicyResourceMap> {
        public XXPolicyResourceMapDao(RangerDaoManagerBase daoManager) {
                super(daoManager);
        }
-       
-       public List<XXPolicyResourceMap> findByPolicyResId(Long polResId) {
-               if(polResId == null) {
-                       return new ArrayList<XXPolicyResourceMap>();
-               }
-               try {
-                       return getEntityManager()
-                                       
.createNamedQuery("XXPolicyResourceMap.findByPolicyResId", tClass)
-                                       .setParameter("polResId", 
polResId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXPolicyResourceMap>();
-               }
-       }
 
        public List<XXPolicyResourceMap> findByPolicyId(Long policyId) {
                if(policyId == null) {

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXResourceDefDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXResourceDefDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXResourceDefDao.java
index aee56e8..646e61d 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/XXResourceDefDao.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXResourceDefDao.java
@@ -97,5 +97,4 @@ public class XXResourceDefDao extends BaseDao<XXResourceDef> {
                        return new ArrayList<XXResourceDef>();
                }
        }
-
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceDao.java
index f87c0ae..7c25d61 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceDao.java
@@ -24,6 +24,7 @@ import java.util.List;
 
 import javax.persistence.NoResultException;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.ranger.authorization.utils.StringUtil;
 import org.apache.ranger.common.db.BaseDao;
@@ -73,15 +74,32 @@ public class XXServiceResourceDao extends 
BaseDao<XXServiceResource> {
        }
 
        public List<XXServiceResource> findTaggedResourcesInServiceId(Long 
serviceId) {
-               if (serviceId == null) {
-                       return new ArrayList<XXServiceResource>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXServiceResource.findTaggedResourcesInServiceId",
 tClass)
-                                       .setParameter("serviceId", 
serviceId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXServiceResource>();
-               }
+           List<XXServiceResource> ret = new ArrayList<>();
+               if (serviceId != null) {
+            List<Object[]> rows = null;
+            try {
+                rows = 
getEntityManager().createNamedQuery("XXServiceResource.findTaggedResourcesInServiceId",
 Object[].class)
+                        .setParameter("serviceId", serviceId).getResultList();
+            } catch (NoResultException e) {
+                // Nothing
+            }
+            if (CollectionUtils.isNotEmpty(rows)) {
+                for (Object[] row : rows) {
+                    XXServiceResource xxServiceResource = new 
XXServiceResource();
+                    xxServiceResource.setId((Long) row[0]);
+                    xxServiceResource.setGuid((String) row[1]);
+                    xxServiceResource.setVersion((Long) row[2]);
+                    xxServiceResource.setIsEnabled((Boolean) row[3]);
+                    xxServiceResource.setResourceSignature((String) row[4]);
+                    xxServiceResource.setServiceId((Long) row[5]);
+                    xxServiceResource.setServiceResourceElements((String) 
row[6]);
+                    xxServiceResource.setTags((String) row[7]);
+
+                    ret.add(xxServiceResource);
+                }
+            }
+        }
+        return ret;
        }
 
        public long countTaggedResourcesInServiceId(Long serviceId) {

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementDao.java
index 72fe214..22a0e01 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementDao.java
@@ -72,16 +72,4 @@ public class XXServiceResourceElementDao extends 
BaseDao<XXServiceResourceElemen
                }
        }
 
-       public List<XXServiceResourceElement> findForServicePlugin(Long 
serviceId) {
-               if (serviceId == null) {
-                       return new ArrayList<XXServiceResourceElement>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXServiceResourceElement.findForServicePlugin",
 tClass)
-                                       .setParameter("serviceId", serviceId)
-                                       .getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXServiceResourceElement>();
-               }
-       }
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementValueDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementValueDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementValueDao.java
index e726d04..3170edd 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementValueDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceResourceElementValueDao.java
@@ -85,19 +85,6 @@ public class XXServiceResourceElementValueDao extends 
BaseDao<XXServiceResourceE
        }
 
        @SuppressWarnings("unchecked")
-       public List<XXServiceResourceElementValue> findForServicePlugin(Long 
serviceId) {
-               if (serviceId == null) {
-                       return new ArrayList<XXServiceResourceElementValue>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXServiceResourceElementValue.findForServicePlugin")
-                                       .setParameter("serviceId", 
serviceId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXServiceResourceElementValue>();
-               }
-       }
-
-       @SuppressWarnings("unchecked")
        public List<XXServiceResourceElementValue> findByResourceId(Long 
resourceId) {
                if (resourceId == null) {
                        return new ArrayList<XXServiceResourceElementValue>();

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXServiceVersionInfoDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceVersionInfoDao.java
 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceVersionInfoDao.java
index 0098bff..8e4376d 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXServiceVersionInfoDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXServiceVersionInfoDao.java
@@ -23,6 +23,7 @@ import java.util.List;
 import javax.persistence.NoResultException;
 
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.ranger.biz.ServiceDBStore;
 import org.apache.ranger.common.db.BaseDao;
 import org.apache.ranger.entity.XXServiceVersionInfo;
 
@@ -30,6 +31,7 @@ import org.apache.ranger.entity.XXServiceVersionInfo;
  */
 
 public class XXServiceVersionInfoDao extends BaseDao<XXServiceVersionInfo> {
+
        /**
         * Default Constructor
         */
@@ -121,14 +123,14 @@ public class XXServiceVersionInfoDao extends 
BaseDao<XXServiceVersionInfo> {
                }
 
                for(XXServiceVersionInfo serviceVersionInfo : 
serviceVersionInfos) {
-                       Long currentTagVersion = 
serviceVersionInfo.getTagVersion();
+                       final RangerDaoManager finaldaoManager            = 
daoManager;
+                       final Long                     finalServiceId           
  = serviceVersionInfo.getServiceId();
+                       final ServiceDBStore.VERSION_TYPE versionType = 
ServiceDBStore.VERSION_TYPE.TAG_VERSION;
 
-                       if(currentTagVersion == null) {
-                               currentTagVersion = Long.valueOf(0);
-                       }
+                       Runnable serviceVersionUpdater = new 
ServiceDBStore.ServiceVersionUpdater(finaldaoManager, finalServiceId, 
versionType);
 
-                       serviceVersionInfo.setTagVersion(currentTagVersion + 1);
-                       serviceVersionInfo.setTagUpdateTime(updateTime);
+                       
daoManager.getRangerTransactionSynchronizationAdapter().executeOnTransactionCommit(serviceVersionUpdater);
                }
+
        }
 }

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDao.java
index e361b33..a24e3db 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDao.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDao.java
@@ -57,18 +57,6 @@ public class XXTagAttributeDao extends 
BaseDao<XXTagAttribute> {
                }
        }
 
-       public List<XXTagAttribute> findForServicePlugin(Long serviceId) {
-               if (serviceId == null) {
-                       return new ArrayList<XXTagAttribute>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXTagAttribute.findForServicePlugin", 
tClass)
-                                       .setParameter("serviceId", 
serviceId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXTagAttribute>();
-               }
-       }
-
        public List<XXTagAttribute> findByServiceIdAndOwner(Long serviceId, 
Short owner) {
                if (serviceId == null) {
                        return new ArrayList<XXTagAttribute>();

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDefDao.java
----------------------------------------------------------------------
diff --git 
a/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDefDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDefDao.java
index 145399f..294c222 100644
--- 
a/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDefDao.java
+++ 
b/security-admin/src/main/java/org/apache/ranger/db/XXTagAttributeDefDao.java
@@ -57,18 +57,6 @@ public class XXTagAttributeDefDao extends 
BaseDao<XXTagAttributeDef> {
                }
        }
 
-       public List<XXTagAttributeDef> findForServicePlugin(Long serviceId) {
-               if (serviceId == null) {
-                       return new ArrayList<XXTagAttributeDef>();
-               }
-               try {
-                       return 
getEntityManager().createNamedQuery("XXTagAttributeDef.findForServicePlugin", 
tClass)
-                                       .setParameter("serviceId", 
serviceId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXTagAttributeDef>();
-               }
-       }
-
        public List<XXTagAttributeDef> findByResourceId(Long resourceId) {
                if (resourceId == null) {
                        return new ArrayList<XXTagAttributeDef>();

http://git-wip-us.apache.org/repos/asf/ranger/blob/c84b98fb/security-admin/src/main/java/org/apache/ranger/db/XXTagDao.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/db/XXTagDao.java 
b/security-admin/src/main/java/org/apache/ranger/db/XXTagDao.java
index 77428db..199a155 100644
--- a/security-admin/src/main/java/org/apache/ranger/db/XXTagDao.java
+++ b/security-admin/src/main/java/org/apache/ranger/db/XXTagDao.java
@@ -124,19 +124,6 @@ public class XXTagDao extends BaseDao<XXTag> {
                }
        }
 
-       public List<XXTag> findForServicePlugin(Long serviceId) {
-               if (serviceId == null) {
-                       return new ArrayList<XXTag>();
-               }
-
-               try {
-                       return 
getEntityManager().createNamedQuery("XXTag.findForServicePlugin", tClass)
-                                       .setParameter("serviceId", 
serviceId).getResultList();
-               } catch (NoResultException e) {
-                       return new ArrayList<XXTag>();
-               }
-       }
-
        public List<XXTag> findByServiceIdAndOwner(Long serviceId, Short owner) 
{
                if (serviceId == null) {
                        return new ArrayList<XXTag>();

Reply via email to