Search in sources :

Example 11 with WebMessageUtils.notFound

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound 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 12 with WebMessageUtils.notFound

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound 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)

Example 13 with WebMessageUtils.notFound

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.

the class AbstractCrudController method putJsonObject.

//--------------------------------------------------------------------------
// PUT
//--------------------------------------------------------------------------
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void putJsonObject(@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 = deserializeJsonEntity(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);
}
Also used : User(org.hisp.dhis.user.User) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with WebMessageUtils.notFound

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.

the class AbstractCrudController method partialUpdateObject.

@RequestMapping(value = "/{uid}", method = RequestMethod.PATCH)
public void partialUpdateObject(@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.");
    }
    String payload = StreamUtils.copyToString(request.getInputStream(), Charset.forName("UTF-8"));
    List<String> properties = new ArrayList<>();
    T object = null;
    if (isJson(request)) {
        properties = getJsonProperties(payload);
        object = renderService.fromJson(payload, getEntityClass());
    } else if (isXml(request)) {
        properties = getXmlProperties(payload);
        object = renderService.fromXml(payload, getEntityClass());
    }
    prePatchEntity(persistedObject, object);
    properties = getPersistedProperties(properties);
    if (properties.isEmpty() || object == null) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        return;
    }
    Schema schema = getSchema();
    for (String keyProperty : properties) {
        Property property = schema.getProperty(keyProperty);
        Object value = property.getGetterMethod().invoke(object);
        property.getSetterMethod().invoke(persistedObject, value);
    }
    manager.update(persistedObject);
    postPatchEntity(persistedObject);
}
Also used : User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Property(org.hisp.dhis.schema.Property) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with WebMessageUtils.notFound

use of org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound in project dhis2-core by dhis2.

the class MapController method putJsonObject.

@Override
@RequestMapping(value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void putJsonObject(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map map = mappingService.getMap(uid);
    if (map == null) {
        throw new WebMessageException(WebMessageUtils.notFound("Map does not exist: " + uid));
    }
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    Map newMap = deserializeJsonEntity(request, response);
    newMap.setUid(uid);
    mergeService.merge(new MergeParams<>(newMap, map).setMergeMode(params.getMergeMode()));
    mappingService.updateMap(map);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) MergeParams(org.hisp.dhis.schema.MergeParams) Map(org.hisp.dhis.mapping.Map) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)59 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)17 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)17 User (org.hisp.dhis.user.User)12 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)11 InputStream (java.io.InputStream)7 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)7 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)7 Dashboard (org.hisp.dhis.dashboard.Dashboard)7 Property (org.hisp.dhis.schema.Property)7 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)6 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)6 Schema (org.hisp.dhis.schema.Schema)6 DashboardItem (org.hisp.dhis.dashboard.DashboardItem)5 Event (org.hisp.dhis.dxf2.events.event.Event)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)5 IOException (java.io.IOException)4 DataElement (org.hisp.dhis.dataelement.DataElement)4