Search in sources :

Example 51 with WebMessage

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

the class AbstractCrudController method deleteCollectionItem.

@DeleteMapping(value = "/{uid}/{property}/{itemId}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public WebMessage deleteCollectionItem(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @PathVariable("itemId") String pvItemId, HttpServletResponse response) throws Exception {
    List<T> objects = getEntity(pvUid);
    if (objects.isEmpty()) {
        throw new WebMessageException(notFound(getEntityClass(), pvUid));
    }
    IdentifiableObjects items = new IdentifiableObjects();
    items.setIdentifiableObjects(singletonList(new BaseIdentifiableObject(pvItemId, "", "")));
    return deleteCollectionItems(pvProperty, objects.get(0), items);
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) IdentifiableObjects(org.hisp.dhis.common.IdentifiableObjects) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 52 with WebMessage

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

the class AbstractCrudController method putXmlObject.

@PutMapping(value = "/{uid}", consumes = { APPLICATION_XML_VALUE, TEXT_XML_VALUE })
@ResponseBody
public WebMessage putXmlObject(@PathVariable("uid") String pvUid, @CurrentUser User currentUser, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<T> objects = getEntity(pvUid);
    if (objects.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    if (!aclService.canUpdate(currentUser, objects.get(0))) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    T parsed = deserializeXmlEntity(request);
    ((BaseIdentifiableObject) parsed).setUid(pvUid);
    preUpdateEntity(objects.get(0), parsed);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(parsed);
    ImportReport importReport = importService.importMetadata(params);
    WebMessage webMessage = objectReport(importReport);
    if (importReport.getStatus() == Status.OK) {
        T entity = manager.get(getEntityClass(), pvUid);
        postUpdateEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    return webMessage;
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PutMapping(org.springframework.web.bind.annotation.PutMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 53 with WebMessage

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

the class AbstractCrudController method postObject.

private WebMessage postObject(T parsed) 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.");
    }
    parsed.getTranslations().clear();
    preCreateEntity(parsed);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(parsed);
    return postObject(getObjectReport(importService.importMetadata(params)));
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException)

Example 54 with WebMessage

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

the class AbstractCrudController method putJsonObject.

// --------------------------------------------------------------------------
// PUT
// --------------------------------------------------------------------------
@PutMapping(value = "/{uid}", consumes = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage putJsonObject(@PathVariable("uid") String pvUid, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    List<T> objects = getEntity(pvUid);
    if (objects.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    if (!aclService.canUpdate(currentUser, objects.get(0))) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    T parsed = deserializeJsonEntity(request);
    ((BaseIdentifiableObject) parsed).setUid(pvUid);
    preUpdateEntity(objects.get(0), parsed);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(parsed);
    // default to FULL unless ERRORS_NOT_OWNER has been requested
    if (ImportReportMode.ERRORS_NOT_OWNER != params.getImportReportMode()) {
        params.setImportReportMode(ImportReportMode.FULL);
    }
    ImportReport importReport = importService.importMetadata(params);
    WebMessage webMessage = objectReport(importReport);
    if (importReport.getStatus() == Status.OK) {
        T entity = manager.get(getEntityClass(), pvUid);
        postUpdateEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    return webMessage;
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PutMapping(org.springframework.web.bind.annotation.PutMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 55 with WebMessage

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

the class AbstractCrudController method replaceTranslations.

@PutMapping(value = "/{uid}/translations")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public WebMessage replaceTranslations(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    BaseIdentifiableObject persistedObject = (BaseIdentifiableObject) entities.get(0);
    if (!aclService.canUpdate(currentUser, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    T inputObject = renderService.fromJson(request.getInputStream(), getEntityClass());
    HashSet<Translation> translations = new HashSet<>(inputObject.getTranslations());
    persistedObject.setTranslations(translations);
    MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap());
    params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(persistedObject).setImportMode(ObjectBundleMode.VALIDATE);
    ImportReport importReport = importService.importMetadata(params);
    if (!importReport.hasErrorReports()) {
        manager.save(persistedObject);
        return null;
    }
    return importReport(importReport);
}
Also used : Translation(org.hisp.dhis.translation.Translation) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) HashSet(java.util.HashSet) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PutMapping(org.springframework.web.bind.annotation.PutMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ResponseBody (org.springframework.web.bind.annotation.ResponseBody)49 PostMapping (org.springframework.web.bind.annotation.PostMapping)29 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)28 InputStream (java.io.InputStream)24 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)20 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)17 PutMapping (org.springframework.web.bind.annotation.PutMapping)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)15 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 JobConfiguration (org.hisp.dhis.scheduling.JobConfiguration)10 User (org.hisp.dhis.user.User)10 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)10 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)9 List (java.util.List)8 Event (org.hisp.dhis.dxf2.events.event.Event)8 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)7 IOException (java.io.IOException)6 FileResourceWebMessageResponse (org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse)6