Search in sources :

Example 21 with ErrorReport

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

the class DefaultSchemaValidator method validateCollection.

private List<? extends ErrorReport> validateCollection(Class<?> klass, Object propertyObject, Property property) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (!Collection.class.isInstance(propertyObject)) {
        return errorReports;
    }
    Collection<?> value = (Collection<?>) propertyObject;
    if (value.size() < property.getMin() || value.size() > property.getMax()) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4007, property.getName(), property.getMin(), property.getMax(), value.size()).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 22 with ErrorReport

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

the class DefaultSchemaValidator method validateDouble.

private List<? extends ErrorReport> validateDouble(Class<?> klass, Object propertyObject, Property property) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (!Double.class.isInstance(propertyObject)) {
        return errorReports;
    }
    Double value = (Double) propertyObject;
    if (!GenericValidator.isInRange(value, property.getMin(), property.getMax())) {
        errorReports.add(new ErrorReport(klass, ErrorCode.E4008, property.getName(), property.getMin(), property.getMax(), value).setErrorKlass(property.getKlass()).setErrorProperty(property.getName()));
    }
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ArrayList(java.util.ArrayList)

Example 23 with ErrorReport

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

the class DefaultUserService method validateUser.

@Override
public List<ErrorReport> validateUser(User user, User currentUser) {
    List<ErrorReport> errors = new ArrayList<>();
    if (currentUser == null || currentUser.getUserCredentials() == null || user == null || user.getUserCredentials() == null) {
        return errors;
    }
    // validate user role
    boolean canGrantOwnUserAuthorityGroups = (Boolean) systemSettingManager.getSystemSetting(SettingKey.CAN_GRANT_OWN_USER_AUTHORITY_GROUPS);
    user.getUserCredentials().getUserAuthorityGroups().forEach(ur -> {
        if (!currentUser.getUserCredentials().canIssueUserRole(ur, canGrantOwnUserAuthorityGroups)) {
            errors.add(new ErrorReport(UserAuthorityGroup.class, ErrorCode.E3003, currentUser, ur));
        }
    });
    // validate group
    boolean canAdd = currentUser.getUserCredentials().isAuthorized(UserGroup.AUTH_USER_ADD);
    if (canAdd) {
        return errors;
    }
    boolean canAddInGroup = currentUser.getUserCredentials().isAuthorized(UserGroup.AUTH_USER_ADD_IN_GROUP);
    if (!canAddInGroup) {
        errors.add(new ErrorReport(UserGroup.class, ErrorCode.E3004, currentUser));
        return errors;
    }
    user.getGroups().forEach(ug -> {
        if (!(currentUser.canManage(ug) || userGroupService.canAddOrRemoveMember(ug.getUid()))) {
            errors.add(new ErrorReport(UserGroup.class, ErrorCode.E3005, currentUser, ug));
        }
    });
    return errors;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) ArrayList(java.util.ArrayList)

Example 24 with ErrorReport

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

the class DefaultObjectBundleValidationService method checkDuplicateIds.

private TypeReport checkDuplicateIds(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> persistedObjects, List<IdentifiableObject> nonPersistedObjects, Preheat preheat, PreheatIdentifier identifier) {
    TypeReport typeReport = new TypeReport(klass);
    if (persistedObjects.isEmpty() && nonPersistedObjects.isEmpty()) {
        return typeReport;
    }
    Map<Class<?>, String> idMap = new HashMap<>();
    Iterator<IdentifiableObject> iterator = persistedObjects.iterator();
    int idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        if (idMap.containsKey(object.getClass()) && idMap.get(object.getClass()).equals(object.getUid())) {
            ErrorReport errorReport = new ErrorReport(object.getClass(), ErrorCode.E5004, object.getUid(), object.getClass()).setMainId(object.getUid()).setErrorProperty("id");
            ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReport(errorReport);
            typeReport.addObjectReport(objectReport);
            typeReport.getStats().incIgnored();
            iterator.remove();
        } else {
            idMap.put(object.getClass(), object.getUid());
        }
        idx++;
    }
    iterator = nonPersistedObjects.iterator();
    idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        if (idMap.containsKey(object.getClass()) && idMap.get(object.getClass()).equals(object.getUid())) {
            ErrorReport errorReport = new ErrorReport(object.getClass(), ErrorCode.E5004, object.getUid(), object.getClass()).setMainId(object.getUid()).setErrorProperty("id");
            ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReport(errorReport);
            typeReport.addObjectReport(objectReport);
            typeReport.getStats().incIgnored();
            iterator.remove();
        } else {
            idMap.put(object.getClass(), object.getUid());
        }
        idx++;
    }
    return typeReport;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) HashMap(java.util.HashMap) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 25 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport 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;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) TypeReport(org.hisp.dhis.feedback.TypeReport) UserCredentials(org.hisp.dhis.user.UserCredentials) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Service(org.springframework.stereotype.Service) Map(java.util.Map) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) UserService(org.hisp.dhis.user.UserService) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Timer(org.hisp.dhis.commons.timer.Timer) SchemaService(org.hisp.dhis.schema.SchemaService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) SchemaValidator(org.hisp.dhis.schema.validation.SchemaValidator) List(java.util.List) AclService(org.hisp.dhis.security.acl.AclService) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HashMap(java.util.HashMap) Map(java.util.Map) 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