Search in sources :

Example 51 with IdentifiableObject

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

the class OrganisationUnitController method getEntityList.

@Override
@SuppressWarnings("unchecked")
protected List<OrganisationUnit> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
    List<OrganisationUnit> objects = Lists.newArrayList();
    User currentUser = currentUserService.getCurrentUser();
    boolean anySpecialPropertySet = ObjectUtils.anyIsTrue(options.isTrue("userOnly"), options.isTrue("userDataViewOnly"), options.isTrue("userDataViewFallback"), options.isTrue("levelSorted"));
    boolean anyQueryPropertySet = ObjectUtils.firstNonNull(options.get("query"), options.getInt("level"), options.getInt("maxLevel")) != null || options.isTrue("withinUserHierarchy");
    String memberObject = options.get("memberObject");
    String memberCollection = options.get("memberCollection");
    if (options.isTrue("userOnly")) {
        objects = new ArrayList<>(currentUser.getOrganisationUnits());
    } else if (options.isTrue("userDataViewOnly")) {
        objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
    } else if (options.isTrue("userDataViewFallback")) {
        if (currentUser.hasDataViewOrganisationUnit()) {
            objects = new ArrayList<>(currentUser.getDataViewOrganisationUnits());
        } else {
            objects = organisationUnitService.getOrganisationUnitsAtLevel(1);
        }
    } else if (options.isTrue("levelSorted")) {
        objects = new ArrayList<>(manager.getAll(getEntityClass()));
        Collections.sort(objects, OrganisationUnitByLevelComparator.INSTANCE);
    } else if (anyQueryPropertySet) {
        OrganisationUnitQueryParams params = new OrganisationUnitQueryParams();
        params.setQuery(options.get("query"));
        params.setLevel(options.getInt("level"));
        params.setMaxLevels(options.getInt("maxLevel"));
        params.setParents(options.isTrue("withinUserHierarchy") ? currentUser.getOrganisationUnits() : Sets.newHashSet());
        objects = organisationUnitService.getOrganisationUnitsByQuery(params);
    }
    // ---------------------------------------------------------------------
    // Standard Query handling
    // ---------------------------------------------------------------------
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
    query.setUser(currentUser);
    query.setDefaultOrder();
    if (anySpecialPropertySet || anyQueryPropertySet) {
        query.setObjects(objects);
    }
    List<OrganisationUnit> list = (List<OrganisationUnit>) queryService.query(query);
    // ---------------------------------------------------------------------
    // Collection member count in hierarchy handling
    // ---------------------------------------------------------------------
    IdentifiableObject member = null;
    if (memberObject != null && memberCollection != null && (member = manager.get(memberObject)) != null) {
        for (OrganisationUnit unit : list) {
            Long count = organisationUnitService.getOrganisationUnitHierarchyMemberCount(unit, member, memberCollection);
            unit.setMemberCount((count != null ? count.intValue() : 0));
        }
    }
    return list;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) ArrayList(java.util.ArrayList) OrganisationUnitQueryParams(org.hisp.dhis.organisationunit.OrganisationUnitQueryParams) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 52 with IdentifiableObject

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

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

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

Example 55 with IdentifiableObject

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

the class DefaultObjectBundleValidationService method checkReferences.

private List<PreheatErrorReport> checkReferences(IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier, boolean skipSharing) {
    List<PreheatErrorReport> preheatErrorReports = new ArrayList<>();
    if (object == null) {
        return preheatErrorReports;
    }
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    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()) {
            IdentifiableObject refObject = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
            IdentifiableObject ref = preheat.get(identifier, refObject);
            if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
                if (!("user".equals(p.getName()) && User.class.isAssignableFrom(p.getKlass()) && skipSharing)) {
                    preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getName()));
                }
            }
        } else {
            Collection<IdentifiableObject> objects = ReflectionUtils.newCollectionInstance(p.getKlass());
            Collection<IdentifiableObject> refObjects = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
            for (IdentifiableObject refObject : refObjects) {
                if (Preheat.isDefault(refObject))
                    continue;
                IdentifiableObject ref = preheat.get(identifier, refObject);
                if (ref == null && refObject != null && !Preheat.isDefaultClass(refObject.getClass())) {
                    preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(refObject), identifier.getIdentifiersWithName(object), p.getCollectionName()));
                } else {
                    objects.add(refObject);
                }
            }
            ReflectionUtils.invokeMethod(object, p.getSetterMethod(), objects);
        }
    });
    if (schema.havePersistedProperty("attributeValues")) {
        object.getAttributeValues().stream().filter(attributeValue -> attributeValue.getAttribute() != null && preheat.get(identifier, attributeValue.getAttribute()) == null).forEach(attributeValue -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(attributeValue.getAttribute()), identifier.getIdentifiersWithName(object), "attributeValues")));
    }
    if (schema.havePersistedProperty("userGroupAccesses")) {
        object.getUserGroupAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUserGroup() != null && preheat.get(identifier, userGroupAccess.getUserGroup()) == null).forEach(userGroupAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userGroupAccesses.getUserGroup()), identifier.getIdentifiersWithName(object), "userGroupAccesses")));
    }
    if (schema.havePersistedProperty("userAccesses")) {
        object.getUserAccesses().stream().filter(userGroupAccess -> !skipSharing && userGroupAccess.getUser() != null && preheat.get(identifier, userGroupAccess.getUser()) == null).forEach(userAccesses -> preheatErrorReports.add(new PreheatErrorReport(identifier, object.getClass(), ErrorCode.E5002, identifier.getIdentifiersWithName(userAccesses.getUser()), identifier.getIdentifiersWithName(object), "userAccesses")));
    }
    return preheatErrorReports;
}
Also used : AtomicMode(org.hisp.dhis.dxf2.metadata.AtomicMode) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PropertyType(org.hisp.dhis.schema.PropertyType) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) PreheatIdentifier(org.hisp.dhis.preheat.PreheatIdentifier) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) TypeReport(org.hisp.dhis.feedback.TypeReport) UserCredentials(org.hisp.dhis.user.UserCredentials) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Service(org.springframework.stereotype.Service) Map(java.util.Map) 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) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) UserService(org.hisp.dhis.user.UserService) Iterator(java.util.Iterator) Collection(java.util.Collection) Set(java.util.Set) Timer(org.hisp.dhis.commons.timer.Timer) SchemaService(org.hisp.dhis.schema.SchemaService) Property(org.hisp.dhis.schema.Property) Collectors(java.util.stream.Collectors) SchemaValidator(org.hisp.dhis.schema.validation.SchemaValidator) List(java.util.List) AclService(org.hisp.dhis.security.acl.AclService) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) 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