Search in sources :

Example 56 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class DefaultObjectBundleValidationService method checkUniqueAttributes.

private TypeReport checkUniqueAttributes(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier) {
    TypeReport typeReport = new TypeReport(klass);
    Schema schema = schemaService.getDynamicSchema(klass);
    if (objects.isEmpty() || !schema.havePersistedProperty("attributeValues")) {
        return typeReport;
    }
    Iterator<IdentifiableObject> iterator = objects.iterator();
    int idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        List<ErrorReport> errorReports = checkUniqueAttributes(klass, object, preheat, identifier);
        if (!errorReports.isEmpty()) {
            ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReports(errorReports);
            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) Schema(org.hisp.dhis.schema.Schema) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 57 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class DefaultObjectBundleValidationService method runValidationHooks.

private TypeReport runValidationHooks(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 object = iterator.next();
        List<ErrorReport> errorReports = new ArrayList<>();
        objectBundleHooks.forEach(hook -> errorReports.addAll(hook.validate(object, bundle)));
        if (!errorReports.isEmpty()) {
            ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReports(errorReports);
            typeReport.addObjectReport(objectReport);
            typeReport.getStats().incIgnored();
            iterator.remove();
            continue;
        }
        idx++;
    }
    return typeReport;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 58 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject 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 59 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class EmbeddedObjectObjectBundleHook method handleEmbeddedObjects.

private <T extends IdentifiableObject> void handleEmbeddedObjects(T object, ObjectBundle bundle, Collection<Property> properties) {
    for (Property property : properties) {
        if (property.isCollection()) {
            Collection<?> objects = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            objects.forEach(o -> {
                if (property.isIdentifiableObject()) {
                    ((BaseIdentifiableObject) o).setAutoFields();
                }
                preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
            });
        } else {
            Object o = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
            if (property.isIdentifiableObject()) {
                ((BaseIdentifiableObject) o).setAutoFields();
            }
            preheatService.connectReferences(o, bundle.getPreheat(), bundle.getPreheatIdentifier());
        }
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Property(org.hisp.dhis.schema.Property)

Example 60 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class OrganisationUnitObjectBundleHook method sortOrganisationUnits.

private void sortOrganisationUnits(ObjectBundle bundle) {
    List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(OrganisationUnit.class, false);
    List<IdentifiableObject> persistedObjects = bundle.getObjects(OrganisationUnit.class, true);
    nonPersistedObjects.sort(new OrganisationUnitParentCountComparator());
    persistedObjects.sort(new OrganisationUnitParentCountComparator());
}
Also used : OrganisationUnitParentCountComparator(org.hisp.dhis.organisationunit.comparator.OrganisationUnitParentCountComparator) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)124 List (java.util.List)76 Test (org.junit.Test)67 DhisSpringTest (org.hisp.dhis.DhisSpringTest)64 ClassPathResource (org.springframework.core.io.ClassPathResource)54 DataElement (org.hisp.dhis.dataelement.DataElement)44 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)39 User (org.hisp.dhis.user.User)37 DataSet (org.hisp.dhis.dataset.DataSet)24 ArrayList (java.util.ArrayList)22 ObjectReport (org.hisp.dhis.feedback.ObjectReport)22 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Schema (org.hisp.dhis.schema.Schema)19 HashMap (java.util.HashMap)18 TypeReport (org.hisp.dhis.feedback.TypeReport)18 Set (java.util.Set)15 ErrorReport (org.hisp.dhis.feedback.ErrorReport)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)15 Map (java.util.Map)14 Property (org.hisp.dhis.schema.Property)13