Search in sources :

Example 1 with VXXTrxLog

use of org.apache.ranger.entity.view.VXXTrxLog in project ranger by apache.

the class XTrxLogService method generatePredicate.

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 (Map.Entry<String, Object> entry : paramList.entrySet()) {
        String key = entry.getKey();
        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 = entry.getValue();
            boolean isListValue = false;
            if (paramValue != null && paramValue instanceof Collection) {
                isListValue = true;
            }
            // 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 {
                        stringPredicate = criteriaBuilder.equal(rootEntityType.get(attr), paramValue);
                    }
                    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.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);
                }
            }
        }
    }
    return predicate;
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate) SingularAttribute(javax.persistence.metamodel.SingularAttribute) SearchField(org.apache.ranger.common.SearchField) Collection(java.util.Collection) Metamodel(javax.persistence.metamodel.Metamodel) Map(java.util.Map) VXXTrxLog(org.apache.ranger.entity.view.VXXTrxLog)

Example 2 with VXXTrxLog

use of org.apache.ranger.entity.view.VXXTrxLog in project ranger by apache.

the class XTrxLogService method searchXTrxLogs.

@Override
public VXTrxLogList searchXTrxLogs(SearchCriteria searchCriteria) {
    EntityManager em = daoManager.getEntityManager();
    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);
    selectCQ.where(predicate);
    if ("asc".equalsIgnoreCase(searchCriteria.getSortType())) {
        selectCQ.orderBy(criteriaBuilder.asc(rootEntityType.get("createTime")));
    } else {
        selectCQ.orderBy(criteriaBuilder.desc(rootEntityType.get("createTime")));
    }
    int startIndex = searchCriteria.getStartIndex();
    int pageSize = searchCriteria.getMaxRows();
    List<VXXTrxLog> resultList = em.createQuery(selectCQ).setFirstResult(startIndex).setMaxResults(pageSize).getResultList();
    int maxRowSize = Integer.MAX_VALUE;
    int minRowSize = 0;
    XXServiceDef xxServiceDef = daoManager.getXXServiceDef().findByName(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_KMS_NAME);
    UserSessionBase session = ContextUtil.getCurrentUserSession();
    if (session != null && session.isKeyAdmin()) {
        resultList = em.createQuery(selectCQ).setFirstResult(minRowSize).setMaxResults(maxRowSize).getResultList();
    }
    if (session != null && session.isAuditKeyAdmin()) {
        resultList = em.createQuery(selectCQ).setFirstResult(minRowSize).setMaxResults(maxRowSize).getResultList();
    }
    List<VXTrxLog> trxLogList = new ArrayList<VXTrxLog>();
    for (VXXTrxLog xTrxLog : resultList) {
        VXTrxLog trxLog = mapCustomViewToViewObj(xTrxLog);
        if (trxLog.getUpdatedBy() != null) {
            XXPortalUser xXPortalUser = daoManager.getXXPortalUser().getById(Long.parseLong(trxLog.getUpdatedBy()));
            if (xXPortalUser != null) {
                trxLog.setOwner(xXPortalUser.getLoginId());
            }
        }
        trxLogList.add(trxLog);
    }
    List<VXTrxLog> keyAdminTrxLogList = new ArrayList<VXTrxLog>();
    if (session != null && xxServiceDef != null && (session.isKeyAdmin() || session.isAuditKeyAdmin())) {
        List<VXTrxLog> vXTrxLogs = new ArrayList<VXTrxLog>();
        for (VXTrxLog xTrxLog : trxLogList) {
            int parentObjectClassType = xTrxLog.getParentObjectClassType();
            Long parentObjectId = xTrxLog.getParentObjectId();
            if (parentObjectClassType == AppConstants.CLASS_TYPE_XA_SERVICE_DEF && parentObjectId.equals(xxServiceDef.getId())) {
                vXTrxLogs.add(xTrxLog);
            } else if (parentObjectClassType == AppConstants.CLASS_TYPE_XA_SERVICE && !(parentObjectId.equals(xxServiceDef.getId()))) {
                for (VXTrxLog vxTrxLog : trxLogList) {
                    if (parentObjectClassType == vxTrxLog.getObjectClassType() && parentObjectId.equals(vxTrxLog.getObjectId()) && vxTrxLog.getParentObjectId().equals(xxServiceDef.getId())) {
                        vXTrxLogs.add(xTrxLog);
                        break;
                    }
                }
            } else if (xTrxLog.getObjectClassType() == AppConstants.CLASS_TYPE_XA_USER || xTrxLog.getObjectClassType() == AppConstants.CLASS_TYPE_RANGER_POLICY || xTrxLog.getObjectClassType() == AppConstants.HIST_OBJ_STATUS_UPDATED) {
                XXPortalUser xxPortalUser = null;
                if (xTrxLog.getUpdatedBy() != null) {
                    xxPortalUser = daoManager.getXXPortalUser().getById(Long.parseLong(xTrxLog.getUpdatedBy()));
                }
                if (xxPortalUser != null && xxPortalUser.getId() != null) {
                    List<XXPortalUserRole> xxPortalUserRole = daoManager.getXXPortalUserRole().findByUserId(xxPortalUser.getId());
                    if (xxPortalUserRole != null && (xxPortalUserRole.get(0).getUserRole().equalsIgnoreCase("ROLE_KEY_ADMIN") || xxPortalUserRole.get(0).getUserRole().equalsIgnoreCase("ROLE_KEY_ADMIN_AUDITOR"))) {
                        vXTrxLogs.add(xTrxLog);
                    }
                }
            }
        }
        keyadminCount = (long) vXTrxLogs.size();
        if (vXTrxLogs != null && !vXTrxLogs.isEmpty()) {
            for (int k = startIndex; k <= pageSize; k++) {
                if (k < vXTrxLogs.size()) {
                    keyAdminTrxLogList.add(vXTrxLogs.get(k));
                }
            }
        }
    }
    VXTrxLogList vxTrxLogList = new VXTrxLogList();
    vxTrxLogList.setStartIndex(startIndex);
    vxTrxLogList.setPageSize(pageSize);
    if (session != null && (session.isKeyAdmin() || session.isAuditKeyAdmin())) {
        vxTrxLogList.setVXTrxLogs(keyAdminTrxLogList);
    } else {
        vxTrxLogList.setVXTrxLogs(trxLogList);
    }
    return vxTrxLogList;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) XXServiceDef(org.apache.ranger.entity.XXServiceDef) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate) UserSessionBase(org.apache.ranger.common.UserSessionBase) XXPortalUser(org.apache.ranger.entity.XXPortalUser) EntityManager(javax.persistence.EntityManager) VXTrxLog(org.apache.ranger.view.VXTrxLog) VXTrxLogList(org.apache.ranger.view.VXTrxLogList) XXPortalUserRole(org.apache.ranger.entity.XXPortalUserRole) VXXTrxLog(org.apache.ranger.entity.view.VXXTrxLog)

Example 3 with VXXTrxLog

use of org.apache.ranger.entity.view.VXXTrxLog in project ranger by apache.

the class XTrxLogService method searchXTrxLogsCount.

public Long searchXTrxLogsCount(SearchCriteria searchCriteria) {
    EntityManager em = daoManager.getEntityManager();
    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);
    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;
        }
    }
    UserSessionBase session = ContextUtil.getCurrentUserSession();
    if (session != null && session.isKeyAdmin()) {
        count = keyadminCount;
    }
    if (session != null && session.isAuditKeyAdmin()) {
        count = keyadminCount;
    }
    return count;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) VXXTrxLog(org.apache.ranger.entity.view.VXXTrxLog) Predicate(javax.persistence.criteria.Predicate) UserSessionBase(org.apache.ranger.common.UserSessionBase)

Aggregations

Predicate (javax.persistence.criteria.Predicate)3 VXXTrxLog (org.apache.ranger.entity.view.VXXTrxLog)3 ArrayList (java.util.ArrayList)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 UserSessionBase (org.apache.ranger.common.UserSessionBase)2 Collection (java.util.Collection)1 Date (java.util.Date)1 Map (java.util.Map)1 Metamodel (javax.persistence.metamodel.Metamodel)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 SearchField (org.apache.ranger.common.SearchField)1 XXPortalUser (org.apache.ranger.entity.XXPortalUser)1 XXPortalUserRole (org.apache.ranger.entity.XXPortalUserRole)1 XXServiceDef (org.apache.ranger.entity.XXServiceDef)1 VXTrxLog (org.apache.ranger.view.VXTrxLog)1 VXTrxLogList (org.apache.ranger.view.VXTrxLogList)1