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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations