Skip to content

Commit

Permalink
fixes empty interview file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Aug 7, 2024
1 parent e72bab1 commit 5a34b85
Showing 1 changed file with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package dita.globodiet.survey.recall24;

import java.util.Optional;
import java.util.function.Consumer;

import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -59,7 +60,11 @@ public InterviewSet24.Dto parse(
final @NonNull DataSource source,
final @Nullable Consumer<Message> messageConsumer) {
var dto = JaxbUtils.tryRead(_Dtos.Itv.class, source)
.valueAsNonNullElseFail();
.valueAsNullableElseFail();
if(dto==null) {
warnEmptyDataSource(source, messageConsumer);
return InterviewSet24.empty();
}
return createFromDto(dto, messageConsumer);
}

Expand All @@ -71,38 +76,38 @@ public InterviewSet24.Dto parse(
final Clob interviewSource,
final @Nullable Consumer<Message> messageConsumer) {
var dto = JaxbUtils.tryRead(_Dtos.Itv.class, interviewSource.getChars().toString())
.valueAsNonNullElseFail();
.valueAsNullableElseFail();
if(dto==null) {
warnEmptyDataSource(interviewSource, messageConsumer);
return InterviewSet24.empty();
}
return createFromDto(dto, messageConsumer);
}
public InterviewSet24.Dto parse2(
final Clob interviewSource,
final @Nullable Consumer<Message> messageConsumer) {
var dto = JaxbUtils.tryRead(_Dtos.Itv.class, interviewSource.getChars().toString())
.valueAsNonNullElseFail();
return createFromDto2(dto, messageConsumer);
}

// -- HELPER

private InterviewSet24.Dto createFromDto(
final @NonNull _Dtos.Itv dto,
final @Nullable Consumer<Message> messageConsumer) {
return Recall24DtoUtils
.join(
dto.getInterviews().stream()
.map(_InterviewConverter::toInterview24)
.toList(),
messageConsumer);
.join(
dto.getInterviews().stream()
.map(_InterviewConverter::toInterview24)
.toList(),
messageConsumer);
}
private InterviewSet24.Dto createFromDto2(
final @NonNull _Dtos.Itv dto,

private void warnEmptyDataSource(
final Object source,
final @Nullable Consumer<Message> messageConsumer) {
return Recall24DtoUtils
.join(
dto.getInterviews().stream()
.map(_InterviewConverter::toInterview24)
.toList(),
messageConsumer);
var messageConsumerOrFallback = Optional.ofNullable(messageConsumer)
.orElseGet(Message::consumerWritingToSyserr);
var sourceName = switch (source) {
case Clob clob -> clob.getName();
default -> source.getClass().getName();
};
messageConsumerOrFallback
.accept(Message.warn("empty interview data source detected: %s", sourceName));
}

}

0 comments on commit 5a34b85

Please sign in to comment.