Search in sources :

Example 1 with ErrorReport

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

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

the class MetadataRetryContext method setupImportReport.

//----------------------------------------------------------------------------------------
// Private Methods
//----------------------------------------------------------------------------------------
private void setupImportReport(ImportReport importReport) {
    Status status = importReport.getStatus();
    if (Status.ERROR.equals(status)) {
        StringBuilder report = new StringBuilder();
        List<ErrorReport> errorReports = importReport.getErrorReports();
        for (ErrorReport errorReport : errorReports) {
            if (errorReport != null) {
                report.append(errorReport.toString() + "\n");
            }
        }
        retryContext.setAttribute(MetadataSyncTask.METADATA_SYNC_REPORT, report.toString());
    }
}
Also used : Status(org.hisp.dhis.feedback.Status) ErrorReport(org.hisp.dhis.feedback.ErrorReport)

Example 3 with ErrorReport

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

the class ObjectBundleServiceTest method testPreheatValidations.

@Test
public void testPreheatValidations() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate1.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.isEmpty());
    List<ObjectReport> objectReports = validate.getObjectReports(DataElement.class);
    assertFalse(objectReports.isEmpty());
    for (ObjectReport objectReport : objectReports) {
        for (ErrorCode errorCode : objectReport.getErrorCodes()) {
            List<ErrorReport> errorReports = objectReport.getErrorReportsByCode().get(errorCode);
            assertFalse(errorReports.isEmpty());
            for (ErrorReport errorReport : errorReports) {
                assertTrue(PreheatErrorReport.class.isInstance(errorReport));
                PreheatErrorReport preheatErrorReport = (PreheatErrorReport) errorReport;
                assertEquals(PreheatIdentifier.UID, preheatErrorReport.getPreheatIdentifier());
                if (DataElementCategoryCombo.class.isInstance(preheatErrorReport.getValue())) {
                    assertEquals("p0KPaWEg3cf", preheatErrorReport.getObjectReference().getUid());
                } else if (User.class.isInstance(preheatErrorReport.getValue())) {
                    assertEquals("GOLswS44mh8", preheatErrorReport.getObjectReference().getUid());
                } else if (OptionSet.class.isInstance(preheatErrorReport.getValue())) {
                    assertEquals("pQYCiuosBnZ", preheatErrorReport.getObjectReference().getUid());
                }
            }
        }
    }
}
Also used : User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) List(java.util.List) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with ErrorReport

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

the class ObjectBundleServiceTest method testPreheatValidationsWithCatCombo.

@Test
public void testPreheatValidationsWithCatCombo() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/de_validate1.json").getInputStream(), RenderFormat.JSON);
    DataElementCategoryCombo categoryCombo = manager.getByName(DataElementCategoryCombo.class, "default");
    categoryCombo.setUid("p0KPaWEg3cf");
    manager.update(categoryCombo);
    OptionSet optionSet = new OptionSet("OptionSet: pQYCiuosBnZ", ValueType.TEXT);
    optionSet.setAutoFields();
    optionSet.setUid("pQYCiuosBnZ");
    manager.save(optionSet);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertFalse(validate.getTypeReportMap().isEmpty());
    List<ObjectReport> objectReports = validate.getObjectReports(DataElement.class);
    assertFalse(objectReports.isEmpty());
    for (ObjectReport objectReport : objectReports) {
        for (ErrorCode errorCode : objectReport.getErrorCodes()) {
            List<ErrorReport> errorReports = objectReport.getErrorReportsByCode().get(errorCode);
            assertFalse(errorReports.isEmpty());
            for (ErrorReport errorReport : errorReports) {
                assertTrue(PreheatErrorReport.class.isInstance(errorReport));
                PreheatErrorReport preheatErrorReport = (PreheatErrorReport) errorReport;
                assertEquals(PreheatIdentifier.UID, preheatErrorReport.getPreheatIdentifier());
                if (DataElementCategoryCombo.class.isInstance(preheatErrorReport.getValue())) {
                    assertFalse(true);
                } else if (User.class.isInstance(preheatErrorReport.getValue())) {
                    assertEquals("GOLswS44mh8", preheatErrorReport.getObjectReference().getUid());
                } else if (OptionSet.class.isInstance(preheatErrorReport.getValue())) {
                    assertFalse(true);
                }
            }
        }
    }
}
Also used : DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) User(org.hisp.dhis.user.User) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) List(java.util.List) ErrorCode(org.hisp.dhis.feedback.ErrorCode) OptionSet(org.hisp.dhis.option.OptionSet) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with ErrorReport

use of org.hisp.dhis.feedback.ErrorReport 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)

Aggregations

ErrorReport (org.hisp.dhis.feedback.ErrorReport)30 ObjectReport (org.hisp.dhis.feedback.ObjectReport)17 ArrayList (java.util.ArrayList)15 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)14 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)14 TypeReport (org.hisp.dhis.feedback.TypeReport)13 User (org.hisp.dhis.user.User)8 Schema (org.hisp.dhis.schema.Schema)5 List (java.util.List)4 ErrorCode (org.hisp.dhis.feedback.ErrorCode)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)3 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)3 Test (org.junit.Test)3 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2