Search in sources :

Example 6 with ErrorReport

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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 7 with ErrorReport

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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) TypeReport(org.hisp.dhis.feedback.TypeReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 8 with ErrorReport

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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with ErrorReport

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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 10 with ErrorReport

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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

ErrorReport (org.hisp.dhis.feedback.ErrorReport)30 ObjectReport (org.hisp.dhis.feedback.ObjectReport)17 ArrayList (java.util.ArrayList)15 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)14 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)14 TypeReport (org.hisp.dhis.feedback.TypeReport)13 User (org.hisp.dhis.user.User)8 Schema (org.hisp.dhis.schema.Schema)5 List (java.util.List)4 ErrorCode (org.hisp.dhis.feedback.ErrorCode)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)3 Test (org.junit.Test)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2