Search in sources :

Example 21 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class PeriodTypeObjectBundleHook method preUpdate.

@Override
public void preUpdate(IdentifiableObject object, IdentifiableObject persistedObject, ObjectBundle bundle) {
    Schema schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
    for (Property property : schema.getPropertyMap().values()) {
        if (PeriodType.class.isAssignableFrom(property.getKlass())) {
            PeriodType periodType = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (periodType != null) {
                periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
                ReflectionUtils.invokeMethod(object, property.getSetterMethod(), periodType);
            }
        }
    }
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 22 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class GistBuilder method getPluckPropertyName.

private String getPluckPropertyName(Field field, Class<?> ownerType, boolean forceTextual) {
    String propertyName = field.getTransformationArgument();
    Property property = context.switchedTo(ownerType).resolveMandatory(propertyName);
    if (forceTextual && property.getKlass() != String.class) {
        throw new UnsupportedOperationException("Only textual properties can be plucked, but " + propertyName + " is a: " + property.getKlass());
    }
    return propertyName;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) GistLogic.isAccessProperty(org.hisp.dhis.gist.GistLogic.isAccessProperty) Property(org.hisp.dhis.schema.Property) GistLogic.isHrefProperty(org.hisp.dhis.gist.GistLogic.isHrefProperty)

Example 23 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class GistBuilder method createFieldHQL.

private String createFieldHQL(int index, Field field) {
    String path = field.getPropertyPath();
    if (Field.REFS_PATH.equals(path)) {
        return HQL_NULL;
    }
    if (field.isAttribute()) {
        if (field.getTransformation() == Transform.PLUCK) {
            return "jsonb_extract_path_text(e.attributeValues, '" + field.getPropertyPath() + "', 'value')";
        }
        int attrValuesFieldIndex = getSameParentFieldIndex("", ATTRIBUTES_PROPERTY);
        addTransformer(row -> row[index] = attributeValue(path, row[attrValuesFieldIndex]));
        return HQL_NULL;
    }
    Property property = context.resolveMandatory(path);
    if (query.isTranslate() && property.isTranslatable() && query.getTranslationLocale() != null) {
        int translationsFieldIndex = getSameParentFieldIndex(path, TRANSLATIONS_PROPERTY);
        addTransformer(row -> row[index] = translate(row[index], property.getTranslationKey(), row[translationsFieldIndex]));
    }
    if (isHrefProperty(property)) {
        String endpointRoot = getSameParentEndpointRoot(path);
        Integer idFieldIndex = getSameParentFieldIndex(path, ID_PROPERTY);
        if (idFieldIndex != null && endpointRoot != null) {
            addTransformer(row -> row[index] = toEndpointURL(endpointRoot, row[idFieldIndex]));
        }
        return HQL_NULL;
    }
    if (isAccessProperty(property)) {
        int sharingFieldIndex = getSameParentFieldIndex(path, SHARING_PROPERTY);
        @SuppressWarnings("unchecked") Class<? extends IdentifiableObject> objType = (Class<? extends IdentifiableObject>) (isNonNestedPath(path) ? query.getElementType() : property.getKlass());
        addTransformer(row -> row[index] = access.asAccess(objType, (Sharing) row[sharingFieldIndex]));
        return HQL_NULL;
    }
    if (field.getTransformation() == Transform.FROM) {
        return createFromTransformedFieldHQL(index, field, path, property);
    }
    if (isPersistentReferenceField(property)) {
        return createReferenceFieldHQL(index, field);
    }
    if (isPersistentCollectionField(property)) {
        return createCollectionFieldHQL(index, field);
    }
    if (property.isCollection() && property.getOwningRole() != null) {
        return "size(e." + getMemberPath(path) + ")";
    }
    String memberPath = getMemberPath(path);
    return "e." + memberPath;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) GistLogic.isAccessProperty(org.hisp.dhis.gist.GistLogic.isAccessProperty) Property(org.hisp.dhis.schema.Property) GistLogic.isHrefProperty(org.hisp.dhis.gist.GistLogic.isHrefProperty) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 24 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class GistBuilder method createReferenceFieldHQL.

private String createReferenceFieldHQL(int index, Field field) {
    String tableName = "t_" + index;
    String path = field.getPropertyPath();
    Property property = context.resolveMandatory(path);
    RelativePropertyContext fieldContext = context.switchedTo(property.getKlass());
    String propertyName = determineReferenceProperty(field, fieldContext, false);
    Schema propertySchema = fieldContext.getHome();
    if (propertyName == null || propertySchema.getRelativeApiEndpoint() == null) {
        // embed the object directly
        if (!property.isRequired()) {
            return String.format("(select %1$s from %2$s %1$s where %1$s = e.%3$s)", tableName, property.getKlass().getSimpleName(), getMemberPath(path));
        }
        return "e." + getMemberPath(path);
    }
    if (property.isIdentifiableObject()) {
        String endpointRoot = getEndpointRoot(property);
        if (endpointRoot != null && query.isReferences()) {
            int refIndex = fieldIndexByPath.get(Field.REFS_PATH);
            addTransformer(row -> addEndpointURL(row, refIndex, field, isNullOrEmpty(row[index]) ? null : toEndpointURL(endpointRoot, row[index])));
        }
    }
    if (field.getTransformation() == Transform.ID_OBJECTS) {
        addTransformer(row -> row[index] = toIdObject(row[index]));
    }
    if (property.isRequired()) {
        return "e." + getMemberPath(path) + "." + propertyName;
    }
    return String.format("(select %1$s.%2$s from %3$s %1$s where %1$s = e.%4$s)", tableName, propertyName, property.getKlass().getSimpleName(), getMemberPath(path));
}
Also used : RelativePropertyContext(org.hisp.dhis.schema.RelativePropertyContext) Schema(org.hisp.dhis.schema.Schema) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) GistLogic.isAccessProperty(org.hisp.dhis.gist.GistLogic.isAccessProperty) Property(org.hisp.dhis.schema.Property) GistLogic.isHrefProperty(org.hisp.dhis.gist.GistLogic.isHrefProperty)

Example 25 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class GistValidator method validateField.

private void validateField(Field f, RelativePropertyContext context) {
    String path = f.getPropertyPath();
    if (Field.REFS_PATH.equals(path) || f.isAttribute()) {
        return;
    }
    Property field = context.resolveMandatory(path);
    if (!isNonNestedPath(path)) {
        List<Property> pathElements = context.resolvePath(path);
        Property head = pathElements.get(0);
        if (head.isCollection() && head.isPersisted()) {
            throw createIllegalProperty(field, "Property `%s` computes to many values and therefore cannot be used as a field.");
        }
    }
    Transform transformation = f.getTransformation();
    String transArgs = f.getTransformationArgument();
    if (transformation == Transform.PLUCK && transArgs != null) {
        Property plucked = context.switchedTo(getBaseType(field)).resolveMandatory(transArgs);
        if (!plucked.isPersisted()) {
            throw createIllegalProperty(plucked, "Property `%s` cannot be plucked as it is not a persistent field.");
        }
    }
    if (transformation == Transform.FROM) {
        validateFromTransformation(context, field, transArgs);
    }
    if (!field.isReadable()) {
        throw createNoReadAccess(f, null);
    }
    validateFieldAccess(f, context);
}
Also used : Transform(org.hisp.dhis.schema.annotation.Gist.Transform) Property(org.hisp.dhis.schema.Property)

Aggregations

Property (org.hisp.dhis.schema.Property)126 Schema (org.hisp.dhis.schema.Schema)69 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)26 Collection (java.util.Collection)21 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)21 List (java.util.List)20 Map (java.util.Map)16 Test (org.junit.jupiter.api.Test)16 Attribute (org.hisp.dhis.attribute.Attribute)14 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)14 Collectors (java.util.stream.Collectors)13 User (org.hisp.dhis.user.User)13 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 SimpleNode (org.hisp.dhis.node.types.SimpleNode)12 Query (org.hisp.dhis.query.Query)12 SchemaService (org.hisp.dhis.schema.SchemaService)12 Transactional (org.springframework.transaction.annotation.Transactional)12