Search in sources :

Example 71 with IdentifiableObject

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

the class DefaultObjectBundleValidationService method checkDuplicateIds.

private TypeReport checkDuplicateIds(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> persistedObjects, List<IdentifiableObject> nonPersistedObjects, Preheat preheat, PreheatIdentifier identifier) {
    TypeReport typeReport = new TypeReport(klass);
    if (persistedObjects.isEmpty() && nonPersistedObjects.isEmpty()) {
        return typeReport;
    }
    Map<Class<?>, String> idMap = new HashMap<>();
    Iterator<IdentifiableObject> iterator = persistedObjects.iterator();
    int idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        if (idMap.containsKey(object.getClass()) && idMap.get(object.getClass()).equals(object.getUid())) {
            ErrorReport errorReport = new ErrorReport(object.getClass(), ErrorCode.E5004, object.getUid(), object.getClass()).setMainId(object.getUid()).setErrorProperty("id");
            ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReport(errorReport);
            typeReport.addObjectReport(objectReport);
            typeReport.getStats().incIgnored();
            iterator.remove();
        } else {
            idMap.put(object.getClass(), object.getUid());
        }
        idx++;
    }
    iterator = nonPersistedObjects.iterator();
    idx = 0;
    while (iterator.hasNext()) {
        IdentifiableObject object = iterator.next();
        if (idMap.containsKey(object.getClass()) && idMap.get(object.getClass()).equals(object.getUid())) {
            ErrorReport errorReport = new ErrorReport(object.getClass(), ErrorCode.E5004, object.getUid(), object.getClass()).setMainId(object.getUid()).setErrorProperty("id");
            ObjectReport objectReport = new ObjectReport(object.getClass(), idx);
            objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
            objectReport.addErrorReport(errorReport);
            typeReport.addObjectReport(objectReport);
            typeReport.getStats().incIgnored();
            iterator.remove();
        } else {
            idMap.put(object.getClass(), object.getUid());
        }
        idx++;
    }
    return typeReport;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) HashMap(java.util.HashMap) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 72 with IdentifiableObject

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

the class DefaultObjectBundleValidationService method checkUniqueness.

private List<ErrorReport> checkUniqueness(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null || Preheat.isDefault(object))
        return errorReports;
    if (!preheat.getUniquenessMap().containsKey(object.getClass())) {
        preheat.getUniquenessMap().put(object.getClass(), new HashMap<>());
    }
    Map<String, Map<Object, String>> uniquenessMap = preheat.getUniquenessMap().get(object.getClass());
    Schema schema = schemaService.getDynamicSchema(object.getClass());
    List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
    uniqueProperties.forEach(property -> {
        if (!uniquenessMap.containsKey(property.getName())) {
            uniquenessMap.put(property.getName(), new HashMap<>());
        }
        Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        if (value != null) {
            String persistedUid = uniquenessMap.get(property.getName()).get(value);
            if (persistedUid != null) {
                if (!object.getUid().equals(persistedUid)) {
                    errorReports.add(new ErrorReport(object.getClass(), ErrorCode.E5003, property.getName(), value, identifier.getIdentifiersWithName(object), persistedUid).setMainId(persistedUid).setErrorProperty(property.getName()));
                }
            } else {
                uniquenessMap.get(property.getName()).put(value, object.getUid());
            }
        }
    });
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) 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) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.hisp.dhis.schema.Property)

Example 73 with IdentifiableObject

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

the class DefaultObjectBundleValidationService method checkMandatoryAttributes.

private TypeReport checkMandatoryAttributes(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 = checkMandatoryAttributes(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 74 with IdentifiableObject

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

the class DefaultMetadataExportService method getMetadataWithDependenciesAsNode.

@Override
public RootNode getMetadataWithDependenciesAsNode(IdentifiableObject object) {
    RootNode rootNode = NodeUtils.createMetadata();
    rootNode.addChild(new SimpleNode("date", new Date(), true));
    SetMap<Class<? extends IdentifiableObject>, IdentifiableObject> metadata = getMetadataWithDependencies(object);
    for (Class<? extends IdentifiableObject> klass : metadata.keySet()) {
        rootNode.addChild(fieldFilterService.filter(klass, Lists.newArrayList(metadata.get(klass)), Lists.newArrayList(":owner")));
    }
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Date(java.util.Date) SimpleNode(org.hisp.dhis.node.types.SimpleNode) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 75 with IdentifiableObject

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

the class DefaultMetadataExportService method getMetadataAsNode.

@Override
public RootNode getMetadataAsNode(MetadataExportParams params) {
    RootNode rootNode = NodeUtils.createMetadata();
    SystemInfo systemInfo = systemService.getSystemInfo();
    ComplexNode system = rootNode.addChild(new ComplexNode("system"));
    system.addChild(new SimpleNode("id", systemInfo.getSystemId()));
    system.addChild(new SimpleNode("rev", systemInfo.getRevision()));
    system.addChild(new SimpleNode("version", systemInfo.getVersion()));
    system.addChild(new SimpleNode("date", systemInfo.getServerDate()));
    Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = getMetadata(params);
    for (Class<? extends IdentifiableObject> klass : metadata.keySet()) {
        rootNode.addChild(fieldFilterService.filter(klass, metadata.get(klass), params.getFields(klass)));
    }
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) SystemInfo(org.hisp.dhis.system.SystemInfo) ComplexNode(org.hisp.dhis.node.types.ComplexNode) List(java.util.List) ArrayList(java.util.ArrayList) SimpleNode(org.hisp.dhis.node.types.SimpleNode) 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