use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class AbstractCrudController method postObject.
private WebMessage postObject(T parsed) 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.");
}
parsed.getTranslations().clear();
preCreateEntity(parsed);
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(parsed);
return postObject(getObjectReport(importService.importMetadata(params)));
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class AbstractCrudController method putJsonObject.
// --------------------------------------------------------------------------
// PUT
// --------------------------------------------------------------------------
@PutMapping(value = "/{uid}", consumes = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage putJsonObject(@PathVariable("uid") String pvUid, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
List<T> objects = getEntity(pvUid);
if (objects.isEmpty()) {
return notFound(getEntityClass(), pvUid);
}
if (!aclService.canUpdate(currentUser, objects.get(0))) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
T parsed = deserializeJsonEntity(request);
((BaseIdentifiableObject) parsed).setUid(pvUid);
preUpdateEntity(objects.get(0), parsed);
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(parsed);
// default to FULL unless ERRORS_NOT_OWNER has been requested
if (ImportReportMode.ERRORS_NOT_OWNER != params.getImportReportMode()) {
params.setImportReportMode(ImportReportMode.FULL);
}
ImportReport importReport = importService.importMetadata(params);
WebMessage webMessage = objectReport(importReport);
if (importReport.getStatus() == Status.OK) {
T entity = manager.get(getEntityClass(), pvUid);
postUpdateEntity(entity);
} else {
webMessage.setStatus(Status.ERROR);
}
return webMessage;
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class AbstractCrudController method updateObjectProperty.
@PatchMapping("/{uid}/{property}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void updateObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @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()) {
throw new WebMessageException(notFound(getEntityClass(), pvUid));
}
if (!getSchema().haveProperty(pvProperty)) {
throw new WebMessageException(notFound("Property " + pvProperty + " does not exist on " + getEntityName()));
}
Property property = getSchema().getProperty(pvProperty);
T persistedObject = entities.get(0);
if (!aclService.canUpdate(currentUser, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!property.isWritable()) {
throw new UpdateAccessDeniedException("This property is read-only.");
}
T object = deserialize(request);
if (object == null) {
throw new WebMessageException(badRequest("Unknown payload format."));
}
prePatchEntity(persistedObject);
Object value = property.getGetterMethod().invoke(object);
property.getSetterMethod().invoke(persistedObject, value);
Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(persistedObject);
ImportReport importReport = importService.importMetadata(params);
if (importReport.getStatus() != Status.OK) {
throw new WebMessageException(objectReport(importReport));
}
postPatchEntity(persistedObject);
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class AbstractCrudController method replaceTranslations.
@PutMapping(value = "/{uid}/translations")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public WebMessage replaceTranslations(@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);
}
BaseIdentifiableObject persistedObject = (BaseIdentifiableObject) entities.get(0);
if (!aclService.canUpdate(currentUser, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
T inputObject = renderService.fromJson(request.getInputStream(), getEntityClass());
HashSet<Translation> translations = new HashSet<>(inputObject.getTranslations());
persistedObject.setTranslations(translations);
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(persistedObject).setImportMode(ObjectBundleMode.VALIDATE);
ImportReport importReport = importService.importMetadata(params);
if (!importReport.hasErrorReports()) {
manager.save(persistedObject);
return null;
}
return importReport(importReport);
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class MetadataImportExportController method postJsonMetadata.
@PostMapping(value = "", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage postJsonMetadata(HttpServletRequest request) throws IOException {
MetadataImportParams params = metadataImportService.getParamsFromMap(contextService.getParameterValuesMap());
final Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects = renderService.fromMetadata(StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream()), RenderFormat.JSON);
params.setObjects(objects);
if (params.hasJobId()) {
return startAsyncMetadata(params);
}
ImportReport importReport = metadataImportService.importMetadata(params);
return importReport(importReport).withPlainResponseBefore(DhisApiVersion.V38);
}
Aggregations