Search in sources :

Example 1 with Filter

use of org.hisp.dhis.gist.GistQuery.Filter in project dhis2-core by dhis2.

the class DefaultGistAccessControl method canReadObject.

@Override
public boolean canReadObject(Class<? extends PrimaryKeyObject> type, String uid) {
    if (!IdentifiableObject.class.isAssignableFrom(type)) {
        return true;
    }
    @SuppressWarnings("unchecked") Class<? extends IdentifiableObject> ioType = (Class<? extends IdentifiableObject>) type;
    if (!aclService.isClassShareable(ioType)) {
        return aclService.canRead(currentUser, ioType);
    }
    List<?> res = gistService.gist(GistQuery.builder().elementType(ioType).autoType(GistAutoType.M).fields(singletonList(new Field("sharing", Transform.NONE))).filters(singletonList(new Filter("id", Comparison.EQ, uid))).build());
    Sharing sharing = res.isEmpty() ? new Sharing() : (Sharing) res.get(0);
    BaseIdentifiableObject object = new BaseIdentifiableObject();
    object.setSharing(sharing);
    return aclService.canRead(currentUser, object, ioType);
}
Also used : Field(org.hisp.dhis.gist.GistQuery.Field) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Filter(org.hisp.dhis.gist.GistQuery.Filter) Sharing(org.hisp.dhis.user.sharing.Sharing) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Example 2 with Filter

use of org.hisp.dhis.gist.GistQuery.Filter in project dhis2-core by dhis2.

the class GistBuilder method createFiltersHQL.

private String createFiltersHQL() {
    String rootJunction = query.isAnyFilter() ? " or " : " and ";
    List<Filter> filters = query.getFilters();
    if (!query.hasFilterGroups()) {
        return join(filters, rootJunction, "1=1", this::createFilterHQL);
    }
    String groupJunction = query.isAnyFilter() ? " and " : " or ";
    Map<Integer, List<Filter>> grouped = filters.stream().collect(groupingBy(Filter::getGroup, toList()));
    StringBuilder hql = new StringBuilder();
    for (List<Filter> group : grouped.values()) {
        if (!group.isEmpty()) {
            hql.append('(');
            for (Filter f : group) {
                int index = filters.indexOf(f);
                hql.append(createFilterHQL(index, f));
                hql.append(groupJunction);
            }
            hql.append("1=1");
            hql.append(')');
        }
        hql.append(rootJunction);
    }
    hql.append("1=1");
    return hql.toString().replaceAll("(?: and | or )1=1", "");
}
Also used : GistLogic.isCollectionSizeFilter(org.hisp.dhis.gist.GistLogic.isCollectionSizeFilter) Filter(org.hisp.dhis.gist.GistQuery.Filter) GistLogic.isStringLengthFilter(org.hisp.dhis.gist.GistLogic.isStringLengthFilter) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List)

Example 3 with Filter

use of org.hisp.dhis.gist.GistQuery.Filter in project dhis2-core by dhis2.

the class GistBuilder method addCountParameters.

public void addCountParameters(BiConsumer<String, Object> dest, BiFunction<String, Class<?>, Object> argumentParser) {
    Owner owner = query.getOwner();
    if (owner != null) {
        dest.accept("OwnerId", owner.getId());
    }
    int i = 0;
    for (Filter filter : query.getFilters()) {
        Comparison operator = filter.getOperator();
        if (!operator.isUnary() && !operator.isAccessCompare()) {
            Object value = filter.isAttribute() ? filter.getValue()[0] : getParameterValue(context.resolveMandatory(filter.getPropertyPath()), filter, argumentParser);
            dest.accept("f_" + i, operator.isStringCompare() ? completeLikeExpression(operator, stringParameterValue(operator, value)) : value);
        }
        i++;
    }
}
Also used : Owner(org.hisp.dhis.gist.GistQuery.Owner) GistLogic.isCollectionSizeFilter(org.hisp.dhis.gist.GistLogic.isCollectionSizeFilter) Filter(org.hisp.dhis.gist.GistQuery.Filter) GistLogic.isStringLengthFilter(org.hisp.dhis.gist.GistLogic.isStringLengthFilter) Comparison(org.hisp.dhis.gist.GistQuery.Comparison) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 4 with Filter

use of org.hisp.dhis.gist.GistQuery.Filter in project dhis2-core by dhis2.

the class GistPlanner method withPresetFields.

/**
 * Expands any field presets into individual fields while taking explicitly
 * removed fields into account.
 */
private List<Field> withPresetFields(List<Field> fields) {
    Set<String> explicit = fields.stream().map(Field::getPropertyPath).collect(toSet());
    List<Field> expanded = new ArrayList<>();
    for (Field f : fields) {
        String path = f.getPropertyPath();
        if (isPresetField(path)) {
            Schema schema = context.getHome();
            Predicate<Property> canRead = getAccessFilter(schema);
            schema.getProperties().stream().filter(getPresetFilter(path)).sorted(GistPlanner::propertyTypeOrder).forEach(p -> {
                if (!explicit.contains(p.key()) && !explicit.contains("-" + p.key()) && !explicit.contains("!" + p.key())) {
                    if (canRead.test(p)) {
                        expanded.add(new Field(p.key(), Transform.AUTO));
                    }
                    addReferenceFields(expanded, path, schema, p);
                }
            });
        } else if (isExcludeField(path)) {
            expanded.removeIf(field -> field.getPropertyPath().equals(path.substring(1)));
        } else {
            expanded.add(f);
        }
    }
    return expanded;
}
Also used : PropertyType(org.hisp.dhis.schema.PropertyType) GistLogic.isAttributePath(org.hisp.dhis.gist.GistLogic.isAttributePath) Comparison(org.hisp.dhis.gist.GistQuery.Comparison) UnaryOperator(java.util.function.UnaryOperator) RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) GistLogic.pathOnSameParent(org.hisp.dhis.gist.GistLogic.pathOnSameParent) Collections.singletonList(java.util.Collections.singletonList) GistLogic.isPersistentCollectionField(org.hisp.dhis.gist.GistLogic.isPersistentCollectionField) ArrayList(java.util.ArrayList) GistLogic.isCollectionSizeFilter(org.hisp.dhis.gist.GistLogic.isCollectionSizeFilter) COLLECTION(org.hisp.dhis.schema.PropertyType.COLLECTION) PrimaryKeyObject(org.hisp.dhis.common.PrimaryKeyObject) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Collectors.toSet(java.util.stream.Collectors.toSet) Filter(org.hisp.dhis.gist.GistQuery.Filter) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) NameableObject(org.hisp.dhis.common.NameableObject) Field(org.hisp.dhis.gist.GistQuery.Field) GistLogic.isPersistentReferenceField(org.hisp.dhis.gist.GistLogic.isPersistentReferenceField) Predicate(java.util.function.Predicate) GistLogic.parentPath(org.hisp.dhis.gist.GistLogic.parentPath) Transform(org.hisp.dhis.schema.annotation.Gist.Transform) Set(java.util.Set) Property(org.hisp.dhis.schema.Property) GistLogic.isIncludedField(org.hisp.dhis.gist.GistLogic.isIncludedField) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) GistLogic.isNonNestedPath(org.hisp.dhis.gist.GistLogic.isNonNestedPath) Schema(org.hisp.dhis.schema.Schema) AllArgsConstructor(lombok.AllArgsConstructor) Arrays.stream(java.util.Arrays.stream) GistLogic.effectiveTransform(org.hisp.dhis.gist.GistLogic.effectiveTransform) GistLogic.isPersistentCollectionField(org.hisp.dhis.gist.GistLogic.isPersistentCollectionField) Field(org.hisp.dhis.gist.GistQuery.Field) GistLogic.isPersistentReferenceField(org.hisp.dhis.gist.GistLogic.isPersistentReferenceField) GistLogic.isIncludedField(org.hisp.dhis.gist.GistLogic.isIncludedField) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Aggregations

Filter (org.hisp.dhis.gist.GistQuery.Filter)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 GistLogic.isCollectionSizeFilter (org.hisp.dhis.gist.GistLogic.isCollectionSizeFilter)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 GistLogic.isStringLengthFilter (org.hisp.dhis.gist.GistLogic.isStringLengthFilter)2 Comparison (org.hisp.dhis.gist.GistQuery.Comparison)2 Field (org.hisp.dhis.gist.GistQuery.Field)2 Arrays.stream (java.util.Arrays.stream)1 Collections.singletonList (java.util.Collections.singletonList)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 UnaryOperator (java.util.function.UnaryOperator)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 AllArgsConstructor (lombok.AllArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)1 NameableObject (org.hisp.dhis.common.NameableObject)1