use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.
the class HibernateDataValueStore method getDataValues.
// -------------------------------------------------------------------------
// Collections of DataValues
// -------------------------------------------------------------------------
@Override
public List<DataValue> getDataValues(DataExportParams params) {
Set<DataElement> dataElements = params.getAllDataElements();
Set<OrganisationUnit> organisationUnits = params.getAllOrganisationUnits();
// ---------------------------------------------------------------------
// HQL parameters
// ---------------------------------------------------------------------
String hql = "select dv from DataValue dv " + "inner join dv.dataElement de " + "inner join dv.period pe " + "inner join dv.source ou " + "inner join dv.categoryOptionCombo co " + "inner join dv.attributeOptionCombo ao " + "where de.id in (:dataElements) ";
if (params.hasPeriods()) {
hql += "and pe.id in (:periods) ";
} else if (params.hasStartEndDate()) {
hql += "and (pe.startDate >= :startDate and pe.endDate <= :endDate) ";
}
if (params.isIncludeDescendantsForOrganisationUnits()) {
hql += "and (";
for (OrganisationUnit unit : params.getOrganisationUnits()) {
hql += "ou.path like '" + unit.getPath() + "%' or ";
}
hql = removeLastOr(hql);
hql += ") ";
} else if (!organisationUnits.isEmpty()) {
hql += "and ou.id in (:orgUnits) ";
}
if (params.hasAttributeOptionCombos()) {
hql += "and ao.id in (:attributeOptionCombos) ";
}
if (params.hasLastUpdated() || params.hasLastUpdatedDuration()) {
hql += "and dv.lastUpdated >= :lastUpdated ";
}
if (!params.isIncludeDeleted()) {
hql += "and dv.deleted is false ";
}
if (params.isOrderByOrgUnitPath()) {
hql += "order by ou.path ";
}
if (params.isOrderByPeriod()) {
hql += params.isOrderByOrgUnitPath() ? "," : "order by";
hql += " pe.startDate, pe.endDate ";
}
// ---------------------------------------------------------------------
// Query parameters
// ---------------------------------------------------------------------
Query<DataValue> query = getQuery(hql).setParameterList("dataElements", getIdentifiers(dataElements));
if (params.hasPeriods()) {
Set<Period> periods = params.getPeriods().stream().map(p -> periodStore.reloadPeriod(p)).collect(Collectors.toSet());
query.setParameterList("periods", getIdentifiers(periods));
} else if (params.hasStartEndDate()) {
query.setParameter("startDate", params.getStartDate()).setParameter("endDate", params.getEndDate());
}
if (!params.isIncludeDescendantsForOrganisationUnits() && !organisationUnits.isEmpty()) {
query.setParameterList("orgUnits", getIdentifiers(organisationUnits));
}
if (params.hasAttributeOptionCombos()) {
query.setParameterList("attributeOptionCombos", getIdentifiers(params.getAttributeOptionCombos()));
}
if (params.hasLastUpdated()) {
query.setParameter("lastUpdated", params.getLastUpdated());
} else if (params.hasLastUpdatedDuration()) {
query.setParameter("lastUpdated", DateUtils.nowMinusDuration(params.getLastUpdatedDuration()));
}
if (params.hasLimit()) {
query.setMaxResults(params.getLimit());
}
return query.list();
}
use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.
the class DefaultCompleteDataSetRegistrationService method getMissingCompulsoryFields.
@Override
@Transactional(readOnly = true)
public List<DataElementOperand> getMissingCompulsoryFields(DataSet dataSet, Period period, OrganisationUnit organisationUnit, CategoryOptionCombo attributeOptionCombo) {
List<DataElementOperand> missingDataElementOperands = new ArrayList<>();
if (!dataSet.getCompulsoryDataElementOperands().isEmpty()) {
DataExportParams params = new DataExportParams();
params.setDataElementOperands(dataSet.getCompulsoryDataElementOperands());
params.setPeriods(Sets.newHashSet(period));
params.setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo));
params.setOrganisationUnits(Sets.newHashSet(organisationUnit));
List<DeflatedDataValue> deflatedDataValues = dataValueService.getDeflatedDataValues(params);
MapMapMap<Long, Long, Long, Boolean> dataPresent = new MapMapMap<>();
for (DeflatedDataValue dv : deflatedDataValues) {
dataPresent.putEntry(dv.getSourceId(), dv.getDataElementId(), dv.getCategoryOptionComboId(), true);
}
User currentUser = currentUserService.getCurrentUser();
for (DataElementOperand deo : dataSet.getCompulsoryDataElementOperands()) {
List<String> errors = accessManager.canWrite(currentUser, deo);
if (!errors.isEmpty()) {
continue;
}
MapMap<Long, Long, Boolean> ouDataPresent = dataPresent.get(organisationUnit.getId());
if (ouDataPresent != null) {
Map<Long, Boolean> deDataPresent = ouDataPresent.get(deo.getDataElement().getId());
if (deDataPresent != null && (deo.getCategoryOptionCombo() == null || deDataPresent.get(deo.getCategoryOptionCombo().getId()) != null)) {
continue;
}
}
missingDataElementOperands.add(deo);
}
}
return missingDataElementOperands;
}
use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.
the class PredictionServiceTest method getDataValue.
private String getDataValue(DataElement dataElement, CategoryOptionCombo combo, CategoryOptionCombo attributeOptionCombo, OrganisationUnit source, Period period) {
DataExportParams params = new DataExportParams().setDataElementOperands(Sets.newHashSet(new DataElementOperand(dataElement, combo))).setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo)).setOrganisationUnits(Sets.newHashSet(source)).setPeriods(Sets.newHashSet(periodService.reloadPeriod(period)));
List<DeflatedDataValue> values = dataValueService.getDeflatedDataValues(params);
if (values != null && values.size() > 0) {
return values.get(0).getValue();
}
return null;
}
use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.
the class DataValidationTask method getDataValueMap.
/**
* Gets data elements and data element operands from the datavalue table.
*/
private void getDataValueMap() {
DataExportParams params = new DataExportParams();
params.setDataElements(periodTypeX.getDataElements());
params.setDataElementOperands(periodTypeX.getDataElementOperands());
params.setIncludedDate(period.getStartDate());
params.setOrganisationUnits(new HashSet<>(orgUnits));
params.setPeriodTypes(periodTypeX.getAllowedPeriodTypes());
params.setCoDimensionConstraints(context.getCoDimensionConstraints());
params.setCogDimensionConstraints(context.getCogDimensionConstraints());
if (context.getAttributeCombo() != null) {
params.setAttributeOptionCombos(Sets.newHashSet(context.getAttributeCombo()));
}
List<DeflatedDataValue> dataValues = dataValueService.getDeflatedDataValues(params);
dataMap = new MapMapMap<>();
MapMapMap<Long, String, DimensionalItemObject, Long> duplicateCheck = new MapMapMap<>();
for (DeflatedDataValue dv : dataValues) {
DataElement dataElement = periodTypeX.getDataElementIdMap().get(dv.getDataElementId());
String deoIdKey = periodTypeX.getDeoIds(dv.getDataElementId(), dv.getCategoryOptionComboId());
DataElementOperand dataElementOperand = periodTypeX.getDataElementOperandIdMap().get(deoIdKey);
Period p = getPeriod(dv.getPeriodId());
long orgUnitId = dv.getSourceId();
String attributeOptionComboUid = getAttributeOptionCombo(dv.getAttributeOptionComboId()).getUid();
if (dataElement != null) {
Object value = getObjectValue(dv.getValue(), dataElement.getValueType());
addValueToDataMap(orgUnitId, attributeOptionComboUid, dataElement, value, p, duplicateCheck);
}
if (dataElementOperand != null) {
Object value = getObjectValue(dv.getValue(), dataElementOperand.getDataElement().getValueType());
addValueToDataMap(orgUnitId, attributeOptionComboUid, dataElementOperand, value, p, duplicateCheck);
}
}
}
use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.
the class DataSetController method getForm.
private Form getForm(List<DataSet> dataSets, OrganisationUnit ou, Period pe, String categoryOptions, boolean metaData) throws IOException {
DataSet dataSet = dataSets.get(0);
Form form = FormUtils.fromDataSet(dataSets.get(0), metaData, null);
Set<String> options = null;
if (StringUtils.isNotEmpty(categoryOptions) && categoryOptions.startsWith("[") && categoryOptions.endsWith("]")) {
String[] split = categoryOptions.substring(1, categoryOptions.length() - 1).split(",");
options = new HashSet<>(Lists.newArrayList(split));
}
if (ou != null && pe != null) {
Set<CategoryOptionCombo> attrOptionCombos = options == null || options.isEmpty() ? null : Sets.newHashSet(inputUtils.getAttributeOptionCombo(dataSet.getCategoryCombo(), options, IdScheme.UID));
List<DataValue> dataValues = dataValueService.getDataValues(new DataExportParams().setDataElements(dataSets.get(0).getDataElements()).setPeriods(Sets.newHashSet(pe)).setOrganisationUnits(Sets.newHashSet(ou)).setAttributeOptionCombos(attrOptionCombos));
FormUtils.fillWithDataValues(form, dataValues);
}
return form;
}
Aggregations