Search in sources :

Example 6 with IdentifiableObject

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

the class DefaultMetadataExportService method getMetadata.

@Override
@SuppressWarnings("unchecked")
public Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> getMetadata(MetadataExportParams params) {
    Timer timer = new SystemTimer().start();
    Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = new HashMap<>();
    if (params.getUser() == null) {
        params.setUser(currentUserService.getCurrentUser());
    }
    if (params.getClasses().isEmpty()) {
        schemaService.getMetadataSchemas().stream().filter(Schema::isIdentifiableObject).forEach(schema -> params.getClasses().add((Class<? extends IdentifiableObject>) schema.getKlass()));
    }
    log.info("(" + params.getUsername() + ") Export:Start");
    for (Class<? extends IdentifiableObject> klass : params.getClasses()) {
        Query query;
        if (params.getQuery(klass) != null) {
            query = params.getQuery(klass);
        } else {
            OrderParams orderParams = new OrderParams(Sets.newHashSet(params.getDefaultOrder()));
            query = queryService.getQueryFromUrl(klass, params.getDefaultFilter(), orderParams.getOrders(schemaService.getDynamicSchema(klass)));
        }
        if (query.getUser() == null) {
            query.setUser(params.getUser());
        }
        query.setDefaultOrder();
        List<? extends IdentifiableObject> objects = queryService.query(query);
        if (!objects.isEmpty()) {
            log.info("(" + params.getUsername() + ") Exported " + objects.size() + " objects of type " + klass.getSimpleName());
            metadata.put(klass, objects);
        }
    }
    log.info("(" + params.getUsername() + ") Export:Done took " + timer.toString());
    return metadata;
}
Also used : SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) Timer(org.hisp.dhis.commons.timer.Timer) Query(org.hisp.dhis.query.Query) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 7 with IdentifiableObject

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

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

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

the class DefaultAttributeService method validateAttributeValues.

@Override
public <T extends IdentifiableObject> List<ErrorReport> validateAttributeValues(T object, Set<AttributeValue> attributeValues) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (attributeValues.isEmpty()) {
        return errorReports;
    }
    Map<String, AttributeValue> attributeValueMap = attributeValues.stream().filter(av -> av.getAttribute() != null).collect(Collectors.toMap(av -> av.getAttribute().getUid(), av -> av));
    Iterator<AttributeValue> iterator = object.getAttributeValues().iterator();
    List<Attribute> mandatoryAttributes = getMandatoryAttributes(object.getClass());
    while (iterator.hasNext()) {
        AttributeValue attributeValue = iterator.next();
        if (attributeValue.getAttribute() != null && attributeValueMap.containsKey(attributeValue.getAttribute().getUid())) {
            AttributeValue av = attributeValueMap.get(attributeValue.getAttribute().getUid());
            if (attributeValue.isUnique()) {
                if (!manager.isAttributeValueUnique(object.getClass(), object, attributeValue.getAttribute(), av.getValue())) {
                    errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4009, attributeValue.getAttribute().getUid(), av.getValue()).setMainId(attributeValue.getAttribute().getUid()).setErrorProperty("value"));
                }
            }
            attributeValueMap.remove(attributeValue.getAttribute().getUid());
            mandatoryAttributes.remove(attributeValue.getAttribute());
        }
    }
    for (String uid : attributeValueMap.keySet()) {
        AttributeValue attributeValue = attributeValueMap.get(uid);
        if (attributeValue.getAttribute() != null && !attributeValue.getAttribute().getSupportedClasses().contains(object.getClass())) {
            errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4010, attributeValue.getAttribute().getUid(), object.getClass().getSimpleName()).setMainId(uid).setErrorProperty("id"));
        } else {
            mandatoryAttributes.remove(attributeValue.getAttribute());
        }
    }
    mandatoryAttributes.forEach(att -> errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4011, att.getUid()).setMainId(att.getUid())));
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) NonUniqueAttributeValueException(org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException) Iterator(java.util.Iterator) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ValueType(org.hisp.dhis.common.ValueType) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) MissingMandatoryAttributeValueException(org.hisp.dhis.attribute.exception.MissingMandatoryAttributeValueException) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Maps(com.google.api.client.util.Maps) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Transactional(org.springframework.transaction.annotation.Transactional) ArrayList(java.util.ArrayList)

Example 10 with IdentifiableObject

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

the class DefaultRenderService method fromMetadata.

@Override
@SuppressWarnings("unchecked")
public Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> fromMetadata(InputStream inputStream, RenderFormat format) throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> map = new HashMap<>();
    ObjectMapper mapper;
    if (RenderFormat.JSON == format) {
        mapper = jsonMapper;
    } else if (RenderFormat.XML == format) {
        throw new IllegalArgumentException("XML format is not supported.");
    } else {
        return map;
    }
    JsonNode rootNode = mapper.readTree(inputStream);
    Iterator<String> fieldNames = rootNode.fieldNames();
    while (fieldNames.hasNext()) {
        String fieldName = fieldNames.next();
        JsonNode node = rootNode.get(fieldName);
        Schema schema = schemaService.getSchemaByPluralName(fieldName);
        if (schema == null || !schema.isIdentifiableObject()) {
            log.info("Skipping unknown property '" + fieldName + "'.");
            continue;
        }
        if (!schema.isMetadata()) {
            log.debug("Skipping non-metadata property `" + fieldName + "`.");
            continue;
        }
        List<IdentifiableObject> collection = new ArrayList<>();
        for (JsonNode item : node) {
            IdentifiableObject value = mapper.treeToValue(item, (Class<? extends IdentifiableObject>) schema.getKlass());
            if (value != null)
                collection.add(value);
        }
        map.put((Class<? extends IdentifiableObject>) schema.getKlass(), collection);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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