use of org.hisp.dhis.dxf2.webmessage.WebMessage 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.dxf2.webmessage.WebMessage 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.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class AbstractCrudController method putXmlObject.
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
public void putXmlObject(@PathVariable("uid") String pvUid, HttpServletRequest request, HttpServletResponse response) throws Exception {
List<T> objects = getEntity(pvUid);
if (objects.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
}
User user = currentUserService.getCurrentUser();
if (!aclService.canUpdate(user, objects.get(0))) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
T parsed = deserializeXmlEntity(request, response);
((BaseIdentifiableObject) parsed).setUid(pvUid);
preUpdateEntity(objects.get(0), parsed);
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.UPDATE).addObject(parsed);
ImportReport importReport = importService.importMetadata(params);
WebMessage webMessage = WebMessageUtils.objectReport(importReport);
if (importReport.getStatus() == Status.OK) {
T entity = manager.get(getEntityClass(), pvUid);
postUpdateEntity(entity);
} else {
webMessage.setStatus(Status.ERROR);
}
webMessageService.send(webMessage, response, request);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class MaintenanceController method pruneDataByDataElement.
@RequestMapping(value = "/dataPruning/dataElements/{uid}", method = { RequestMethod.PUT, RequestMethod.POST })
@PreAuthorize("hasRole('ALL')")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void pruneDataByDataElement(@PathVariable String uid, HttpServletResponse response) throws Exception {
DataElement dataElement = dataElementService.getDataElement(uid);
if (dataElement == null) {
webMessageService.sendJson(WebMessageUtils.conflict("Data element does not exist: " + uid), response);
return;
}
boolean result = maintenanceService.pruneData(dataElement);
WebMessage message = result ? WebMessageUtils.ok("Data was pruned successfully") : WebMessageUtils.conflict("Data could not be pruned");
webMessageService.sendJson(message, response);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class MaintenanceController method pruneDataByOrganisationUnit.
@RequestMapping(value = "/dataPruning/organisationUnits/{uid}", method = { RequestMethod.PUT, RequestMethod.POST })
@PreAuthorize("hasRole('ALL')")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void pruneDataByOrganisationUnit(@PathVariable String uid, HttpServletResponse response) throws Exception {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(uid);
if (organisationUnit == null) {
webMessageService.sendJson(WebMessageUtils.conflict("Organisation unit does not exist: " + uid), response);
return;
}
boolean result = maintenanceService.pruneData(organisationUnit);
WebMessage message = result ? WebMessageUtils.ok("Data was pruned successfully") : WebMessageUtils.conflict("Data could not be pruned");
webMessageService.sendJson(message, response);
}
Aggregations