You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by zh...@apache.org on 2017/03/08 06:04:12 UTC
ranger git commit: RANGER-1419:Do some code improvement in Java
method XTrxLogService.searchXTrxLogs
Repository: ranger
Updated Branches:
refs/heads/master 6ad60b5d3 -> 6854f4d3c
RANGER-1419:Do some code improvement in Java method XTrxLogService.searchXTrxLogs
Signed-off-by: zhangqiang2 <zh...@zte.com.cn>
Project: http://git-wip-us.apache.org/repos/asf/ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/ranger/commit/6854f4d3
Tree: http://git-wip-us.apache.org/repos/asf/ranger/tree/6854f4d3
Diff: http://git-wip-us.apache.org/repos/asf/ranger/diff/6854f4d3
Branch: refs/heads/master
Commit: 6854f4d3c7624a340a26e4df194effc7d792dbf1
Parents: 6ad60b5
Author: zhangqiang2 <zh...@zte.com.cn>
Authored: Tue Mar 7 08:46:55 2017 +0800
Committer: zhangqiang2 <zh...@zte.com.cn>
Committed: Wed Mar 8 01:02:41 2017 -0500
----------------------------------------------------------------------
.../apache/ranger/service/XTrxLogService.java | 310 +++++++------------
1 file changed, 114 insertions(+), 196 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ranger/blob/6854f4d3/security-admin/src/main/java/org/apache/ranger/service/XTrxLogService.java
----------------------------------------------------------------------
diff --git a/security-admin/src/main/java/org/apache/ranger/service/XTrxLogService.java b/security-admin/src/main/java/org/apache/ranger/service/XTrxLogService.java
index 20db24e..6ca2d22 100644
--- a/security-admin/src/main/java/org/apache/ranger/service/XTrxLogService.java
+++ b/security-admin/src/main/java/org/apache/ranger/service/XTrxLogService.java
@@ -34,6 +34,7 @@ import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.SingularAttribute;
+import org.apache.commons.lang.StringUtils;
import org.apache.ranger.common.SearchCriteria;
import org.apache.ranger.common.SearchField;
import org.apache.ranger.common.SortField;
@@ -47,6 +48,7 @@ import org.apache.ranger.view.VXTrxLogList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
@Service
@Scope("singleton")
@@ -80,100 +82,14 @@ public class XTrxLogService extends XTrxLogServiceBase<XXTrxLog, VXTrxLog> {
@Override
public VXTrxLogList searchXTrxLogs(SearchCriteria searchCriteria) {
-
EntityManager em = daoMgr.getEntityManager();
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
- Metamodel entityMetaModel = em.getMetamodel();
- Class<VXXTrxLog> klass = VXXTrxLog.class;
- EntityType<VXXTrxLog> entityType = entityMetaModel.entity(klass);
- CriteriaQuery<VXXTrxLog> selectCQ = criteriaBuilder.createQuery(klass);
- Root<VXXTrxLog> rootEntityType = selectCQ.from(klass);
- Predicate predicate = criteriaBuilder.conjunction();
- String fieldName=null;
- String clientFieldName =null;
- Object paramValue = null;
- boolean isListValue = false;
- SingularAttribute attr =null;
- Collection<Number> intValueList = null;
- Date fieldValue =null;
- Predicate stringPredicate =null;
- Predicate intPredicate = null;
- Predicate datePredicate =null;
- Map<String, Object> paramList = searchCriteria.getParamList();
- for(String key : paramList.keySet()){
- for(SearchField searchField : searchFields){
- fieldName = searchField.getFieldName();
- clientFieldName = searchField.getClientFieldName();
- paramValue = paramList.get(key);
- isListValue = false;
- if (paramValue != null && paramValue instanceof Collection) {
- isListValue = true;
- }
- if(fieldName != null){
- fieldName = fieldName.contains(".") ? fieldName.substring(fieldName.indexOf(".") + 1) : fieldName;
- }
- if(key.equalsIgnoreCase(clientFieldName)){
- // build where clause depending upon given parameters
- if(searchField.getDataType() == SearchField.DATA_TYPE.STRING) {
- // build where clause for String datatypes
- attr = entityType.getSingularAttribute(fieldName);
- if(attr != null){
- stringPredicate = criteriaBuilder.equal(rootEntityType.get(attr), paramValue);
- if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.PARTIAL)) {
- String val = "%" + paramValue + "%";
- stringPredicate = criteriaBuilder.like(rootEntityType.get(attr), val);
- }
- predicate = criteriaBuilder.and(predicate, stringPredicate);
-
- }
- } else if (searchField.getDataType() == SearchField.DATA_TYPE.INT_LIST ||
- isListValue && searchField.getDataType() == SearchField.DATA_TYPE.INTEGER) {
- // build where clause for integer lists or integers datatypes
- intValueList = null;
- if (paramValue != null && (paramValue instanceof Integer || paramValue instanceof Long)) {
- intValueList = new ArrayList<Number>();
- intValueList.add((Number) paramValue);
- } else {
- intValueList = (Collection<Number>) paramValue;
- }
- for(Number value : intValueList){
- attr = entityType.getSingularAttribute(fieldName);
- if(attr != null){
- intPredicate = criteriaBuilder.equal(rootEntityType.get(attr), value);
- predicate = criteriaBuilder.and(predicate, intPredicate);
- }
- }
-
- } else if (searchField.getDataType() == SearchField.DATA_TYPE.DATE){
- // build where clause for date datatypes
- fieldValue = (Date) paramList.get(searchField
- .getClientFieldName());
- attr = entityType.getSingularAttribute(fieldName);
- if (fieldValue != null) {
- if (searchField.getCustomCondition() == null) {
- datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
- if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.LESS_THAN)) {
- datePredicate = criteriaBuilder.lessThan(rootEntityType.get(attr), fieldValue);
- } else if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.LESS_EQUAL_THAN)) {
- datePredicate = criteriaBuilder.lessThanOrEqualTo(rootEntityType.get(attr), fieldValue);
- } else if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.GREATER_THAN)) {
- datePredicate = criteriaBuilder.greaterThan(rootEntityType.get(attr), fieldValue);
- } else if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.GREATER_EQUAL_THAN)) {
- datePredicate = criteriaBuilder.greaterThanOrEqualTo(rootEntityType.get(attr), fieldValue);
- } else {
- datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
- }
- predicate = criteriaBuilder.and(predicate, datePredicate);
- }
- }
-
- }
- }
- }
- }
-
+ CriteriaQuery<VXXTrxLog> selectCQ = criteriaBuilder.createQuery(VXXTrxLog.class);
+ Root<VXXTrxLog> rootEntityType = selectCQ.from(VXXTrxLog.class);
+ Predicate predicate = generatePredicate(searchCriteria, em, criteriaBuilder, rootEntityType);
+
selectCQ.where(predicate);
- if(searchCriteria.getSortType()!=null && searchCriteria.getSortType().equalsIgnoreCase("asc")){
+ if("asc".equalsIgnoreCase(searchCriteria.getSortType())){
selectCQ.orderBy(criteriaBuilder.asc(rootEntityType.get("createTime")));
}else{
selectCQ.orderBy(criteriaBuilder.desc(rootEntityType.get("createTime")));
@@ -181,131 +97,133 @@ public class XTrxLogService extends XTrxLogServiceBase<XXTrxLog, VXTrxLog> {
int startIndex = searchCriteria.getStartIndex();
int pageSize = searchCriteria.getMaxRows();
List<VXXTrxLog> resultList = em.createQuery(selectCQ).setFirstResult(startIndex).setMaxResults(pageSize).getResultList();
- VXTrxLogList vxTrxLogList = new VXTrxLogList();
- vxTrxLogList.setStartIndex(startIndex);
- vxTrxLogList.setPageSize(pageSize);
+
List<VXTrxLog> trxLogList = new ArrayList<VXTrxLog>();
- XXPortalUser xXPortalUser=null;
for(VXXTrxLog xTrxLog : resultList){
VXTrxLog trxLog = mapCustomViewToViewObj(xTrxLog);
- xXPortalUser=null;
- if(trxLog.getUpdatedBy()!=null){
- xXPortalUser= rangerDaoManager.getXXPortalUser().getById(
+
+ if(trxLog.getUpdatedBy() != null){
+ XXPortalUser xXPortalUser= rangerDaoManager.getXXPortalUser().getById(
Long.parseLong(trxLog.getUpdatedBy()));
- }
- if(xXPortalUser!=null){
- trxLog.setOwner(xXPortalUser.getLoginId());
+ if(xXPortalUser != null){
+ trxLog.setOwner(xXPortalUser.getLoginId());
+ }
}
+
trxLogList.add(trxLog);
- }
- //vxTrxLogList.setTotalCount(count);
+ }
+
+ VXTrxLogList vxTrxLogList = new VXTrxLogList();
+ vxTrxLogList.setStartIndex(startIndex);
+ vxTrxLogList.setPageSize(pageSize);
vxTrxLogList.setVXTrxLogs(trxLogList);
return vxTrxLogList;
}
-
- public Long searchXTrxLogsCount(SearchCriteria searchCriteria) {
-
+
+ public Long searchXTrxLogsCount(SearchCriteria searchCriteria) {
EntityManager em = daoMgr.getEntityManager();
- CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
- Class<VXXTrxLog> klass = VXXTrxLog.class;
- CriteriaQuery<VXXTrxLog> criteriaQuery = criteriaBuilder.createQuery(klass);
- Root<VXXTrxLog> rootEntityType = criteriaQuery.from(klass);
- Metamodel entityMetaModel = em.getMetamodel();
- EntityType<VXXTrxLog> entityType = entityMetaModel.entity(klass);
- Map<String, Object> paramList = searchCriteria.getParamList();
+ CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
+ CriteriaQuery<VXXTrxLog> selectCQ = criteriaBuilder.createQuery(VXXTrxLog.class);
+ Root<VXXTrxLog> rootEntityType = selectCQ.from(VXXTrxLog.class);
+ Predicate predicate = generatePredicate(searchCriteria, em, criteriaBuilder, rootEntityType);
+
CriteriaQuery<Long> countCQ = criteriaBuilder.createQuery(Long.class);
- Predicate predicate = criteriaBuilder.conjunction();
- String fieldName=null;
- String clientFieldName =null;
- Object paramValue = null;
- boolean isListValue = false;
- SingularAttribute attr =null;
- Collection<Number> intValueList = null;
- Date fieldValue =null;
- Predicate stringPredicate =null;
- Predicate intPredicate = null;
- Predicate datePredicate =null;
- for(String key : paramList.keySet()){
- for(SearchField searchField : searchFields){
- fieldName = searchField.getFieldName();
- clientFieldName = searchField.getClientFieldName();
- paramValue = paramList.get(key);
- isListValue = false;
+ countCQ.select(criteriaBuilder.count(rootEntityType)).where(predicate);
+ List<Long> countList = em.createQuery(countCQ).getResultList();
+ Long count = 0L;
+ if(!CollectionUtils.isEmpty(countList)) {
+ count = countList.get(0);
+ if(count == null) {
+ count = 0L;
+ }
+ }
+ return count;
+ }
+
+ private Predicate generatePredicate(SearchCriteria searchCriteria, EntityManager em,
+ CriteriaBuilder criteriaBuilder, Root<VXXTrxLog> rootEntityType) {
+ Predicate predicate = criteriaBuilder.conjunction();
+ Map<String, Object> paramList = searchCriteria.getParamList();
+ if (CollectionUtils.isEmpty(paramList)) {
+ return predicate;
+ }
+
+ Metamodel entityMetaModel = em.getMetamodel();
+ EntityType<VXXTrxLog> entityType = entityMetaModel.entity(VXXTrxLog.class);
+
+ for (String key : paramList.keySet()) {
+ for (SearchField searchField : searchFields) {
+ if (!key.equalsIgnoreCase(searchField.getClientFieldName())) {
+ continue;
+ }
+
+ String fieldName = searchField.getFieldName();
+ if (!StringUtils.isEmpty(fieldName)) {
+ fieldName = fieldName.contains(".") ? fieldName.substring(fieldName.indexOf(".") + 1) : fieldName;
+ }
+
+ Object paramValue = paramList.get(key);
+ boolean isListValue = false;
if (paramValue != null && paramValue instanceof Collection) {
isListValue = true;
- }
- if(fieldName != null){
- fieldName = fieldName.contains(".") ? fieldName.substring(fieldName.indexOf(".") + 1) : fieldName;
- }
- if(key.equalsIgnoreCase(clientFieldName)){
- // build where clause depending upon given parameters
- if(searchField.getDataType() == SearchField.DATA_TYPE.STRING) {
- // build where clause for String datatypes
- attr = entityType.getSingularAttribute(fieldName);
- if(attr != null){
- stringPredicate = criteriaBuilder.equal(rootEntityType.get(attr), paramValue);
- if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.PARTIAL)) {
- String val = "%" + paramValue + "%";
- stringPredicate = criteriaBuilder.like(rootEntityType.get(attr), val);
- }
- predicate = criteriaBuilder.and(predicate, stringPredicate);
-
- }
- } else if (searchField.getDataType() == SearchField.DATA_TYPE.INT_LIST ||
- isListValue && searchField.getDataType() == SearchField.DATA_TYPE.INTEGER) {
- // build where clause for integer lists or integers datatypes
- intValueList = null;
- if (paramValue != null && (paramValue instanceof Integer || paramValue instanceof Long)) {
- intValueList = new ArrayList<Number>();
- intValueList.add((Number) paramValue);
+ }
+
+ // build where clause depending upon given parameters
+ if (SearchField.DATA_TYPE.STRING.equals(searchField.getDataType())) {
+ // build where clause for String datatypes
+ SingularAttribute attr = entityType.getSingularAttribute(fieldName);
+ if (attr != null) {
+ Predicate stringPredicate = null;
+ if (SearchField.SEARCH_TYPE.PARTIAL.equals(searchField.getSearchType())) {
+ String val = "%" + paramValue + "%";
+ stringPredicate = criteriaBuilder.like(rootEntityType.get(attr), val);
} else {
- intValueList = (Collection<Number>) paramValue;
+ stringPredicate = criteriaBuilder.equal(rootEntityType.get(attr), paramValue);
}
- for(Number value : intValueList){
- attr = entityType.getSingularAttribute(fieldName);
- if(attr != null){
- intPredicate = criteriaBuilder.equal(rootEntityType.get(attr), value);
- predicate = criteriaBuilder.and(predicate, intPredicate);
- }
+ predicate = criteriaBuilder.and(predicate, stringPredicate);
+ }
+
+ } else if (SearchField.DATA_TYPE.INT_LIST.equals(searchField.getDataType()) || isListValue
+ && SearchField.DATA_TYPE.INTEGER.equals(searchField.getDataType())) {
+ // build where clause for integer lists or integers datatypes
+ Collection<Number> intValueList = null;
+ if (paramValue != null && (paramValue instanceof Integer || paramValue instanceof Long)) {
+ intValueList = new ArrayList<Number>();
+ intValueList.add((Number) paramValue);
+ } else {
+ intValueList = (Collection<Number>) paramValue;
+ }
+ for (Number value : intValueList) {
+ SingularAttribute attr = entityType.getSingularAttribute(fieldName);
+ if (attr != null) {
+ Predicate intPredicate = criteriaBuilder.equal(rootEntityType.get(attr), value);
+ predicate = criteriaBuilder.and(predicate, intPredicate);
}
-
- } else if (searchField.getDataType() == SearchField.DATA_TYPE.DATE){
- // build where clause for date datatypes
- fieldValue = (Date) paramList.get(searchField
- .getClientFieldName());
- attr = entityType.getSingularAttribute(fieldName);
- if (fieldValue != null) {
- if (searchField.getCustomCondition() == null) {
- datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
- if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.LESS_THAN)) {
- datePredicate = criteriaBuilder.lessThan(rootEntityType.get(attr), fieldValue);
- } else if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.LESS_EQUAL_THAN)) {
- datePredicate = criteriaBuilder.lessThanOrEqualTo(rootEntityType.get(attr), fieldValue);
- } else if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.GREATER_THAN)) {
- datePredicate = criteriaBuilder.greaterThan(rootEntityType.get(attr), fieldValue);
- } else if (searchField.getSearchType().equals(SearchField.SEARCH_TYPE.GREATER_EQUAL_THAN)) {
- datePredicate = criteriaBuilder.greaterThanOrEqualTo(rootEntityType.get(attr), fieldValue);
- } else {
- datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
- }
- predicate = criteriaBuilder.and(predicate, datePredicate);
- }
+ }
+
+ } else if (SearchField.DATA_TYPE.DATE.equals(searchField.getDataType())) {
+ // build where clause for date datatypes
+ Date fieldValue = (Date) paramList.get(searchField.getClientFieldName());
+ if (fieldValue != null && searchField.getCustomCondition() == null) {
+ SingularAttribute attr = entityType.getSingularAttribute(fieldName);
+ Predicate datePredicate = null;
+ if (SearchField.SEARCH_TYPE.LESS_THAN.equals(searchField.getSearchType())) {
+ datePredicate = criteriaBuilder.lessThan(rootEntityType.get(attr), fieldValue);
+ } else if (SearchField.SEARCH_TYPE.LESS_EQUAL_THAN.equals(searchField.getSearchType())) {
+ datePredicate = criteriaBuilder.lessThanOrEqualTo(rootEntityType.get(attr), fieldValue);
+ } else if (SearchField.SEARCH_TYPE.GREATER_THAN.equals(searchField.getSearchType())) {
+ datePredicate = criteriaBuilder.greaterThan(rootEntityType.get(attr), fieldValue);
+ } else if (SearchField.SEARCH_TYPE.GREATER_EQUAL_THAN.equals(searchField.getSearchType())) {
+ datePredicate = criteriaBuilder.greaterThanOrEqualTo(rootEntityType.get(attr), fieldValue);
+ } else {
+ datePredicate = criteriaBuilder.equal(rootEntityType.get(attr), fieldValue);
}
-
+ predicate = criteriaBuilder.and(predicate, datePredicate);
}
}
}
- }
- countCQ.select(criteriaBuilder.count(rootEntityType)).where(predicate);
- List<Long> countList = em.createQuery(countCQ).getResultList();
- Long count = 0L;
- if(countList != null && !countList.isEmpty()) {
- count = countList.get(0);
- if(count == null) {
- count = 0L;
- }
- }
- return count;
+ }
+ return predicate;
}
private VXTrxLog mapCustomViewToViewObj(VXXTrxLog vXXTrxLog){