Search in sources :

Example 11 with WebMessageException

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);
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) IOException(java.io.IOException) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with WebMessageException

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);
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with WebMessageException

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) JFreeChart(org.jfree.chart.JFreeChart) Chart(org.hisp.dhis.chart.Chart) JFreeChart(org.jfree.chart.JFreeChart) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with WebMessageException

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);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ObjectTranslation(org.hisp.dhis.translation.ObjectTranslation) ObjectReport(org.hisp.dhis.feedback.ObjectReport) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) ErrorReport(org.hisp.dhis.feedback.ErrorReport) TypeReport(org.hisp.dhis.feedback.TypeReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with WebMessageException

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);
}
Also used : User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)134 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)118 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)31 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)28 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)27 DataSet (org.hisp.dhis.dataset.DataSet)21 Period (org.hisp.dhis.period.Period)21 User (org.hisp.dhis.user.User)20 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)18 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)15 ArrayList (java.util.ArrayList)14 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)14 Interpretation (org.hisp.dhis.interpretation.Interpretation)13 Date (java.util.Date)9 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)9 InputStream (java.io.InputStream)8 Grid (org.hisp.dhis.common.Grid)8 Event (org.hisp.dhis.dxf2.events.event.Event)8 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)8 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)8