use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.
the class LoadFormAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
dataSet = dataSetService.getDataSet(dataSetId);
if (dataSet == null) {
return INPUT;
}
FormType formType = dataSet.getFormType();
if (formType.isCustom() && dataSet.hasDataEntryForm()) {
dataEntryForm = dataSet.getDataEntryForm();
customDataEntryFormCode = dataEntryFormService.prepareDataEntryFormForEntry(dataEntryForm, dataSet, i18n);
return formType.toString();
}
// ---------------------------------------------------------------------
// Section / default form
// ---------------------------------------------------------------------
List<DataElement> dataElements = new ArrayList<>(dataSet.getDataElements());
if (dataElements.isEmpty()) {
return INPUT;
}
Collections.sort(dataElements);
orderedDataElements = ListMap.getListMap(dataElements, de -> de.getCategoryCombo(dataSet));
orderedCategoryCombos = getDataElementCategoryCombos(dataElements, dataSet);
for (DataElementCategoryCombo categoryCombo : orderedCategoryCombos) {
List<DataElementCategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
orderedCategoryOptionCombos.put(categoryCombo.getId(), optionCombos);
// -----------------------------------------------------------------
// Perform ordering of categories and their options so that they
// could be displayed as in the paper form. Note that the total
// number of entry cells to be generated are the multiple of options
// from each category.
// -----------------------------------------------------------------
numberOfTotalColumns.put(categoryCombo.getId(), optionCombos.size());
orderedCategories.put(categoryCombo.getId(), categoryCombo.getCategories());
Map<Integer, List<DataElementCategoryOption>> optionsMap = new HashMap<>();
for (DataElementCategory category : categoryCombo.getCategories()) {
optionsMap.put(category.getId(), category.getCategoryOptions());
}
orderedOptionsMap.put(categoryCombo.getId(), optionsMap);
// -----------------------------------------------------------------
// Calculating the number of times each category should be repeated
// -----------------------------------------------------------------
Map<Integer, Integer> catRepeat = new HashMap<>();
Map<Integer, Collection<Integer>> colRepeat = new HashMap<>();
int catColSpan = optionCombos.size();
for (DataElementCategory cat : categoryCombo.getCategories()) {
int categoryOptionSize = cat.getCategoryOptions().size();
if (categoryOptionSize > 0 && catColSpan >= categoryOptionSize) {
catColSpan = catColSpan / categoryOptionSize;
int total = optionCombos.size() / (catColSpan * categoryOptionSize);
Collection<Integer> cols = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
cols.add(i);
}
colRepeat.put(cat.getId(), cols);
catRepeat.put(cat.getId(), catColSpan);
}
}
catColRepeat.put(categoryCombo.getId(), colRepeat);
}
// ---------------------------------------------------------------------
// Get data entry form
// ---------------------------------------------------------------------
DataSet dsOriginal = dataSet;
if (dataSet.getFormType().isDefault()) {
DataSet dataSetCopy = new DataSet();
dataSetCopy.setUid(dataSet.getUid());
dataSetCopy.setName(dataSet.getName());
dataSetCopy.setShortName(dataSet.getShortName());
dataSetCopy.setRenderAsTabs(dataSet.isRenderAsTabs());
dataSetCopy.setRenderHorizontally(dataSet.isRenderHorizontally());
dataSetCopy.setDataElementDecoration(dataSet.isDataElementDecoration());
dataSet = dataSetCopy;
for (int i = 0; i < orderedCategoryCombos.size(); i++) {
DataElementCategoryCombo categoryCombo = orderedCategoryCombos.get(i);
String name = !categoryCombo.isDefault() ? categoryCombo.getName() : dataSetCopy.getName();
Section section = new Section();
section.setUid(CodeGenerator.generateUid());
section.setId(i);
section.setName(name);
section.setSortOrder(i);
section.setDataSet(dataSetCopy);
dataSetCopy.getSections().add(section);
section.getDataElements().addAll(orderedDataElements.get(categoryCombo));
section.setIndicators(new ArrayList<>(dataSet.getIndicators()));
}
formType = FormType.SECTION;
}
if (CodeGenerator.isValidUid(multiOrganisationUnit)) {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(multiOrganisationUnit);
List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
for (OrganisationUnit child : organisationUnit.getChildren()) {
if (child.getDataSets().contains(dsOriginal)) {
organisationUnitChildren.add(child);
}
}
Collections.sort(organisationUnitChildren);
organisationUnits.addAll(organisationUnitChildren);
getSectionForm(dataElements, dataSet);
formType = FormType.SECTION_MULTIORG;
}
getSectionForm(dataElements, dataSet);
return formType.toString();
}
use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.
the class EventQueryParamsTest method testGetDuplicateQueryItems.
@Test
public void testGetDuplicateQueryItems() {
QueryItem iA = new QueryItem(createDataElement('A', new DataElementCategoryCombo()));
QueryItem iB = new QueryItem(createDataElement('B', new DataElementCategoryCombo()));
QueryItem iC = new QueryItem(createDataElement('B', new DataElementCategoryCombo()));
QueryItem iD = new QueryItem(createDataElement('D', new DataElementCategoryCombo()));
EventQueryParams params = new EventQueryParams.Builder().addItem(iA).addItem(iB).addItem(iC).addItem(iD).build();
List<QueryItem> duplicates = params.getDuplicateQueryItems();
assertEquals(1, duplicates.size());
assertTrue(duplicates.contains(iC));
}
use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.
the class DefaultCompleteDataSetRegistrationExchangeService method validateAocMatchesDataSetCc.
private static void validateAocMatchesDataSetCc(MetaDataProperties mdProps) throws ImportConflictException {
// TODO MdCache?
DataElementCategoryCombo aocCC = mdProps.attrOptCombo.getCategoryCombo();
DataElementCategoryCombo dsCc = mdProps.dataSet.getCategoryCombo();
if (!aocCC.equals(dsCc)) {
throw new ImportConflictException(new ImportConflict(aocCC.getUid(), String.format("Attribute option combo: %s must have category combo: %s", aocCC.getUid(), dsCc.getUid())));
}
}
use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.
the class DefaultAdxDataService method writeDataValueSet.
@Override
public void writeDataValueSet(DataExportParams params, OutputStream out) throws AdxException {
dataValueSetService.decideAccess(params);
dataValueSetService.validate(params);
XMLWriter adxWriter = XMLFactory.getXMLWriter(out);
adxWriter.openElement(AdxDataService.ROOT);
adxWriter.writeAttribute("xmlns", AdxDataService.NAMESPACE);
for (DataSet dataSet : params.getDataSets()) {
AdxDataSetMetadata metadata = new AdxDataSetMetadata(dataSet);
DataElementCategoryCombo categoryCombo = dataSet.getCategoryCombo();
for (DataElementCategoryOptionCombo aoc : categoryCombo.getOptionCombos()) {
Map<String, String> attributeDimensions = metadata.getExplodedCategoryAttributes(aoc.getId());
for (OrganisationUnit orgUnit : params.getOrganisationUnits()) {
for (Period period : params.getPeriods()) {
adxWriter.openElement(AdxDataService.GROUP);
adxWriter.writeAttribute(AdxDataService.DATASET, dataSet.getCode());
adxWriter.writeAttribute(AdxDataService.PERIOD, AdxPeriod.serialize(period));
adxWriter.writeAttribute(AdxDataService.ORGUNIT, orgUnit.getCode());
for (String attribute : attributeDimensions.keySet()) {
adxWriter.writeAttribute(attribute, attributeDimensions.get(attribute));
}
for (DataValue dv : dataValueService.getDataValues(orgUnit, period, dataSet.getDataElements(), aoc)) {
adxWriter.openElement(AdxDataService.DATAVALUE);
adxWriter.writeAttribute(AdxDataService.DATAELEMENT, dv.getDataElement().getCode());
DataElementCategoryOptionCombo coc = dv.getCategoryOptionCombo();
Map<String, String> categoryDimensions = metadata.getExplodedCategoryAttributes(coc.getId());
for (String attribute : categoryDimensions.keySet()) {
adxWriter.writeAttribute(attribute, categoryDimensions.get(attribute));
}
if (dv.getDataElement().getValueType().isNumeric()) {
adxWriter.writeAttribute(AdxDataService.VALUE, dv.getValue());
} else {
adxWriter.writeAttribute(AdxDataService.VALUE, "0");
adxWriter.openElement(AdxDataService.ANNOTATION);
adxWriter.writeCharacters(dv.getValue());
// ANNOTATION
adxWriter.closeElement();
}
// DATAVALUE
adxWriter.closeElement();
}
// GROUP
adxWriter.closeElement();
}
}
}
}
// ADX
adxWriter.closeElement();
adxWriter.closeWriter();
}
use of org.hisp.dhis.dataelement.DataElementCategoryCombo in project dhis2-core by dhis2.
the class DefaultAdxDataService method parseAdxGroupToDxf.
// -------------------------------------------------------------------------
// Utility methods
// -------------------------------------------------------------------------
private List<ImportConflict> parseAdxGroupToDxf(XMLReader adxReader, XMLStreamWriter dxfWriter, ImportOptions importOptions, CachingMap<String, DataSet> dataSetMap, IdentifiableObjectCallable<DataSet> dataSetCallable, CachingMap<String, DataElement> dataElementMap, IdentifiableObjectCallable<DataElement> dataElementCallable) throws XMLStreamException, AdxException {
List<ImportConflict> adxConflicts = new LinkedList<>();
IdentifiableProperty dataElementIdScheme = importOptions.getIdSchemes().getDataElementIdScheme().getIdentifiableProperty();
Map<String, String> groupAttributes = adxReader.readAttributes();
if (!groupAttributes.containsKey(AdxDataService.PERIOD)) {
throw new AdxException(AdxDataService.PERIOD + " attribute is required on 'group'");
}
if (!groupAttributes.containsKey(AdxDataService.ORGUNIT)) {
throw new AdxException(AdxDataService.ORGUNIT + " attribute is required on 'group'");
}
// translate ADX period to DXF
String periodStr = groupAttributes.get(AdxDataService.PERIOD);
groupAttributes.remove(AdxDataService.PERIOD);
Period period = AdxPeriod.parse(periodStr);
groupAttributes.put(AdxDataService.PERIOD, period.getIsoDate());
// process ADX group attributes
if (!groupAttributes.containsKey(AdxDataService.ATTOPTCOMBO) && groupAttributes.containsKey(AdxDataService.DATASET)) {
log.debug("No attribute option combo present, check data set for attribute category combo");
String dataSetStr = trimToNull(groupAttributes.get(AdxDataService.DATASET));
final DataSet dataSet = dataSetMap.get(dataSetStr, dataSetCallable.setId(dataSetStr));
if (dataSet == null) {
throw new AdxException("No data set matching identifier: " + groupAttributes.get(AdxDataService.DATASET));
}
groupAttributes.put(AdxDataService.DATASET, dataSet.getUid());
DataElementCategoryCombo attributeCombo = dataSet.getCategoryCombo();
convertAttributesToDxf(groupAttributes, AdxDataService.ATTOPTCOMBO, attributeCombo, dataElementIdScheme);
}
// process the dataValues
while (adxReader.moveToStartElement(AdxDataService.DATAVALUE, AdxDataService.GROUP)) {
try {
parseADXDataValueToDxf(adxReader, dxfWriter, groupAttributes, importOptions, dataElementMap, dataElementCallable);
} catch (AdxException ex) {
adxConflicts.add(ex.getImportConflict());
log.info("ADX data value conflict: " + ex.getImportConflict());
}
}
return adxConflicts;
}
Aggregations