Search in sources :

Example 11 with WebMessageUtils.conflict

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

the class DataApprovalController method getDataApprovalList.

private List<DataApproval> getDataApprovalList(Approvals approvals) throws WebMessageException {
    List<DataSet> dataSets = objectManager.getByUid(DataSet.class, approvals.getDs());
    List<Period> periods = PeriodType.getPeriodsFromIsoStrings(approvals.getPe());
    periods = periodService.reloadPeriods(periods);
    User user = currentUserService.getCurrentUser();
    DataElementCategoryOptionCombo defaultOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    Date date = new Date();
    // Avoid duplicates when different data sets have the same work flow
    Set<DataApproval> set = new HashSet<>();
    for (DataSet dataSet : dataSets) {
        if (dataSet.getWorkflow() == null) {
            throw new WebMessageException(WebMessageUtils.conflict("DataSet has no approval workflow: " + dataSet.getName()));
        }
        Set<DataElementCategoryOptionCombo> dataSetOptionCombos = dataSet.getCategoryCombo() != null ? dataSet.getCategoryCombo().getOptionCombos() : null;
        for (Approval approval : approvals.getApprovals()) {
            OrganisationUnit unit = organisationUnitService.getOrganisationUnit(approval.getOu());
            DataElementCategoryOptionCombo optionCombo = categoryService.getDataElementCategoryOptionCombo(approval.getAoc());
            optionCombo = ObjectUtils.firstNonNull(optionCombo, defaultOptionCombo);
            for (Period period : periods) {
                if (dataSetOptionCombos != null && dataSetOptionCombos.contains(optionCombo)) {
                    DataApproval dataApproval = new DataApproval(null, dataSet.getWorkflow(), period, unit, optionCombo, false, date, user);
                    set.add(dataApproval);
                }
            }
        }
    }
    return new ArrayList<>(set);
}
Also used : DataApproval(org.hisp.dhis.dataapproval.DataApproval) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) Date(java.util.Date) Approval(org.hisp.dhis.webapi.webdomain.approval.Approval) DataApproval(org.hisp.dhis.dataapproval.DataApproval) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) HashSet(java.util.HashSet)

Example 12 with WebMessageUtils.conflict

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

the class DataSetController method getVersion.

@RequestMapping(value = "/{uid}/version", method = RequestMethod.GET)
public void getVersion(@PathVariable("uid") String uid, @RequestParam Map<String, String> parameters, HttpServletResponse response) throws Exception {
    DataSet dataSet = manager.get(DataSet.class, uid);
    if (dataSet == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Data set does not exist: " + uid));
    }
    Map<String, Integer> versionMap = new HashMap<>();
    versionMap.put("version", dataSet.getVersion());
    renderService.toJson(response.getOutputStream(), versionMap);
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) HashMap(java.util.HashMap) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with WebMessageUtils.conflict

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

the class DataApprovalController method removeApproval.

// -------------------------------------------------------------------------
// Delete
// -------------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_APPROVE_DATA') or hasRole('F_APPROVE_DATA_LOWER_LEVELS')")
@RequestMapping(value = APPROVALS_PATH, method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void removeApproval(@RequestParam Set<String> ds, @RequestParam String pe, @RequestParam String ou, HttpServletResponse response) throws WebMessageException {
    Set<DataSet> dataSets = parseDataSetsWithWorkflow(ds);
    if (dataSets.size() != ds.size()) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal data set identifier in this list: " + ds));
    }
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrgUnit(ou);
    DataApprovalLevel dataApprovalLevel = getAndValidateApprovalLevel(organisationUnit);
    User user = currentUserService.getCurrentUser();
    List<DataApproval> dataApprovalList = newArrayList();
    for (DataSet dataSet : dataSets) {
        dataApprovalList.addAll(getApprovalsAsList(dataApprovalLevel, dataSet.getWorkflow(), period, organisationUnit, false, new Date(), user));
    }
    dataApprovalService.unapproveData(dataApprovalList);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataApprovalLevel(org.hisp.dhis.dataapproval.DataApprovalLevel) DataApproval(org.hisp.dhis.dataapproval.DataApproval) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Period(org.hisp.dhis.period.Period) Date(java.util.Date) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with WebMessageUtils.conflict

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

the class CompleteDataSetRegistrationController method registerCompleteDataSet.

private CompleteDataSetRegistration registerCompleteDataSet(DataSet dataSet, Period period, OrganisationUnit orgUnit, DataElementCategoryOptionCombo attributeOptionCombo, String storedBy, Date completionDate) throws WebMessageException {
    I18nFormat format = i18nManager.getI18nFormat();
    if (dataSet == null) {
        throw new WebMessageException(WebMessageUtils.conflict("dataSet can not be null."));
    }
    if (period == null) {
        throw new WebMessageException(WebMessageUtils.conflict("period can not be null"));
    }
    if (orgUnit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("organisationUnit can not be null"));
    }
    if (attributeOptionCombo == null) {
        throw new WebMessageException(WebMessageUtils.conflict("attributeOptionCombo can not be null"));
    }
    CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, orgUnit, attributeOptionCombo);
    if (registration == null) {
        registration = new CompleteDataSetRegistration();
        registration.setDataSet(dataSet);
        registration.setPeriod(period);
        registration.setSource(orgUnit);
        registration.setAttributeOptionCombo(attributeOptionCombo);
        registration.setDate(completionDate != null ? completionDate : new Date());
        registration.setStoredBy(storedBy != null ? storedBy : currentUserService.getCurrentUsername());
        registration.setPeriodName(format.formatPeriod(registration.getPeriod()));
        registrationService.saveCompleteDataSetRegistration(registration);
    } else {
        registration.setDate(completionDate != null ? completionDate : new Date());
        registration.setStoredBy(storedBy != null ? storedBy : currentUserService.getCurrentUsername());
        registration.setPeriodName(format.formatPeriod(registration.getPeriod()));
        registrationService.updateCompleteDataSetRegistration(registration);
    }
    return registration;
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) I18nFormat(org.hisp.dhis.i18n.I18nFormat) Date(java.util.Date)

Example 15 with WebMessageUtils.conflict

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

the class CompleteDataSetRegistrationController method saveCompleteDataSetRegistration.

// Legacy (<= V25)
@ApiVersion({ DhisApiVersion.V23, DhisApiVersion.V24, DhisApiVersion.V25 })
@RequestMapping(method = RequestMethod.POST, produces = "text/plain")
public void saveCompleteDataSetRegistration(@RequestParam String ds, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam(required = false) Date cd, @RequestParam(required = false) String sb, @RequestParam(required = false) boolean multiOu, HttpServletResponse response) throws WebMessageException {
    DataSet dataSet = dataSetService.getDataSet(ds);
    if (dataSet == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal data set identifier: " + ds));
    }
    Period period = PeriodType.getPeriodFromIsoString(pe);
    if (period == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal period identifier: " + pe));
    }
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou);
    if (organisationUnit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal organisation unit identifier: " + ou));
    }
    DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(cc, cp, false);
    if (attributeOptionCombo == null) {
        return;
    }
    if (dataSetService.isLocked(dataSet, period, organisationUnit, attributeOptionCombo, null, multiOu)) {
        throw new WebMessageException(WebMessageUtils.conflict("Data set is locked: " + ds));
    }
    // ---------------------------------------------------------------------
    // Register as completed data set
    // ---------------------------------------------------------------------
    Set<OrganisationUnit> children = organisationUnit.getChildren();
    String storedBy = (sb == null) ? currentUserService.getCurrentUsername() : sb;
    Date completionDate = (cd == null) ? new Date() : cd;
    List<CompleteDataSetRegistration> registrations = new ArrayList<>();
    if (!multiOu) {
        CompleteDataSetRegistration completeDataSetRegistration = registerCompleteDataSet(dataSet, period, organisationUnit, attributeOptionCombo, storedBy, completionDate);
        if (completeDataSetRegistration != null) {
            registrations.add(completeDataSetRegistration);
        }
    } else {
        addRegistrationsForOrgUnits(registrations, Sets.union(children, Sets.newHashSet(organisationUnit)), dataSet, period, attributeOptionCombo, storedBy, completionDate);
    }
    registrationService.saveCompleteDataSetRegistrations(registrations, true);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) 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