use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class DataSetController method updateCustomDataEntryFormJson.
@RequestMapping(value = "/{uid}/form", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiVersion(value = DhisApiVersion.ALL, exclude = DhisApiVersion.V23)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateCustomDataEntryFormJson(@PathVariable("uid") String uid, HttpServletRequest request) throws WebMessageException {
DataSet dataSet = dataSetService.getDataSet(uid);
if (dataSet == null) {
throw new WebMessageException(WebMessageUtils.notFound("DataSet not found for uid: " + uid));
}
DataEntryForm form = dataSet.getDataEntryForm();
DataEntryForm newForm;
try {
newForm = renderService.fromJson(request.getInputStream(), DataEntryForm.class);
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.badRequest("Failed to parse request", e.getMessage()));
}
if (form == null) {
if (!newForm.hasForm()) {
throw new WebMessageException(WebMessageUtils.badRequest("Missing required parameter 'htmlCode'"));
}
newForm.setName(dataSet.getName());
dataEntryFormService.addDataEntryForm(newForm);
dataSet.setDataEntryForm(newForm);
} else {
if (newForm.getHtmlCode() != null) {
form.setHtmlCode(dataEntryFormService.prepareDataEntryFormForSave(newForm.getHtmlCode()));
}
if (newForm.getStyle() != null) {
form.setStyle(newForm.getStyle());
}
dataEntryFormService.updateDataEntryForm(form);
}
dataSet.increaseVersion();
dataSetService.updateDataSet(dataSet);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class DataSetController method updateCustomDataEntryFormHtml.
@RequestMapping(value = { "/{uid}/customDataEntryForm", "/{uid}/form" }, method = { RequestMethod.PUT, RequestMethod.POST }, consumes = "text/html")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateCustomDataEntryFormHtml(@PathVariable("uid") String uid, @RequestBody String formContent, HttpServletResponse response) throws Exception {
DataSet dataSet = dataSetService.getDataSet(uid);
if (dataSet == null) {
throw new WebMessageException(WebMessageUtils.notFound("DataSet not found for uid: " + uid));
}
DataEntryForm form = dataSet.getDataEntryForm();
if (form == null) {
form = new DataEntryForm(dataSet.getName(), DisplayDensity.NORMAL, formContent);
dataEntryFormService.addDataEntryForm(form);
dataSet.setDataEntryForm(form);
} else {
form.setHtmlCode(formContent);
dataEntryFormService.updateDataEntryForm(form);
}
dataSet.increaseVersion();
dataSetService.updateDataSet(dataSet);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class ChartController method getChart.
//--------------------------------------------------------------------------
// Get data
//--------------------------------------------------------------------------
@RequestMapping(value = { "/{uid}/data", "/{uid}/data.png" }, method = RequestMethod.GET)
public void getChart(@PathVariable("uid") String uid, @RequestParam(value = "date", required = false) Date date, @RequestParam(value = "ou", required = false) String ou, @RequestParam(value = "width", defaultValue = "800", required = false) int width, @RequestParam(value = "height", defaultValue = "500", required = false) int height, @RequestParam(value = "attachment", required = false) boolean attachment, HttpServletResponse response) throws IOException, WebMessageException {
Chart chart = chartService.getChartNoAcl(uid);
if (chart == null) {
throw new WebMessageException(WebMessageUtils.notFound("Chart does not exist: " + uid));
}
OrganisationUnit unit = ou != null ? organisationUnitService.getOrganisationUnit(ou) : null;
JFreeChart jFreeChart = chartService.getJFreeChart(chart, date, unit, i18nManager.getI18nFormat());
String filename = CodecUtils.filenameEncode(chart.getName()) + ".png";
contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, attachment);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class AbstractCrudController method replaceTranslations.
@RequestMapping(value = "/{uid}/translations", method = RequestMethod.PUT)
public void replaceTranslations(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
}
T persistedObject = entities.get(0);
User user = currentUserService.getCurrentUser();
if (!aclService.canUpdate(user, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
T object = renderService.fromJson(request.getInputStream(), getEntityClass());
TypeReport typeReport = new TypeReport(ObjectTranslation.class);
List<ObjectTranslation> objectTranslations = Lists.newArrayList(object.getTranslations());
for (int idx = 0; idx < object.getTranslations().size(); idx++) {
ObjectReport objectReport = new ObjectReport(ObjectTranslation.class, idx);
ObjectTranslation translation = objectTranslations.get(idx);
if (translation.getLocale() == null) {
objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "locale").setErrorKlass(getEntityClass()));
}
if (translation.getProperty() == null) {
objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "property").setErrorKlass(getEntityClass()));
}
if (translation.getValue() == null) {
objectReport.addErrorReport(new ErrorReport(ObjectTranslation.class, ErrorCode.E4000, "value").setErrorKlass(getEntityClass()));
}
typeReport.addObjectReport(objectReport);
if (!objectReport.isEmpty()) {
typeReport.getStats().incIgnored();
}
}
if (!typeReport.getErrorReports().isEmpty()) {
WebMessage webMessage = WebMessageUtils.typeReport(typeReport);
webMessageService.send(webMessage, response, request);
return;
}
manager.updateTranslations(persistedObject, object.getTranslations());
manager.update(persistedObject);
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
}
use of org.hisp.dhis.dxf2.webmessage.WebMessageException in project dhis2-core by dhis2.
the class AbstractCrudController method deleteObject.
//--------------------------------------------------------------------------
// DELETE
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void deleteObject(@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.canDelete(user, objects.get(0))) {
throw new DeleteAccessDeniedException("You don't have the proper permissions to delete this object.");
}
preDeleteEntity(objects.get(0));
MetadataImportParams params = new MetadataImportParams().setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.DELETE).addObject(objects.get(0));
ImportReport importReport = importService.importMetadata(params);
postDeleteEntity();
webMessageService.send(WebMessageUtils.objectReport(importReport), response, request);
}
Aggregations