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;
}
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());
}
}
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());
}
}
}
}
}
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);
}
}
}
}
}
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);
}
Aggregations