Content of file EmbeddedAudioMetadataImporter.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>EmbeddedAudioMetadataImporter.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">blueMarine II :: Media Scanner</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.metadata.impl.audio.embedded</a> > <span class="el_source">EmbeddedAudioMetadataImporter.java</span></div><h1>EmbeddedAudioMetadataImporter.java</h1><pre class="source lang-java linenums"><span class="fc" id="L1">/*</span>
* *********************************************************************************************************************
*
* blueMarine II: Semantic Media Centre
* http://tidalwave.it/projects/bluemarine2
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/bluemarine2-src
* git clone https://github.com/tidalwave-it/bluemarine2-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.bluemarine2.metadata.impl.audio.embedded;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.vocabulary.DC;
import org.eclipse.rdf4j.model.vocabulary.FOAF;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import org.eclipse.rdf4j.model.vocabulary.RDFS;
import it.tidalwave.util.ConcurrentHashMapWithOptionals;
import it.tidalwave.util.Id;
import it.tidalwave.util.TimeProvider;
import it.tidalwave.util.annotation.VisibleForTesting;
import it.tidalwave.messagebus.annotation.ListensTo;
import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
import it.tidalwave.bluemarine2.util.ModelBuilder;
import it.tidalwave.bluemarine2.model.MediaItem;
import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
import it.tidalwave.bluemarine2.model.vocabulary.*;
import it.tidalwave.bluemarine2.mediascanner.impl.MediaItemImportRequest;
import it.tidalwave.bluemarine2.mediascanner.impl.ProgressHandler;
import it.tidalwave.bluemarine2.mediascanner.impl.StatementManager;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.*;
import static it.tidalwave.bluemarine2.util.Formatters.*;
import static it.tidalwave.bluemarine2.util.RdfUtilities.*;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
/***********************************************************************************************************************
*
* This class generates RDF triples out of the {@link Metadata} embedded in an audio file.
*
* <pre>
* mo:AudioFile
* IRI computed from the fingerprint of the contents
* bm:importedFrom http://bluemarine.tidalwave.it/source#embedded
* rdfs:label the display name
* dc:title the title
* mo:encodes points to the signal
* bm:latestIndexingTime the latest import time
* bm:path the path of the file
* bm:fileSize the file size
* foaf:sha1 the fingerprint of the file
*
* mo:DigitalSignal
* IRI computed from the fingerprint of related file
* bm:importedFrom http://bluemarine.tidalwave.it/source#embedded
* mo:bitsPerSample the bits per sample
* mo:duration the duration
* mo:sample_rate the sample rate
* mo:published_as points to the Track
* MISSING mo:channels
* MISSING mo:time
* MISSING mo:trmid
*
* mo:Track
* IRI computed from the fingerprint of related file
* bm:importedFrom http://bluemarine.tidalwave.it/source#embedded
* rdfs:label the display name
* dc:title the title
* mo:track_number the track number in the record
* bm:discCount the number of disks in a collection
* bm:discNumber the index of the disk in a collection
* bm:iTunesCddb1 the CDDB1 attribute encoded by iTunes plus the track index
* foaf:maker points to the MusicArtists
*
* mo:Record
* IRI computed from the fingerprint of the name
* bm:importedFrom http://bluemarine.tidalwave.it/source#embedded
* rdfs:label the display name (ALBUM from audiofile metadata, or the name of the folder)
* dc:title the title (see above)
* mo:mediaType CD
* mo:track points to the Tracks
* bm:iTunesCddb1 the CDDB1 attribute encoded by iTunes
* foaf:maker points to the MusicArtists (union of the makers of Tracks)
* MISSING dc:date
* MISSING dc:language
* MISSING mo:release TODO points to the Label (EMI, etc...)
*
* mo:MusicArtist
* IRI computed from the fingerprint of the name
* bm:importedFrom http://bluemarine.tidalwave.it/source#embedded
* rdfs:label the display name
* foaf:name the name
* (in case of a group also the predicates below)
* dbtune:artist_type 2, which means a group
* purl:collaborates_with the MusicArtists in the group
* </pre>
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="fc" id="L135">@SimpleMessageSubscriber @Slf4j</span>
<span class="fc" id="L136">public class EmbeddedAudioMetadataImporter</span>
{
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
<span class="pc" id="L143"> @Immutable @RequiredArgsConstructor @Getter @ToString</span>
static class Pair
{
@Nonnull
<span class="fc" id="L147"> private final IRI iri;</span>
@Nonnull
<span class="fc" id="L150"> private final String name;</span>
}
@Inject
private StatementManager statementManager;
@Inject
private TimeProvider timeProvider;
@Inject
private ProgressHandler progress;
// Set would suffice, but there's no ConcurrentSet
<span class="fc" id="L163"> private final ConcurrentHashMapWithOptionals<IRI, Optional<String>> seenArtistUris =</span>
new ConcurrentHashMapWithOptionals<>();
<span class="fc" id="L166"> private final ConcurrentHashMapWithOptionals<IRI, Boolean> seenRecordUris = new ConcurrentHashMapWithOptionals<>();</span>
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void reset()
{
// FIXME: should load existing URIs from the Persistence
<span class="nc" id="L176"> seenArtistUris.clear();</span>
<span class="nc" id="L177"> seenRecordUris.clear();</span>
<span class="nc" id="L178"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@VisibleForTesting void onMediaItemImportRequest (@ListensTo final MediaItemImportRequest request)
{
<span class="fc" id="L186"> request.getSha1().ifPresent(sha1 -></span>
{
try
{
<span class="fc" id="L190"> log.info("onMediaItemImportRequest({})", request);</span>
<span class="fc" id="L191"> statementManager.requestAdd(importMediaItem(request.getMediaItem(), sha1));</span>
}
finally
{
<span class="fc" id="L195"> progress.incrementImportedMediaItems();</span>
}
<span class="fc" id="L197"> });</span>
<span class="fc" id="L198"> }</span>
/*******************************************************************************************************************
*
* Processes a {@link MediaItem}.
*
* @param mediaItem the item
* @param sha2 the fingerprint of the file
* @return the model
*
******************************************************************************************************************/
@Nonnull
private Model importMediaItem (@Nonnull final MediaItem mediaItem, @Nonnull final byte[] sha1)
{
<span class="fc" id="L212"> log.debug("importMediaItem({})", mediaItem);</span>
<span class="fc" id="L214"> final Metadata metadata = mediaItem.getMetadata();</span>
<span class="fc" id="L216"> final Optional<String> trackTitle = metadata.get(TITLE);</span>
<span class="fc" id="L217"> final Optional<String> makerName = metadata.get(ARTIST);</span>
<span class="fc" id="L218"> final PathAwareEntity parent = mediaItem.getParent().get();</span>
<span class="fc" id="L219"> final String recordTitle = metadata.get(ALBUM).orElse(parent.getPath().toFile().getName());</span>
<span class="fc" id="L220"> final Optional<Integer> diskCount = emptyIfOne(metadata.get(DISK_COUNT));</span>
<span class="fc" id="L221"> final Optional<Integer> diskNumber = diskCount.flatMap(dc -> metadata.get(DISK_NUMBER));</span>
<span class="fc" id="L222"> final Id uniqueId = uniqueTrackId(metadata, toBase64String(sha1));</span>
<span class="fc" id="L223"> final IRI audioFileIri = BMMO.audioFileIriFor(toBase64String(sha1));</span>
<span class="fc" id="L224"> final IRI signalIri = BMMO.signalIriFor(uniqueId);</span>
<span class="fc" id="L225"> final IRI trackIri = BMMO.trackIriFor(uniqueId);</span>
<span class="fc" id="L226"> final IRI recordIri = recordIriOf(metadata, recordTitle);</span>
<span class="fc" id="L227"> final Optional<IRI> newRecordIri = seenRecordUris.putIfAbsentAndGetNewKey(recordIri, true);</span>
<span class="fc" id="L229"> final List<IRI> makerUris = makerName.map(name -> List.of(artistIriOf(name))).orElse(emptyList());</span>
<span class="fc" id="L230"> final List<Pair> artists = makerName.map(name -> Stream.of(name.split("[;]")).map(String::trim)).orElse(Stream.empty())</span>
<span class="fc" id="L231"> .map(name -> new Pair(artistIriOf(name), name))</span>
<span class="fc" id="L232"> .collect(toList());</span>
<span class="fc" id="L234"> final List<Pair> newArtists = artists.stream().filter(</span>
<span class="fc" id="L235"> p -> seenArtistUris.putIfAbsentAndGetNewKey(p.getIri(), Optional.empty()).isPresent())</span>
<span class="fc" id="L236"> .collect(toList());</span>
<span class="fc" id="L237"> final List<IRI> newArtistIris = newArtists.stream().map(Pair::getIri).collect(toList());</span>
<span class="fc" id="L238"> final List<Value> newArtistLiterals = newArtists.stream().map(p -> literalFor(p.getName())).collect(toList());</span>
<span class="fc bfc" id="L240" title="All 2 branches covered."> final Optional<IRI> newGroupIri = (artists.size() <= 1) ? Optional.empty()</span>
<span class="fc" id="L241"> : seenArtistUris.putIfAbsentAndGetNewKey(makerUris.get(0), Optional.empty()); // FIXME: only first one?</span>