use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.
the class AnalyticsUtils method getDataValueSetFromGrid.
/**
* Generates a data value set based on the given grid with aggregated data.
* Sets the created and last updated fields to the current date.
*
* @param params the data query parameters.
* @param grid the grid.
* @return a data value set.
*/
public static DataValueSet getDataValueSetFromGrid(DataQueryParams params, Grid grid) {
int dxInx = grid.getIndexOfHeader(DATA_X_DIM_ID);
int peInx = grid.getIndexOfHeader(PERIOD_DIM_ID);
int ouInx = grid.getIndexOfHeader(ORGUNIT_DIM_ID);
int coInx = grid.getIndexOfHeader(CATEGORYOPTIONCOMBO_DIM_ID);
int aoInx = grid.getIndexOfHeader(ATTRIBUTEOPTIONCOMBO_DIM_ID);
int vlInx = grid.getWidth() - 1;
Assert.isTrue(dxInx >= 0, "Data dimension index must be greater than or equal to zero");
Assert.isTrue(peInx >= 0, "Period dimension index must be greater than or equal to zero");
Assert.isTrue(ouInx >= 0, "Org unit dimension index must be greater than or equal to zero");
Assert.isTrue(coInx >= 0, "Category option combo dimension index must be greater than or equal to zero");
Assert.isTrue(aoInx >= 0, "Attribute option combo dimension index must be greater than or equal to zero");
Assert.isTrue(vlInx >= 0, "Value index must be greater than or equal to zero");
String created = DateUtils.getMediumDateString();
DataValueSet dvs = new DataValueSet();
Set<String> primaryKeys = Sets.newHashSet();
for (List<Object> row : grid.getRows()) {
DataValue dv = new DataValue();
Object coc = row.get(coInx);
Object aoc = row.get(aoInx);
dv.setDataElement(String.valueOf(row.get(dxInx)));
dv.setPeriod(String.valueOf(row.get(peInx)));
dv.setOrgUnit(String.valueOf(row.get(ouInx)));
dv.setCategoryOptionCombo(coc != null ? String.valueOf(coc) : null);
dv.setAttributeOptionCombo(aoc != null ? String.valueOf(aoc) : null);
dv.setValue(String.valueOf(row.get(vlInx)));
dv.setComment(KEY_AGG_VALUE);
dv.setStoredBy(KEY_AGG_VALUE);
dv.setCreated(created);
dv.setLastUpdated(created);
if (!params.isDuplicatesOnly() || !primaryKeys.add(dv.getPrimaryKey())) {
dvs.getDataValues().add(dv);
}
}
return dvs;
}
use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.
the class JdbcEventStore method getEventRows.
@Override
public List<EventRow> getEventRows(EventSearchParams params, List<OrganisationUnit> organisationUnits) {
List<EventRow> eventRows = new ArrayList<>();
String sql = buildSql(params, organisationUnits);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
log.debug("Event query SQL: " + sql);
EventRow eventRow = new EventRow();
eventRow.setEvent("not_valid");
Set<String> notes = new HashSet<>();
IdSchemes idSchemes = ObjectUtils.firstNonNull(params.getIdSchemes(), new IdSchemes());
while (rowSet.next()) {
if (rowSet.getString("psi_uid") == null) {
continue;
}
if (eventRow.getUid() == null || !eventRow.getUid().equals(rowSet.getString("psi_uid"))) {
eventRow = new EventRow();
eventRow.setUid(rowSet.getString("psi_uid"));
eventRow.setEvent(IdSchemes.getValue(rowSet.getString("psi_uid"), rowSet.getString("psi_code"), idSchemes.getProgramStageInstanceIdScheme()));
eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
eventRow.setTrackedEntityInstanceOrgUnit(rowSet.getString("tei_ou"));
eventRow.setTrackedEntityInstanceOrgUnitName(rowSet.getString("tei_ou_name"));
eventRow.setTrackedEntityInstanceCreated(rowSet.getString("tei_created"));
eventRow.setTrackedEntityInstanceInactive(rowSet.getBoolean("tei_inactive"));
eventRow.setDeleted(rowSet.getBoolean("psi_deleted"));
eventRow.setProgram(IdSchemes.getValue(rowSet.getString("p_uid"), rowSet.getString("p_code"), idSchemes.getProgramIdScheme()));
eventRow.setProgramStage(IdSchemes.getValue(rowSet.getString("ps_uid"), rowSet.getString("ps_code"), idSchemes.getProgramStageIdScheme()));
eventRow.setOrgUnit(IdSchemes.getValue(rowSet.getString("ou_uid"), rowSet.getString("ou_code"), idSchemes.getOrgUnitIdScheme()));
ProgramType programType = ProgramType.fromValue(rowSet.getString("p_type"));
if (programType == ProgramType.WITHOUT_REGISTRATION) {
eventRow.setEnrollment(rowSet.getString("pi_uid"));
eventRow.setFollowup(rowSet.getBoolean("pi_followup"));
}
eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
eventRow.setOrgUnitName(rowSet.getString("ou_name"));
eventRow.setDueDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_duedate")));
eventRow.setEventDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_executiondate")));
eventRows.add(eventRow);
}
if (rowSet.getString("pav_value") != null && rowSet.getString("ta_uid") != null) {
String valueType = rowSet.getString("ta_valuetype");
Attribute attribute = new Attribute();
attribute.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_created")));
attribute.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_lastupdated")));
attribute.setValue(rowSet.getString("pav_value"));
attribute.setDisplayName(rowSet.getString("ta_name"));
attribute.setValueType(valueType != null ? ValueType.valueOf(valueType.toUpperCase()) : null);
attribute.setAttribute(rowSet.getString("ta_uid"));
eventRow.getAttributes().add(attribute);
}
if (rowSet.getString("pdv_value") != null && rowSet.getString("de_uid") != null) {
DataValue dataValue = new DataValue();
dataValue.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("pdv_created")));
dataValue.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("pdv_lastupdated")));
dataValue.setValue(rowSet.getString("pdv_value"));
dataValue.setProvidedElsewhere(rowSet.getBoolean("pdv_providedelsewhere"));
dataValue.setDataElement(IdSchemes.getValue(rowSet.getString("de_uid"), rowSet.getString("de_code"), idSchemes.getDataElementIdScheme()));
dataValue.setStoredBy(rowSet.getString("pdv_storedby"));
eventRow.getDataValues().add(dataValue);
}
if (rowSet.getString("psinote_value") != null && !notes.contains(rowSet.getString("psinote_id"))) {
Note note = new Note();
note.setValue(rowSet.getString("psinote_value"));
note.setStoredDate(rowSet.getString("psinote_storeddate"));
note.setStoredBy(rowSet.getString("psinote_storedby"));
eventRow.getNotes().add(note);
notes.add(rowSet.getString("psinote_id"));
}
}
return eventRows;
}
use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method checkAttributes.
private List<ImportConflict> checkAttributes(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
for (Attribute attribute : trackedEntityInstance.getAttributes()) {
TrackedEntityAttribute entityAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
if (entityAttribute == null) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Invalid attribute " + attribute.getAttribute()));
continue;
}
if (entityAttribute.isUnique()) {
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), trackedEntityInstance.getOrgUnit());
org.hisp.dhis.trackedentity.TrackedEntityInstance tei = teiService.getTrackedEntityInstance(trackedEntityInstance.getTrackedEntityInstance());
importConflicts.addAll(checkScope(tei, entityAttribute, attribute.getValue(), organisationUnit));
}
importConflicts.addAll(validateAttributeType(attribute, importOptions));
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.
the class DefaultCompleteDataSetRegistrationExchangeService method validateAttrOptCombo.
private void validateAttrOptCombo(MetaDataProperties mdProps, MetaDataCaches mdCaches, ImportConfig config) throws ImportConflictException {
final Period pe = mdProps.period;
if (mdProps.attrOptCombo == null) {
if (config.requireAttrOptionCombos) {
throw new ImportConflictException(new ImportConflict("Attribute option combo", "Attribute option combo is required but is not specified"));
} else {
mdProps.attrOptCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
}
}
final DataElementCategoryOptionCombo aoc = mdProps.attrOptCombo;
DateRange range = aoc.getDateRange();
if ((range.getStartDate() != null && range.getStartDate().compareTo(pe.getStartDate()) > 0) || (range.getEndDate() != null && range.getEndDate().compareTo(pe.getEndDate()) < 0)) {
throw new ImportConflictException(new ImportConflict(mdProps.orgUnit.getUid(), String.format("Period: %s is not within range of attribute option combo: %s", pe.getIsoDate(), aoc.getUid())));
}
final String aocOrgUnitKey = aoc.getUid() + mdProps.orgUnit.getUid();
boolean isOrgUnitValidForAoc = mdCaches.attrOptComboOrgUnitMap.get(aocOrgUnitKey, () -> {
Set<OrganisationUnit> aocOrgUnits = aoc.getOrganisationUnits();
return aocOrgUnits == null || mdProps.orgUnit.isDescendant(aocOrgUnits);
});
if (!isOrgUnitValidForAoc) {
throw new ImportConflictException(new ImportConflict(mdProps.orgUnit.getUid(), String.format("Organisation unit: %s is not valid for attribute option combo %s", mdProps.orgUnit.getUid(), aoc.getUid())));
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.
the class AbstractEnrollmentService method validateAttributeType.
private List<ImportConflict> validateAttributeType(Attribute attribute, ImportOptions importOptions) {
List<ImportConflict> importConflicts = Lists.newArrayList();
TrackedEntityAttribute teAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
if (teAttribute == null) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Does not point to a valid attribute."));
return importConflicts;
}
String errorMessage = trackedEntityAttributeService.validateValueType(teAttribute, attribute.getValue());
if (errorMessage != null) {
importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
}
return importConflicts;
}
Aggregations