use of org.hisp.dhis.preheat.Preheat in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
if (object == null) {
return preheatErrorReports;
}
Schema schema = schemaService.getDynamicSchema(object.getClass());
schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
return;
}
if (!p.isCollection()) {
IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
}
}
} else {
Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
for (IdentifiableObject refObject : refObjects) {
if (Preheat.isDefault(refObject))
continue;
IdentifiableObject ref = preheat.get(identifier, refObject);
if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
} else {
objects.add(refObject);
}
}
ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
}
});
if (schema.havePersistedProperty("attributeValues")) {
object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
}
if (schema.havePersistedProperty("userGroupAccesses")) {
object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
}
if (schema.havePersistedProperty("userAccesses")) {
object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
}
return preheatErrorReports;
}
use of org.hisp.dhis.preheat.Preheat in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueness.
private List<ErrorReport> checkUniqueness(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null || Preheat.isDefault(object))
return errorReports;
if (!preheat.getUniquenessMap().containsKey(object.getClass())) {
preheat.getUniquenessMap().put(object.getClass(), new HashMap<>());
}
Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().get(object.getClass());
Schema schema = schemaService.getDynamicSchema(object.getClass());
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
uniqueProperties.forEach(property -> {
if (!uniquenessMap.containsKey(property.getName())) {
uniquenessMap.put(property.getName(), new HashMap<>());
}
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null) {
String persistedUid = uniquenessMap.get(property.getName()).get(value);
if (persistedUid != null) {
if (!object.getUid().equals(persistedUid)) {
errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E5003, property.getName(), value, identifier.getIdentifiersWithName(object), persistedUid).setMainId(persistedUid).setErrorProperty(property.getName()));
}
} else {
uniquenessMap.get(property.getName()).put(value, object.getUid());
}
}
});
return errorReports;
}
Aggregations