Search in sources :

Example 6 with TypeReport

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

the class DefaultObjectBundleService method commit.

@Override
public ObjectBundleCommitReport commit(ObjectBundle bundle) {
    Map<Class<?>, TypeReport> typeReports = new HashMap<>();
    ObjectBundleCommitReport commitReport = new ObjectBundleCommitReport(typeReports);
    if (ObjectBundleMode.VALIDATE == bundle.getObjectBundleMode()) {
        // skip if validate only
        return commitReport;
    }
    List<Class<? extends IdentifiableObject>> klasses = getSortedClasses(bundle);
    Session session = sessionFactory.getCurrentSession();
    objectBundleHooks.forEach(hook -> hook.preCommit(bundle));
    for (Class<? extends IdentifiableObject> klass : klasses) {
        List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(klass, false);
        List<IdentifiableObject> persistedObjects = bundle.getObjects(klass, true);
        objectBundleHooks.forEach(hook -> hook.preTypeImport(klass, nonPersistedObjects, bundle));
        if (bundle.getImportMode().isCreateAndUpdate()) {
            TypeReport typeReport = new TypeReport(klass);
            typeReport.merge(handleCreates(session, klass, nonPersistedObjects, bundle));
            typeReport.merge(handleUpdates(session, klass, persistedObjects, bundle));
            typeReports.put(klass, typeReport);
        } else if (bundle.getImportMode().isCreate()) {
            typeReports.put(klass, handleCreates(session, klass, nonPersistedObjects, bundle));
        } else if (bundle.getImportMode().isUpdate()) {
            typeReports.put(klass, handleUpdates(session, klass, persistedObjects, bundle));
        } else if (bundle.getImportMode().isDelete()) {
            typeReports.put(klass, handleDeletes(session, klass, persistedObjects, bundle));
        }
        objectBundleHooks.forEach(hook -> hook.postTypeImport(klass, persistedObjects, bundle));
        if (FlushMode.AUTO == bundle.getFlushMode())
            session.flush();
    }
    if (!bundle.getImportMode().isDelete()) {
        objectBundleHooks.forEach(hook -> hook.postCommit(bundle));
    }
    dbmsManager.clearSession();
    cacheManager.clearCache();
    bundle.setObjectBundleStatus(ObjectBundleStatus.COMMITTED);
    return commitReport;
}
Also used : ObjectBundleCommitReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport) HashMap(java.util.HashMap) TypeReport(org.hisp.dhis.feedback.TypeReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Session(org.hibernate.Session)

Example 7 with TypeReport

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

the class DefaultObjectBundleService method handleDeletes.

private TypeReport handleDeletes(Session session, Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects.isEmpty()) {
        return typeReport;
    }
    String message = "(" + bundle.getUsername() + ") Deleting " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
    log.info(message);
    if (bundle.hasTaskId()) {
        notifier.notify(bundle.getTaskId(), message);
    }
    List<IdentifiableObject> persistedObjects = bundle.getPreheat().getAll(bundle.getPreheatIdentifier(), objects);
    for (int idx = 0; idx < persistedObjects.size(); idx++) {
        IdentifiableObject object = persistedObjects.get(idx);
        ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
        objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
        typeReport.addObjectReport(objectReport);
        objectBundleHooks.forEach(hook -> hook.preDelete(object, bundle));
        manager.delete(object, bundle.getUser());
        bundle.getPreheat().remove(bundle.getPreheatIdentifier(), object);
        if (log.isDebugEnabled()) {
            String msg = "(" + bundle.getUsername() + ") Deleted object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(object) + "'";
            log.debug(msg);
        }
        if (FlushMode.OBJECT == bundle.getFlushMode())
            session.flush();
    }
    return typeReport;
}
Also used : TypeReport(org.hisp.dhis.feedback.TypeReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 8 with TypeReport

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

the class DefaultObjectBundleValidationService method validateForDelete.

private TypeReport validateForDelete(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) {
            if (Preheat.isDefaultClass(identifiableObject.getClass()))
                continue;
            ObjectReport objectReport = new ObjectReport(klass, idx, object != null ? object.getUid() : null);
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E5001, bundle.getPreheatIdentifier(), bundle.getPreheatIdentifier().getIdentifiersWithName(identifiableObject)).setMainId(object != null ? object.getUid() : null));
            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 9 with TypeReport

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

the class DefaultObjectBundleValidationService method validate.

@Override
public ObjectBundleValidationReport validate(ObjectBundle bundle) {
    Timer timer = new SystemTimer().start();
    ObjectBundleValidationReport validation = new ObjectBundleValidationReport();
    if ((bundle.getUser() == null || bundle.getUser().isSuper()) && bundle.isSkipValidation()) {
        log.warn("Skipping validation for metadata import by user '" + bundle.getUsername() + "'. Not recommended.");
        return validation;
    }
    List<Class<? extends IdentifiableObject>> klasses = getSortedClasses(bundle);
    for (Class<? extends IdentifiableObject> klass : klasses) {
        TypeReport typeReport = new TypeReport(klass);
        List<IdentifiableObject> nonPersistedObjects = bundle.getObjects(klass, false);
        List<IdentifiableObject> persistedObjects = bundle.getObjects(klass, true);
        List<IdentifiableObject> allObjects = bundle.getObjectMap().get(klass);
        handleDefaults(nonPersistedObjects);
        handleDefaults(persistedObjects);
        typeReport.merge(checkDuplicateIds(klass, persistedObjects, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
        if (bundle.getImportMode().isCreateAndUpdate()) {
            typeReport.merge(runValidationHooks(klass, nonPersistedObjects, bundle));
            typeReport.merge(runValidationHooks(klass, persistedObjects, bundle));
            typeReport.merge(validateSecurity(klass, nonPersistedObjects, bundle, ImportStrategy.CREATE));
            typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.UPDATE));
            typeReport.merge(validateBySchemas(klass, nonPersistedObjects, bundle));
            typeReport.merge(validateBySchemas(klass, persistedObjects, bundle));
            typeReport.merge(checkUniqueness(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueness(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
            if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
                typeReport.getStats().incIgnored();
            }
            typeReport.getStats().incCreated(nonPersistedObjects.size());
            typeReport.getStats().incUpdated(persistedObjects.size());
            typeReport.merge(checkReferences);
        } else if (bundle.getImportMode().isCreate()) {
            typeReport.merge(runValidationHooks(klass, nonPersistedObjects, bundle));
            typeReport.merge(validateSecurity(klass, nonPersistedObjects, bundle, ImportStrategy.CREATE));
            typeReport.merge(validateForCreate(klass, persistedObjects, bundle));
            typeReport.merge(validateBySchemas(klass, nonPersistedObjects, bundle));
            typeReport.merge(checkUniqueness(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, nonPersistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
            if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
                typeReport.getStats().incIgnored();
            }
            typeReport.getStats().incCreated(nonPersistedObjects.size());
            typeReport.merge(checkReferences);
        } else if (bundle.getImportMode().isUpdate()) {
            typeReport.merge(runValidationHooks(klass, persistedObjects, bundle));
            typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.UPDATE));
            typeReport.merge(validateForUpdate(klass, nonPersistedObjects, bundle));
            typeReport.merge(validateBySchemas(klass, persistedObjects, bundle));
            typeReport.merge(checkUniqueness(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkMandatoryAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            typeReport.merge(checkUniqueAttributes(klass, persistedObjects, bundle.getPreheat(), bundle.getPreheatIdentifier()));
            TypeReport checkReferences = checkReferences(klass, allObjects, bundle.getPreheat(), bundle.getPreheatIdentifier(), bundle.isSkipSharing());
            if (!checkReferences.getErrorReports().isEmpty() && AtomicMode.ALL == bundle.getAtomicMode()) {
                typeReport.getStats().incIgnored();
            }
            typeReport.getStats().incUpdated(persistedObjects.size());
            typeReport.merge(checkReferences);
        } else if (bundle.getImportMode().isDelete()) {
            typeReport.merge(validateSecurity(klass, persistedObjects, bundle, ImportStrategy.DELETE));
            typeReport.merge(validateForDelete(klass, nonPersistedObjects, bundle));
            typeReport.getStats().incDeleted(persistedObjects.size());
        }
        validation.addTypeReport(typeReport);
    }
    validateAtomicity(bundle, validation);
    bundle.setObjectBundleStatus(ObjectBundleStatus.VALIDATED);
    log.info("(" + bundle.getUsername() + ") Import:Validation took " + timer.toString());
    return validation;
}
Also used : SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Timer(org.hisp.dhis.commons.timer.Timer) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) TypeReport(org.hisp.dhis.feedback.TypeReport) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 10 with TypeReport

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

the class DefaultObjectBundleValidationService method validateSecurity.

private TypeReport validateSecurity(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle, ImportStrategy importMode) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects == null || objects.isEmpty()) {
        return typeReport;
    }
    Iterator<IdentifiableObject> iterator = objects.iterator();
    PreheatIdentifier identifier = bundle.getPreheatIdentifier();
    int idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        if (importMode.isCreate()) {
            if (!aclService.canCreate(bundle.getUser(), klass)) {
                ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3000, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
                typeReport.addObjectReport(objectReport);
                typeReport.getStats().incIgnored();
                iterator.remove();
                continue;
            }
        } else {
            IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
            if (importMode.isUpdate()) {
                if (!aclService.canUpdate(bundle.getUser(), persistedObject)) {
                    ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                    objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                    objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3001, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
                    typeReport.addObjectReport(objectReport);
                    typeReport.getStats().incIgnored();
                    iterator.remove();
                    continue;
                }
            } else if (importMode.isDelete()) {
                if (!aclService.canDelete(bundle.getUser(), persistedObject)) {
                    ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
                    objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
                    objectReport.addErrorReport(new ErrorReport(klass, ErrorCode.E3002, identifier.getIdentifiersWithName(bundle.getUser()), identifier.getIdentifiersWithName(object)));
                    typeReport.addObjectReport(objectReport);
                    typeReport.getStats().incIgnored();
                    iterator.remove();
                    continue;
                }
            }
        }
        if (User.class.isInstance(object)) {
            User user = (User) object;
            List<ErrorReport> errorReports = userService.validateUser(user, bundle.getUser());
            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;
            }
        }
        List<ErrorReport> sharingErrorReports = aclService.verifySharing(object, bundle.getUser());
        if (!sharingErrorReports.isEmpty()) {
            ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReports(sharingErrorReports);
            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) User(org.hisp.dhis.user.User) TypeReport(org.hisp.dhis.feedback.TypeReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

TypeReport (org.hisp.dhis.feedback.TypeReport)25 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)16 ObjectReport (org.hisp.dhis.feedback.ObjectReport)16 ErrorReport (org.hisp.dhis.feedback.ErrorReport)12 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)11 ObjectBundleCommitReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport)5 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 User (org.hisp.dhis.user.User)3 Test (org.junit.Test)3 Session (org.hibernate.Session)2 SystemTimer (org.hisp.dhis.commons.timer.SystemTimer)2 Timer (org.hisp.dhis.commons.timer.Timer)2 DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)2 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)2 Schema (org.hisp.dhis.schema.Schema)2 List (java.util.List)1 Map (java.util.Map)1 Log (org.apache.commons.logging.Log)1