use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class MarkForFollowupAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() {
DataElement dataElement = dataElementService.getDataElement(dataElementId);
Period period = periodService.getPeriod(periodId);
OrganisationUnit source = organisationUnitService.getOrganisationUnit(sourceId);
DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDataElementCategoryOptionCombo(categoryOptionComboId);
DataValue dataValue = dataValueService.getDataValue(dataElement, period, source, categoryOptionCombo);
if (dataValue != null) {
boolean isMarked = dataValue.isFollowup();
dataValue.setFollowup(!isMarked);
dataValueService.updateDataValue(dataValue);
message = !isMarked ? "marked" : "unmarked";
log.info(!isMarked ? "Data value marked for follow-up" : "Data value unmarked for follow-up");
}
return SUCCESS;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class ExpressionUpgrader method upgradeExpression.
private String upgradeExpression(String expression) {
if (expression == null || expression.trim().isEmpty()) {
return null;
}
boolean changes = false;
StringBuffer sb = new StringBuffer();
try {
// -----------------------------------------------------------------
// Constants
// -----------------------------------------------------------------
Matcher matcher = OLD_CONSTANT_PATTERN.matcher(expression);
while (matcher.find()) {
Constant constant = constantService.getConstant(Integer.parseInt(matcher.group(1)));
String replacement = "C{" + constant.getUid() + "}";
matcher.appendReplacement(sb, replacement);
changes = true;
}
matcher.appendTail(sb);
expression = sb.toString();
// -----------------------------------------------------------------
// Operands
// -----------------------------------------------------------------
matcher = OLD_OPERAND_PATTERN.matcher(expression);
sb = new StringBuffer();
while (matcher.find()) {
DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
String replacement = "#{" + de.getUid();
if (matcher.groupCount() == 2 && matcher.group(2) != null && !matcher.group(2).trim().isEmpty()) {
DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
replacement += "." + coc.getUid();
}
replacement += "}";
matcher.appendReplacement(sb, replacement);
changes = true;
}
matcher.appendTail(sb);
expression = sb.toString();
} catch (Exception ex) {
log.error("Failed to upgrade expression: " + expression);
// Log and continue
log.error(ex);
}
if (changes) {
log.info("Upgraded expression: " + expression);
}
return changes ? expression : null;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class ExpressionUpgrader method upgradeDataEntryForms.
private void upgradeDataEntryForms() {
Collection<DataEntryForm> forms = dataEntryFormService.getAllDataEntryForms();
for (DataEntryForm form : forms) {
if (DataEntryForm.CURRENT_FORMAT > form.getFormat() && form.getHtmlCode() != null && !form.getHtmlCode().trim().isEmpty()) {
try {
// ---------------------------------------------------------
// Identifiers
// ---------------------------------------------------------
Matcher matcher = IDENTIFIER_PATTERN.matcher(form.getHtmlCode());
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
String replacement = "id=\"" + de.getUid() + "-" + coc.getUid() + "-val\"";
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
form.setHtmlCode(sb.toString());
// ---------------------------------------------------------
// Data element totals
// ---------------------------------------------------------
matcher = DATAELEMENT_TOTAL_PATTERN.matcher(form.getHtmlCode());
sb = new StringBuffer();
while (matcher.find()) {
DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
String replacement = "dataelementid=\"" + de.getUid() + "\"";
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
form.setHtmlCode(sb.toString());
// ---------------------------------------------------------
// Indicators
// ---------------------------------------------------------
matcher = INDICATOR_PATTERN.matcher(form.getHtmlCode());
sb = new StringBuffer();
while (matcher.find()) {
Indicator in = indicatorService.getIndicator(Integer.parseInt(matcher.group(1)));
String replacement = "indicatorid=\"" + in.getUid() + "\"";
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
form.setHtmlCode(sb.toString());
// ---------------------------------------------------------
// Update format and save
// ---------------------------------------------------------
form.setFormat(DataEntryForm.CURRENT_FORMAT);
dataEntryFormService.updateDataEntryForm(form);
log.info("Upgraded custom data entry form: " + form.getName());
} catch (Exception ex) {
log.error("Upgrading custom data entry form failed: " + form.getName());
// Log and continue
log.error(ex);
}
}
}
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DefaultDataQueryService method getDimension.
// TODO optimize so that org unit levels + boundary are used in query instead of fetching all org units one by one
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
final boolean allItems = items.isEmpty();
if (DATA_X_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
for (String uid : items) {
if (uid.startsWith(KEY_DE_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
}
} else if (uid.startsWith(KEY_IN_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
}
} else {
DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
if (dimItemObject != null) {
dataDimensionItems.add(dimItemObject);
}
}
}
if (dataDimensionItems.isEmpty()) {
throw new IllegalQueryException("Dimension dx is present in query without any valid dimension options");
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems);
return object;
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> cocs = new ArrayList<>();
for (String uid : items) {
DataElementCategoryOptionCombo coc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
if (coc != null) {
cocs.add(coc);
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, cocs);
return object;
} else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> aocs = new ArrayList<>();
for (String uid : items) {
DataElementCategoryOptionCombo aoc = idObjectManager.getObject(DataElementCategoryOptionCombo.class, inputIdScheme, uid);
if (aoc != null) {
aocs.add(aoc);
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, aocs);
return object;
} else if (PERIOD_DIM_ID.equals(dimension)) {
Calendar calendar = PeriodType.getCalendar();
List<Period> periods = new ArrayList<>();
for (String isoPeriod : items) {
if (RelativePeriodEnum.contains(isoPeriod)) {
RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriod);
List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true);
periods.addAll(relativePeriods);
} else {
Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
if (period != null) {
periods.add(period);
}
}
}
// Remove duplicates
periods = periods.stream().distinct().collect(Collectors.toList());
if (periods.isEmpty() && !allowAllPeriodItems) {
throw new IllegalQueryException("Dimension pe is present in query without any valid dimension options");
}
for (Period period : periods) {
String name = format != null ? format.formatPeriod(period) : null;
period.setName(name);
period.setShortName(name);
if (!calendar.isIso8601()) {
period.setUid(getLocalPeriodIdentifier(period, calendar));
}
}
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods));
return object;
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> ous = new ArrayList<>();
List<Integer> levels = new ArrayList<>();
List<OrganisationUnitGroup> groups = new ArrayList<>();
for (String ou : items) {
if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(userOrgUnits);
} else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
} else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
} else if (ou != null && ou.startsWith(KEY_LEVEL)) {
int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
if (level > 0) {
levels.add(level);
}
} else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
if (group != null) {
groups.add(group);
}
} else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
if (unit != null) {
ous.add(unit);
}
}
}
// Remove duplicates
ous = ous.stream().distinct().collect(Collectors.toList());
List<DimensionalItemObject> orgUnits = new ArrayList<>();
List<OrganisationUnit> ousList = asTypedList(ous);
if (!levels.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
}
if (!groups.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
}
if (levels.isEmpty() && groups.isEmpty()) {
orgUnits.addAll(ous);
}
if (orgUnits.isEmpty()) {
throw new IllegalQueryException("Dimension ou is present in query without any valid dimension options");
}
// Remove duplicates
orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits);
return object;
} else if (LONGITUDE_DIM_ID.contains(dimension)) {
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
return object;
} else if (LATITUDE_DIM_ID.contains(dimension)) {
DimensionalObject object = new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
return object;
} else {
DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
if (dimObject != null && dimObject.isDataDimension()) {
Class<?> dimClass = ReflectionUtils.getRealClass(dimObject.getClass());
Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getByUidOrdered(itemClass, items)) : dimObject.getItems();
DimensionalObject object = new BaseDimensionalObject(dimension, dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
return object;
}
}
if (allowNull) {
return null;
}
throw new IllegalQueryException("Dimension identifier does not reference any dimension: " + dimension);
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class ValidationRuleStoreTest method setUpTest.
// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() throws Exception {
dataElementA = createDataElement('A');
dataElementB = createDataElement('B');
dataElementC = createDataElement('C');
dataElementD = createDataElement('D');
dataElementService.addDataElement(dataElementA);
dataElementService.addDataElement(dataElementB);
dataElementService.addDataElement(dataElementC);
dataElementService.addDataElement(dataElementD);
dataElements = new HashSet<>();
dataElements.add(dataElementA);
dataElements.add(dataElementB);
dataElements.add(dataElementC);
dataElements.add(dataElementD);
DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
optionCombos = new HashSet<>();
optionCombos.add(categoryOptionCombo);
expressionA = new Expression("expressionA", "descriptionA");
expressionB = new Expression("expressionB", "descriptionB");
expressionService.addExpression(expressionB);
expressionService.addExpression(expressionA);
periodType = PeriodType.getAvailablePeriodTypes().iterator().next();
}
Aggregations