Search in sources :

Example 11 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.

the class DefaultObjectBundleValidationService method validateForCreate.

private TypeReport validateForCreate(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) {
            ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E5000, bundle.getPreheatIdentifier(), bundle.getPreheatIdentifier().getIdentifiersWithName(identifiableObject)).setMainId(identifiableObject.getUid()));
            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 12 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.

the class ObjectBundleCommitReport method getErrorReportsByCode.

//-----------------------------------------------------------------------------------
// Utility Methods
//-----------------------------------------------------------------------------------
public List<ErrorReport> getErrorReportsByCode(Class<?> klass, ErrorCode errorCode) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (!typeReportMap.containsKey(klass)) {
        return errorReports;
    }
    List<ObjectReport> objectReports = typeReportMap.get(klass).getObjectReports();
    for (ObjectReport objectReport : objectReports) {
        List<ErrorReport> byCode = objectReport.getErrorReportsByCode().get(errorCode);
        if (byCode != null) {
            errorReports.addAll(byCode);
        }
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ArrayList(java.util.ArrayList) ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Example 13 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.

the class DefaultAclService method verifySharing.

@Override
public <T extends IdentifiableObject> List<ErrorReport> verifySharing(T object, User user) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null || !isShareable(object.getClass()) || user == null) {
        return errorReports;
    }
    if (!AccessStringHelper.isValid(object.getPublicAccess())) {
        errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3010, object.getPublicAccess()));
        return errorReports;
    }
    boolean canMakePublic = canMakePublic(user, object.getClass());
    boolean canMakePrivate = canMakePrivate(user, object.getClass());
    boolean canMakeExternal = canMakeExternal(user, object.getClass());
    if (object.getExternalAccess()) {
        if (!canMakeExternal) {
            errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3006, user.getUsername(), object.getClass()));
        }
    }
    if (AccessStringHelper.DEFAULT.equals(object.getPublicAccess())) {
        if (canMakePublic || canMakePrivate) {
            return errorReports;
        }
        errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3009, user.getUsername(), object.getClass()));
    } else {
        if (canMakePublic) {
            return errorReports;
        }
        errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E3008, user.getUsername(), object.getClass()));
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ArrayList(java.util.ArrayList)

Example 14 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.

the class DefaultSchemaValidator method validateString.

private List<? extends ErrorReport> validateString(Class<?> klass, Object propertyObject, Property property) {
    List<ErrorReport> errorReports = new ArrayList<>();
    // TODO How should empty strings be handled? they are not valid color, password, url, etc of course.
    if (!String.class.isInstance(propertyObject) || StringUtils.isEmpty(propertyObject)) {
        return errorReports;
    }
    String value = (String) propertyObject;
    // check column max length
    if (value.length() > property.getLength()) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4001, property.getName(), property.getLength(), value.length()).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
        return errorReports;
    }
    if (value.length() < property.getMin() || value.length() > property.getMax()) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4002, property.getName(), property.getMin(), property.getMax(), value.length()).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    }
    if (PropertyType.EMAIL == property.getPropertyType() && !GenericValidator.isEmail(value)) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4003, property.getName(), value).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    } else if (PropertyType.URL == property.getPropertyType() && !isUrl(value)) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4004, property.getName(), value).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    } else if (!BCRYPT_PATTERN.matcher(value).matches() && PropertyType.PASSWORD == property.getPropertyType() && !ValidationUtils.passwordIsValid(value)) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4005, property.getName(), value).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    } else if (PropertyType.COLOR == property.getPropertyType() && !ValidationUtils.isValidHexColor(value)) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4006, property.getName(), value).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ArrayList(java.util.ArrayList)

Example 15 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport in project dhis2-core by dhis2.

the class DefaultSchemaValidator method validate.

@Override
public List<ErrorReport> validate(Object object, boolean persisted) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null) {
        return errorReports;
    }
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    if (schema == null) {
        return errorReports;
    }
    Class<?> klass = object.getClass();
    for (Property property : schema.getProperties()) {
        if (persisted && !property.isPersisted()) {
            continue;
        }
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value == null) {
            if (property.isRequired() && !Preheat.isDefaultClass(property.getKlass())) {
                errorReports.add(new ErrorReport(klass, ErrorCode.E4000, property.getName()).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
            }
            continue;
        }
        errorReports.addAll(validateString(klass, value, property));
        errorReports.addAll(validateCollection(klass, value, property));
        errorReports.addAll(validateInteger(klass, value, property));
        errorReports.addAll(validateFloat(klass, value, property));
        errorReports.addAll(validateDouble(klass, value, property));
    }
    if (User.class.isInstance(object)) {
        User user = (User) object;
        if (user.getUserCredentials() != null) {
            errorReports.addAll(validate(user.getUserCredentials(), persisted));
        }
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) User(org.hisp.dhis.user.User) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

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