use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method preUpdate.
@Override
public void preUpdate(IdentifiableObject object, IdentifiableObject persistedObject, ObjectBundle bundle) {
Schema schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
if (schema == null || schema.getEmbeddedObjectProperties().isEmpty()) {
return;
}
Collection<Property> properties = schema.getEmbeddedObjectProperties().values();
clearEmbeddedObjects(persistedObject, bundle, properties);
handleEmbeddedObjects(object, bundle, properties);
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method validate.
@Override
public void validate(IdentifiableObject object, ObjectBundle bundle, Consumer<ErrorReport> addReports) {
Class<? extends IdentifiableObject> klass = object.getClass();
Schema schema = schemaService.getDynamicSchema(klass);
schema.getEmbeddedObjectProperties().keySet().stream().forEach(propertyName -> {
Property property = schema.getEmbeddedObjectProperties().get(propertyName);
Object propertyObject = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (property.getPropertyType().equals(PropertyType.COMPLEX)) {
schemaValidator.validateEmbeddedObject(propertyObject, klass).forEach(unformattedError -> addReports.accept(formatEmbeddedErrorReport(unformattedError, propertyName)));
} else if (property.getPropertyType().equals(PropertyType.COLLECTION)) {
Collection<?> collection = (Collection<?>) propertyObject;
for (Object item : collection) {
schemaValidator.validateEmbeddedObject(property.getItemKlass().cast(item), klass).forEach(unformattedError -> addReports.accept(formatEmbeddedErrorReport(unformattedError, propertyName)));
}
}
});
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class EmbeddedObjectObjectBundleHook method handleProperty.
private void handleProperty(Object object, ObjectBundle bundle, Property property) {
if (object == null || bundle == null || property == null) {
return;
}
if (property.isIdentifiableObject()) {
((BaseIdentifiableObject) object).setAutoFields();
}
Schema embeddedSchema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
for (Property embeddedProperty : embeddedSchema.getPropertyMap().values()) {
if (PeriodType.class.isAssignableFrom(embeddedProperty.getKlass())) {
PeriodType periodType = ReflectionUtils.invokeMethod(object, embeddedProperty.getGetterMethod());
if (periodType != null) {
periodType = bundle.getPreheat().getPeriodTypeMap().get(periodType.getName());
ReflectionUtils.invokeMethod(object, embeddedProperty.getSetterMethod(), periodType);
}
}
}
preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class UniquenessCheck method checkUniqueness.
private List<ErrorReport> checkUniqueness(IdentifiableObject object, PreheatIdentifier identifier, Map<String, Map<Object, String>> uniquenessMap, List<Property> uniqueProperties) {
List<ErrorReport> errorReports = new ArrayList<>();
uniqueProperties.forEach(property -> {
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null) {
String objectIdentifier = uniquenessMap.computeIfAbsent(property.getName(), key -> new HashMap<>()).get(value);
if (objectIdentifier != null) {
if (!identifier.getIdentifier(object).equals(objectIdentifier)) {
String identifiersWithName = identifier.getIdentifiersWithName(object);
ErrorReport errorReport = new ErrorReport(HibernateProxyUtils.getRealClass(object), ErrorCode.E5003, property.getName(), value, identifiersWithName, objectIdentifier);
errorReports.add(errorReport.setMainId(objectIdentifier).setErrorProperty(property.getName()));
}
} else {
uniquenessMap.get(property.getName()).put(value, identifier.getIdentifier(object));
}
}
});
return errorReports;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class UniquenessCheck method checkUniqueness.
private List<ErrorReport> checkUniqueness(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, ValidationContext ctx) {
if (object == null || preheat.isDefault(object)) {
return emptyList();
}
@SuppressWarnings("unchecked") Class<? extends IdentifiableObject> objType = HibernateProxyUtils.getRealClass(object);
Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().computeIfAbsent(objType, key -> new HashMap<>());
Schema schema = ctx.getSchemaService().getDynamicSchema(objType);
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
if (uniqueProperties.isEmpty()) {
return emptyList();
}
return checkUniqueness(object, identifier, uniquenessMap, uniqueProperties);
}
Aggregations