Search in sources :

Example 61 with ID

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

the class EventController method deleteEvent.

// -------------------------------------------------------------------------
// DELETE
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_DELETE')")
public void deleteEvent(HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid) throws WebMessageException {
    if (!programStageInstanceService.programStageInstanceExists(uid)) {
        throw new WebMessageException(WebMessageUtils.notFound("Event not found for ID " + uid));
    }
    response.setStatus(HttpServletResponse.SC_OK);
    try {
        ImportSummary importSummary = eventService.deleteEvent(uid);
        webMessageService.send(WebMessageUtils.importSummary(importSummary), response, request);
    } catch (Exception ex) {
        webMessageService.send(WebMessageUtils.conflict("Unable to delete event " + uid, ex.getMessage()), response, request);
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) IOException(java.io.IOException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 62 with ID

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

the class DataValueController method getDataValueFile.

// ---------------------------------------------------------------------
// GET file
// ---------------------------------------------------------------------
@RequestMapping(value = "/files", method = RequestMethod.GET)
public void getDataValueFile(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = getAndValidateDataElement(de);
    if (!dataElement.isFileType()) {
        throw new WebMessageException(WebMessageUtils.conflict("DataElement must be of type file"));
    }
    DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
    DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
    // ---------------------------------------------------------------------
    // Get data value
    // ---------------------------------------------------------------------
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    if (dataValue == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
    }
    // ---------------------------------------------------------------------
    // Get file resource
    // ---------------------------------------------------------------------
    String uid = dataValue.getValue();
    FileResource fileResource = fileResourceService.getFileResource(uid);
    if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
        throw new WebMessageException(WebMessageUtils.notFound("A data value file resource with id " + uid + " does not exist."));
    }
    FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();
    if (storageStatus != FileResourceStorageStatus.STORED) {
        // Special case:
        //  The FileResource exists and has been tied to this DataValue, however, the underlying file
        //  content is still not stored to the (most likely external) file store provider.
        // HTTP 409, for lack of a more suitable status code
        WebMessage webMessage = WebMessageUtils.conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.");
        webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
        throw new WebMessageException(webMessage);
    }
    ByteSource content = fileResourceService.getFileResourceContent(fileResource);
    if (content == null) {
        throw new WebMessageException(WebMessageUtils.notFound("The referenced file could not be found"));
    }
    // ---------------------------------------------------------------------
    // Attempt to build signed URL request for content and redirect
    // ---------------------------------------------------------------------
    URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(uid);
    if (signedGetUri != null) {
        response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
        response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
        return;
    }
    // ---------------------------------------------------------------------
    // Build response and return
    // ---------------------------------------------------------------------
    response.setContentType(fileResource.getContentType());
    response.setContentLength(new Long(fileResource.getContentLength()).intValue());
    response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
    // ---------------------------------------------------------------------
    // Request signing is not available, stream content back to client
    // ---------------------------------------------------------------------
    InputStream inputStream = null;
    try {
        inputStream = content.openStream();
        IOUtils.copy(inputStream, response.getOutputStream());
    } catch (IOException e) {
        throw new WebMessageException(WebMessageUtils.error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) InputStream(java.io.InputStream) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) IOException(java.io.IOException) URI(java.net.URI) DataElement(org.hisp.dhis.dataelement.DataElement) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) ByteSource(com.google.common.io.ByteSource) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 63 with ID

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

the class DataApprovalController method getApprovalByCategoryOptionCombos.

@RequestMapping(value = APPROVALS_PATH + "/categoryOptionCombos", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_JSON)
public void getApprovalByCategoryOptionCombos(@RequestParam Set<String> ds, @RequestParam String pe, @RequestParam(required = false) String ou, HttpServletResponse response) throws IOException, WebMessageException {
    Set<DataSet> dataSets = parseDataSetsWithWorkflow(ds);
    Period period = PeriodType.getPeriodFromIsoString(pe);
    if (period == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal period identifier: " + pe));
    }
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(ou);
    if (orgUnit != null && orgUnit.isRoot()) {
        // Look for all org units.
        orgUnit = null;
    }
    SetMap<DataApprovalWorkflow, DataElementCategoryCombo> workflowCategoryComboMap = new SetMap<>();
    for (DataSet dataSet : dataSets) {
        workflowCategoryComboMap.putValue(dataSet.getWorkflow(), dataSet.getCategoryCombo());
    }
    List<DataApprovalStatus> statusList = new ArrayList<>();
    for (DataApprovalWorkflow workflow : workflowCategoryComboMap.keySet()) {
        for (DataElementCategoryCombo attributeCombo : workflowCategoryComboMap.get(workflow)) {
            statusList.addAll(dataApprovalService.getUserDataApprovalsAndPermissions(workflow, period, orgUnit, attributeCombo));
        }
    }
    List<Map<String, Object>> list = new ArrayList<>();
    for (DataApprovalStatus status : statusList) {
        Map<String, Object> item = new HashMap<>();
        Map<String, String> approvalLevel = new HashMap<>();
        if (status.getApprovedLevel() != null) {
            approvalLevel.put("id", status.getApprovedLevel().getUid());
            approvalLevel.put("level", String.valueOf(status.getApprovedLevel().getLevel()));
        }
        item.put("id", status.getAttributeOptionComboUid());
        item.put("level", approvalLevel);
        item.put("ou", status.getOrganisationUnitUid());
        item.put("ouName", status.getOrganisationUnitName());
        item.put("accepted", status.isAccepted());
        item.put("permissions", status.getPermissions());
        list.add(item);
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), list);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) HashMap(java.util.HashMap) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) DataApprovalWorkflow(org.hisp.dhis.dataapproval.DataApprovalWorkflow) DataApprovalStatus(org.hisp.dhis.dataapproval.DataApprovalStatus) SetMap(org.hisp.dhis.common.SetMap) Map(java.util.Map) SetMap(org.hisp.dhis.common.SetMap) HashMap(java.util.HashMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 64 with ID

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

the class SmsGatewayController method removeGateway.

// -------------------------------------------------------------------------
// DELETE
// -------------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_MOBILE_SENDSMS')")
@RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
public void removeGateway(@PathVariable String uid, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
    SmsGatewayConfig gateway = gatewayAdminService.getGatewayConfigurationByUid(uid);
    if (gateway == null) {
        throw new WebMessageException(WebMessageUtils.notFound("No gateway found with id: " + uid));
    }
    gatewayAdminService.removeGatewayByUid(uid);
    webMessageService.send(WebMessageUtils.ok("Gateway removed successfully"), response, request);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) SmsGatewayConfig(org.hisp.dhis.sms.config.SmsGatewayConfig) BulkSmsGatewayConfig(org.hisp.dhis.sms.config.BulkSmsGatewayConfig) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 65 with ID

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

the class TrackedEntityInstanceController method updateTrackedEntityInstanceJson.

@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')")
public void updateTrackedEntityInstanceJson(@PathVariable String id, ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    ImportSummary importSummary = trackedEntityInstanceService.updateTrackedEntityInstanceJson(id, inputStream, importOptions);
    importSummary.setImportOptions(importOptions);
    webMessageService.send(WebMessageUtils.importSummary(importSummary), response, request);
}
Also used : InputStream(java.io.InputStream) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)41 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)39 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)26 InputStream (java.io.InputStream)24 User (org.hisp.dhis.user.User)21 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)18 IOException (java.io.IOException)17 Event (org.hisp.dhis.dxf2.events.event.Event)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)15 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)13 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)12 GetMapping (org.springframework.web.bind.annotation.GetMapping)12 ArrayList (java.util.ArrayList)11 List (java.util.List)11 DataElement (org.hisp.dhis.dataelement.DataElement)10 Test (org.junit.jupiter.api.Test)10 TrackedEntityInstanceParams (org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams)9 Lists (com.google.common.collect.Lists)8 Collectors (java.util.stream.Collectors)8 HashMap (java.util.HashMap)7