use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class AbstractCrudController method bulkSharing.
@ResponseBody
@PatchMapping(path = "/sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws Exception {
final BulkJsonPatch bulkJsonPatch = jsonMapper.readValue(request.getInputStream(), BulkJsonPatch.class);
BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatch(bulkJsonPatch, patchParams);
if (patchedObjects.isEmpty() || (atomic && patchParams.hasErrorReports())) {
ImportReport importReport = new ImportReport();
importReport.addTypeReports(patchParams.getTypeReports());
importReport.setStatus(Status.ERROR);
return importReport(importReport);
}
Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
params.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).addObjects(patchedObjects);
ImportReport importReport = importService.importMetadata(params);
if (patchParams.hasErrorReports()) {
importReport.addTypeReports(patchParams.getTypeReports());
importReport.setStatus(importReport.getStatus() == Status.OK ? Status.WARNING : importReport.getStatus());
}
return importReport(importReport);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class AbstractCrudController method patchObject.
// --------------------------------------------------------------------------
// PATCH
// --------------------------------------------------------------------------
/**
* Adds support for HTTP Patch using JSON Patch (RFC 6902), updated object
* is run through normal metadata importer and internally looks like a
* normal PUT (after the JSON Patch has been applied).
*
* For now we only support the official mimetype
* "application/json-patch+json" but in future releases we might also want
* to support "application/json" after the old patch behavior has been
* removed.
*/
@ResponseBody
@PatchMapping(path = "/{uid}", consumes = "application/json-patch+json")
public WebMessage patchObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
return notFound(getEntityClass(), pvUid);
}
final T persistedObject = entities.get(0);
if (!aclService.canUpdate(currentUser, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
manager.resetNonOwnerProperties(persistedObject);
prePatchEntity(persistedObject);
final JsonPatch patch = jsonMapper.readValue(request.getInputStream(), JsonPatch.class);
final T patchedObject = jsonPatchManager.apply(patch, persistedObject);
// we don't allow changing IDs
((BaseIdentifiableObject) patchedObject).setId(persistedObject.getId());
// we don't allow changing UIDs
((BaseIdentifiableObject) patchedObject).setUid(persistedObject.getUid());
// Only supports new Sharing format
((BaseIdentifiableObject) patchedObject).clearLegacySharingCollections();
prePatchEntity(persistedObject, patchedObject);
Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
if (!parameterValuesMap.containsKey("importReportMode")) {
parameterValuesMap.put("importReportMode", Collections.singletonList("ERRORS_NOT_OWNER"));
}
MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(patchedObject);
ImportReport importReport = importService.importMetadata(params);
WebMessage webMessage = objectReport(importReport);
if (importReport.getStatus() == Status.OK) {
T entity = manager.get(getEntityClass(), pvUid);
postPatchEntity(entity);
} else {
webMessage.setStatus(Status.ERROR);
}
return webMessage;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class CompleteDataSetRegistrationController method postCompleteRegistrationsJson.
@PostMapping(consumes = CONTENT_TYPE_JSON, produces = CONTENT_TYPE_JSON)
@ResponseBody
public WebMessage postCompleteRegistrationsJson(ImportOptions importOptions, HttpServletRequest request) throws IOException {
if (importOptions.isAsync()) {
return asyncImport(importOptions, ImportCompleteDataSetRegistrationsTask.FORMAT_JSON, request);
}
ImportSummary summary = registrationExchangeService.saveCompleteDataSetRegistrationsJson(request.getInputStream(), importOptions);
summary.setImportOptions(importOptions);
return importSummary(summary).withPlainResponseBefore(DhisApiVersion.V38);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class CompleteDataSetRegistrationController method asyncImport.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private WebMessage asyncImport(ImportOptions importOptions, String format, HttpServletRequest request) throws IOException {
Pair<InputStream, Path> tmpFile = saveTmpFile(request.getInputStream());
JobConfiguration jobId = new JobConfiguration("inMemoryCompleteDataSetRegistrationImport", COMPLETE_DATA_SET_REGISTRATION_IMPORT, currentUserService.getCurrentUser().getUid(), true);
taskExecutor.executeTask(new ImportCompleteDataSetRegistrationsTask(registrationExchangeService, sessionFactory, tmpFile.getLeft(), tmpFile.getRight(), importOptions, format, jobId));
return jobConfigurationReport(jobId).setLocation("/system/tasks/" + COMPLETE_DATA_SET_REGISTRATION_IMPORT);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class MessageConversationController method postObject.
private WebMessage postObject(HttpServletRequest request, MessageConversation messageConversation) throws WebMessageException {
Set<User> users = Sets.newHashSet(messageConversation.getUsers());
messageConversation.getUsers().clear();
messageConversation.getUsers().addAll(getUsersToMessageConversation(messageConversation, users));
if (messageConversation.getUsers().isEmpty()) {
throw new WebMessageException(conflict("No recipients selected."));
}
String metaData = MessageService.META_USER_AGENT + request.getHeader(ContextUtils.HEADER_USER_AGENT);
Set<FileResource> attachments = new HashSet<>();
for (FileResource fr : messageConversation.getAttachments()) {
FileResource fileResource = fileResourceService.getFileResource(fr.getUid());
if (fileResource == null) {
throw new WebMessageException(conflict("Attachment '" + fr.getUid() + "' not found."));
}
if (!fileResource.getDomain().equals(FileResourceDomain.MESSAGE_ATTACHMENT) || fileResource.isAssigned()) {
throw new WebMessageException(conflict("Attachment '" + fr.getUid() + "' is already used or not a valid attachment."));
}
fileResource.setAssigned(true);
fileResourceService.updateFileResource(fileResource);
attachments.add(fileResource);
}
long id = messageService.sendPrivateMessage(messageConversation.getUsers(), messageConversation.getSubject(), messageConversation.getText(), metaData, attachments);
org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(id);
return created("Message conversation created").setLocation(MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid());
}
Aggregations