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"));
}
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;
}
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);
}
}
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);
}
}
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));
}
Aggregations