Search in sources :

Example 51 with Property

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

the class DefaultFieldFilterServiceTest method toCollectionNodeSkipSharingNoFields.

@Test
void toCollectionNodeSkipSharingNoFields() throws Exception {
    final Attribute attribute = new Attribute();
    final Map<String, Property> propertyMap = new HashMap<>();
    addProperty(propertyMap, attribute, "dataElementAttribute");
    addProperty(propertyMap, attribute, "user");
    addProperty(propertyMap, attribute, "publicAccess");
    addProperty(propertyMap, attribute, "userGroupAccesses");
    addProperty(propertyMap, attribute, "userAccesses");
    addProperty(propertyMap, attribute, "externalAccess");
    final Schema rootSchema = new Schema(Attribute.class, "attribute", "attributes");
    rootSchema.setPropertyMap(propertyMap);
    Mockito.when(schemaService.getDynamicSchema(Mockito.eq(Attribute.class))).thenReturn(rootSchema);
    final Schema booleanSchema = new Schema(boolean.class, "boolean", "booleans");
    Mockito.when(schemaService.getDynamicSchema(Mockito.eq(boolean.class))).thenReturn(booleanSchema);
    final FieldFilterParams params = new FieldFilterParams(Collections.singletonList(attribute), Collections.emptyList(), Defaults.INCLUDE, true);
    CollectionNode node = service.toCollectionNode(Attribute.class, params);
    Assertions.assertEquals(1, node.getChildren().size());
    Set<String> names = extractNodeNames(node.getChildren().get(0).getChildren());
    Assertions.assertTrue(names.contains("dataElementAttribute"));
    Assertions.assertFalse(names.contains("user"));
    Assertions.assertFalse(names.contains("publicAccess"));
    Assertions.assertFalse(names.contains("userGroupAccesses"));
    Assertions.assertFalse(names.contains("userAccesses"));
    Assertions.assertFalse(names.contains("externalAccess"));
}
Also used : Attribute(org.hisp.dhis.attribute.Attribute) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Test(org.junit.jupiter.api.Test)

Example 52 with Property

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

the class FieldPathHelper method calculatePathCount.

/**
 * Calculates a weighted map of paths to find candidates for default
 * expansion.
 */
private Map<String, Long> calculatePathCount(Collection<FieldPath> fieldPaths) {
    Map<String, Long> pathCount = new HashMap<>();
    for (FieldPath fieldPath : fieldPaths) {
        Property property = fieldPath.getProperty();
        if (property == null) {
            continue;
        }
        List<String> paths = new ArrayList<>();
        for (String path : fieldPath.getPath()) {
            paths.add(path);
            pathCount.compute(StringUtils.join(paths, FieldPath.FIELD_PATH_SEPARATOR), (key, count) -> count == null ? 1L : count + 1L);
        }
        if (isReference(property) || isComplex(property)) {
            pathCount.compute(fieldPath.toFullPath(), (key, count) -> count == null ? 1L : count + 1L);
        }
    }
    return pathCount;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 53 with Property

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

the class FieldPathHelper method applyDefaults.

/**
 * Applies (recursively) default expansion on the remaining field paths, for
 * example the path 'dataElements.dataElementGroups' would get expanded to
 * 'dataElements.dataElementGroups.id' so that we expose the reference
 * identifier.
 */
private void applyDefaults(FieldPath fieldPath, Class<?> klass, Map<String, FieldPath> fieldPathMap) {
    List<String> paths = new ArrayList<>(fieldPath.getPath());
    paths.add(fieldPath.getName());
    Schema schema = getSchemaByPath(paths, klass);
    if (schema == null) {
        return;
    }
    Property property = fieldPath.getProperty();
    if (isComplex(property)) {
        expandComplex(fieldPathMap, paths, schema);
    } else if (isReference(property)) {
        expandReference(fieldPathMap, paths, schema);
    }
}
Also used : Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 54 with Property

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

the class FieldPathHelper method applyDefault.

private void applyDefault(FieldPath fieldPath, Map<String, FieldPath> fieldPathMap) {
    List<String> paths = new ArrayList<>(fieldPath.getPath());
    paths.add(fieldPath.getName());
    Property property = fieldPath.getProperty();
    fieldPathMap.put(fieldPath.toFullPath(), fieldPath);
    if (property.isSimple()) {
        return;
    }
    Schema schema = schemaService.getDynamicSchema(property.isCollection() ? property.getItemKlass() : property.getKlass());
    if (schema == null) {
        return;
    }
    if (isComplex(property)) {
        expandComplex(fieldPathMap, paths, schema);
    } else if (isReference(property)) {
        expandReference(fieldPathMap, paths, schema);
    }
}
Also used : Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 55 with Property

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

the class FieldPathHelper method applyPresets.

/**
 * Applies field presets. See {@link FieldPreset}.
 *
 * @param presets the list of {@link FieldPath}.
 * @param fieldPathMap mapping of full path and {@link FieldPath} to be
 *        populated.
 * @param rootKlass the root class type of the entity.
 */
public void applyPresets(List<FieldPath> presets, Map<String, FieldPath> fieldPathMap, Class<?> rootKlass) {
    List<FieldPath> fieldPaths = new ArrayList<>();
    for (FieldPath preset : presets) {
        Schema schema = getSchemaByPath(preset.getPath(), rootKlass);
        if (schema == null) {
            continue;
        }
        if (FieldPreset.ALL.equals(preset.getName())) {
            schema.getProperties().forEach(p -> fieldPaths.add(toFieldPath(preset.getPath(), p)));
        } else if (FieldPreset.OWNER.equals(preset.getName())) {
            schema.getProperties().stream().filter(Property::isOwner).forEach(p -> fieldPaths.add(toFieldPath(preset.getPath(), p)));
        } else if (FieldPreset.IDENTIFIABLE.equals(preset.getName())) {
            schema.getProperties().stream().filter(p -> FieldPreset.IDENTIFIABLE_FIELDS.contains(p.getName())).forEach(p -> fieldPaths.add(toFieldPath(preset.getPath(), p)));
        } else if (FieldPreset.SIMPLE.equals(preset.getName())) {
            schema.getProperties().stream().filter(p -> p.getPropertyType().isSimple()).forEach(p -> fieldPaths.add(toFieldPath(preset.getPath(), p)));
        }
    }
    fieldPaths.forEach(fp -> fieldPathMap.put(fp.toFullPath(), fp));
}
Also used : PropertyType(org.hisp.dhis.schema.PropertyType) Collection(java.util.Collection) RequiredArgsConstructor(lombok.RequiredArgsConstructor) HashMap(java.util.HashMap) SchemaService(org.hisp.dhis.schema.SchemaService) Sharing(org.hisp.dhis.user.sharing.Sharing) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) UserAccess(org.hisp.dhis.user.sharing.UserAccess) List(java.util.List) Component(org.springframework.stereotype.Component) UserGroupAccess(org.hisp.dhis.user.sharing.UserGroupAccess) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) Schema(org.hisp.dhis.schema.Schema) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) 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