use of org.hisp.dhis.webapi.webdomain.form.Form in project dhis2-core by dhis2.
the class DataSetController method getFormXml.
@RequestMapping(value = "/{uid}/form", method = RequestMethod.GET, produces = { "application/xml", "text/xml" })
public void getFormXml(@PathVariable("uid") String uid, @RequestParam(value = "ou", required = false) String orgUnit, @RequestParam(value = "pe", required = false) String period, @RequestParam(value = "catOpts", required = false) String categoryOptions, @RequestParam(required = false) boolean metaData, TranslateParams translateParams, HttpServletResponse response) throws IOException, WebMessageException {
setUserContext(translateParams);
List<DataSet> dataSets = getEntity(uid, NO_WEB_OPTIONS);
if (dataSets.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound("DataSet not found for uid: " + uid));
}
OrganisationUnit ou = manager.get(OrganisationUnit.class, orgUnit);
if (ou == null) {
throw new WebMessageException(WebMessageUtils.notFound("Organisation unit does not exist: " + orgUnit));
}
Period pe = PeriodType.getPeriodFromIsoString(period);
Form form = getForm(dataSets, ou, pe, categoryOptions, metaData);
renderService.toXml(response.getOutputStream(), form);
}
use of org.hisp.dhis.webapi.webdomain.form.Form in project dhis2-core by dhis2.
the class DataSetController method getFormJson.
@RequestMapping(value = "/{uid}/form", method = RequestMethod.GET, produces = "application/json")
public void getFormJson(@PathVariable("uid") String uid, @RequestParam(value = "ou", required = false) String orgUnit, @RequestParam(value = "pe", required = false) String period, @RequestParam(value = "categoryOptions", required = false) String categoryOptions, @RequestParam(required = false) boolean metaData, TranslateParams translateParams, HttpServletResponse response) throws IOException, WebMessageException {
setUserContext(translateParams);
List<DataSet> dataSets = getEntity(uid, NO_WEB_OPTIONS);
if (dataSets.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound("DataSet not found for uid: " + uid));
}
OrganisationUnit ou = manager.get(OrganisationUnit.class, orgUnit);
if (ou == null) {
throw new WebMessageException(WebMessageUtils.notFound("Organisation unit does not exist: " + orgUnit));
}
Period pe = PeriodType.getPeriodFromIsoString(period);
Form form = getForm(dataSets, ou, pe, categoryOptions, metaData);
renderService.toJson(response.getOutputStream(), form);
}
use of org.hisp.dhis.webapi.webdomain.form.Form in project dhis2-core by dhis2.
the class DataSetController method getForm.
private Form getForm(List<DataSet> dataSets, OrganisationUnit ou, Period pe, String categoryOptions, boolean metaData) throws IOException {
DataSet dataSet = dataSets.get(0);
Form form = FormUtils.fromDataSet(dataSets.get(0), metaData, null);
Set<String> options = null;
if (StringUtils.isNotEmpty(categoryOptions) && categoryOptions.startsWith("[") && categoryOptions.endsWith("]")) {
String[] split = categoryOptions.substring(1, categoryOptions.length() - 1).split(",");
options = new HashSet<>(Lists.newArrayList(split));
}
if (ou != null && pe != null) {
List<DataValue> dataValues;
if (options != null && !options.isEmpty()) {
DataElementCategoryOptionCombo attrOptionCombo = inputUtils.getAttributeOptionCombo(dataSet.getCategoryCombo(), options, IdScheme.UID);
dataValues = dataValueService.getDataValues(ou, pe, dataSets.get(0).getDataElements(), attrOptionCombo);
} else {
dataValues = dataValueService.getDataValues(new DataExportParams().setDataElements(dataSets.get(0).getDataElements()).setPeriods(Sets.newHashSet(pe)).setOrganisationUnits(Sets.newHashSet(ou)));
}
FormUtils.fillWithDataValues(form, dataValues);
}
return form;
}
use of org.hisp.dhis.webapi.webdomain.form.Form in project dhis2-core by dhis2.
the class FormUtils method fromDataSet.
public static Form fromDataSet(DataSet dataSet, boolean metaData, Set<OrganisationUnit> userOrganisationUnits) {
Form form = new Form();
form.setLabel(dataSet.getDisplayName());
form.setSubtitle(dataSet.getDisplayShortName());
form.getOptions().put(KEY_PERIOD_TYPE, dataSet.getPeriodType().getName());
form.getOptions().put(KEY_OPEN_FUTURE_PERIODS, dataSet.getOpenFuturePeriods());
form.getOptions().put(KEY_EXPIRY_DAYS, dataSet.getExpiryDays());
form.setCategoryCombo(getCategoryCombo(dataSet, userOrganisationUnits));
if (dataSet.hasSections()) {
List<Section> sections = new ArrayList<>(dataSet.getSections());
Collections.sort(sections, SectionOrderComparator.INSTANCE);
for (Section section : sections) {
List<Field> fields = inputFromDataElements(new ArrayList<>(section.getDataElements()), new ArrayList<>(section.getGreyedFields()));
Group group = new Group();
group.setLabel(section.getDisplayName());
group.setDescription(section.getDescription());
group.setDataElementCount(section.getDataElements().size());
group.setFields(fields);
if (metaData) {
group.getMetaData().put(KEY_DATA_ELEMENTS, NameableObjectUtils.getAsNameableObjects(section.getDataElements()));
group.getMetaData().put(KEY_INDICATORS, NameableObjectUtils.getAsNameableObjects(section.getIndicators()));
}
form.getGroups().add(group);
}
} else {
List<Field> fields = inputFromDataElements(new ArrayList<>(dataSet.getDataElements()));
Group group = new Group();
group.setLabel(DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME);
group.setDescription(DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME);
group.setDataElementCount(dataSet.getDataElements().size());
group.setFields(fields);
if (metaData) {
group.getMetaData().put(KEY_DATA_ELEMENTS, NameableObjectUtils.getAsNameableObjects(new ArrayList<>(dataSet.getDataElements())));
}
form.getGroups().add(group);
}
return form;
}
use of org.hisp.dhis.webapi.webdomain.form.Form in project dhis2-core by dhis2.
the class FormUtils method fromProgram.
public static Form fromProgram(Program program) {
Assert.notNull(program, "Program cannot be null");
Form form = new Form();
form.setLabel(program.getDisplayName());
if (!StringUtils.isEmpty(program.getDescription())) {
form.getOptions().put("description", program.getDescription());
}
if (!StringUtils.isEmpty(program.getEnrollmentDateLabel())) {
form.getOptions().put("dateOfEnrollmentDescription", program.getEnrollmentDateLabel());
}
if (!StringUtils.isEmpty(program.getIncidentDateLabel())) {
form.getOptions().put("dateOfIncidentDescription", program.getIncidentDateLabel());
}
form.getOptions().put("type", program.getProgramType().getValue());
ProgramStage programStage = program.getProgramStageByStage(1);
if (programStage == null) {
if (program.isWithoutRegistration()) {
throw new IllegalStateException("Program is without registration");
} else {
return form;
}
}
form.getOptions().put("captureCoordinates", programStage.getCaptureCoordinates());
if (programStage.getProgramStageSections().size() > 0) {
for (ProgramStageSection section : programStage.getProgramStageSections()) {
List<Field> fields = inputFromDataElements(section.getDataElements());
Group group = new Group();
group.setLabel(section.getDisplayName());
group.setDataElementCount(section.getDataElements().size());
group.setFields(fields);
form.getGroups().add(group);
}
} else {
List<Field> fields = inputFromProgramStageDataElements(new ArrayList<>(programStage.getProgramStageDataElements()));
Group group = new Group();
group.setLabel("default");
group.setFields(fields);
group.setDataElementCount(programStage.getProgramStageDataElements().size());
form.getGroups().add(group);
}
return form;
}
Aggregations