Skip to content

Commit

Permalink
Extracted enricher lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixRilling committed Jun 18, 2023
1 parent 920b1af commit c42ffa4
Showing 1 changed file with 8 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,27 @@ public class MusicbrainzEnricherService {
private final MusicbrainzAutoQueryService musicbrainzAutoQueryService;
private final HistoryService historyService;

MusicbrainzEnricherService(ApplicationContext applicationContext,
MusicbrainzAutoQueryService musicbrainzAutoQueryService,
HistoryService historyService) {
MusicbrainzEnricherService(ApplicationContext applicationContext, MusicbrainzAutoQueryService musicbrainzAutoQueryService, HistoryService historyService) {
this.applicationContext = applicationContext;
this.musicbrainzAutoQueryService = musicbrainzAutoQueryService;
this.historyService = historyService;
}

public void runInAutoQueryMode(@NotNull DataType dataType) {
final AbstractEnrichmentService<?, ?> enrichmentService = findFittingEnrichmentService(dataType);
switch (dataType) {
case RELEASE -> musicbrainzAutoQueryService.autoQueryReleases(mbid -> executeEnrichment(
dataType,
mbid,
findFittingEnrichmentService(dataType)));
case RELEASE_GROUP -> musicbrainzAutoQueryService.autoQueryReleaseGroups(mbid -> executeEnrichment(dataType,
mbid,
findFittingEnrichmentService(dataType)));
case RELEASE ->
musicbrainzAutoQueryService.autoQueryReleases(mbid -> executeEnrichment(dataType, mbid, enrichmentService));
case RELEASE_GROUP ->
musicbrainzAutoQueryService.autoQueryReleaseGroups(mbid -> executeEnrichment(dataType, mbid, enrichmentService));
}
}

public void runInSingleMode(@NotNull DataType dataType, @NotNull UUID mbid) {
executeEnrichment(dataType, mbid, findFittingEnrichmentService(dataType));
}

private void executeEnrichment(@NotNull DataType dataType,
@NotNull UUID mbid,
AbstractEnrichmentService<?, ?> enrichmentService) {
private void executeEnrichment(@NotNull DataType dataType, @NotNull UUID mbid, AbstractEnrichmentService<?, ?> enrichmentService) {
LOGGER.info("Starting enrichment for {} '{}'.", dataType, mbid);
try {
enrichmentService.executeEnrichment(mbid);
Expand All @@ -61,12 +55,6 @@ private void executeEnrichment(@NotNull DataType dataType,

@NotNull
private AbstractEnrichmentService<?, ?> findFittingEnrichmentService(@NotNull DataType dataType) {
return applicationContext.getBeansOfType(AbstractEnrichmentService.class)
.values()
.stream()
.filter(enrichmentService -> enrichmentService.getDataType() == dataType)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No enrichment service exists for data type '%s'.".formatted(
dataType)));
return applicationContext.getBeansOfType(AbstractEnrichmentService.class).values().stream().filter(enrichmentService -> enrichmentService.getDataType() == dataType).findFirst().orElseThrow(() -> new IllegalArgumentException("No enrichment service exists for data type '%s'.".formatted(dataType)));
}
}

0 comments on commit c42ffa4

Please sign in to comment.