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