use of org.hisp.dhis.feedback.ObjectReport in project dhis2-core by dhis2.
the class DefaultObjectBundleValidationService method checkUniqueness.
private TypeReport checkUniqueness(Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, Preheat preheat, PreheatIdentifier identifier) {
TypeReport typeReport = new TypeReport(klass);
if (objects.isEmpty()) {
return typeReport;
}
Iterator<IdentifiableObject> iterator = objects.iterator();
int idx = 0;
while (iterator.hasNext()) {
IdentifiableObject object = iterator.next();
List<ErrorReport> errorReports = new ArrayList<>();
if (User.class.isInstance(object)) {
User user = (User) object;
errorReports.addAll(checkUniqueness(User.class, user, preheat, identifier));
errorReports.addAll(checkUniqueness(UserCredentials.class, user.getUserCredentials(), preheat, identifier));
} else {
errorReports = checkUniqueness(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;
}
use of org.hisp.dhis.feedback.ObjectReport in project dhis2-core by dhis2.
the class AbstractCrudController method postXmlObject.
@RequestMapping(method = RequestMethod.POST, consumes = { "application/xml", "text/xml" })
public void postXmlObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = currentUserService.getCurrentUser();
if (!aclService.canCreate(user, getEntityClass())) {
throw new CreateAccessDeniedException("You don't have the proper permissions to create this object.");
}
T parsed = deserializeXmlEntity(request, response);
parsed.getTranslations().clear();
preCreateEntity(parsed);
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
ObjectReport objectReport = getObjectReport(importReport);
WebMessage webMessage = WebMessageUtils.objectReport(objectReport);
if (objectReport != null && webMessage.getStatus() == Status.OK) {
String location = contextService.getApiPath() + getSchema().getRelativeApiEndpoint() + "/" + objectReport.getUid();
webMessage.setHttpStatus(HttpStatus.CREATED);
response.setHeader(ContextUtils.HEADER_LOCATION, location);
T entity = manager.get(getEntityClass(), objectReport.getUid());
postCreateEntity(entity);
} else {
webMessage.setStatus(Status.ERROR);
}
webMessageService.send(webMessage, response, request);
}
use of org.hisp.dhis.feedback.ObjectReport in project dhis2-core by dhis2.
the class AbstractCrudController method postJsonObject.
//--------------------------------------------------------------------------
// POST
//--------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
public void postJsonObject(HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = currentUserService.getCurrentUser();
if (!aclService.canCreate(user, getEntityClass())) {
throw new CreateAccessDeniedException("You don't have the proper permissions to create this object.");
}
T parsed = deserializeJsonEntity(request, response);
parsed.getTranslations().clear();
preCreateEntity(parsed);
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
ObjectReport objectReport = getObjectReport(importReport);
WebMessage webMessage = WebMessageUtils.objectReport(objectReport);
if (objectReport != null && webMessage.getStatus() == Status.OK) {
String location = contextService.getApiPath() + getSchema().getRelativeApiEndpoint() + "/" + objectReport.getUid();
webMessage.setHttpStatus(HttpStatus.CREATED);
response.setHeader(ContextUtils.HEADER_LOCATION, location);
T entity = manager.get(getEntityClass(), objectReport.getUid());
postCreateEntity(entity);
} else {
webMessage.setStatus(Status.ERROR);
}
webMessageService.send(webMessage, response, request);
}
use of org.hisp.dhis.feedback.ObjectReport in project dhis2-core by dhis2.
the class UserController method inviteUser.
/**
* Creates a user invitation and invites the user.
*
* @param user user object parsed from the POST request.
*/
private ObjectReport inviteUser(User user, User currentUser, HttpServletRequest request) throws Exception {
RestoreOptions restoreOptions = user.getUsername() == null || user.getUsername().isEmpty() ? RestoreOptions.INVITE_WITH_USERNAME_CHOICE : RestoreOptions.INVITE_WITH_DEFINED_USERNAME;
securityService.prepareUserForInvite(user);
ImportReport importReport = createUser(user, currentUser);
ObjectReport objectReport = getObjectReport(importReport);
if (importReport.getStatus() == Status.OK && importReport.getStats().getCreated() == 1) {
securityService.sendRestoreMessage(user.getUserCredentials(), ContextUtils.getContextPath(request), restoreOptions);
}
return objectReport;
}
use of org.hisp.dhis.feedback.ObjectReport in project dhis2-core by dhis2.
the class ObjectBundleServiceAttributesTest method testValidateMetadataAttributeValuesUnique.
@Test
public void testValidateMetadataAttributeValuesUnique() throws IOException {
defaultSetupWithAttributes();
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/metadata_with_unique_attributes.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validationReport = objectBundleValidationService.validate(bundle);
List<ObjectReport> objectReports = validationReport.getObjectReports(DataElement.class);
assertFalse(objectReports.isEmpty());
assertEquals(2, validationReport.getErrorReportsByCode(DataElement.class, ErrorCode.E4009).size());
}
Aggregations