use of org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationContext in project dhis2-core by dhis2.
the class UniquenessCheck method checkUniqueness.
private List<ErrorReport> checkUniqueness(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, ValidationContext ctx) {
if (object == null || preheat.isDefault(object)) {
return emptyList();
}
@SuppressWarnings("unchecked") Class<? extends IdentifiableObject> objType = HibernateProxyUtils.getRealClass(object);
Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().computeIfAbsent(objType, key -> new HashMap<>());
Schema schema = ctx.getSchemaService().getDynamicSchema(objType);
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
if (uniqueProperties.isEmpty()) {
return emptyList();
}
return checkUniqueness(object, identifier, uniquenessMap, uniqueProperties);
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationContext in project dhis2-core by dhis2.
the class NotOwnerReferencesCheck method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, PreheatIdentifier identifier, ValidationContext ctx) {
if (object == null) {
return emptyList();
}
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
Schema schema = ctx.getSchemaService().getDynamicSchema(HibernateProxyUtils.getRealClass(object));
schema.getProperties().stream().filter(p -> !p.isOwner() && p.isWritable() && (PropertyType.REFERENCE == p.getPropertyType() && schema.getKlass() != p.getKlass() || PropertyType.REFERENCE == p.getItemPropertyType() && schema.getKlass() != p.getItemKlass())).forEach(p -> {
if (!p.isCollection()) {
checkReference(object, identifier, preheatErrorReports, p);
} else {
checkCollection(object, identifier, preheatErrorReports, p);
}
});
return preheatErrorReports;
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationContext in project dhis2-core by dhis2.
the class GeoJsonAttributesCheckTest method setUpTest.
@BeforeEach
public void setUpTest() {
organisationUnit = new OrganisationUnit();
organisationUnit.setName("A");
attribute = new Attribute();
attribute.setUid("geoJson");
attribute.setName("geoJson");
validationContext = Mockito.mock(ValidationContext.class);
Schema schema = new Schema(OrganisationUnit.class, "organisationUnit", "organisationUnits");
Property property = new Property();
property.setPersisted(true);
schema.getPropertyMap().put("attributeValues", property);
SchemaService schemaService = Mockito.mock(SchemaService.class);
when(schemaService.getDynamicSchema(OrganisationUnit.class)).thenReturn(schema);
when(validationContext.getSchemaService()).thenReturn(schemaService);
Preheat preheat = Mockito.mock(Preheat.class);
when(preheat.getAttributeIdsByValueType(OrganisationUnit.class, ValueType.GEOJSON)).thenReturn(Sets.newSet("geoJson"));
objectBundle = Mockito.mock(ObjectBundle.class);
when(objectBundle.getPreheat()).thenReturn(preheat);
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationContext in project dhis2-core by dhis2.
the class ReferencesCheck method checkReferences.
private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing, ValidationContext ctx) {
if (object == null) {
return emptyList();
}
List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
Schema schema = ctx.getSchemaService().getDynamicSchema(HibernateProxyUtils.getRealClass(object));
schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).forEach(p -> {
if (skipCheck(p.getKlass()) || skipCheck(p.getItemKlass())) {
return;
}
if (!p.isCollection()) {
checkReference(object, preheat, identifier, skipSharing, preheatErrorReports, p);
} else {
checkCollection(object, preheat, identifier, preheatErrorReports, p);
}
});
if (schema.havePersistedProperty("attributeValues")) {
checkAttributeValues(object, preheat, identifier, preheatErrorReports);
}
if (schema.havePersistedProperty("sharing") && !skipSharing && object.getSharing() != null) {
checkSharing(object, preheat, preheatErrorReports);
}
return preheatErrorReports;
}
use of org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationContext in project dhis2-core by dhis2.
the class UniqueMultiPropertiesCheck method check.
@Override
public <T extends IdentifiableObject> void check(ObjectBundle bundle, Class<T> klass, List<T> persistedObjects, List<T> nonPersistedObjects, ImportStrategy importStrategy, ValidationContext context, Consumer<ObjectReport> addReports) {
Schema schema = context.getSchemaService().getSchema(klass);
Map<List<String>, List<IdentifiableObject>> propertyValuesToObjects = new HashMap<>();
for (T identifiableObject : nonPersistedObjects) {
for (Map.Entry<Collection<String>, Collection<Function<IdentifiableObject, String>>> entry : schema.getUniqueMultiPropertiesExctractors().entrySet()) {
List<String> propertyValues = entry.getValue().stream().map(valueExtractor -> valueExtractor.apply(identifiableObject)).collect(Collectors.toList());
propertyValuesToObjects.computeIfAbsent(propertyValues, key -> new ArrayList<>()).add(identifiableObject);
}
}
for (Map.Entry<List<String>, List<IdentifiableObject>> entry : propertyValuesToObjects.entrySet()) {
List<IdentifiableObject> objects = entry.getValue();
if (objects.size() > 1) {
for (IdentifiableObject object : objects) {
ErrorReport errorReport = new ErrorReport(klass, E5005, String.join(", ", entry.getKey()), objects.stream().map(IdentifiableObject::getUid).collect(joining(", ")));
addReports.accept(ValidationUtils.createObjectReport(errorReport, object, bundle));
context.markForRemoval(object);
}
}
}
}
Aggregations