use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method updateTrackedEntityInstanceJson.
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')")
public void updateTrackedEntityInstanceJson(@PathVariable String id, ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
ImportSummary importSummary = trackedEntityInstanceService.updateTrackedEntityInstanceJson(id, inputStream, importOptions);
importSummary.setImportOptions(importOptions);
webMessageService.send(WebMessageUtils.importSummary(importSummary), response, request);
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method postTrackedEntityInstanceXml.
@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')")
public void postTrackedEntityInstanceXml(@RequestParam(defaultValue = "CREATE_AND_UPDATE") ImportStrategy strategy, ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
importOptions.setStrategy(strategy);
InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstanceXml(inputStream, importOptions);
response.setContentType(MediaType.APPLICATION_XML_VALUE);
importSummaries.getImportSummaries().stream().filter(importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals(ImportStatus.ERROR) && !importOptions.getImportStrategy().isDelete()).forEach(importSummary -> importSummary.setHref(ContextUtils.getRootPath(request) + TrackedEntityInstanceSchemaDescriptor.API_ENDPOINT + "/" + importSummary.getReference()));
if (importSummaries.getImportSummaries().size() == 1) {
ImportSummary importSummary = importSummaries.getImportSummaries().get(0);
importSummary.setImportOptions(importOptions);
if (!importSummary.getStatus().equals(ImportStatus.ERROR)) {
response.setHeader("Location", getResourcePath(request, importSummary));
}
}
response.setStatus(HttpServletResponse.SC_CREATED);
webMessageService.send(WebMessageUtils.importSummaries(importSummaries), response, request);
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkAttributes.
private List<ImportConflict> checkAttributes(Enrollment enrollment, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
Map<String, String> attributeValueMap = Maps.newHashMap();
for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
}
// ignore attributes which do not belong to this program
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
importConflicts.addAll(validateAttributeType(attribute, importOptions));
}
TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
for (TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet()) {
Boolean mandatory = mandatoryMap.get(trackedEntityAttribute);
if (mandatory && !attributeValueMap.containsKey(trackedEntityAttribute.getUid())) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Missing mandatory attribute " + trackedEntityAttribute.getUid()));
continue;
}
if (trackedEntityAttribute.isUnique()) {
OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, instance.getOrgUnit());
importConflicts.addAll(checkScope(trackedEntityInstance, trackedEntityAttribute, attributeValueMap.get(trackedEntityAttribute.getUid()), organisationUnit, program));
}
attributeValueMap.remove(trackedEntityAttribute.getUid());
}
if (!attributeValueMap.isEmpty()) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Only program attributes is allowed for enrollment " + attributeValueMap));
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractEnrollmentService method updateAttributeValues.
private void updateAttributeValues(Enrollment enrollment, ImportOptions importOptions) {
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
Map<String, String> attributeValueMap = Maps.newHashMap();
for (Attribute attribute : enrollment.getAttributes()) {
attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
}
trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> attributeValueMap.containsKey(value.getAttribute().getUid())).forEach(value -> {
String newValue = attributeValueMap.get(value.getAttribute().getUid());
value.setValue(newValue);
trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(value);
attributeValueMap.remove(value.getAttribute().getUid());
});
for (String key : attributeValueMap.keySet()) {
TrackedEntityAttribute attribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), key);
if (attribute != null) {
TrackedEntityAttributeValue value = new TrackedEntityAttributeValue();
value.setValue(attributeValueMap.get(key));
value.setAttribute(attribute);
trackedEntityAttributeValueService.addTrackedEntityAttributeValue(value);
trackedEntityInstance.addAttributeValue(value);
}
}
}
use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.
the class AbstractEventService method updateEvent.
private ImportSummary updateEvent(Event event, User user, boolean singleValue, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(event.getEvent());
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(event.getEvent());
if (programStageInstance == null) {
importSummary.getConflicts().add(new ImportConflict("Invalid Event ID.", event.getEvent()));
return importSummary.incrementIgnored();
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
if (organisationUnit == null) {
organisationUnit = programStageInstance.getOrganisationUnit();
}
Date executionDate = new Date();
if (event.getEventDate() != null) {
executionDate = DateUtils.parseDate(event.getEventDate());
programStageInstance.setExecutionDate(executionDate);
}
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = DateUtils.parseDate(event.getDueDate());
}
String storedBy = getStoredBy(event, null, user);
programStageInstance.setStoredBy(storedBy);
String completedBy = getCompletedBy(event, null, user);
if (event.getStatus() == EventStatus.ACTIVE) {
programStageInstance.setStatus(EventStatus.ACTIVE);
programStageInstance.setCompletedBy(null);
programStageInstance.setCompletedDate(null);
} else if (programStageInstance.getStatus() != event.getStatus() && event.getStatus() == EventStatus.COMPLETED) {
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstance.setCompletedBy(completedBy);
programStageInstance.setCompletedDate(executionDate);
if (programStageInstance.isCompleted()) {
programStageInstanceService.completeProgramStageInstance(programStageInstance, importOptions.isSkipNotifications(), i18nManager.getI18nFormat());
}
} else if (event.getStatus() == EventStatus.SKIPPED) {
programStageInstance.setStatus(EventStatus.SKIPPED);
} else if (event.getStatus() == EventStatus.SCHEDULE) {
programStageInstance.setStatus(EventStatus.SCHEDULE);
}
programStageInstance.setDueDate(dueDate);
programStageInstance.setOrganisationUnit(organisationUnit);
if (!singleValue) {
if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
if (event.getCoordinate() != null && event.getCoordinate().isValid()) {
programStageInstance.setLatitude(event.getCoordinate().getLatitude());
programStageInstance.setLongitude(event.getCoordinate().getLongitude());
} else {
programStageInstance.setLatitude(null);
programStageInstance.setLongitude(null);
}
}
}
Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
validateExpiryDays(event, program, programStageInstance);
if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
if (attributeOptionCombo == null) {
importSummary.getConflicts().add(new ImportConflict("Invalid attribute option combo identifier:", event.getAttributeCategoryOptions()));
return importSummary.incrementIgnored();
}
programStageInstance.setAttributeOptionCombo(attributeOptionCombo);
}
programStageInstance.setDeleted(event.isDeleted());
programStageInstanceService.updateProgramStageInstance(programStageInstance);
updateTrackedEntityInstance(programStageInstance);
saveTrackedEntityComment(programStageInstance, event, storedBy);
Set<TrackedEntityDataValue> dataValues = new HashSet<>(dataValueService.getTrackedEntityDataValues(programStageInstance));
Map<String, TrackedEntityDataValue> existingDataValues = getDataElementDataValueMap(dataValues);
for (DataValue value : event.getDataValues()) {
DataElement dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), value.getDataElement());
TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
if (!validateDataValue(dataElement, value.getValue(), importSummary)) {
continue;
}
if (dataValue != null) {
if (StringUtils.isEmpty(value.getValue()) && dataElement.isFileType() && !StringUtils.isEmpty(dataValue.getValue())) {
fileResourceService.deleteFileResource(dataValue.getValue());
}
dataValue.setValue(value.getValue());
dataValue.setProvidedElsewhere(value.getProvidedElsewhere());
dataValueService.updateTrackedEntityDataValue(dataValue);
dataValues.remove(dataValue);
} else {
TrackedEntityDataValue existingDataValue = existingDataValues.get(value.getDataElement());
saveDataValue(programStageInstance, event.getStoredBy(), dataElement, value.getValue(), value.getProvidedElsewhere(), existingDataValue, null);
}
}
if (!singleValue) {
dataValues.forEach(dataValueService::deleteTrackedEntityDataValue);
}
importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
return importSummary;
}
Aggregations