Search in sources :

Example 41 with WebMessageUtils.conflict

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

the class FileResourceController method saveFileResource.

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public WebMessage saveFileResource(@RequestParam MultipartFile file) throws WebMessageException, IOException {
    String filename = StringUtils.defaultIfBlank(FilenameUtils.getName(file.getOriginalFilename()), DEFAULT_FILENAME);
    String contentType = file.getContentType();
    contentType = isValidContentType(contentType) ? contentType : DEFAULT_CONTENT_TYPE;
    long contentLength = file.getSize();
    if (contentLength <= 0) {
        throw new WebMessageException(WebMessageUtils.conflict("Could not read file or file is empty."));
    }
    ByteSource bytes = new MultipartFileByteSource(file);
    String contentMd5 = bytes.hash(Hashing.md5()).toString();
    FileResource fileResource = new FileResource(filename, contentType, contentLength, contentMd5, FileResourceDomain.DATA_VALUE);
    fileResource.setAssigned(false);
    fileResource.setCreated(new Date());
    fileResource.setUser(currentUserService.getCurrentUser());
    File tmpFile = toTempFile(file);
    String uid = fileResourceService.saveFileResource(fileResource, tmpFile);
    if (uid == null) {
        throw new WebMessageException(WebMessageUtils.error("Saving the file failed."));
    }
    WebMessage webMessage = new WebMessage(Status.OK, HttpStatus.ACCEPTED);
    webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
    return webMessage;
}
Also used : FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) FileResource(org.hisp.dhis.fileresource.FileResource) ByteSource(com.google.common.io.ByteSource) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Date(java.util.Date) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 42 with WebMessageUtils.conflict

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

the class InterpretationController method writeChartInterpretation.

@RequestMapping(value = "/chart/{uid}", method = RequestMethod.POST, consumes = { "text/html", "text/plain" })
public void writeChartInterpretation(@PathVariable("uid") String uid, @RequestParam(value = "ou", required = false) String orgUnitUid, @RequestBody String text, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    Chart chart = idObjectManager.get(Chart.class, uid);
    if (chart == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Chart does not exist or is not accessible: " + uid));
    }
    OrganisationUnit orgUnit = getUserOrganisationUnit(orgUnitUid, chart, currentUserService.getCurrentUser());
    createIntepretation(new Interpretation(chart, orgUnit, text), request, response);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Interpretation(org.hisp.dhis.interpretation.Interpretation) EventChart(org.hisp.dhis.eventchart.EventChart) Chart(org.hisp.dhis.chart.Chart) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with WebMessageUtils.conflict

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

the class InterpretationController method like.

// -------------------------------------------------------------------------
// Likes
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}/like", method = RequestMethod.POST)
public void like(@PathVariable("uid") String uid, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    Interpretation interpretation = interpretationService.getInterpretation(uid);
    if (interpretation == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Interpretation does not exist: " + uid));
    }
    boolean like = interpretationService.likeInterpretation(interpretation.getId());
    if (like) {
        webMessageService.send(WebMessageUtils.created("Like added to interpretation"), response, request);
    } else {
        webMessageService.send(WebMessageUtils.conflict("Could not add like, user had already liked interpretation"), response, request);
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Interpretation(org.hisp.dhis.interpretation.Interpretation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 44 with WebMessageUtils.conflict

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

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

the class InterpretationController method updateComment.

@RequestMapping(value = "/{uid}/comments/{cuid}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateComment(@PathVariable("uid") String uid, @PathVariable("cuid") String cuid, HttpServletResponse response, @RequestBody String content) throws WebMessageException {
    Interpretation interpretation = interpretationService.getInterpretation(uid);
    if (interpretation == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Interpretation does not exist: " + uid));
    }
    for (InterpretationComment comment : interpretation.getComments()) {
        if (comment.getUid().equals(cuid)) {
            if (!currentUserService.getCurrentUser().equals(comment.getUser()) && !currentUserService.currentUserIsSuper()) {
                throw new AccessDeniedException("You are not allowed to update this comment.");
            }
            comment.setText(content);
        }
    }
    interpretationService.updateInterpretation(interpretation);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InterpretationComment(org.hisp.dhis.interpretation.InterpretationComment) Interpretation(org.hisp.dhis.interpretation.Interpretation) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)51 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)44 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)20 Period (org.hisp.dhis.period.Period)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)11 DataSet (org.hisp.dhis.dataset.DataSet)10 Interpretation (org.hisp.dhis.interpretation.Interpretation)10 ArrayList (java.util.ArrayList)9 User (org.hisp.dhis.user.User)9 Date (java.util.Date)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)7 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 Serializable (java.io.Serializable)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 DataValue (org.hisp.dhis.datavalue.DataValue)4 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)4 ByteSource (com.google.common.io.ByteSource)3