Search in sources :

Example 1 with FormOrganisationUnit

use of org.hisp.dhis.webapi.webdomain.FormOrganisationUnit in project dhis2-core by dhis2.

the class CurrentUserController method getDataSets.

@RequestMapping(value = { "/assignedDataSets", "/dataSets" }, produces = { "application/json", "text/*" })
public void getDataSets(@RequestParam(defaultValue = "false") boolean optionSets, @RequestParam(defaultValue = "50") int maxOptions, HttpServletResponse response, @RequestParam Map<String, String> parameters) throws IOException, NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    Forms forms = new Forms();
    Set<OrganisationUnit> organisationUnits = new HashSet<>();
    Set<DataSet> userDataSets;
    Set<OrganisationUnit> userOrganisationUnits = new HashSet<>(currentUser.getOrganisationUnits());
    if (currentUser.getUserCredentials().getAllAuthorities().contains("ALL")) {
        userDataSets = new HashSet<>(dataSetService.getAllDataSets());
        if (userOrganisationUnits.isEmpty()) {
            userOrganisationUnits = new HashSet<>(organisationUnitService.getRootOrganisationUnits());
        }
    } else {
        userDataSets = currentUser.getUserCredentials().getAllDataSets();
    }
    if (parameters.containsKey("includeDescendants") && Boolean.parseBoolean(parameters.get("includeDescendants"))) {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnitService.getOrganisationUnitWithChildren(organisationUnit.getUid()));
        }
        userOrganisationUnits.addAll(children);
    } else {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnit.getChildren());
        }
        userOrganisationUnits.addAll(children);
    }
    for (OrganisationUnit ou : userOrganisationUnits) {
        Set<DataSet> dataSets = new HashSet<>(Sets.intersection(ou.getDataSets(), userDataSets));
        if (dataSets.size() > 0) {
            organisationUnits.add(ou);
        }
    }
    for (OrganisationUnit organisationUnit : organisationUnits) {
        FormOrganisationUnit formOrganisationUnit = new FormOrganisationUnit();
        formOrganisationUnit.setId(organisationUnit.getUid());
        formOrganisationUnit.setLabel(organisationUnit.getDisplayName());
        formOrganisationUnit.setLevel(organisationUnit.getLevel());
        if (organisationUnit.getParent() != null) {
            formOrganisationUnit.setParent(organisationUnit.getParent().getUid());
        }
        Set<DataSet> dataSets = new HashSet<>(Sets.intersection(organisationUnit.getDataSets(), userDataSets));
        for (DataSet dataSet : dataSets) {
            String uid = dataSet.getUid();
            FormDataSet formDataSet = new FormDataSet();
            formDataSet.setId(uid);
            formDataSet.setLabel(dataSet.getDisplayName());
            dataSet.getCategoryCombo().getCategories().forEach(cat -> {
                cat.setAccess(aclService.getAccess(cat, currentUser));
                cat.getCategoryOptions().forEach(catOpts -> catOpts.setAccess(aclService.getAccess(catOpts, currentUser)));
            });
            forms.getForms().put(uid, FormUtils.fromDataSet(dataSet, false, userOrganisationUnits));
            formOrganisationUnit.getDataSets().add(formDataSet);
            if (optionSets) {
                for (DataElement dataElement : dataSet.getDataElements()) {
                    if (dataElement.hasOptionSet()) {
                        int size = maxOptions;
                        if (size >= dataElement.getOptionSet().getOptions().size()) {
                            size = dataElement.getOptionSet().getOptions().size();
                        }
                        forms.getOptionSets().put(dataElement.getOptionSet().getUid(), dataElement.getOptionSet().getOptionValues().subList(0, size - 1));
                    }
                }
            }
        }
        forms.getOrganisationUnits().put(formOrganisationUnit.getId(), formOrganisationUnit);
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), forms);
}
Also used : Forms(org.hisp.dhis.webapi.webdomain.Forms) FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) FormDataSet(org.hisp.dhis.webapi.webdomain.FormDataSet) ArrayList(java.util.ArrayList) FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) FormDataSet(org.hisp.dhis.webapi.webdomain.FormDataSet) DataElement(org.hisp.dhis.dataelement.DataElement) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with FormOrganisationUnit

use of org.hisp.dhis.webapi.webdomain.FormOrganisationUnit in project dhis2-core by dhis2.

the class CurrentUserController method getPrograms.

@RequestMapping(value = { "/assignedPrograms", "/programs" }, produces = { "application/json", "text/*" })
public void getPrograms(HttpServletResponse response, @RequestParam Map<String, String> parameters, @RequestParam(required = false) String type) throws IOException, NotAuthenticatedException {
    User currentUser = currentUserService.getCurrentUser();
    if (currentUser == null) {
        throw new NotAuthenticatedException();
    }
    Set<OrganisationUnit> userOrganisationUnits = new HashSet<>();
    Set<OrganisationUnit> organisationUnits = new HashSet<>();
    Set<Program> programs = new HashSet<>();
    Map<String, List<Program>> programAssociations = new HashMap<>();
    Set<Program> userPrograms;
    if (type == null) {
        userPrograms = programService.getUserPrograms();
    } else {
        userPrograms = programService.getUserPrograms(ProgramType.fromValue(type));
    }
    if (currentUserService.currentUserIsSuper() && currentUser.getOrganisationUnits().isEmpty()) {
        userOrganisationUnits.addAll(organisationUnitService.getRootOrganisationUnits());
    } else {
        userOrganisationUnits.addAll(currentUser.getOrganisationUnits());
    }
    if (parameters.containsKey("includeDescendants") && Boolean.parseBoolean(parameters.get("includeDescendants"))) {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnitService.getOrganisationUnitWithChildren(organisationUnit.getUid()));
        }
        userOrganisationUnits.addAll(children);
    } else {
        List<OrganisationUnit> children = new ArrayList<>();
        for (OrganisationUnit organisationUnit : userOrganisationUnits) {
            children.addAll(organisationUnit.getChildren());
        }
        userOrganisationUnits.addAll(children);
    }
    for (OrganisationUnit organisationUnit : userOrganisationUnits) {
        List<Program> ouPrograms = new ArrayList<>(programService.getPrograms(organisationUnit));
        if (!ouPrograms.isEmpty()) {
            for (Program program : ouPrograms) {
                if (userPrograms.contains(program)) {
                    organisationUnits.add(organisationUnit);
                    programs.add(program);
                    programAssociations.putIfAbsent(organisationUnit.getUid(), new ArrayList<>());
                    programAssociations.get(organisationUnit.getUid()).add(program);
                }
            }
        }
    }
    Forms forms = new Forms();
    for (OrganisationUnit organisationUnit : organisationUnits) {
        FormOrganisationUnit formOrganisationUnit = new FormOrganisationUnit();
        formOrganisationUnit.setId(organisationUnit.getUid());
        formOrganisationUnit.setLabel(organisationUnit.getDisplayName());
        formOrganisationUnit.setLevel(organisationUnit.getLevel());
        if (organisationUnit.getParent() != null) {
            formOrganisationUnit.setParent(organisationUnit.getParent().getUid());
        }
        for (Program program : programAssociations.get(organisationUnit.getUid())) {
            FormProgram formProgram = new FormProgram();
            formProgram.setId(program.getUid());
            formProgram.setLabel(program.getDisplayName());
            formOrganisationUnit.getPrograms().add(formProgram);
        }
        forms.getOrganisationUnits().put(formOrganisationUnit.getId(), formOrganisationUnit);
    }
    for (Program program : programs) {
        forms.getForms().put(program.getUid(), FormUtils.fromProgram(program));
    }
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), forms);
}
Also used : FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Forms(org.hisp.dhis.webapi.webdomain.Forms) FormProgram(org.hisp.dhis.webapi.webdomain.FormProgram) User(org.hisp.dhis.user.User) FormProgram(org.hisp.dhis.webapi.webdomain.FormProgram) Program(org.hisp.dhis.program.Program) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FormOrganisationUnit(org.hisp.dhis.webapi.webdomain.FormOrganisationUnit) NotAuthenticatedException(org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 User (org.hisp.dhis.user.User)2 NotAuthenticatedException (org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException)2 FormOrganisationUnit (org.hisp.dhis.webapi.webdomain.FormOrganisationUnit)2 Forms (org.hisp.dhis.webapi.webdomain.Forms)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 HashMap (java.util.HashMap)1 List (java.util.List)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 DataSet (org.hisp.dhis.dataset.DataSet)1 Program (org.hisp.dhis.program.Program)1 FormDataSet (org.hisp.dhis.webapi.webdomain.FormDataSet)1 FormProgram (org.hisp.dhis.webapi.webdomain.FormProgram)1