use of org.hisp.dhis.dataelement.DataElement 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.DataElement in project dhis2-core by dhis2.
the class DefaultQueryPlanner method validate.
// -------------------------------------------------------------------------
// DefaultQueryPlanner implementation
// -------------------------------------------------------------------------
@Override
public void validate(DataQueryParams params) throws IllegalQueryException {
String violation = null;
if (params == null) {
throw new IllegalQueryException("Params cannot be null");
}
final List<DimensionalItemObject> dataElements = Lists.newArrayList(params.getDataElements());
params.getProgramDataElements().stream().forEach(pde -> dataElements.add(((ProgramDataElementDimensionItem) pde).getDataElement()));
final List<DataElement> nonAggDataElements = FilterUtils.inverseFilter(asTypedList(dataElements), AggregatableDataElementFilter.INSTANCE);
if (params.getDimensions().isEmpty()) {
violation = "At least one dimension must be specified";
}
if (!params.getDimensionsAsFilters().isEmpty()) {
violation = "Dimensions cannot be specified as dimension and filter simultaneously: " + params.getDimensionsAsFilters();
}
if (!params.hasPeriods() && !params.isSkipPartitioning() && !params.hasStartEndDate()) {
violation = "At least one period must be specified as dimension or filter";
}
if (params.hasPeriods() && params.hasStartEndDate()) {
violation = "Periods and start and end dates cannot be specified simultaneously";
}
if (!params.getFilterIndicators().isEmpty() && params.getFilterOptions(DATA_X_DIM_ID).size() > 1) {
violation = "Only a single indicator can be specified as filter";
}
if (!params.getFilterReportingRates().isEmpty() && params.getFilterOptions(DATA_X_DIM_ID).size() > 1) {
violation = "Only a single reporting rate can be specified as filter";
}
if (params.getFilters().contains(new BaseDimensionalObject(CATEGORYOPTIONCOMBO_DIM_ID))) {
violation = "Category option combos cannot be specified as filter";
}
if (!params.getDuplicateDimensions().isEmpty()) {
violation = "Dimensions cannot be specified more than once: " + params.getDuplicateDimensions();
}
if (!params.getAllReportingRates().isEmpty() && !params.containsOnlyDimensionsAndFilters(COMPLETENESS_DIMENSION_TYPES)) {
violation = "Reporting rates can only be specified together with dimensions of type: " + COMPLETENESS_DIMENSION_TYPES;
}
if (params.hasDimensionOrFilter(CATEGORYOPTIONCOMBO_DIM_ID) && params.getAllDataElements().isEmpty()) {
violation = "Assigned categories cannot be specified when data elements are not specified";
}
if (params.hasDimensionOrFilter(CATEGORYOPTIONCOMBO_DIM_ID) && (params.getAllDataElements().size() != params.getAllDataDimensionItems().size())) {
violation = "Assigned categories can only be specified together with data elements, not indicators or reporting rates";
}
if (!nonAggDataElements.isEmpty()) {
violation = "Data elements must be of a value and aggregation type that allow aggregation: " + getUids(nonAggDataElements);
}
if (params.isOutputFormat(OutputFormat.DATA_VALUE_SET)) {
if (!params.hasDimension(DATA_X_DIM_ID)) {
violation = "A data dimension 'dx' must be specified when output format is DATA_VALUE_SET";
}
if (!params.hasDimension(PERIOD_DIM_ID)) {
violation = "A period dimension 'pe' must be specified when output format is DATA_VALUE_SET";
}
if (!params.hasDimension(ORGUNIT_DIM_ID)) {
violation = "An organisation unit dimension 'ou' must be specified when output format is DATA_VALUE_SET";
}
}
if (violation != null) {
log.warn(String.format("Analytics validation failed: %s", violation));
throw new IllegalQueryException(violation);
}
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultEventDataQueryService method getValueDimension.
private DimensionalItemObject getValueDimension(String value) {
if (value == null) {
return null;
}
DataElement de = dataElementService.getDataElement(value);
if (de != null && de.isNumericType()) {
return de;
}
TrackedEntityAttribute at = attributeService.getTrackedEntityAttribute(value);
if (at != null && at.isNumericType()) {
return at;
}
throw new IllegalQueryException("Value identifier does not reference any " + "data element or attribute which are numeric type and part of the program: " + value);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultEventDataQueryService method getQueryItemFromDimension.
private QueryItem getQueryItemFromDimension(String dimension, Program program) {
String[] split = dimension.split(ITEM_SEP);
String item = split[0];
LegendSet legendSet = split.length > 1 && split[1] != null ? legendSetService.getLegendSet(split[1]) : null;
DataElement de = dataElementService.getDataElement(item);
if (de != null && program.containsDataElement(de)) {
ValueType valueType = legendSet != null ? ValueType.TEXT : de.getValueType();
return new QueryItem(de, legendSet, valueType, de.getAggregationType(), de.getOptionSet());
}
TrackedEntityAttribute at = attributeService.getTrackedEntityAttribute(item);
if (at != null && program.containsAttribute(at)) {
ValueType valueType = legendSet != null ? ValueType.TEXT : at.getValueType();
return new QueryItem(at, legendSet, valueType, at.getAggregationType(), at.getOptionSet());
}
ProgramIndicator pi = programIndicatorService.getProgramIndicatorByUid(item);
if (pi != null && program.getProgramIndicators().contains(pi)) {
return new QueryItem(pi, legendSet, ValueType.NUMBER, pi.getAggregationType(), null);
}
throw new IllegalQueryException("Item identifier does not reference any data element, attribute or indicator part of the program: " + item);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class EventQueryParams method fromDataQueryParams.
public static EventQueryParams fromDataQueryParams(DataQueryParams dataQueryParams) {
EventQueryParams params = new EventQueryParams();
dataQueryParams.copyTo(params);
EventQueryParams.Builder builder = new EventQueryParams.Builder(params);
for (DimensionalItemObject object : dataQueryParams.getProgramDataElements()) {
ProgramDataElementDimensionItem element = (ProgramDataElementDimensionItem) object;
DataElement dataElement = element.getDataElement();
QueryItem item = new QueryItem(dataElement, (dataElement.getLegendSets().isEmpty() ? null : dataElement.getLegendSets().get(0)), dataElement.getValueType(), dataElement.getAggregationType(), dataElement.getOptionSet());
item.setProgram(element.getProgram());
builder.addItem(item);
}
for (DimensionalItemObject object : dataQueryParams.getProgramAttributes()) {
ProgramTrackedEntityAttributeDimensionItem element = (ProgramTrackedEntityAttributeDimensionItem) object;
TrackedEntityAttribute attribute = element.getAttribute();
QueryItem item = new QueryItem(attribute, (attribute.getLegendSets().isEmpty() ? null : attribute.getLegendSets().get(0)), attribute.getValueType(), attribute.getAggregationType(), attribute.getOptionSet());
item.setProgram(element.getProgram());
builder.addItem(item);
}
for (DimensionalItemObject object : dataQueryParams.getFilterProgramDataElements()) {
ProgramDataElementDimensionItem element = (ProgramDataElementDimensionItem) object;
DataElement dataElement = element.getDataElement();
QueryItem item = new QueryItem(dataElement, (dataElement.getLegendSets().isEmpty() ? null : dataElement.getLegendSets().get(0)), dataElement.getValueType(), dataElement.getAggregationType(), dataElement.getOptionSet());
item.setProgram(element.getProgram());
builder.addItemFilter(item);
}
for (DimensionalItemObject object : dataQueryParams.getFilterProgramAttributes()) {
ProgramTrackedEntityAttributeDimensionItem element = (ProgramTrackedEntityAttributeDimensionItem) object;
TrackedEntityAttribute attribute = element.getAttribute();
QueryItem item = new QueryItem(attribute, (attribute.getLegendSets().isEmpty() ? null : attribute.getLegendSets().get(0)), attribute.getValueType(), attribute.getAggregationType(), attribute.getOptionSet());
builder.addItemFilter(item);
}
for (DimensionalItemObject object : dataQueryParams.getProgramIndicators()) {
ProgramIndicator programIndicator = (ProgramIndicator) object;
builder.addItemProgramIndicator(programIndicator);
}
return builder.withAggregateData(true).removeDimension(DATA_X_DIM_ID).build();
}
Aggregations