use of org.hisp.dhis.webapi.webdomain.FormProgram 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);
}
Aggregations