Search in sources :

Example 36 with TypeReport

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

the class DefaultObjectBundleValidationService method checkUniqueness.

private TypeReport checkUniqueness(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects.isEmpty()) {
        return typeReport;
    }
    Iterator<IdentifiableObject> iterator = objects.iterator();
    int idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        List<ErrorReport> errorReports = new ArrayList<>();
        if (User.class.isInstance(object)) {
            User user = (User) object;
            errorReports.addAll(checkUniqueness(User.class, user, preheat, identifier));
            errorReports.addAll(checkUniqueness(UserCredentials.class, user.getUserCredentials(), preheat, identifier));
        } else {
            errorReports = checkUniqueness(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) User(org.hisp.dhis.user.User) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) UserCredentials(org.hisp.dhis.user.UserCredentials) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 37 with TypeReport

use of org.hisp.dhis.feedback.TypeReport 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 38 with TypeReport

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

the class DefaultCollectionService method addCollectionItems.

@Override
@Transactional
public TypeReport addCollectionItems(IdentifiableObject object, String propertyName, Collection<? extends IdentifiableObject> objects) throws Exception {
    Property property = validateUpdate(object, propertyName, "Only identifiable object collections can be added to.");
    Collection<String> itemCodes = getItemCodes(objects);
    if (itemCodes.isEmpty()) {
        return TypeReport.empty(property.getItemKlass());
    }
    TypeReport report = new TypeReport(property.getItemKlass());
    manager.refresh(object);
    if (property.isOwner()) {
        addOwnedCollectionItems(object, property, itemCodes, report);
    } else {
        addNonOwnedCollectionItems(object, property, itemCodes, report);
    }
    dbmsManager.clearSession();
    cacheManager.clearCache();
    return report;
}
Also used : TypeReport(org.hisp.dhis.feedback.TypeReport) Property(org.hisp.dhis.schema.Property) Transactional(org.springframework.transaction.annotation.Transactional)

Example 39 with TypeReport

use of org.hisp.dhis.feedback.TypeReport 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 40 with TypeReport

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

Aggregations

TypeReport (org.hisp.dhis.feedback.TypeReport)52 ObjectReport (org.hisp.dhis.feedback.ObjectReport)28 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)24 ErrorReport (org.hisp.dhis.feedback.ErrorReport)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)13 List (java.util.List)7 Test (org.junit.jupiter.api.Test)7 Transactional (org.springframework.transaction.annotation.Transactional)6 ArrayList (java.util.ArrayList)5 ObjectBundleCommitReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport)5 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)5 User (org.hisp.dhis.user.User)5 HashMap (java.util.HashMap)4 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)4 DataElement (org.hisp.dhis.dataelement.DataElement)4 SchemaService (org.hisp.dhis.schema.SchemaService)4 Service (org.springframework.stereotype.Service)4 AllArgsConstructor (lombok.AllArgsConstructor)3 Session (org.hibernate.Session)3 MergeParams (org.hisp.dhis.schema.MergeParams)3