Search in sources :

Example 1 with ExternalDataUpdateException

use of de.symeda.sormas.api.externaldata.ExternalDataUpdateException in project SORMAS-Project by hzi-braunschweig.

the class ExternalDataUtil method updateExternalData.

public static <T extends AbstractDomainObject & HasExternalData> void updateExternalData(List<ExternalDataDto> externalData, Function<List<String>, List<T>> entityRetrieveFunction, Consumer<T> saveEntityConsumer) throws ExternalDataUpdateException {
    if (externalData.isEmpty()) {
        return;
    }
    Map<String, ExternalDataDto> externalDataDtoMap = externalData.stream().collect(Collectors.toMap(ExternalDataDto::getUuid, Function.identity()));
    List<String> uuids = new ArrayList<>(externalDataDtoMap.keySet());
    List<T> entitiesToUpdate = entityRetrieveFunction.apply(uuids);
    for (T externalDataEntity : entitiesToUpdate) {
        ExternalDataDto externalDataUpdate = externalDataDtoMap.get(externalDataEntity.getUuid());
        if (StringUtils.isNotBlank(externalDataEntity.getExternalId()) && StringUtils.isNotBlank(externalDataUpdate.getExternalId())) {
            throw new ExternalDataUpdateException(String.format("Entity with uuid %s already has externalId", externalDataEntity.getUuid()));
        } else if (StringUtils.isNotBlank(externalDataUpdate.getExternalId())) {
            externalDataEntity.setExternalId(externalDataUpdate.getExternalId());
        }
        if (StringUtils.isNotBlank(externalDataEntity.getExternalToken()) && StringUtils.isNotBlank(externalDataUpdate.getExternalToken())) {
            throw new ExternalDataUpdateException(String.format("Entity with uuid %s already has externalToken", externalDataEntity.getUuid()));
        } else if (StringUtils.isNotBlank(externalDataUpdate.getExternalToken())) {
            externalDataEntity.setExternalToken(externalDataUpdate.getExternalToken());
        }
        saveEntityConsumer.accept(externalDataEntity);
    }
}
Also used : ExternalDataUpdateException(de.symeda.sormas.api.externaldata.ExternalDataUpdateException) ArrayList(java.util.ArrayList) ExternalDataDto(de.symeda.sormas.api.externaldata.ExternalDataDto)

Aggregations

ExternalDataDto (de.symeda.sormas.api.externaldata.ExternalDataDto)1 ExternalDataUpdateException (de.symeda.sormas.api.externaldata.ExternalDataUpdateException)1 ArrayList (java.util.ArrayList)1