use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.
the class ObjectBundleServiceTest method testCreateDataSetWithSectionsAndGreyedFields.
@Test
public void testCreateDataSetWithSectionsAndGreyedFields() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections_gf.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.COMMIT);
params.setImportStrategy(ImportStrategy.CREATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertTrue(validate.getErrorReports().isEmpty());
objectBundleService.commit(bundle);
List<DataSet> dataSets = manager.getAll(DataSet.class);
List<Section> sections = manager.getAll(Section.class);
List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
List<DataElement> dataElements = manager.getAll(DataElement.class);
List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
List<User> users = manager.getAll(User.class);
List<DataElementOperand> dataElementOperands = manager.getAll(DataElementOperand.class);
List<TrackedEntity> trackedEntities = manager.getAll(TrackedEntity.class);
List<OrganisationUnitLevel> organisationUnitLevels = manager.getAll(OrganisationUnitLevel.class);
assertFalse(organisationUnits.isEmpty());
assertEquals(1, organisationUnitLevels.size());
assertEquals(1, trackedEntities.size());
assertFalse(dataElements.isEmpty());
assertFalse(users.isEmpty());
assertFalse(userRoles.isEmpty());
assertEquals(1, dataSets.size());
assertEquals(2, sections.size());
assertEquals(1, dataElementOperands.size());
DataSet dataSet = dataSets.get(0);
assertEquals(2, dataSet.getSections().size());
Section section1 = sections.get(0);
Section section2 = sections.get(1);
assertEquals(1, section1.getDataElements().size());
assertEquals(1, section2.getDataElements().size());
assertNotNull(section1.getDataSet());
assertNotNull(section2.getDataSet());
Section section = manager.get(Section.class, "C50M0WxaI7y");
assertNotNull(section.getDataSet());
assertEquals(1, section.getCategoryCombos().size());
assertEquals(1, section.getGreyedFields().size());
DataElementCategoryCombo categoryCombo = manager.get(DataElementCategoryCombo.class, "faV8QvLgIwB");
assertNotNull(categoryCombo);
DataElementCategory category = manager.get(DataElementCategory.class, "XJGLlMAMCcn");
assertNotNull(category);
DataElementCategoryOption categoryOption1 = manager.get(DataElementCategoryOption.class, "JYiFOMKa25J");
DataElementCategoryOption categoryOption2 = manager.get(DataElementCategoryOption.class, "tdaMRD34m8o");
assertNotNull(categoryOption1);
assertNotNull(categoryOption2);
}
use of org.hisp.dhis.dataelement.DataElementCategory 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.DataElementCategory in project dhis2-core by dhis2.
the class DefaultAdxDataService method getCodeCategoryMap.
private Map<String, DataElementCategory> getCodeCategoryMap(DataElementCategoryCombo categoryCombo) throws AdxException {
Map<String, DataElementCategory> categoryMap = new HashMap<>();
List<DataElementCategory> categories = categoryCombo.getCategories();
for (DataElementCategory category : categories) {
String categoryCode = category.getCode();
if (categoryCode == null || !XMLChar.isValidName(categoryCode)) {
throw new AdxException("Category code for " + category.getName() + " is missing or invalid: " + categoryCode);
}
categoryMap.put(category.getCode(), category);
}
return categoryMap;
}
use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.
the class DefaultAdxDataService method getCatOptComboFromAttributes.
private DataElementCategoryOptionCombo getCatOptComboFromAttributes(Map<String, String> attributes, DataElementCategoryCombo catcombo, IdentifiableProperty scheme) throws AdxException {
CategoryComboMap catcomboMap;
try {
catcomboMap = new CategoryComboMap(catcombo, scheme);
} catch (CategoryComboMapException ex) {
log.info("Failed to create category combo map from: " + catcombo);
throw new AdxException(ex.getMessage());
}
String compositeIdentifier = StringUtils.EMPTY;
for (DataElementCategory category : catcomboMap.getCategories()) {
String categoryCode = category.getCode();
if (categoryCode == null) {
throw new AdxException("No category matching: " + categoryCode);
}
String catAttribute = attributes.get(categoryCode);
if (catAttribute == null) {
throw new AdxException("Missing required attribute from category combo: " + categoryCode);
}
compositeIdentifier += "\"" + catAttribute + "\"";
}
DataElementCategoryOptionCombo catOptionCombo = catcomboMap.getCategoryOptionCombo(compositeIdentifier);
if (catOptionCombo == null) {
throw new AdxException("Invalid attributes:" + attributes);
}
return catOptionCombo;
}
use of org.hisp.dhis.dataelement.DataElementCategory in project dhis2-core by dhis2.
the class DhisConvenienceTest method createDataElementCategory.
/**
* @param categoryUniqueIdentifier A unique character to identify the
* category.
* @param categoryOptions the category options.
* @return DataElementCategory
*/
public static DataElementCategory createDataElementCategory(char categoryUniqueIdentifier, DataElementCategoryOption... categoryOptions) {
DataElementCategory dataElementCategory = new DataElementCategory("DataElementCategory" + categoryUniqueIdentifier, DataDimensionType.DISAGGREGATION);
dataElementCategory.setAutoFields();
for (DataElementCategoryOption categoryOption : categoryOptions) {
dataElementCategory.addCategoryOption(categoryOption);
}
return dataElementCategory;
}
Aggregations