use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method validateForDelete.
private TypeReport validateForDelete(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
TypeReport typeReport = new TypeReport(klass);
if (objects == null || objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject identifiableObject = iterator.next();
IdentifiableObject object = bundle.getPreheat().get(bundle.getPreheatIdentifier(), identifiableObject);
if (object == null || object.getId() == 0) {
if (Preheat.isDefaultClass(identifiableObject.getClass()))
continue;
ObjectReport objectReport = new ObjectReport(klass, idx, object != null ? object.getUid() : null);
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E5001, bundle.getPreheatIdentifier(), bundle.getPreheatIdentifier().getIdentifiersWithName(identifiableObject)).setMainId(object != null ? object.getUid() : null));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method validateSecurity.
private TypeReport validateSecurity(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle, ImportStrategy importMode) {
TypeReport typeReport = new TypeReport(klass);
if (objects == null || objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
PreheatIdentifier identifier = bundle.getPreheatIdentifier();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
if (importMode.isCreate()) {
if (!aclService.canCreate(bundle.getUser(), klass)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3000, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
} else {
IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
if (importMode.isUpdate()) {
if (!aclService.canUpdate(bundle.getUser(), persistedObject)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3001, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
} else if (importMode.isDelete()) {
if (!aclService.canDelete(bundle.getUser(), persistedObject)) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3002, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
}
}
if (User.class.isInstance(object)) {
User user = (User) object;
List<ErrorReport> errorReports = userService.validateUser(user, bundle.getUser());
if (!errorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
}
List<ErrorReport> sharingErrorReports = aclService.verifySharing(object, bundle.getUser());
if (!sharingErrorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(sharingErrorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueAttributes.
private List<ErrorReport> checkUniqueAttributes(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
List<ErrorReport> errorReports = new ArrayList<>();
if (object == null || Preheat.isDefault(object) || !preheat.getUniqueAttributes().containsKey(klass)) {
return errorReports;
}
Set<AttributeValue> attributeValues = object.getAttributeValues();
// make copy for modification
List<String> uniqueAttributes = new ArrayList<>(preheat.getUniqueAttributes().get(klass));
if (!preheat.getUniqueAttributeValues().containsKey(klass)) {
preheat.getUniqueAttributeValues().put(klass, new HashMap<>());
}
Map<String, Map<String, String>> uniqueAttributeValues = preheat.getUniqueAttributeValues().get(klass);
if (uniqueAttributes.isEmpty()) {
return errorReports;
}
attributeValues.forEach(attributeValue -> {
Attribute attribute = preheat.get(identifier, attributeValue.getAttribute());
if (attribute == null || !attribute.isUnique() || StringUtils.isEmpty(attributeValue.getValue())) {
return;
}
if (uniqueAttributeValues.containsKey(attribute.getUid())) {
Map<String, String> values = uniqueAttributeValues.get(attribute.getUid());
if (values.containsKey(attributeValue.getValue()) && !values.get(attributeValue.getValue()).equals(object.getUid())) {
errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4009, IdentifiableObjectUtils.getDisplayName(attribute), attributeValue.getValue()).setMainId(attribute.getUid()).setErrorProperty("value"));
} else {
uniqueAttributeValues.get(attribute.getUid()).put(attributeValue.getValue(), object.getUid());
}
} else {
uniqueAttributeValues.put(attribute.getUid(), new HashMap<>());
uniqueAttributeValues.get(attribute.getUid()).put(attributeValue.getValue(), object.getUid());
}
});
return errorReports;
}
use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueAttributes.
private TypeReport checkUniqueAttributes(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier) {
TypeReport typeReport = new TypeReport(klass);
Schema schema = schemaService.getDynamicSchema(klass);
if (objects.isEmpty() || !schema.havePersistedProperty("attributeValues")) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
List<ErrorReport> errorReports = checkUniqueAttributes(klass, object, preheat, identifier);
if (!errorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
}
idx++;
}
return typeReport;
}
use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method runValidationHooks.
private TypeReport runValidationHooks(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
TypeReport typeReport = new TypeReport(klass);
if (objects == null || objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
List<ErrorReport> errorReports = new ArrayList<>();
objectBundleHooks.forEach(hook -> errorReports.addAll(hook.validate(object, bundle)));
if (!errorReports.isEmpty()) {
ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
objectReport.addErrorReports(errorReports);
typeReport.addObjectReport(objectReport);
typeReport.getStats().incIgnored();
iterator.remove();
continue;
}
idx++;
}
return typeReport;
}
Aggregations