Search in sources :

Example 31 with ID

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

the class TrackedEntityInstanceSupportService method getTrackedEntityInstance.

@SneakyThrows
public TrackedEntityInstance getTrackedEntityInstance(String id, String pr, List<String> fields) {
    User user = currentUserService.getCurrentUser();
    TrackedEntityInstanceParams trackedEntityInstanceParams = getTrackedEntityInstanceParams(fields);
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(id, trackedEntityInstanceParams);
    if (trackedEntityInstance == null) {
        throw new NotFoundException("TrackedEntityInstance", id);
    }
    if (pr != null) {
        Program program = programService.getProgram(pr);
        if (program == null) {
            throw new NotFoundException("Program", pr);
        }
        List<String> errors = trackerAccessManager.canRead(user, instanceService.getTrackedEntityInstance(trackedEntityInstance.getTrackedEntityInstance()), program, false);
        if (!errors.isEmpty()) {
            if (program.getAccessLevel() == AccessLevel.CLOSED) {
                throw new WebMessageException(unauthorized(TrackerOwnershipManager.PROGRAM_ACCESS_CLOSED));
            }
            throw new WebMessageException(unauthorized(TrackerOwnershipManager.OWNERSHIP_ACCESS_DENIED));
        }
        if (trackedEntityInstanceParams.isIncludeProgramOwners()) {
            List<ProgramOwner> filteredProgramOwners = trackedEntityInstance.getProgramOwners().stream().filter(tei -> tei.getProgram().equals(pr)).collect(Collectors.toList());
            trackedEntityInstance.setProgramOwners(filteredProgramOwners);
        }
    } else {
        // return only tracked entity type attributes
        TrackedEntityType trackedEntityType = trackedEntityTypeService.getTrackedEntityType(trackedEntityInstance.getTrackedEntityType());
        if (trackedEntityType != null) {
            List<String> tetAttributes = trackedEntityType.getTrackedEntityAttributes().stream().map(TrackedEntityAttribute::getUid).collect(Collectors.toList());
            trackedEntityInstance.setAttributes(trackedEntityInstance.getAttributes().stream().filter(att -> tetAttributes.contains(att.getAttribute())).collect(Collectors.toList()));
        }
    }
    return trackedEntityInstance;
}
Also used : ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) SneakyThrows(lombok.SneakyThrows) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) Collectors(java.util.stream.Collectors) Program(org.hisp.dhis.program.Program) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) List(java.util.List) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Service(org.springframework.stereotype.Service) ProgramOwner(org.hisp.dhis.dxf2.events.trackedentity.ProgramOwner) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) ProgramService(org.hisp.dhis.program.ProgramService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) WebMessageUtils.unauthorized(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.unauthorized) AccessLevel(org.hisp.dhis.common.AccessLevel) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) Joiner(com.google.common.base.Joiner) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) SneakyThrows(lombok.SneakyThrows)

Example 32 with ID

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

the class DataAnalysisController method getValidationRuleExpressionDetials.

@GetMapping("validationRulesExpression")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ValidationRuleExpressionDetails getValidationRuleExpressionDetials(@RequestParam String validationRuleId, @RequestParam String periodId, @RequestParam String organisationUnitId, @RequestParam(required = false) String attributeOptionComboId) throws WebMessageException {
    ValidationRule validationRule = validationRuleService.getValidationRule(validationRuleId);
    if (validationRule == null) {
        throw new WebMessageException(notFound("Can't find ValidationRule with id =" + validationRuleId));
    }
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
    if (organisationUnit == null) {
        throw new WebMessageException(notFound("Can't find OrganisationUnit with id =" + organisationUnitId));
    }
    Period period = periodService.getPeriod(periodId);
    if (period == null) {
        throw new WebMessageException(notFound("Can't find Period with id =" + periodId));
    }
    CategoryOptionCombo attributeOptionCombo;
    if (attributeOptionComboId == null) {
        attributeOptionCombo = categoryService.getDefaultCategoryOptionCombo();
    } else {
        attributeOptionCombo = categoryService.getCategoryOptionCombo(attributeOptionComboId);
        if (attributeOptionCombo == null) {
            throw new WebMessageException(notFound("Can't find AttributeOptionCombo with id = " + attributeOptionComboId));
        }
    }
    ValidationAnalysisParams params = validationService.newParamsBuilder(Lists.newArrayList(validationRule), organisationUnit, Lists.newArrayList(period)).withAttributeOptionCombo(attributeOptionCombo).build();
    return validationService.getValidationRuleExpressionDetails(params);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Period(org.hisp.dhis.period.Period) ValidationAnalysisParams(org.hisp.dhis.validation.ValidationAnalysisParams) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) ValidationRule(org.hisp.dhis.validation.ValidationRule) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 33 with ID

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

the class MessageConversationController method postObject.

private WebMessage postObject(HttpServletRequest request, MessageConversation messageConversation) throws WebMessageException {
    Set<User> users = Sets.newHashSet(messageConversation.getUsers());
    messageConversation.getUsers().clear();
    messageConversation.getUsers().addAll(getUsersToMessageConversation(messageConversation, users));
    if (messageConversation.getUsers().isEmpty()) {
        throw new WebMessageException(conflict("No recipients selected."));
    }
    String metaData = MessageService.META_USER_AGENT + request.getHeader(ContextUtils.HEADER_USER_AGENT);
    Set<FileResource> attachments = new HashSet<>();
    for (FileResource fr : messageConversation.getAttachments()) {
        FileResource fileResource = fileResourceService.getFileResource(fr.getUid());
        if (fileResource == null) {
            throw new WebMessageException(conflict("Attachment '" + fr.getUid() + "' not found."));
        }
        if (!fileResource.getDomain().equals(FileResourceDomain.MESSAGE_ATTACHMENT) || fileResource.isAssigned()) {
            throw new WebMessageException(conflict("Attachment '" + fr.getUid() + "' is already used or not a valid attachment."));
        }
        fileResource.setAssigned(true);
        fileResourceService.updateFileResource(fileResource);
        attachments.add(fileResource);
    }
    long id = messageService.sendPrivateMessage(messageConversation.getUsers(), messageConversation.getSubject(), messageConversation.getText(), metaData, attachments);
    org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(id);
    return created("Message conversation created").setLocation(MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid());
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) HashSet(java.util.HashSet)

Example 34 with ID

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

the class MessageConversationController method getAttachment.

@GetMapping("/{mcUid}/{msgUid}/attachments/{fileUid}")
public void getAttachment(@PathVariable(value = "mcUid") String mcUid, @PathVariable(value = "msgUid") String msgUid, @PathVariable(value = "fileUid") String fileUid, @CurrentUser User currentUser, HttpServletResponse response) throws WebMessageException {
    Message message = getMessage(mcUid, msgUid, currentUser);
    FileResource fr = fileResourceService.getFileResource(fileUid);
    if (message == null) {
        throw new WebMessageException(notFound("No message found with id '" + msgUid + "' for message conversation with id '" + mcUid + "'"));
    }
    boolean attachmentExists = message.getAttachments().stream().filter(att -> att.getUid().equals(fileUid)).count() == 1;
    if (fr == null || !attachmentExists) {
        throw new WebMessageException(notFound("No messageattachment found with id '" + fileUid + "'"));
    }
    if (!fr.getDomain().equals(FileResourceDomain.MESSAGE_ATTACHMENT)) {
        throw new WebMessageException(conflict("Invalid messageattachment."));
    }
    fileResourceUtils.configureFileResourceResponse(response, fr);
}
Also used : UserMessage(org.hisp.dhis.message.UserMessage) Message(org.hisp.dhis.message.Message) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 35 with ID

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

the class RelationshipController method updateRelationshipXml.

@PutMapping(path = "/{id}", consumes = APPLICATION_XML_VALUE, produces = APPLICATION_XML_VALUE)
@ResponseBody
public WebMessage updateRelationshipXml(@PathVariable String id, ImportOptions importOptions, HttpServletRequest request) throws IOException {
    Relationship relationship = relationshipService.getRelationshipByUid(id);
    if (relationship == null) {
        return notFound("No relationship with id '" + id + "' was found.");
    }
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    ImportSummary importSummary = relationshipService.updateRelationshipXml(id, inputStream, importOptions);
    importSummary.setImportOptions(importOptions);
    return importSummary(importSummary).withPlainResponseBefore(DhisApiVersion.V38);
}
Also used : InputStream(java.io.InputStream) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Relationship(org.hisp.dhis.dxf2.events.trackedentity.Relationship) PutMapping(org.springframework.web.bind.annotation.PutMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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