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...)
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>
<span class="fc" id="L243"> return new ModelBuilder()</span>
<span class="fc" id="L244"> .with( audioFileIri, RDF.TYPE, MO.C_AUDIO_FILE)</span>
<span class="fc" id="L245"> .with( audioFileIri, BMMO.P_IMPORTED_FROM, BMMO.O_SOURCE_EMBEDDED)</span>
<span class="fc" id="L246"> .with( audioFileIri, FOAF.SHA1, literalFor(toHexString(sha1)))</span>
<span class="fc" id="L247"> .with( audioFileIri, MO.P_ENCODES, signalIri)</span>
<span class="fc" id="L248"> .with( audioFileIri, BMMO.P_PATH, literalFor(mediaItem.getRelativePath()))</span>
<span class="fc" id="L249"> .with( audioFileIri, BMMO.P_LATEST_INDEXING_TIME, literalFor(getLastModifiedTime(mediaItem.getPath())))</span>
<span class="fc" id="L250"> .withOptional(audioFileIri, BMMO.P_FILE_SIZE, literalForLong(metadata.get(FILE_SIZE)))</span>
<span class="fc" id="L252"> .with( signalIri, RDF.TYPE, MO.C_DIGITAL_SIGNAL)</span>
<span class="fc" id="L253"> .with( signalIri, BMMO.P_IMPORTED_FROM, BMMO.O_SOURCE_EMBEDDED)</span>
<span class="fc" id="L254"> .with( signalIri, MO.P_PUBLISHED_AS, trackIri)</span>
<span class="fc" id="L255"> .withOptional(signalIri, MO.P_SAMPLE_RATE, literalForInt(metadata.get(SAMPLE_RATE)))</span>
<span class="fc" id="L256"> .withOptional(signalIri, MO.P_BITS_PER_SAMPLE, literalForInt(metadata.get(BIT_RATE)))</span>
<span class="fc" id="L257"> .withOptional(signalIri, MO.P_DURATION, literalForFloat(metadata.get(DURATION)</span>
<span class="fc" id="L258"> .map(Duration::toMillis)</span>
<span class="fc" id="L259"> .map(l -> (float)l)))</span>
<span class="fc" id="L260"> .with( trackIri, RDF.TYPE, MO.C_TRACK)</span>
<span class="fc" id="L261"> .with( trackIri, BMMO.P_IMPORTED_FROM, BMMO.O_SOURCE_EMBEDDED)</span>
<span class="fc" id="L262"> .withOptional(trackIri, BMMO.P_ITUNES_CDDB1, literalFor(metadata.get(ITUNES_COMMENT)</span>
<span class="fc" id="L263"> .map(ITunesComment::getTrackId)))</span>
<span class="fc" id="L264"> .withOptional(trackIri, MO.P_TRACK_NUMBER, literalForInt(metadata.get(TRACK_NUMBER)))</span>
<span class="fc" id="L265"> .withOptional(trackIri, RDFS.LABEL, literalFor(trackTitle))</span>
<span class="fc" id="L266"> .withOptional(trackIri, DC.TITLE, literalFor(trackTitle))</span>
<span class="fc" id="L267"> .with( trackIri, FOAF.MAKER, makerUris.stream())</span>
<span class="fc" id="L269"> .withOptional(newRecordIri, RDF.TYPE, MO.C_RECORD)</span>
<span class="fc" id="L270"> .withOptional(newRecordIri, BMMO.P_IMPORTED_FROM, BMMO.O_SOURCE_EMBEDDED)</span>
<span class="fc" id="L271"> .withOptional(newRecordIri, MO.P_MEDIA_TYPE, MO.C_CD)</span>
<span class="fc" id="L272"> .withOptional(newRecordIri, RDFS.LABEL, literalFor(recordTitle))</span>
<span class="fc" id="L273"> .withOptional(newRecordIri, DC.TITLE, literalFor(recordTitle))</span>
<span class="fc" id="L274"> .withOptional(newRecordIri, MO.P_TRACK_COUNT, literalForInt(metadata.get(CDDB)</span>
<span class="fc" id="L275"> .map(Cddb::getTrackCount)))</span>
<span class="fc" id="L276"> .withOptional(newRecordIri, BMMO.P_DISK_NUMBER, literalForInt(diskNumber))</span>
<span class="fc" id="L277"> .withOptional(newRecordIri, BMMO.P_DISK_COUNT, literalForInt(diskCount))</span>
<span class="fc" id="L278"> .withOptional(newRecordIri, BMMO.P_ITUNES_CDDB1, literalFor(metadata.get(ITUNES_COMMENT)</span>
<span class="fc" id="L279"> .map(ITunesComment::getCddb1)))</span>
<span class="fc" id="L280"> .with( recordIri, MO.P_TRACK, trackIri)</span>
<span class="fc" id="L281"> .with( recordIri, FOAF.MAKER, makerUris.stream())</span>
<span class="fc" id="L283"> .with( newArtistIris, RDF.TYPE, MO.C_MUSIC_ARTIST)</span>
<span class="fc" id="L284"> .with( newArtistIris, BMMO.P_IMPORTED_FROM, BMMO.O_SOURCE_EMBEDDED)</span>
<span class="fc" id="L285"> .with( newArtistIris, RDFS.LABEL, newArtistLiterals)</span>
<span class="fc" id="L286"> .with( newArtistIris, FOAF.NAME, newArtistLiterals)</span>
<span class="fc" id="L288"> .withOptional(newGroupIri, RDF.TYPE, MO.C_MUSIC_ARTIST)</span>
<span class="fc" id="L289"> .withOptional(newGroupIri, BMMO.P_IMPORTED_FROM, BMMO.O_SOURCE_EMBEDDED)</span>
<span class="fc" id="L290"> .withOptional(newGroupIri, RDFS.LABEL, literalFor(makerName))</span>
<span class="fc" id="L291"> .withOptional(newGroupIri, FOAF.NAME, literalFor(makerName))</span>
<span class="fc" id="L292"> .withOptional(newGroupIri, DbTune.P_ARTIST_TYPE, literalFor((short)2))</span>
<span class="fc" id="L293"> .withOptional(newGroupIri, Purl.P_COLLABORATES_WITH, artists.stream().map(Pair::getIri))</span>
<span class="fc" id="L294"> .toModel();</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private Instant getLastModifiedTime (@Nonnull final Path path)
{
try
{
<span class="fc" id="L306"> return Files.getLastModifiedTime(path).toInstant();</span>
}
<span class="nc" id="L308"> catch (IOException e) // should never happen</span>
{
<span class="nc" id="L310"> log.warn("Cannot get last modified time for {}: assuming now", path);</span>
<span class="nc" id="L311"> return timeProvider.currentInstant();</span>
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static IRI recordIriOf (@Nonnull final Metadata metadata, @Nonnull final String recordTitle)
{
<span class="fc" id="L322"> final Optional<Cddb> cddb = metadata.get(CDDB);</span>
<span class="fc" id="L323"> return BMMO.recordIriFor(cddb.map(value -> createSha1IdNew(value.getToc()))</span>
<span class="fc" id="L324"> .orElseGet(() -> createSha1IdNew("RECORD:" + recordTitle)));</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private Id uniqueTrackId (@Nonnull final Metadata metadata, @Nonnull final String default_)
{
<span class="fc" id="L334"> final Optional<Cddb> cddb = metadata.get(CDDB);</span>
<span class="fc" id="L335"> final Optional<Integer> trackNumber = metadata.get(TRACK_NUMBER);</span>
<span class="pc bpc" id="L337" title="1 of 4 branches missed."> return (cddb.isPresent() && trackNumber.isPresent())</span>
<span class="fc" id="L338"> ? createSha1IdNew(cddb.get().getToc() + "/" + trackNumber.get())</span>
<span class="fc" id="L339"> : Id.of(default_);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private IRI artistIriOf (@Nonnull final String name)
{
<span class="fc" id="L349"> return BMMO.artistIriFor(createSha1IdNew("ARTIST:" + name));</span>
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
private static Optional<Integer> emptyIfOne (@Nonnull final Optional<Integer> number)
{
<span class="fc bfc" id="L360" title="All 2 branches covered."> return number.flatMap(n -> (n == 1) ? Optional.empty() : Optional.of(n));</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>