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