use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class MetadataImportExportController method bulkSharing.
@ResponseBody
@PatchMapping(value = "sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws IOException {
final BulkJsonPatches bulkJsonPatches = jsonMapper.readValue(request.getInputStream(), BulkJsonPatches.class);
BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatches(bulkJsonPatches, 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 importParams = metadataImportService.getParamsFromMap(parameterValuesMap);
importParams.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).setAtomicMode(atomic ? AtomicMode.ALL : AtomicMode.NONE).addObjects(patchedObjects);
ImportReport importReport = metadataImportService.importMetadata(importParams);
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.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class ApiTokenController method postJsonObject.
@PostMapping(consumes = "application/json")
@ResponseBody
public WebMessage postJsonObject(HttpServletRequest request) throws Exception {
final ApiToken apiToken = deserializeJsonEntity(request);
User user = currentUserService.getCurrentUser();
if (!aclService.canCreate(user, getEntityClass())) {
throw new CreateAccessDeniedException("You don't have the proper permissions to create this object.");
}
apiToken.getTranslations().clear();
// Validate input values is ok
validateBeforeCreate(apiToken);
// We only make personal access tokens for now
apiToken.setType(ApiTokenType.PERSONAL_ACCESS_TOKEN);
// Generate key and set default values
apiTokenService.initToken(apiToken);
// Save raw key to send in response
final String rawKey = apiToken.getKey();
// Hash the raw token key and overwrite value in the entity to persist
final String hashedKey = apiTokenService.hashKey(apiToken.getKey());
apiToken.setKey(hashedKey);
// Continue POST import as usual
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(apiToken);
final ObjectReport objectReport = importService.importMetadata(params).getFirstObjectReport();
final String uid = objectReport.getUid();
WebMessage webMessage = objectReport(objectReport);
if (webMessage.getStatus() == Status.OK) {
webMessage.setHttpStatus(HttpStatus.CREATED);
webMessage.setLocation(getSchema().getRelativeApiEndpoint() + "/" + uid);
// Set our custom web response object that includes the new
// generated key.
webMessage.setResponse(new ApiTokenCreationResponse(objectReport, rawKey));
} else {
webMessage.setStatus(Status.ERROR);
}
return webMessage;
}
Aggregations