Search in sources :

Example 1 with ValidationContext

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);
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) ValidationUtils.createObjectReport(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.createObjectReport) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) Schema(org.hisp.dhis.schema.Schema) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.hisp.dhis.schema.Property)

Example 2 with ValidationContext

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;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Collections.emptyList(java.util.Collections.emptyList) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Collection(java.util.Collection) Property(org.hisp.dhis.schema.Property) ArrayList(java.util.ArrayList) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) TypeReport(org.hisp.dhis.feedback.TypeReport) List(java.util.List) Component(org.springframework.stereotype.Component) ValidationUtils.joinObjects(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.joinObjects) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList)

Example 3 with ValidationContext

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) Attribute(org.hisp.dhis.attribute.Attribute) SchemaService(org.hisp.dhis.schema.SchemaService) Schema(org.hisp.dhis.schema.Schema) Preheat(org.hisp.dhis.preheat.Preheat) Property(org.hisp.dhis.schema.Property) ValidationContext(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ValidationContext

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;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) ArrayList(java.util.ArrayList) TypeReport(org.hisp.dhis.feedback.TypeReport) 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) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Sharing(org.hisp.dhis.user.sharing.Sharing) Property(org.hisp.dhis.schema.Property) List(java.util.List) Component(org.springframework.stereotype.Component) ValidationUtils.joinObjects(org.hisp.dhis.dxf2.metadata.objectbundle.validation.ValidationUtils.joinObjects) PeriodType(org.hisp.dhis.period.PeriodType) Schema(org.hisp.dhis.schema.Schema) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList)

Example 5 with ValidationContext

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);
            }
        }
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Collection(java.util.Collection) E5005(org.hisp.dhis.feedback.ErrorCode.E5005) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) Component(org.springframework.stereotype.Component) Map(java.util.Map) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ObjectBundle (org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle)5 Schema (org.hisp.dhis.schema.Schema)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 ObjectReport (org.hisp.dhis.feedback.ObjectReport)4 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)4 Property (org.hisp.dhis.schema.Property)4 Component (org.springframework.stereotype.Component)4 Collection (java.util.Collection)3 Collections.emptyList (java.util.Collections.emptyList)3 ErrorCode (org.hisp.dhis.feedback.ErrorCode)3 HibernateProxyUtils (org.hisp.dhis.hibernate.HibernateProxyUtils)3 Preheat (org.hisp.dhis.preheat.Preheat)3 PreheatIdentifier (org.hisp.dhis.preheat.PreheatIdentifier)3 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2