Search in sources :

Example 1 with SearchField

use of org.apache.ranger.common.SearchField in project ranger by apache.

the class AbstractBaseResourceService method buildUserConditions.

protected Predicate buildUserConditions(Map<String, Object> paramList, List<SearchField> searchFields, CriteriaBuilder cb, Root<? extends XXDBBase> from) {
    Predicate userConditions = cb.conjunction();
    for (SearchField searchField : searchFields) {
        if (paramList.containsKey(searchField.getClientFieldName())) {
            Path<Object> tableField = null;
            String fieldName = searchField.getFieldName();
            // stuff to handle jpql syntax (e.g. obj.id, obj.city.city etc).
            // There has to be better way of dealing with this. Will look
            // again.
            int dotIndex = fieldName.indexOf(".");
            if (dotIndex != -1) {
                fieldName = fieldName.substring(dotIndex + 1);
            }
            dotIndex = fieldName.indexOf(".");
            if (dotIndex == -1) {
                tableField = from.get(fieldName);
            } else {
                String joinTable = fieldName.substring(0, dotIndex);
                fieldName = fieldName.substring(dotIndex + 1);
                tableField = from.join(joinTable).get(fieldName);
            }
            Object value = paramList.get(searchField.getClientFieldName());
            if (value == null) {
                userConditions = cb.and(userConditions, cb.isNull(tableField));
                continue;
            }
            if (searchField.getDataType() == SearchField.DATA_TYPE.INTEGER || searchField.getDataType() == SearchField.DATA_TYPE.BOOLEAN) {
                userConditions = cb.and(userConditions, cb.equal(tableField, value));
            } else if (searchField.getDataType() == SearchField.DATA_TYPE.STRING) {
                String strFieldValue = (String) value;
                Expression<String> tableFieldWithLowerExpr = cb.lower(tableField.as(String.class));
                if (searchField.getSearchType() == SearchField.SEARCH_TYPE.FULL) {
                    Expression<String> literal = cb.lower(cb.literal(strFieldValue));
                    userConditions = cb.and(userConditions, cb.equal(tableFieldWithLowerExpr, literal));
                } else {
                    Expression<String> literal = cb.lower(cb.literal("%" + strFieldValue + "%"));
                    userConditions = cb.and(userConditions, cb.like(tableFieldWithLowerExpr, literal));
                }
            } else if (searchField.getDataType() == SearchField.DATA_TYPE.INT_LIST) {
                @SuppressWarnings("unchecked") Collection<Number> intValueList = (Collection<Number>) value;
                if (intValueList.size() == 1) {
                    userConditions = cb.and(userConditions, cb.equal(tableField, value));
                } else if (intValueList.size() > 1) {
                    userConditions = cb.and(userConditions, tableField.in(intValueList));
                }
            }
        }
    }
    return userConditions;
}
Also used : SearchField(org.apache.ranger.common.SearchField) Expression(javax.persistence.criteria.Expression) Collection(java.util.Collection) VXDataObject(org.apache.ranger.view.VXDataObject) Predicate(javax.persistence.criteria.Predicate)

Example 2 with SearchField

use of org.apache.ranger.common.SearchField 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 3 with SearchField

use of org.apache.ranger.common.SearchField in project ranger by apache.

the class SolrUtil method searchResources.

public QueryResponse searchResources(SearchCriteria searchCriteria, List<SearchField> searchFields, List<SortField> sortFieldList, SolrClient solrClient) {
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    if (searchCriteria.getParamList() != null) {
        // For now assuming there is only date field where range query will
        // be done. If we there are more than one, then we should create a
        // hashmap for each field name
        Date fromDate = null;
        Date toDate = null;
        String dateFieldName = null;
        for (SearchField searchField : searchFields) {
            Object paramValue = searchCriteria.getParamValue(searchField.getClientFieldName());
            if (paramValue == null || paramValue.toString().isEmpty()) {
                continue;
            }
            String fieldName = searchField.getFieldName();
            if (paramValue instanceof Collection) {
                String fq = orList(fieldName, (Collection<?>) paramValue);
                if (fq != null) {
                    query.addFilterQuery(fq);
                }
            } else if (searchField.getDataType() == SearchField.DATA_TYPE.DATE) {
                if (!(paramValue instanceof Date)) {
                    logger.error("Search field is not a Java Date Object, paramValue = " + paramValue);
                } else {
                    if (searchField.getSearchType() == SEARCH_TYPE.GREATER_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.GREATER_THAN) {
                        fromDate = (Date) paramValue;
                        dateFieldName = fieldName;
                    } else if (searchField.getSearchType() == SEARCH_TYPE.LESS_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.LESS_THAN) {
                        toDate = (Date) paramValue;
                        dateFieldName = fieldName;
                    }
                }
            } else if (searchField.getSearchType() == SEARCH_TYPE.GREATER_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.GREATER_THAN || searchField.getSearchType() == SEARCH_TYPE.LESS_EQUAL_THAN || searchField.getSearchType() == SEARCH_TYPE.LESS_THAN) {
            // NOPMD
            // TODO: Need to handle range here
            } else {
                String fq = setField(fieldName, paramValue);
                if (searchField.getSearchType() == SEARCH_TYPE.PARTIAL) {
                    fq = setFieldForPartialSearch(fieldName, paramValue);
                }
                if (fq != null) {
                    query.addFilterQuery(fq);
                }
            }
        }
        if (fromDate != null || toDate != null) {
            String fq = setDateRange(dateFieldName, fromDate, toDate);
            if (fq != null) {
                query.addFilterQuery(fq);
            }
        }
    }
    setSortClause(searchCriteria, sortFieldList, query);
    query.setStart(searchCriteria.getStartIndex());
    query.setRows(searchCriteria.getMaxRows());
    // query.setFields("myClassType", "id", "score", "globalId");
    if (logger.isDebugEnabled()) {
        logger.debug("SOLR QUERY = " + query);
    }
    QueryResponse response = null;
    try {
        response = runQuery(solrClient, query);
    } catch (Throwable e) {
        logger.error("Error running solr query. Query = " + query + ", response = " + response);
        throw restErrorUtil.createRESTException("Error running solr query, please check solr configs. " + e.getMessage(), MessageEnums.ERROR_SYSTEM);
    }
    if (response == null || response.getStatus() != 0) {
        logger.error("Error running solr query. Query = " + query + ", response = " + response);
        throw restErrorUtil.createRESTException("Unable to connect to Audit store !!", MessageEnums.ERROR_SYSTEM);
    }
    return response;
}
Also used : SearchField(org.apache.ranger.common.SearchField) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Collection(java.util.Collection) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Date(java.util.Date)

Aggregations

Collection (java.util.Collection)3 SearchField (org.apache.ranger.common.SearchField)3 Date (java.util.Date)2 Predicate (javax.persistence.criteria.Predicate)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Expression (javax.persistence.criteria.Expression)1 Metamodel (javax.persistence.metamodel.Metamodel)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 VXXTrxLog (org.apache.ranger.entity.view.VXXTrxLog)1 VXDataObject (org.apache.ranger.view.VXDataObject)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1