Search in sources :

Example 31 with ObjectReport

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

the class ReferencesCheck method check.

@Override
public <T extends IdentifiableObject> TypeReport check(ObjectBundle bundle, Class<T> klass, List<T> persistedObjects, List<T> nonPersistedObjects, ImportStrategy importStrategy, ValidationContext ctx) {
    if (persistedObjects.isEmpty() && nonPersistedObjects.isEmpty()) {
        return TypeReport.empty(klass);
    }
    TypeReport typeReport = new TypeReport(klass);
    for (IdentifiableObject object : joinObjects(persistedObjects, nonPersistedObjects)) {
        List<PreheatErrorReport> errorReports = checkReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing(), ctx);
        if (!errorReports.isEmpty() && object != null) {
            ObjectReport objectReport = new ObjectReport(object, bundle);
            objectReport.addErrorReports(errorReports);
            typeReport.addObjectReport(objectReport);
        }
    }
    if (typeReport.hasErrorReports() && AtomicMode.ALL == bundle.getAtomicMode()) {
        typeReport.getStats().incIgnored();
    }
    return typeReport;
}
Also used : TypeReport(org.hisp.dhis.feedback.TypeReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 32 with ObjectReport

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

the class DefaultObjectBundleService method handleUpdates.

private <T extends IdentifiableObject> TypeReport handleUpdates(Session session, Class<T> klass, List<T> objects, ObjectBundle bundle) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects.isEmpty()) {
        return typeReport;
    }
    String message = "(" + bundle.getUsername() + ") Updating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
    log.info(message);
    if (bundle.hasJobId()) {
        notifier.notify(bundle.getJobId(), message);
    }
    objects.forEach(object -> {
        T persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
        objectBundleHooks.getObjectHooks(object).forEach(hook -> hook.preUpdate(object, persistedObject, bundle));
    });
    session.flush();
    for (T object : objects) {
        T persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
        ObjectReport objectReport = new ObjectReport(object, bundle);
        objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
        typeReport.addObjectReport(objectReport);
        preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
        if (bundle.getMergeMode() != MergeMode.NONE) {
            mergeService.merge(new MergeParams<>(object, persistedObject).setMergeMode(bundle.getMergeMode()).setSkipSharing(bundle.isSkipSharing()).setSkipTranslation(bundle.isSkipTranslation()));
        }
        if (bundle.getOverrideUser() != null) {
            persistedObject.setCreatedBy(bundle.getOverrideUser());
            if (object instanceof User) {
                (object).setCreatedBy(bundle.getOverrideUser());
            }
        }
        session.update(persistedObject);
        bundle.getPreheat().replace(bundle.getPreheatIdentifier(), persistedObject);
        if (log.isDebugEnabled()) {
            String msg = "(" + bundle.getUsername() + ") Updated object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(persistedObject) + "'";
            log.debug(msg);
        }
        if (FlushMode.OBJECT == bundle.getFlushMode()) {
            session.flush();
        }
    }
    session.flush();
    objects.forEach(object -> {
        T persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
        objectBundleHooks.getObjectHooks(object).forEach(hook -> hook.postUpdate(persistedObject, bundle));
    });
    return typeReport;
}
Also used : User(org.hisp.dhis.user.User) TypeReport(org.hisp.dhis.feedback.TypeReport) MergeParams(org.hisp.dhis.schema.MergeParams) ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Example 33 with ObjectReport

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

the class DefaultObjectBundleService method handleCreates.

// -----------------------------------------------------------------------------------
// Utility Methods
// -----------------------------------------------------------------------------------
private <T extends IdentifiableObject> TypeReport handleCreates(Session session, Class<T> klass, List<T> objects, ObjectBundle bundle) {
    TypeReport typeReport = new TypeReport(klass);
    handleDeprecationIfEventReport(klass, objects);
    if (objects.isEmpty()) {
        return typeReport;
    }
    String message = "(" + bundle.getUsername() + ") Creating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
    log.info(message);
    if (bundle.hasJobId()) {
        notifier.notify(bundle.getJobId(), message);
    }
    objects.forEach(object -> objectBundleHooks.getObjectHooks(object).forEach(hook -> hook.preCreate(object, bundle)));
    session.flush();
    for (T object : objects) {
        ObjectReport objectReport = new ObjectReport(object, bundle);
        objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
        typeReport.addObjectReport(objectReport);
        preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
        if (bundle.getOverrideUser() != null) {
            object.setCreatedBy(bundle.getOverrideUser());
            if (object instanceof User) {
                (object).setCreatedBy(bundle.getOverrideUser());
            }
        }
        session.save(object);
        bundle.getPreheat().replace(bundle.getPreheatIdentifier(), object);
        if (log.isDebugEnabled()) {
            String msg = "(" + bundle.getUsername() + ") Created object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(object) + "'";
            log.debug(msg);
        }
        if (FlushMode.OBJECT == bundle.getFlushMode()) {
            session.flush();
        }
    }
    session.flush();
    objects.forEach(object -> objectBundleHooks.getObjectHooks(object).forEach(hook -> hook.postCreate(object, bundle)));
    return typeReport;
}
Also used : HibernateCacheManager(org.hisp.dhis.cache.HibernateCacheManager) PreheatService(org.hisp.dhis.preheat.PreheatService) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) EventReportCompatibilityGuard.handleDeprecationIfEventReport(org.hisp.dhis.dxf2.metadata.objectbundle.EventReportCompatibilityGuard.handleDeprecationIfEventReport) MergeService(org.hisp.dhis.schema.MergeService) Session(org.hibernate.Session) Preheat(org.hisp.dhis.preheat.Preheat) HashMap(java.util.HashMap) MergeParams(org.hisp.dhis.schema.MergeParams) FlushMode(org.hisp.dhis.dxf2.metadata.FlushMode) TypeReport(org.hisp.dhis.feedback.TypeReport) Notifier(org.hisp.dhis.system.notification.Notifier) ObjectBundleCommitReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) Service(org.springframework.stereotype.Service) Map(java.util.Map) User(org.hisp.dhis.user.User) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SessionFactory(org.hibernate.SessionFactory) SchemaService(org.hisp.dhis.schema.SchemaService) MergeMode(org.hisp.dhis.common.MergeMode) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) PreheatParams(org.hisp.dhis.preheat.PreheatParams) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AllArgsConstructor(lombok.AllArgsConstructor) Transactional(org.springframework.transaction.annotation.Transactional) User(org.hisp.dhis.user.User) TypeReport(org.hisp.dhis.feedback.TypeReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Example 34 with ObjectReport

use of org.hisp.dhis.feedback.ObjectReport 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)

Example 35 with ObjectReport

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

the class ValidationUtils method createObjectReport.

public static ObjectReport createObjectReport(List<ErrorReport> reports, IdentifiableObject object, ObjectBundle bundle) {
    ObjectReport objectReport = new ObjectReport(object, bundle);
    objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
    objectReport.addErrorReports(reports);
    return objectReport;
}
Also used : ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Aggregations

ObjectReport (org.hisp.dhis.feedback.ObjectReport)46 TypeReport (org.hisp.dhis.feedback.TypeReport)29 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)22 ErrorReport (org.hisp.dhis.feedback.ErrorReport)22 ArrayList (java.util.ArrayList)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)13 User (org.hisp.dhis.user.User)9 Schema (org.hisp.dhis.schema.Schema)7 List (java.util.List)6 AttributeValue (org.hisp.dhis.attribute.AttributeValue)6 Test (org.junit.jupiter.api.Test)6 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)5 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)5 SchemaService (org.hisp.dhis.schema.SchemaService)5 Service (org.springframework.stereotype.Service)5 HashMap (java.util.HashMap)4 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)4 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)4 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3