Search in sources :

Example 11 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID in project dhis2-core by dhis2.

the class AbstractCrudController method getObjectProperty.

@RequestMapping(value = "/{uid}/{property}", method = RequestMethod.GET)
@ResponseBody
public RootNode getObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @RequestParam Map<String, String> rpParameters, TranslateParams translateParams, HttpServletRequest request, HttpServletResponse response) throws Exception {
    User user = currentUserService.getCurrentUser();
    if (!"translations".equals(pvProperty)) {
        setUserContext(user, translateParams);
    } else {
        setUserContext(null, new TranslateParams(false));
    }
    if (!aclService.canRead(user, getEntityClass())) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
    }
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.add(":all");
    }
    String fieldFilter = "[" + Joiner.on(',').join(fields) + "]";
    return getObjectInternal(pvUid, rpParameters, Lists.newArrayList(), Lists.newArrayList(pvProperty + fieldFilter), user);
}
Also used : User(org.hisp.dhis.user.User) TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 12 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID 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 13 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID 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 14 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID 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 15 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID 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)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)92 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)52 Event (org.hisp.dhis.dxf2.events.event.Event)39 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)37 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)34 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)29 GetMapping (org.springframework.web.bind.annotation.GetMapping)28 User (org.hisp.dhis.user.User)23 Test (org.junit.jupiter.api.Test)21 HashMap (java.util.HashMap)19 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)19 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)19 InputStream (java.io.InputStream)18 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)18 ArrayList (java.util.ArrayList)17 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)17 List (java.util.List)16 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)16 DataElement (org.hisp.dhis.dataelement.DataElement)15 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)15