use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.
private ImportSummary updateTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, String programId, ImportOptions importOptions, boolean singleUpdate, boolean handleEnrollments) {
ImportSummary importSummary = new ImportSummary(dtoEntityInstance.getTrackedEntityInstance());
importOptions = updateImportOptions(importOptions);
dtoEntityInstance.trimValuesToNull();
checkAttributes(dtoEntityInstance, importOptions, importSummary, true);
org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(dtoEntityInstance.getTrackedEntityInstance(), importOptions.getUser());
List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoEntityInstance);
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
Program program = getProgram(importOptions.getIdSchemes(), programId);
if (daoEntityInstance == null || !errors.isEmpty() || organisationUnit == null || importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
if (daoEntityInstance == null) {
String message = "You are trying to add or update tracked entity instance " + dtoEntityInstance.getTrackedEntityInstance() + " that has already been deleted";
importSummary.addConflict("TrackedEntityInstance", message);
} else if (!errors.isEmpty()) {
importSummary.setDescription(errors.toString());
} else if (organisationUnit == null) {
String message = "Org unit " + dtoEntityInstance.getOrgUnit() + " does not exist";
importSummary.addConflict("OrganisationUnit", message);
}
return importSummary;
}
daoEntityInstance.setOrganisationUnit(organisationUnit);
daoEntityInstance.setInactive(dtoEntityInstance.isInactive());
daoEntityInstance.setPotentialDuplicate(dtoEntityInstance.isPotentialDuplicate());
if (dtoEntityInstance.getGeometry() != null) {
FeatureType featureType = daoEntityInstance.getTrackedEntityType().getFeatureType();
if (featureType.equals(FeatureType.NONE) || !featureType.equals(FeatureType.getTypeFromName(dtoEntityInstance.getGeometry().getGeometryType()))) {
importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Geometry does not conform to feature type '" + featureType + "'");
importSummary.getImportCount().incrementIgnored();
return importSummary;
} else {
daoEntityInstance.setGeometry(dtoEntityInstance.getGeometry());
}
} else if (!FeatureType.NONE.equals(dtoEntityInstance.getFeatureType()) && dtoEntityInstance.getCoordinates() != null) {
try {
daoEntityInstance.setGeometry(GeoUtils.getGeometryFromCoordinatesAndType(dtoEntityInstance.getFeatureType(), dtoEntityInstance.getCoordinates()));
} catch (IOException e) {
importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Could not parse coordinates");
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
} else {
daoEntityInstance.setGeometry(null);
}
if (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getAttributes().isEmpty()) {
updateAttributeValues(dtoEntityInstance, daoEntityInstance, program, importOptions.getUser());
}
updateDateFields(dtoEntityInstance, daoEntityInstance);
teiService.updateTrackedEntityInstance(daoEntityInstance);
importSummary.setReference(daoEntityInstance.getUid());
importSummary.getImportCount().incrementUpdated();
if (singleUpdate && (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getRelationships().isEmpty())) {
importSummary.setRelationships(handleRelationships(dtoEntityInstance, daoEntityInstance, importOptions));
}
if (handleEnrollments) {
importSummary.setEnrollments(handleEnrollments(dtoEntityInstance, daoEntityInstance, importOptions));
} else {
for (Enrollment enrollment : dtoEntityInstance.getEnrollments()) {
enrollment.setTrackedEntityType(dtoEntityInstance.getTrackedEntityType());
enrollment.setTrackedEntityInstance(daoEntityInstance.getUid());
}
}
return importSummary;
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method filterOutSkipSyncAttributesIfApplies.
private Set<TrackedEntityAttribute> filterOutSkipSyncAttributesIfApplies(TrackedEntityInstanceParams params, TrackedEntityInstance trackedEntityInstance, Set<TrackedEntityAttribute> readableAttributes) {
Set<TrackedEntityAttribute> readableAttributesCopy;
if (params.isDataSynchronizationQuery()) {
List<String> programs = trackedEntityInstance.getEnrollments().stream().map(Enrollment::getProgram).collect(Collectors.toList());
readableAttributesCopy = readableAttributes.stream().filter(att -> !att.getSkipSynchronization()).collect(Collectors.toSet());
IdSchemes idSchemes = new IdSchemes();
for (String programUid : programs) {
Program program = getProgram(idSchemes, programUid);
if (program != null) {
readableAttributesCopy.addAll(program.getTrackedEntityAttributes().stream().filter(att -> !att.getSkipSynchronization()).collect(Collectors.toSet()));
}
}
} else {
readableAttributesCopy = new HashSet<>(readableAttributes);
}
return readableAttributesCopy;
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AttributeOptionComboDateCheck method check.
@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
CategoryOptionCombo attributeOptionCombo = ctx.getCategoryOptionComboMap().get(event.getUid());
Date executionDate = null;
if (event.getEventDate() != null) {
executionDate = DateUtils.parseDate(event.getEventDate());
}
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = DateUtils.parseDate(event.getDueDate());
}
Date eventDate = executionDate != null ? executionDate : dueDate;
if (eventDate == null) {
return error("Event date can not be empty", event.getEvent());
}
Program program = ctx.getProgramsMap().get(event.getProgram());
for (CategoryOption categoryOption : attributeOptionCombo.getCategoryOptions()) {
if (categoryOption.getStartDate() != null && eventDate.compareTo(categoryOption.getStartDate()) < 0) {
return error("Event date " + getMediumDateString(eventDate) + " is before start date " + getMediumDateString(categoryOption.getStartDate()) + " for attributeOption '" + categoryOption.getName() + "'", event.getEvent());
}
if (categoryOption.getEndDate() != null && eventDate.compareTo(categoryOption.getAdjustedEndDate(program)) > 0) {
return error("Event date " + getMediumDateString(eventDate) + " is after end date " + getMediumDateString(categoryOption.getAdjustedEndDate(program)) + " for attributeOption '" + categoryOption.getName() + "' in program '" + program.getName() + "'", event.getEvent());
}
}
return success();
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ProgramOrgUnitCheck method check.
@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
ProgramInstance programInstance = ctx.getProgramInstanceMap().get(event.getUid());
final Map<Long, List<Long>> programWithOrgUnitsMap = ctx.getProgramWithOrgUnitsMap();
final Map<String, OrganisationUnit> organisationUnitMap = ctx.getOrganisationUnitMap();
if (programInstance != null) {
final OrganisationUnit organisationUnit = organisationUnitMap.get(event.getUid());
final Program program = programInstance.getProgram();
if (organisationUnit != null && program != null) {
List<Long> ouList = programWithOrgUnitsMap.get(program.getId());
if (ouList == null || !ouList.contains(organisationUnit.getId())) {
return error("Program is not assigned to this Organisation Unit: " + event.getOrgUnit(), event.getEvent());
}
}
}
return success();
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class AttributeOptionComboCheck method check.
@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
Program program = ctx.getProgramsMap().get(event.getProgram());
CategoryOptionCombo coc = ctx.getCategoryOptionComboMap().get(event.getUid());
if (coc != null && coc.isDefault() && program.getCategoryCombo() != null && !program.getCategoryCombo().isDefault()) {
ImportSummary importSummary = new ImportSummary(event.getEvent());
importSummary.addConflict("attributeOptionCombo", "Valid attribute option combo must be specified since program does not have the default category combo");
importSummary.setStatus(ImportStatus.ERROR);
return importSummary.incrementIgnored();
}
return success();
}
Aggregations