Search in sources :

Example 6 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 7 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 8 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 9 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)

Example 10 with ObjectReport

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

the class DefaultObjectBundleValidationService method runValidationHooks.

private TypeReport runValidationHooks(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 object = iterator.next();
        List<ErrorReport> errorReports = new ArrayList<>();
        objectBundleHooks.forEach(hook -> errorReports.addAll(hook.validate(object, bundle)));
        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;
        }
        idx++;
    }
    return typeReport;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

ObjectReport (org.hisp.dhis.feedback.ObjectReport)27 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)20 ErrorReport (org.hisp.dhis.feedback.ErrorReport)16 TypeReport (org.hisp.dhis.feedback.TypeReport)16 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)13 List (java.util.List)7 User (org.hisp.dhis.user.User)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)6 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)6 Test (org.junit.Test)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6 ArrayList (java.util.ArrayList)5 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)3 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 HashMap (java.util.HashMap)2 DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)2 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)2 ErrorCode (org.hisp.dhis.feedback.ErrorCode)2 CreateAccessDeniedException (org.hisp.dhis.hibernate.exception.CreateAccessDeniedException)2