Search in sources :

Example 1 with Field

use of org.hisp.dhis.gist.GistQuery.Field 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 Field

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

the class GistPlanner method withCollectionItemPropertyAsTransformation.

private List<Field> withCollectionItemPropertyAsTransformation(List<Field> fields) {
    List<Field> mapped = new ArrayList<>();
    for (Field f : fields) {
        String path = f.getPropertyPath();
        if (!isNonNestedPath(path) && context.resolveMandatory(parentPath(path)).isCollection()) {
            String parentPath = parentPath(path);
            String propertyName = path.substring(path.lastIndexOf('.') + 1);
            Property collection = context.resolveMandatory(parentPath);
            if ("id".equals(propertyName) && PrimaryKeyObject.class.isAssignableFrom(collection.getItemKlass())) {
                mapped.add(f.withPropertyPath(parentPath).withAlias(path).withTransformation(Transform.IDS));
            } else {
                mapped.add(Field.builder().propertyPath(parentPath).alias(path).transformation(Transform.PLUCK).transformationArgument(propertyName).build());
            }
        } else {
            mapped.add(f);
        }
    }
    return mapped;
}
Also used : 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) ArrayList(java.util.ArrayList) PrimaryKeyObject(org.hisp.dhis.common.PrimaryKeyObject) Property(org.hisp.dhis.schema.Property)

Example 3 with Field

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

the class GistPlanner method withEndpointsField.

private List<Field> withEndpointsField(List<Field> fields) {
    if (!query.isReferences()) {
        return fields;
    }
    boolean hasReferences = fields.stream().anyMatch(field -> {
        if (field.isAttribute()) {
            return false;
        }
        Property p = context.resolveMandatory(field.getPropertyPath());
        return isPersistentReferenceField(p) && p.isIdentifiableObject() || isPersistentCollectionField(p);
    });
    if (!hasReferences) {
        return fields;
    }
    ArrayList<Field> extended = new ArrayList<>(fields);
    extended.add(new Field(Field.REFS_PATH, Transform.NONE).withAlias("apiEndpoints"));
    return extended;
}
Also used : 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) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 4 with Field

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

the class GistBuilder method createFromTransformedFieldHQL.

private String createFromTransformedFieldHQL(int index, Field field, String path, Property property) {
    Object bean = newQueryElementInstance();
    if (bean == null) {
        return HQL_NULL;
    }
    String[] sources = field.getTransformationArgument().split(",");
    List<Method> setters = stream(sources).map(context::resolveMandatory).map(Property::getSetterMethod).collect(toList());
    int[] indexes = stream(sources).mapToInt(srcProperty -> getSameParentFieldIndex(path, srcProperty)).toArray();
    Method getter = property.getGetterMethod();
    addTransformer(row -> {
        try {
            for (int i = 0; i < indexes.length; i++) {
                setters.get(i).invoke(bean, row[indexes[i]]);
            }
            row[index] = getter.invoke(bean);
        } catch (Exception ex) {
            log.debug("Failed to perform from transformation", ex);
        }
    });
    return HQL_NULL;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) AttributeValue(org.hisp.dhis.attribute.AttributeValue) GistLogic.isAccessProperty(org.hisp.dhis.gist.GistLogic.isAccessProperty) BiFunction(java.util.function.BiFunction) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Comparison(org.hisp.dhis.gist.GistQuery.Comparison) HashMap(java.util.HashMap) RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) Function(java.util.function.Function) GistLogic.pathOnSameParent(org.hisp.dhis.gist.GistLogic.pathOnSameParent) GistLogic.isPersistentCollectionField(org.hisp.dhis.gist.GistLogic.isPersistentCollectionField) ArrayList(java.util.ArrayList) Translation(org.hisp.dhis.translation.Translation) Owner(org.hisp.dhis.gist.GistQuery.Owner) GistLogic.isCollectionSizeFilter(org.hisp.dhis.gist.GistLogic.isCollectionSizeFilter) GistLogic.getBaseType(org.hisp.dhis.gist.GistLogic.getBaseType) AccessLevel(lombok.AccessLevel) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Method(java.lang.reflect.Method) Filter(org.hisp.dhis.gist.GistQuery.Filter) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Field(org.hisp.dhis.gist.GistQuery.Field) GistLogic.isPersistentReferenceField(org.hisp.dhis.gist.GistLogic.isPersistentReferenceField) GistLogic.parentPath(org.hisp.dhis.gist.GistLogic.parentPath) Collection(java.util.Collection) JpaQueryUtils(org.hisp.dhis.query.JpaQueryUtils) Transform(org.hisp.dhis.schema.annotation.Gist.Transform) Set(java.util.Set) Sharing(org.hisp.dhis.user.sharing.Sharing) GistLogic.isStringLengthFilter(org.hisp.dhis.gist.GistLogic.isStringLengthFilter) Property(org.hisp.dhis.schema.Property) Collectors.joining(java.util.stream.Collectors.joining) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) GistLogic.isHrefProperty(org.hisp.dhis.gist.GistLogic.isHrefProperty) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) GistLogic.isNonNestedPath(org.hisp.dhis.gist.GistLogic.isNonNestedPath) TreeMap(java.util.TreeMap) AclService(org.hisp.dhis.security.acl.AclService) PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) AllArgsConstructor(lombok.AllArgsConstructor) Arrays.stream(java.util.Arrays.stream) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Method(java.lang.reflect.Method)

Example 5 with Field

use of org.hisp.dhis.gist.GistQuery.Field 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

Field (org.hisp.dhis.gist.GistQuery.Field)6 ArrayList (java.util.ArrayList)5 GistLogic.isPersistentCollectionField (org.hisp.dhis.gist.GistLogic.isPersistentCollectionField)5 GistLogic.isPersistentReferenceField (org.hisp.dhis.gist.GistLogic.isPersistentReferenceField)5 GistLogic.isIncludedField (org.hisp.dhis.gist.GistLogic.isIncludedField)4 Property (org.hisp.dhis.schema.Property)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 Filter (org.hisp.dhis.gist.GistQuery.Filter)3 Arrays.stream (java.util.Arrays.stream)2 List (java.util.List)2 Set (java.util.Set)2 Collectors.toList (java.util.stream.Collectors.toList)2 AllArgsConstructor (lombok.AllArgsConstructor)2 Slf4j (lombok.extern.slf4j.Slf4j)2 GistLogic.isCollectionSizeFilter (org.hisp.dhis.gist.GistLogic.isCollectionSizeFilter)2 GistLogic.isNonNestedPath (org.hisp.dhis.gist.GistLogic.isNonNestedPath)2 GistLogic.parentPath (org.hisp.dhis.gist.GistLogic.parentPath)2 GistLogic.pathOnSameParent (org.hisp.dhis.gist.GistLogic.pathOnSameParent)2 Comparison (org.hisp.dhis.gist.GistQuery.Comparison)2 RelativePropertyContext (org.hisp.dhis.schema.RelativePropertyContext)2