Search in sources :

Example 1 with ObjectReport

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

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

the class AbstractCrudController method replaceTranslations.

@RequestMapping(value = "/{uid}/translations", method = RequestMethod.PUT)
public void replaceTranslations(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
    }
    T persistedObject = entities.get(0);
    User user = currentUserService.getCurrentUser();
    if (!aclService.canUpdate(user, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    T object = renderService.fromJson(request.getInputStream(), getEntityClass());
    TypeReport typeReport = new TypeReport(ObjectTranslation.class);
    List<ObjectTranslation> objectTranslations = Lists.newArrayList(object.getTranslations());
    for (int idx = 0; idx < object.getTranslations().size(); idx++) {
        ObjectReport objectReport = new ObjectReport(ObjectTranslation.class, idx);
        ObjectTranslation translation = objectTranslations.get(idx);
        if (translation.getLocale() == null) {
            objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "locale").setErrorKlass(getEntityClass()));
        }
        if (translation.getProperty() == null) {
            objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "property").setErrorKlass(getEntityClass()));
        }
        if (translation.getValue() == null) {
            objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "value").setErrorKlass(getEntityClass()));
        }
        typeReport.addObjectReport(objectReport);
        if (!objectReport.isEmpty()) {
            typeReport.getStats().incIgnored();
        }
    }
    if (!typeReport.getErrorReports().isEmpty()) {
        WebMessage webMessage = WebMessageUtils.typeReport(typeReport);
        webMessageService.send(webMessage, response, request);
        return;
    }
    manager.updateTranslations(persistedObject, object.getTranslations());
    manager.update(persistedObject);
    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) ObjectReport(org.hisp.dhis.feedback.ObjectReport) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) ErrorReport(org.hisp.dhis.feedback.ErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ObjectReport

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

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

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

the class DefaultObjectBundleValidationService method checkUniqueAttributes.

private TypeReport checkUniqueAttributes(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 = checkUniqueAttributes(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)

Aggregations

ObjectReport (org.hisp.dhis.feedback.ObjectReport)46 TypeReport (org.hisp.dhis.feedback.TypeReport)29 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)22 ErrorReport (org.hisp.dhis.feedback.ErrorReport)22 ArrayList (java.util.ArrayList)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)13 User (org.hisp.dhis.user.User)9 Schema (org.hisp.dhis.schema.Schema)7 List (java.util.List)6 AttributeValue (org.hisp.dhis.attribute.AttributeValue)6 Test (org.junit.jupiter.api.Test)6 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)5 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)5 SchemaService (org.hisp.dhis.schema.SchemaService)5 Service (org.springframework.stereotype.Service)5 HashMap (java.util.HashMap)4 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)4 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)4 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3