use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.
the class CategoryOptionComboSupplier method getCategoryOptionCombo.
private CategoryOptionCombo getCategoryOptionCombo(Program program, Event event, ProgramStageInstance psi, IdScheme idScheme) {
final CategoryCombo programCatCombo = program.getCategoryCombo();
String aoc = event.getAttributeOptionCombo();
String attributeCatOptions = event.getAttributeCategoryOptions();
/*
* Event create request contain aoc information in payload but for event
* update aoc should be fetched from ProgramStageInstance
*/
if (psi != null && aoc == null) {
aoc = psi.getAttributeOptionCombo().getUid();
}
CategoryOptionCombo categoryOptionCombo;
if (isNotEmpty(aoc)) {
categoryOptionCombo = attributeOptionComboLoader.getCategoryOptionCombo(idScheme, aoc);
if (categoryOptionCombo == null) {
categoryOptionCombo = attributeOptionComboLoader.getDefault();
}
} else if (programCatCombo != null && isNotEmpty(attributeCatOptions)) {
categoryOptionCombo = attributeOptionComboLoader.getAttributeOptionCombo(programCatCombo, attributeCatOptions, aoc, idScheme);
} else {
categoryOptionCombo = attributeOptionComboLoader.getDefault();
}
return categoryOptionCombo;
}
use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.
the class ProgramSupplier method loadPrograms.
private Map<String, Program> loadPrograms(IdSchemes idSchemes) {
// Get Id scheme for programs, programs should support also the
// attribute scheme based on JSONB
IdScheme idScheme = idSchemes.getProgramIdScheme();
String sqlSelect = "select p.programid as id, p.uid, p.code, p.name, p.sharing as program_sharing, " + "p.type, p.opendaysaftercoenddate, tet.trackedentitytypeid, tet.sharing as tet_sharing, " + "tet.uid as tet_uid, c.categorycomboid as catcombo_id, " + "c.uid as catcombo_uid, c.name as catcombo_name, " + "c.code as catcombo_code, ps.programstageid as ps_id, ps.uid as ps_uid, " + "ps.code as ps_code, ps.name as ps_name, " + "ps.featuretype as ps_feature_type, ps.sort_order, ps.sharing as ps_sharing, " + "ps.repeatable as ps_repeatable, ps.enableuserassignment, ps.validationstrategy";
if (idScheme.isAttribute()) {
sqlSelect += ",p.attributevalues->'" + idScheme.getAttribute() + "'->>'value' as " + ATTRIBUTESCHEME_COL;
}
final String sql = sqlSelect + " from program p " + "LEFT JOIN categorycombo c on p.categorycomboid = c.categorycomboid " + "LEFT JOIN trackedentitytype tet on p.trackedentitytypeid = tet.trackedentitytypeid " + "LEFT JOIN programstage ps on p.programid = ps.programid " + "LEFT JOIN program_organisationunits pou on p.programid = pou.programid " + "LEFT JOIN organisationunit ou on pou.organisationunitid = ou.organisationunitid " + "group by p.programid, tet.trackedentitytypeid, c.categorycomboid, ps.programstageid, ps.sort_order " + "order by p.programid, ps.sort_order";
return jdbcTemplate.query(sql, (ResultSet rs) -> {
Map<String, Program> results = new HashMap<>();
long programId = 0;
while (rs.next()) {
if (programId != rs.getLong("id")) {
Set<ProgramStage> programStages = new HashSet<>();
Program program = new Program();
program.setId(rs.getLong("id"));
program.setUid(rs.getString("uid"));
program.setName(rs.getString("name"));
program.setCode(rs.getString("code"));
program.setOpenDaysAfterCoEndDate(rs.getInt("opendaysaftercoenddate"));
program.setProgramType(ProgramType.fromValue(rs.getString("type")));
program.setSharing(toSharing(rs.getString("program_sharing")));
// Program Stage without a Program Id
if (rs.getLong("ps_id") != 0) {
programStages.add(toProgramStage(rs));
}
CategoryCombo categoryCombo = new CategoryCombo();
categoryCombo.setId(rs.getLong("catcombo_id"));
categoryCombo.setUid(rs.getString("catcombo_uid"));
categoryCombo.setName(rs.getString("catcombo_name"));
categoryCombo.setCode(rs.getString("catcombo_code"));
program.setCategoryCombo(categoryCombo);
long tetId = rs.getLong(TRACKED_ENTITY_TYPE_ID);
if (tetId != 0) {
TrackedEntityType trackedEntityType = new TrackedEntityType();
trackedEntityType.setId(tetId);
trackedEntityType.setUid(rs.getString("tet_uid"));
trackedEntityType.setSharing(toSharing(rs.getString("tet_sharing")));
program.setTrackedEntityType(trackedEntityType);
}
program.setProgramStages(programStages);
results.put(getProgramKey(idScheme, rs), program);
programId = program.getId();
} else {
results.get(getProgramKey(idScheme, rs)).getProgramStages().add(toProgramStage(rs));
}
}
return results;
});
}
use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.
the class DefaultDataSetMetadataExportService method getDataSetMetadata.
// TODO add lock exceptions
// TODO add validation caching (ETag and If-None-Match).
@Override
public ObjectNode getDataSetMetadata() {
User user = currentUserService.getCurrentUser();
List<DataSet> dataSets = idObjectManager.getDataWriteAll(DataSet.class);
Set<DataElement> dataElements = flatMapToSet(dataSets, DataSet::getDataElements);
Set<Indicator> indicators = flatMapToSet(dataSets, DataSet::getIndicators);
Set<CategoryCombo> dataElementCategoryCombos = flatMapToSet(dataElements, DataElement::getCategoryCombos);
Set<CategoryCombo> dataSetCategoryCombos = mapToSet(dataSets, DataSet::getCategoryCombo);
Set<Category> dataElementCategories = flatMapToSet(dataElementCategoryCombos, CategoryCombo::getCategories);
Set<Category> dataSetCategories = flatMapToSet(dataSetCategoryCombos, CategoryCombo::getCategories);
Set<CategoryOption> categoryOptions = getCategoryOptions(dataElementCategories, dataSetCategories, user);
expressionService.substituteIndicatorExpressions(indicators);
ObjectNode rootNode = fieldFilterService.createObjectNode();
rootNode.putArray(DataSetSchemaDescriptor.PLURAL).addAll(asObjectNodes(dataSets, FIELDS_DATA_SETS, DataSet.class));
rootNode.putArray(DataElementSchemaDescriptor.PLURAL).addAll(asObjectNodes(dataElements, FIELDS_DATA_ELEMENTS, DataElement.class));
rootNode.putArray(IndicatorSchemaDescriptor.PLURAL).addAll(asObjectNodes(indicators, FIELDS_INDICATORS, Indicator.class));
rootNode.putArray(CategoryComboSchemaDescriptor.PLURAL).addAll(asObjectNodes(dataElementCategoryCombos, FIELDS_DATAELEMENT_CAT_COMBOS, CategoryCombo.class)).addAll(asObjectNodes(dataSetCategoryCombos, FIELDS_DATA_SET_CAT_COMBOS, CategoryCombo.class));
rootNode.putArray(CategorySchemaDescriptor.PLURAL).addAll(asObjectNodes(dataElementCategories, FIELDS_CATEGORIES, Category.class)).addAll(asObjectNodes(dataSetCategories, FIELDS_CATEGORIES, Category.class));
rootNode.putArray(CategoryOptionSchemaDescriptor.PLURAL).addAll(asObjectNodes(categoryOptions, FIELDS_CATEGORIES, CategoryOption.class));
return rootNode;
}
use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.
the class InputUtils method getAttributeOptionComboInternal.
private CategoryOptionCombo getAttributeOptionComboInternal(String cc, String cp, boolean skipFallback) {
Set<String> opts = TextUtils.splitToArray(cp, TextUtils.SEMICOLON);
if ((cc == null && opts != null || (cc != null && opts == null))) {
throw new IllegalQueryException("Both or none of category combination and category options must be present");
}
CategoryCombo categoryCombo = null;
if (cc != null && (categoryCombo = idObjectManager.get(CategoryCombo.class, cc)) == null) {
throw new IllegalQueryException("Illegal category combo identifier: " + cc);
}
if (categoryCombo == null) {
if (skipFallback) {
return null;
}
categoryCombo = categoryService.getDefaultCategoryCombo();
}
return getAttributeOptionCombo(categoryCombo, opts, null, IdScheme.UID);
}
use of org.hisp.dhis.category.CategoryCombo in project dhis2-core by dhis2.
the class DefaultCompleteDataSetRegistrationExchangeServiceTest method verifyUserHasNoWritePermissionOnCategoryOption.
@Test
void verifyUserHasNoWritePermissionOnCategoryOption() {
OrganisationUnit organisationUnit = createOrganisationUnit('A');
DataSet dataSetA = createDataSet('A', new MonthlyPeriodType());
CategoryCombo categoryCombo = createCategoryCombo('A');
CategoryOption categoryOptionA = createCategoryOption('A');
CategoryOption categoryOptionB = createCategoryOption('B');
CategoryOptionCombo categoryOptionCombo = createCategoryOptionCombo(categoryCombo, categoryOptionA, categoryOptionB);
Period period = createPeriod("201907");
String payload = createPayload(period, organisationUnit, dataSetA, categoryCombo, categoryOptionA, categoryOptionB);
try (MockedConstruction<MetadataCaches> mocked = mockConstruction(MetadataCaches.class, (mock, context) -> {
when(mock.getDataSets()).thenReturn(datasetCache);
when(mock.getPeriods()).thenReturn(periodCache);
when(mock.getOrgUnits()).thenReturn(orgUnitCache);
aocCache = new CachingMap<>();
when(mock.getAttrOptionCombos()).thenReturn(aocCache);
when(mock.getOrgUnitInHierarchyMap()).thenReturn(orgUnitInHierarchyCache);
when(mock.getAttrOptComboOrgUnitMap()).thenReturn(attrOptComboOrgUnitCache);
})) {
when(currentUserService.getCurrentUser()).thenReturn(user);
when(batchHandler.init()).thenReturn(batchHandler);
when(idObjManager.get(CategoryCombo.class, categoryCombo.getUid())).thenReturn(categoryCombo);
when(idObjManager.getObject(CategoryOption.class, IdScheme.UID, categoryOptionA.getUid())).thenReturn(categoryOptionA);
when(idObjManager.getObject(CategoryOption.class, IdScheme.UID, categoryOptionB.getUid())).thenReturn(categoryOptionB);
when(categoryService.getCategoryOptionCombo(categoryCombo, Sets.newHashSet(categoryOptionA, categoryOptionB))).thenReturn(categoryOptionCombo);
when(datasetCache.get(eq(dataSetA.getUid()), any())).thenReturn(dataSetA);
when(periodCache.get(eq(period.getIsoDate()), any())).thenReturn(period);
when(orgUnitCache.get(eq(organisationUnit.getUid()), any())).thenReturn(createOrganisationUnit('A'));
when(orgUnitInHierarchyCache.get(eq(organisationUnit.getUid()), any())).thenReturn(Boolean.TRUE);
when(attrOptComboOrgUnitCache.get(eq(categoryOptionCombo.getUid() + organisationUnit.getUid()), any())).thenReturn(Boolean.TRUE);
when(categoryService.getCategoryOptionCombo(categoryOptionCombo.getUid())).thenReturn(categoryOptionCombo);
// force error on access check for Category Option Combo
when(aclService.canDataWrite(user, dataSetA)).thenReturn(true);
when(aclService.canDataWrite(user, categoryOptionA)).thenReturn(false);
when(aclService.canDataWrite(user, categoryOptionB)).thenReturn(true);
when(notifier.clear(null)).thenReturn(notifier);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS)).thenReturn(false);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ATTRIBUTE_OPTION_COMBOS)).thenReturn(false);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS)).thenReturn(false);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_REQUIRE_ATTRIBUTE_OPTION_COMBO)).thenReturn(false);
when(currentUserService.getCurrentUserOrganisationUnits()).thenReturn(Collections.singleton(createOrganisationUnit('A')));
when(i18nManager.getI18n()).thenReturn(i18n);
when(categoryService.getDefaultCategoryOptionCombo()).thenReturn(DEFAULT_COC);
when(batchHandlerFactory.createBatchHandler(CompleteDataSetRegistrationBatchHandler.class)).thenReturn(batchHandler);
when(notifier.notify(any(), anyString())).thenReturn(notifier);
when(notifier.notify(null, NotificationLevel.INFO, "Import done", true)).thenReturn(notifier);
// call method under test
ImportSummary summary = subject.saveCompleteDataSetRegistrationsJson(new ByteArrayInputStream(payload.getBytes()), new ImportOptions());
assertThat(summary.getStatus(), is(ImportStatus.ERROR));
assertThat(summary.getImportCount().getIgnored(), is(1));
assertEquals(1, summary.getConflictCount());
assertThat(summary.getConflicts().iterator().next().getValue(), is("User has no data write access for CategoryOption: " + categoryOptionA.getUid()));
}
}
Aggregations