private DhisEventDto createDhisEventFromParams(Map<String, Object> params) { DhisEventDto dhisEventDto = new DhisEventDto(); String registationString = (String) params.remove(EventParams.REGISTRATION); boolean registration = registationString.contains("true"); if (registration) { String externalTrackedEntityInstanceId = (String) params.remove(EventParams.EXTERNAL_ID); String trackedEntityInstanceId = trackedEntityInstanceMappingService.mapFromExternalId(externalTrackedEntityInstanceId); dhisEventDto.setTrackedEntityInstance(trackedEntityInstanceId); } String orgUnitId = (String) params.remove(EventParams.LOCATION); String program = (String) params.remove(EventParams.PROGRAM); String date = (String) params.remove(EventParams.DATE); String stage = (String) params.remove(EventParams.STAGE); List<DataValueDto> dataValues = new ArrayList<>(); for (Entry<String, Object> entry : params.entrySet()) { DataValueDto dataValue = new DataValueDto(); dataValue.setDataElement(entry.getKey()); dataValue.setValue((String) entry.getValue()); dataValues.add(dataValue); } dhisEventDto.setProgram(program); dhisEventDto.setEventDate(date); dhisEventDto.setProgramStage(stage); dhisEventDto.setOrgUnit(orgUnitId); dhisEventDto.setDataValues(dataValues); return dhisEventDto; }
private EnrollmentDto createEnrollmentFromParams(Map<String, Object> params) { String program = (String) params.remove(EventParams.PROGRAM); String externalId = (String) params.remove(EventParams.EXTERNAL_ID); String trackedEntityInstanceId = trackedEntityInstanceMappingService.mapFromExternalId(externalId); String orgUnit = (String) params.remove(EventParams.LOCATION); String date = (String) params.remove(EventParams.DATE); List<AttributeDto> attributes = new ArrayList<>(); for (Entry<String, Object> entry : params.entrySet()) { if (entry.getValue() != null) { AttributeDto attribute = new AttributeDto(); attribute.setAttribute(entry.getKey()); attribute.setValue((String) entry.getValue()); attributes.add(attribute); } } EnrollmentDto enrollment = new EnrollmentDto(); enrollment.setProgram(program); enrollment.setOrgUnit(orgUnit); enrollment.setTrackedEntityInstance(trackedEntityInstanceId); enrollment.setDateOfEnrollment(date); enrollment.setAttributes(attributes); return enrollment; }
/** * Parses the MotechEvent and creates a {@link * org.motechproject.dhis2.rest.domain.TrackedEntityInstanceDto} which is then sent to the DHIS2 * server via {@link org.motechproject.dhis2.rest.service.DhisWebService} * * @param event MotechEvent pertaining to tracked entity instance creation. */ @MotechListener(subjects = EventSubjects.CREATE_ENTITY) public void handleCreate(MotechEvent event) { LOGGER.debug("Handling CREATE_ENTITY MotechEvent"); Map<String, Object> params = prepareDhisAttributesMap(event.getParameters()); String externalUUID = (String) params.remove(EventParams.EXTERNAL_ID); TrackedEntityInstanceDto trackedEntityInstance = createTrackedEntityInstanceFromParams(params); LOGGER.debug("Sending request to create entity to the DHIS Web Service"); DhisStatusResponse response = dhisWebService.createTrackedEntityInstance(trackedEntityInstance); LOGGER.trace("Received response from the DHIS server. Status: {}", response.getStatus()); if (response.getStatus() == DhisStatus.SUCCESS || response.getStatus() == DhisStatus.OK) { trackedEntityInstanceMappingService.create(externalUUID, response.getReference()); } }