use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance 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.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addTrackedEntityInstance.
@Override
public ImportSummary addTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
trackedEntityInstance.trimValuesToNull();
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkTrackedEntity(trackedEntityInstance, importOptions));
importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(trackedEntityInstance, importOptions, importSummary);
if (entityInstance == null) {
return importSummary;
}
teiService.addTrackedEntityInstance(entityInstance);
updateRelationships(trackedEntityInstance, entityInstance);
updateAttributeValues(trackedEntityInstance, entityInstance);
updateDateFields(trackedEntityInstance, entityInstance);
teiService.updateTrackedEntityInstance(entityInstance);
importSummary.setReference(entityInstance.getUid());
importSummary.getImportCount().incrementImported();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
return importSummary;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkScope.
private List<ImportConflict> checkScope(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute trackedEntityAttribute, String value, OrganisationUnit organisationUnit, Program program) {
List<ImportConflict> importConflicts = new ArrayList<>();
if (trackedEntityAttribute == null || value == null) {
return importConflicts;
}
String errorMessage = trackedEntityAttributeService.validateScope(trackedEntityAttribute, value, trackedEntityInstance, organisationUnit, program);
if (errorMessage != null) {
importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class EventController method getCsvEvents.
@RequestMapping(value = "", method = RequestMethod.GET, produces = { "application/csv", "application/csv+gzip", "text/csv" })
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD') or hasRole('F_TRACKED_ENTITY_DATAVALUE_READ')")
public void getCsvEvents(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String orgUnit, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Date dueDateStart, @RequestParam(required = false) Date dueDateEnd, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) EventStatus status, @RequestParam(required = false) String attributeCc, @RequestParam(required = false) String attributeCos, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false) String order, @RequestParam(required = false) String attachment, @RequestParam(required = false, defaultValue = "false") boolean includeDeleted, @RequestParam(required = false, defaultValue = "false") boolean skipHeader, IdSchemes idSchemes, HttpServletResponse response, HttpServletRequest request) throws IOException, WebMessageException {
boolean allowNoAttrOptionCombo = trackedEntityInstance != null && entityInstanceService.getTrackedEntityInstance(trackedEntityInstance) != null;
DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(attributeCc, attributeCos, allowNoAttrOptionCombo);
if (attributeOptionCombo == null && !allowNoAttrOptionCombo) {
throw new WebMessageException(WebMessageUtils.conflict("Illegal attribute option combo identifier: " + attributeCc + " " + attributeCos));
}
lastUpdatedStartDate = lastUpdatedStartDate != null ? lastUpdatedStartDate : lastUpdated;
EventSearchParams params = eventService.getFromUrl(program, programStage, programStatus, followUp, orgUnit, ouMode, trackedEntityInstance, startDate, endDate, dueDateStart, dueDateEnd, lastUpdatedStartDate, lastUpdatedEndDate, status, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, getOrderParams(order), null, false, null, null, null, includeDeleted);
Events events = eventService.getEvents(params);
OutputStream outputStream = response.getOutputStream();
response.setContentType("application/csv");
if (ContextUtils.isAcceptCsvGzip(request)) {
response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
outputStream = new GZIPOutputStream(outputStream);
response.setContentType("application/csv+gzip");
}
if (!StringUtils.isEmpty(attachment)) {
response.addHeader("Content-Disposition", "attachment; filename=" + attachment);
}
csvEventService.writeEvents(outputStream, events, !skipHeader);
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractEnrollmentService method validateRequest.
private ImportSummary validateRequest(Program program, org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance, Enrollment enrollment, OrganisationUnit organisationUnit, ImportOptions importOptions) {
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
String error = validateProgramForEnrollment(program, enrollment, organisationUnit, importOptions);
if (!StringUtils.isEmpty(error)) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription(error);
importSummary.incrementIgnored();
return importSummary;
}
ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setSkipPaging(true);
params.setProgram(program);
params.setTrackedEntityInstanceUid(entityInstance.getUid());
// endpoint is used for bulk import and sync purposes as well
if (enrollment.getStatus() != EnrollmentStatus.CANCELLED) {
List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
Set<Enrollment> activeEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE).collect(Collectors.toSet());
// needed
if (!activeEnrollments.isEmpty() && enrollment.getStatus() == EnrollmentStatus.ACTIVE) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active enrollment in program " + program.getUid());
importSummary.incrementIgnored();
return importSummary;
}
// imported enrollment has a state other than CANCELLED
if (program.getOnlyEnrollOnce()) {
Set<Enrollment> activeOrCompletedEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE || e.getStatus() == EnrollmentStatus.COMPLETED).collect(Collectors.toSet());
if (!activeOrCompletedEnrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active or completed enrollment in program " + program.getUid() + ", and this program only allows enrolling one time");
importSummary.incrementIgnored();
return importSummary;
}
}
}
checkAttributes(entityInstance, enrollment, importOptions, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.incrementIgnored();
}
return importSummary;
}
Aggregations