Search in sources :

Example 6 with ApiVersion

use of org.hisp.dhis.webapi.mvc.annotation.ApiVersion in project dhis2-core by dhis2.

the class CompleteDataSetRegistrationController method saveCompleteDataSetRegistration.

@ApiVersion({ DhisApiVersion.V23, DhisApiVersion.V24, DhisApiVersion.V25 })
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", value = MULTIPLE_SAVE_RESOURCE_PATH)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void saveCompleteDataSetRegistration(@RequestBody CompleteDataSetRegistrationRequests completeDataSetRegistrationRequests, HttpServletResponse response) throws WebMessageException {
    List<CompleteDataSetRegistration> registrations = new ArrayList<>();
    for (CompleteDataSetRegistrationRequest completeDataSetRegistrationRequest : completeDataSetRegistrationRequests) {
        String ds = completeDataSetRegistrationRequest.getDs();
        DataSet dataSet = dataSetService.getDataSet(ds);
        if (dataSet == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Illegal data set identifier: " + ds));
        }
        String pe = completeDataSetRegistrationRequest.getPe();
        Period period = PeriodType.getPeriodFromIsoString(pe);
        if (period == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Illegal period identifier: " + pe));
        }
        String ou = completeDataSetRegistrationRequest.getOu();
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou);
        if (organisationUnit == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Illegal organisation unit identifier: " + ou));
        }
        String cc = completeDataSetRegistrationRequest.getCc();
        String cp = completeDataSetRegistrationRequest.getCp();
        DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(cc, cp, false);
        if (attributeOptionCombo == null) {
            return;
        }
        // ---------------------------------------------------------------------
        // Check locked status
        // ---------------------------------------------------------------------
        boolean multiOu = completeDataSetRegistrationRequest.isMultiOu();
        if (dataSetService.isLocked(dataSet, period, organisationUnit, attributeOptionCombo, null, multiOu)) {
            throw new WebMessageException(WebMessageUtils.conflict("Data set is locked: " + ds));
        }
        // ---------------------------------------------------------------------
        // Register as completed data set
        // ---------------------------------------------------------------------
        String sb = completeDataSetRegistrationRequest.getSb();
        String storedBy = (sb == null) ? currentUserService.getCurrentUsername() : sb;
        Date cd = completeDataSetRegistrationRequest.getCd();
        Date completionDate = (cd == null) ? new Date() : cd;
        Set<OrganisationUnit> orgUnits = new HashSet<>();
        orgUnits.add(organisationUnit);
        if (multiOu) {
            orgUnits.addAll(organisationUnit.getChildren());
        }
        addRegistrationsForOrgUnits(registrations, orgUnits, 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) CompleteDataSetRegistrationRequest(org.hisp.dhis.datacompletion.CompleteDataSetRegistrationRequest) Date(java.util.Date) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) HashSet(java.util.HashSet) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with ApiVersion

use of org.hisp.dhis.webapi.mvc.annotation.ApiVersion in project dhis2-core by dhis2.

the class CustomRequestMappingHandlerMapping method getMappingForMethod.

@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
    RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
    if (info == null) {
        return null;
    }
    ApiVersion typeApiVersion = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
    ApiVersion methodApiVersion = AnnotationUtils.findAnnotation(method, ApiVersion.class);
    if (typeApiVersion == null && methodApiVersion == null) {
        return info;
    }
    RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
    if (methodsCondition.getMethods().isEmpty()) {
        methodsCondition = new RequestMethodsRequestCondition(RequestMethod.GET);
    }
    Set<String> rqmPatterns = info.getPatternsCondition().getPatterns();
    Set<String> patterns = new HashSet<>();
    Set<DhisApiVersion> versions = getVersions(typeApiVersion, methodApiVersion);
    for (String pattern : rqmPatterns) {
        versions.stream().filter(version -> !version.isIgnore()).forEach(version -> {
            if (!pattern.startsWith(version.getVersionString())) {
                if (pattern.startsWith("/")) {
                    patterns.add("/" + version.getVersion() + pattern);
                } else {
                    patterns.add("/" + version.getVersion() + "/" + pattern);
                }
            } else {
                patterns.add(pattern);
            }
        });
    }
    PatternsRequestCondition patternsRequestCondition = new PatternsRequestCondition(patterns.toArray(new String[] {}), null, null, true, true, null);
    return new RequestMappingInfo(null, patternsRequestCondition, methodsCondition, info.getParamsCondition(), info.getHeadersCondition(), info.getConsumesCondition(), info.getProducesCondition(), info.getCustomCondition());
}
Also used : DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) HashSet(java.util.HashSet) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) RequestMethodsRequestCondition(org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) Arrays(java.util.Arrays) PatternsRequestCondition(org.springframework.web.servlet.mvc.condition.PatternsRequestCondition) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) Method(java.lang.reflect.Method) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) PatternsRequestCondition(org.springframework.web.servlet.mvc.condition.PatternsRequestCondition) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) RequestMethodsRequestCondition(org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition) HashSet(java.util.HashSet)

Aggregations

DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)7 ApiVersion (org.hisp.dhis.webapi.mvc.annotation.ApiVersion)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ArrayList (java.util.ArrayList)3 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)3 DataSet (org.hisp.dhis.dataset.DataSet)3 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Period (org.hisp.dhis.period.Period)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 CompleteDataSetRegistrationRequest (org.hisp.dhis.datacompletion.CompleteDataSetRegistrationRequest)1 DataEntryForm (org.hisp.dhis.dataentryform.DataEntryForm)1